1 /***************************************************************************
2 * Copyright (C) 2005-2020 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 <QHBoxLayout>
24 #include <QHeaderView>
25 #include <QSpacerItem>
28 #include "clientirclisthelper.h"
31 ChannelListDlg::ChannelListDlg(QWidget* parent)
36 _sortFilter.setSourceModel(&_ircListModel);
37 _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
38 _sortFilter.setFilterKeyColumn(-1);
41 ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(22));
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 // Sort A-Z by default
50 ui.channelListView->sortByColumn(0, Qt::AscendingOrder);
51 ui.channelListView->verticalHeader()->hide();
52 ui.channelListView->horizontalHeader()->setStretchLastSection(true);
54 ui.searchChannelsButton->setAutoDefault(false);
56 setWindowIcon(icon::get("format-list-unordered"));
58 connect(ui.advancedModeLabel, &ClickableLabel::clicked, this, &ChannelListDlg::toggleMode);
59 connect(ui.searchChannelsButton, &QAbstractButton::clicked, this, &ChannelListDlg::requestSearch);
60 connect(ui.channelNameLineEdit, &QLineEdit::returnPressed, this, &ChannelListDlg::requestSearch);
61 connect(ui.filterLineEdit, &QLineEdit::textChanged, &_sortFilter, &QSortFilterProxyModel::setFilterFixedString);
62 connect(Client::ircListHelper(), &ClientIrcListHelper::channelListReceived, this, &ChannelListDlg::receiveChannelList);
63 connect(Client::ircListHelper(), &ClientIrcListHelper::finishedListReported, this, &ChannelListDlg::reportFinishedList);
64 connect(Client::ircListHelper(), &ClientIrcListHelper::errorReported, this, &ChannelListDlg::showError);
65 connect(ui.channelListView, &QAbstractItemView::activated, this, &ChannelListDlg::joinChannel);
67 setAdvancedMode(false);
69 showFilterLine(false);
72 // Set initial input focus
76 void ChannelListDlg::setNetwork(NetworkId netId)
82 _ircListModel.setChannelList();
83 showFilterLine(false);
86 void ChannelListDlg::setChannelFilters(const QString& channelFilters)
88 // Enable advanced mode if searching
89 setAdvancedMode(!channelFilters.isEmpty());
90 // Set channel search text after setting advanced mode so it's not cleared
91 ui.channelNameLineEdit->setText(channelFilters.trimmed());
94 void ChannelListDlg::requestSearch()
96 if (!_netId.isValid()) {
97 // No valid network set yet
101 _listFinished = false;
104 QStringList channelFilters;
105 channelFilters << ui.channelNameLineEdit->text().trimmed();
106 Client::ircListHelper()->requestChannelList(_netId, channelFilters);
109 void ChannelListDlg::receiveChannelList(const NetworkId& netId,
110 const QStringList& channelFilters,
111 const QList<IrcListHelper::ChannelDescription>& channelList)
113 Q_UNUSED(channelFilters)
117 showFilterLine(!channelList.isEmpty());
118 _ircListModel.setChannelList(channelList);
119 enableQuery(_listFinished);
120 // Reset input focus since UI changed
124 void ChannelListDlg::showFilterLine(bool show)
126 ui.line->setVisible(show);
127 ui.filterLabel->setVisible(show);
128 ui.filterLineEdit->setVisible(show);
131 void ChannelListDlg::enableQuery(bool enable)
133 ui.channelNameLineEdit->setEnabled(enable);
134 ui.searchChannelsButton->setEnabled(enable);
137 void ChannelListDlg::setAdvancedMode(bool advanced)
139 _advancedMode = advanced;
142 if (_simpleModeSpacer) {
143 ui.searchLayout->removeItem(_simpleModeSpacer);
144 delete _simpleModeSpacer;
145 _simpleModeSpacer = nullptr;
147 ui.advancedModeLabel->setPixmap(icon::get("edit-clear-locationbar-rtl").pixmap(16));
150 if (!_simpleModeSpacer) {
151 _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
152 ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
154 ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(16));
157 ui.channelNameLineEdit->clear();
158 ui.channelNameLineEdit->setVisible(advanced);
159 ui.searchPatternLabel->setVisible(advanced);
162 void ChannelListDlg::updateInputFocus()
164 // Update keyboard focus to match what options are available. Prioritize the channel name
165 // editor as one likely won't need to filter when already limiting the list.
166 if (ui.channelNameLineEdit->isVisible()) {
167 ui.channelNameLineEdit->setFocus();
169 else if (ui.filterLineEdit->isVisible()) {
170 ui.filterLineEdit->setFocus();
174 void ChannelListDlg::showErrors(bool show)
177 ui.errorTextEdit->clear();
179 ui.errorLabel->setVisible(show);
180 ui.errorTextEdit->setVisible(show);
183 void ChannelListDlg::reportFinishedList()
185 _listFinished = true;
188 void ChannelListDlg::showError(const QString& error)
191 ui.errorTextEdit->moveCursor(QTextCursor::End);
192 ui.errorTextEdit->insertPlainText(error + "\n");
195 void ChannelListDlg::joinChannel(const QModelIndex& index)
197 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId),
198 QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));