Merge branch 'seezer'
[quassel.git] / src / qtui / channellistdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "channellistdlg.h"
22
23 #include "client.h"
24 #include "clientirclisthelper.h"
25
26 #include <QHeaderView>
27
28 ChannelListDlg::ChannelListDlg(QWidget *parent)
29   : QDialog(parent),
30     _listFinished(true),
31     _ircListModel(this),
32     _sortFilter(this)
33 {
34   _sortFilter.setSourceModel(&_ircListModel);
35   _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
36   _sortFilter.setFilterKeyColumn(-1);
37   
38   ui.setupUi(this);
39   ui.channelListView->setSelectionBehavior(QAbstractItemView::SelectRows);
40   ui.channelListView->setSelectionMode(QAbstractItemView::SingleSelection);
41   ui.channelListView->setAlternatingRowColors(true);
42   ui.channelListView->setTabKeyNavigation(false);
43   ui.channelListView->setModel(&_sortFilter);
44   ui.channelListView->setSortingEnabled(true);
45   ui.channelListView->verticalHeader()->hide();
46   ui.channelListView->horizontalHeader()->setStretchLastSection(true);
47
48   ui.searchChannelsButton->setAutoDefault(false);
49
50   connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
51   connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
52   connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
53   connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription>)),
54           this, SLOT(receiveChannelList(NetworkId , QStringList, QList<IrcListHelper::ChannelDescription>)));
55   connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
56   connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
57
58   enableQuery(true);
59   showFilterLine(false);
60 }
61
62
63 void ChannelListDlg::setNetwork(NetworkId netId) {
64   if(_netId == netId)
65     return;
66   
67   _netId = netId;
68   _ircListModel.setChannelList();
69 }
70
71 void ChannelListDlg::requestSearch() {
72   _listFinished = false;
73   enableQuery(false);
74   QStringList channelFilters;
75   channelFilters << ui.channelNameLineEdit->text().trimmed();
76   Client::ircListHelper()->requestChannelList(_netId, channelFilters);
77 }
78
79 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList) {
80   Q_UNUSED(netId)
81   Q_UNUSED(channelFilters)
82
83   showFilterLine(!channelList.isEmpty());
84   _ircListModel.setChannelList(channelList);
85   enableQuery(_listFinished);
86 }
87
88 void ChannelListDlg::showFilterLine(bool show) {
89   ui.line->setVisible(show);
90   ui.filterLabel->setVisible(show);
91   ui.filterLineEdit->setVisible(show);
92 }
93
94 void ChannelListDlg::enableQuery(bool enable) {
95   ui.channelNameLineEdit->setEnabled(enable);
96   ui.searchChannelsButton->setEnabled(enable);
97 }
98
99 void ChannelListDlg::reportFinishedList() {
100   _listFinished = true;
101 }
102
103
104 void ChannelListDlg::joinChannel(const QModelIndex &index) {
105   Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
106 }