fcc716b24a43390cecd705a4c21cdb4afa5344c4
[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 #include <QHBoxLayout>
28 #include <QSpacerItem>
29
30 ChannelListDlg::ChannelListDlg(QWidget *parent)
31   : QDialog(parent),
32     _listFinished(true),
33     _ircListModel(this),
34     _sortFilter(this),
35     _simpleModeSpacer(0),
36     _advancedMode(false)
37 {
38   _sortFilter.setSourceModel(&_ircListModel);
39   _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
40   _sortFilter.setFilterKeyColumn(-1);
41   
42   ui.setupUi(this);
43   ui.channelListView->setSelectionBehavior(QAbstractItemView::SelectRows);
44   ui.channelListView->setSelectionMode(QAbstractItemView::SingleSelection);
45   ui.channelListView->setAlternatingRowColors(true);
46   ui.channelListView->setTabKeyNavigation(false);
47   ui.channelListView->setModel(&_sortFilter);
48   ui.channelListView->setSortingEnabled(true);
49   ui.channelListView->verticalHeader()->hide();
50   ui.channelListView->horizontalHeader()->setStretchLastSection(true);
51
52   ui.searchChannelsButton->setAutoDefault(false);
53
54   connect(ui.advancedModeLabel, SIGNAL(clicked()), this, SLOT(toggleMode()));
55   connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
56   connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
57   connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
58   connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription>)),
59           this, SLOT(receiveChannelList(NetworkId , QStringList, QList<IrcListHelper::ChannelDescription>)));
60   connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
61   connect(Client::ircListHelper(), SIGNAL(errorReported(const QString &)), this, SLOT(showError(const QString &)));
62   connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
63
64   setAdvancedMode(false);
65   enableQuery(true);
66   showFilterLine(false);
67   showErrors(false);
68 }
69
70 void ChannelListDlg::setNetwork(NetworkId netId) {
71   if(_netId == netId)
72     return;
73   
74   _netId = netId;
75   _ircListModel.setChannelList();
76 }
77
78 void ChannelListDlg::requestSearch() {
79   _listFinished = false;
80   enableQuery(false);
81   showErrors(false);
82   QStringList channelFilters;
83   channelFilters << ui.channelNameLineEdit->text().trimmed();
84   Client::ircListHelper()->requestChannelList(_netId, channelFilters);
85 }
86
87 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList) {
88   Q_UNUSED(netId)
89   Q_UNUSED(channelFilters)
90
91   showFilterLine(!channelList.isEmpty());
92   _ircListModel.setChannelList(channelList);
93   enableQuery(_listFinished);
94 }
95
96 void ChannelListDlg::showFilterLine(bool show) {
97   ui.line->setVisible(show);
98   ui.filterLabel->setVisible(show);
99   ui.filterLineEdit->setVisible(show);
100 }
101
102 void ChannelListDlg::enableQuery(bool enable) {
103   ui.channelNameLineEdit->setEnabled(enable);
104   ui.searchChannelsButton->setEnabled(enable);
105 }
106
107 void ChannelListDlg::setAdvancedMode(bool advanced) {
108   _advancedMode = advanced;
109
110   QHBoxLayout *searchLayout = 0;
111 #if QT_VERSION >=  0x040400
112   searchLayout = ui.searchLayout;
113 #else
114   // FIXME: REMOVE WHEN WE DEPEND ON Qt 4.4
115   /*
116    * ok this just sucks: in Qt 4.3 there is no way to search for a layout as uic creates
117    * them without a parent -.-
118    * in this case there are only 2 candidates: ui.hboxLayout and ui.hboxLayout1
119    */
120   if(ui.hboxLayout.findWidget(ui.searchPatternLabel) != -1)
121     searchLayout = ui.hboxLayout;
122   else if(ui.hboxLayout1.findWidget(ui.searchPatternLabel) != -1)
123     searchLayout = ui.hboxLayout1;
124   else
125     /* if this assert trigger we have been compiled on a too old Qt
126      * or uic generated something very unexpected. we cannot find the layout to manipulate.
127      * Please upgrade to a recent version of Qt.
128      */
129     Q_ASSERT(false);
130 #endif
131
132   if(advanced) {
133     if(_simpleModeSpacer) {
134       searchLayout->removeItem(_simpleModeSpacer);
135       delete _simpleModeSpacer;
136       _simpleModeSpacer = 0;
137     }
138     ui.advancedModeLabel->setPixmap(QPixmap(QString::fromUtf8(":/22x22/actions/oxygen/22x22/actions/edit-clear-locationbar-rtl.png")));
139   } else {
140     if(!_simpleModeSpacer) {
141       _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
142       searchLayout->insertSpacerItem(0, _simpleModeSpacer);
143     }
144     ui.advancedModeLabel->setPixmap(QPixmap(QString::fromUtf8(":/22x22/actions/oxygen/22x22/actions/edit-clear.png")));
145   }
146   ui.channelNameLineEdit->clear();
147   ui.channelNameLineEdit->setVisible(advanced);
148   ui.searchPatternLabel->setVisible(advanced);
149 }
150
151 void ChannelListDlg::showErrors(bool show) {
152   if(!show) {
153     ui.errorTextEdit->clear();
154   }
155   ui.errorLabel->setVisible(show);
156   ui.errorTextEdit->setVisible(show);
157 }
158
159
160 void ChannelListDlg::reportFinishedList() {
161   _listFinished = true;
162 }
163
164 void ChannelListDlg::showError(const QString &error) {
165   showErrors(true);
166   ui.errorTextEdit->moveCursor(QTextCursor::End);
167   ui.errorTextEdit->insertPlainText(error + "\n");
168 }
169
170 void ChannelListDlg::joinChannel(const QModelIndex &index) {
171   Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
172 }