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