1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "channellistdlg.h"
23 #include <QHeaderView>
24 #include <QHBoxLayout>
25 #include <QSpacerItem>
28 #include "clientirclisthelper.h"
31 ChannelListDlg::ChannelListDlg(QWidget *parent)
39 _sortFilter.setSourceModel(&_ircListModel);
40 _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
41 _sortFilter.setFilterKeyColumn(-1);
44 ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(22));
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 // Sort A-Z by default
53 ui.channelListView->sortByColumn(0, Qt::AscendingOrder);
54 ui.channelListView->verticalHeader()->hide();
55 ui.channelListView->horizontalHeader()->setStretchLastSection(true);
57 ui.searchChannelsButton->setAutoDefault(false);
59 setWindowIcon(icon::get("format-list-unordered"));
61 connect(ui.advancedModeLabel, SIGNAL(clicked()), this, SLOT(toggleMode()));
62 connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
63 connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
64 connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
65 connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription> )),
66 this, SLOT(receiveChannelList(NetworkId, QStringList, QList<IrcListHelper::ChannelDescription> )));
67 connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
68 connect(Client::ircListHelper(), SIGNAL(errorReported(const QString &)), this, SLOT(showError(const QString &)));
69 connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
71 setAdvancedMode(false);
73 showFilterLine(false);
76 // Set initial input focus
81 void ChannelListDlg::setNetwork(NetworkId netId)
87 _ircListModel.setChannelList();
88 showFilterLine(false);
92 void ChannelListDlg::setChannelFilters(const QString &channelFilters)
94 // Enable advanced mode if searching
95 setAdvancedMode(!channelFilters.isEmpty());
96 // Set channel search text after setting advanced mode so it's not cleared
97 ui.channelNameLineEdit->setText(channelFilters.trimmed());
101 void ChannelListDlg::requestSearch()
103 if (!_netId.isValid()) {
104 // No valid network set yet
108 _listFinished = false;
111 QStringList channelFilters;
112 channelFilters << ui.channelNameLineEdit->text().trimmed();
113 Client::ircListHelper()->requestChannelList(_netId, channelFilters);
117 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList)
119 Q_UNUSED(channelFilters)
123 showFilterLine(!channelList.isEmpty());
124 _ircListModel.setChannelList(channelList);
125 enableQuery(_listFinished);
126 // Reset input focus since UI changed
131 void ChannelListDlg::showFilterLine(bool show)
133 ui.line->setVisible(show);
134 ui.filterLabel->setVisible(show);
135 ui.filterLineEdit->setVisible(show);
139 void ChannelListDlg::enableQuery(bool enable)
141 ui.channelNameLineEdit->setEnabled(enable);
142 ui.searchChannelsButton->setEnabled(enable);
146 void ChannelListDlg::setAdvancedMode(bool advanced)
148 _advancedMode = advanced;
151 if (_simpleModeSpacer) {
152 ui.searchLayout->removeItem(_simpleModeSpacer);
153 delete _simpleModeSpacer;
154 _simpleModeSpacer = 0;
156 ui.advancedModeLabel->setPixmap(icon::get("edit-clear-locationbar-rtl").pixmap(16));
159 if (!_simpleModeSpacer) {
160 _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
161 ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
163 ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(16));
166 ui.channelNameLineEdit->clear();
167 ui.channelNameLineEdit->setVisible(advanced);
168 ui.searchPatternLabel->setVisible(advanced);
172 void ChannelListDlg::updateInputFocus()
174 // Update keyboard focus to match what options are available. Prioritize the channel name
175 // editor as one likely won't need to filter when already limiting the list.
176 if (ui.channelNameLineEdit->isVisible()) {
177 ui.channelNameLineEdit->setFocus();
178 } else if (ui.filterLineEdit->isVisible()) {
179 ui.filterLineEdit->setFocus();
184 void ChannelListDlg::showErrors(bool show)
187 ui.errorTextEdit->clear();
189 ui.errorLabel->setVisible(show);
190 ui.errorTextEdit->setVisible(show);
194 void ChannelListDlg::reportFinishedList()
196 _listFinished = true;
200 void ChannelListDlg::showError(const QString &error)
203 ui.errorTextEdit->moveCursor(QTextCursor::End);
204 ui.errorTextEdit->insertPlainText(error + "\n");
208 void ChannelListDlg::joinChannel(const QModelIndex &index)
210 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));