client: Convert /list #chan to UI advanced search
[quassel.git] / src / qtui / channellistdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "channellistdlg.h"
22
23 #include <QHeaderView>
24 #include <QHBoxLayout>
25 #include <QSpacerItem>
26
27 #include "client.h"
28 #include "clientirclisthelper.h"
29 #include "icon.h"
30
31 ChannelListDlg::ChannelListDlg(QWidget *parent)
32     : QDialog(parent),
33     _listFinished(true),
34     _ircListModel(this),
35     _sortFilter(this),
36     _simpleModeSpacer(0),
37     _advancedMode(false)
38 {
39     _sortFilter.setSourceModel(&_ircListModel);
40     _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
41     _sortFilter.setFilterKeyColumn(-1);
42
43     ui.setupUi(this);
44     ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(22));
45
46     ui.channelListView->setSelectionBehavior(QAbstractItemView::SelectRows);
47     ui.channelListView->setSelectionMode(QAbstractItemView::SingleSelection);
48     ui.channelListView->setAlternatingRowColors(true);
49     ui.channelListView->setTabKeyNavigation(false);
50     ui.channelListView->setModel(&_sortFilter);
51     ui.channelListView->setSortingEnabled(true);
52     ui.channelListView->verticalHeader()->hide();
53     ui.channelListView->horizontalHeader()->setStretchLastSection(true);
54
55     ui.searchChannelsButton->setAutoDefault(false);
56
57     setWindowIcon(icon::get("format-list-unordered"));
58
59     connect(ui.advancedModeLabel, SIGNAL(clicked()), this, SLOT(toggleMode()));
60     connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
61     connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
62     connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
63     connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription> )),
64         this, SLOT(receiveChannelList(NetworkId, QStringList, QList<IrcListHelper::ChannelDescription> )));
65     connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
66     connect(Client::ircListHelper(), SIGNAL(errorReported(const QString &)), this, SLOT(showError(const QString &)));
67     connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
68
69     setAdvancedMode(false);
70     enableQuery(true);
71     showFilterLine(false);
72     showErrors(false);
73 }
74
75
76 void ChannelListDlg::setNetwork(NetworkId netId)
77 {
78     if (_netId == netId)
79         return;
80
81     _netId = netId;
82     _ircListModel.setChannelList();
83     showFilterLine(false);
84 }
85
86
87 void ChannelListDlg::setChannelFilters(const QString &channelFilters)
88 {
89     // Enable advanced mode if searching
90     setAdvancedMode(!channelFilters.isEmpty());
91     // Set channel search text after setting advanced mode so it's not cleared
92     ui.channelNameLineEdit->setText(channelFilters.trimmed());
93 }
94
95
96 void ChannelListDlg::requestSearch()
97 {
98     _listFinished = false;
99     enableQuery(false);
100     showErrors(false);
101     QStringList channelFilters;
102     channelFilters << ui.channelNameLineEdit->text().trimmed();
103     Client::ircListHelper()->requestChannelList(_netId, channelFilters);
104 }
105
106
107 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList)
108 {
109     Q_UNUSED(channelFilters)
110     if (netId != _netId)
111         return;
112
113     showFilterLine(!channelList.isEmpty());
114     _ircListModel.setChannelList(channelList);
115     enableQuery(_listFinished);
116 }
117
118
119 void ChannelListDlg::showFilterLine(bool show)
120 {
121     ui.line->setVisible(show);
122     ui.filterLabel->setVisible(show);
123     ui.filterLineEdit->setVisible(show);
124 }
125
126
127 void ChannelListDlg::enableQuery(bool enable)
128 {
129     ui.channelNameLineEdit->setEnabled(enable);
130     ui.searchChannelsButton->setEnabled(enable);
131 }
132
133
134 void ChannelListDlg::setAdvancedMode(bool advanced)
135 {
136     _advancedMode = advanced;
137
138     if (advanced) {
139         if (_simpleModeSpacer) {
140             ui.searchLayout->removeItem(_simpleModeSpacer);
141             delete _simpleModeSpacer;
142             _simpleModeSpacer = 0;
143         }
144         ui.advancedModeLabel->setPixmap(icon::get("edit-clear-locationbar-rtl").pixmap(16));
145     }
146     else {
147         if (!_simpleModeSpacer) {
148             _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
149             ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
150         }
151         ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(16));
152     }
153
154     ui.channelNameLineEdit->clear();
155     ui.channelNameLineEdit->setVisible(advanced);
156     ui.searchPatternLabel->setVisible(advanced);
157 }
158
159
160 void ChannelListDlg::showErrors(bool show)
161 {
162     if (!show) {
163         ui.errorTextEdit->clear();
164     }
165     ui.errorLabel->setVisible(show);
166     ui.errorTextEdit->setVisible(show);
167 }
168
169
170 void ChannelListDlg::reportFinishedList()
171 {
172     _listFinished = true;
173 }
174
175
176 void ChannelListDlg::showError(const QString &error)
177 {
178     showErrors(true);
179     ui.errorTextEdit->moveCursor(QTextCursor::End);
180     ui.errorTextEdit->insertPlainText(error + "\n");
181 }
182
183
184 void ChannelListDlg::joinChannel(const QModelIndex &index)
185 {
186     Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
187 }