common: Remove the copy assignment operator from DccConfig
[quassel.git] / src / qtui / channellistdlg.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 <QHBoxLayout>
24 #include <QHeaderView>
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     , _ircListModel(this)
34     , _sortFilter(this)
35 {
36     _sortFilter.setSourceModel(&_ircListModel);
37     _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
38     _sortFilter.setFilterKeyColumn(-1);
39
40     ui.setupUi(this);
41     ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(22));
42
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);
53
54     ui.searchChannelsButton->setAutoDefault(false);
55
56     setWindowIcon(icon::get("format-list-unordered"));
57
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);
66
67     setAdvancedMode(false);
68     enableQuery(true);
69     showFilterLine(false);
70     showErrors(false);
71
72     // Set initial input focus
73     updateInputFocus();
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 void ChannelListDlg::setChannelFilters(const QString& channelFilters)
87 {
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());
92 }
93
94 void ChannelListDlg::requestSearch()
95 {
96     if (!_netId.isValid()) {
97         // No valid network set yet
98         return;
99     }
100
101     _listFinished = false;
102     enableQuery(false);
103     showErrors(false);
104     QStringList channelFilters;
105     channelFilters << ui.channelNameLineEdit->text().trimmed();
106     Client::ircListHelper()->requestChannelList(_netId, channelFilters);
107 }
108
109 void ChannelListDlg::receiveChannelList(const NetworkId& netId,
110                                         const QStringList& channelFilters,
111                                         const QList<IrcListHelper::ChannelDescription>& channelList)
112 {
113     Q_UNUSED(channelFilters)
114     if (netId != _netId)
115         return;
116
117     showFilterLine(!channelList.isEmpty());
118     _ircListModel.setChannelList(channelList);
119     enableQuery(_listFinished);
120     // Reset input focus since UI changed
121     updateInputFocus();
122 }
123
124 void ChannelListDlg::showFilterLine(bool show)
125 {
126     ui.line->setVisible(show);
127     ui.filterLabel->setVisible(show);
128     ui.filterLineEdit->setVisible(show);
129 }
130
131 void ChannelListDlg::enableQuery(bool enable)
132 {
133     ui.channelNameLineEdit->setEnabled(enable);
134     ui.searchChannelsButton->setEnabled(enable);
135 }
136
137 void ChannelListDlg::setAdvancedMode(bool advanced)
138 {
139     _advancedMode = advanced;
140
141     if (advanced) {
142         if (_simpleModeSpacer) {
143             ui.searchLayout->removeItem(_simpleModeSpacer);
144             delete _simpleModeSpacer;
145             _simpleModeSpacer = nullptr;
146         }
147         ui.advancedModeLabel->setPixmap(icon::get("edit-clear-locationbar-rtl").pixmap(16));
148     }
149     else {
150         if (!_simpleModeSpacer) {
151             _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
152             ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
153         }
154         ui.advancedModeLabel->setPixmap(icon::get("edit-rename").pixmap(16));
155     }
156
157     ui.channelNameLineEdit->clear();
158     ui.channelNameLineEdit->setVisible(advanced);
159     ui.searchPatternLabel->setVisible(advanced);
160 }
161
162 void ChannelListDlg::updateInputFocus()
163 {
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();
168     }
169     else if (ui.filterLineEdit->isVisible()) {
170         ui.filterLineEdit->setFocus();
171     }
172 }
173
174 void ChannelListDlg::showErrors(bool show)
175 {
176     if (!show) {
177         ui.errorTextEdit->clear();
178     }
179     ui.errorLabel->setVisible(show);
180     ui.errorTextEdit->setVisible(show);
181 }
182
183 void ChannelListDlg::reportFinishedList()
184 {
185     _listFinished = true;
186 }
187
188 void ChannelListDlg::showError(const QString& error)
189 {
190     showErrors(true);
191     ui.errorTextEdit->moveCursor(QTextCursor::End);
192     ui.errorTextEdit->insertPlainText(error + "\n");
193 }
194
195 void ChannelListDlg::joinChannel(const QModelIndex& index)
196 {
197     Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId),
198                                   QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));
199 }