1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "channellistdlg.h"
24 #include "clientirclisthelper.h"
26 #include <QHeaderView>
27 #include <QHBoxLayout>
28 #include <QSpacerItem>
30 ChannelListDlg::ChannelListDlg(QWidget *parent)
38 _sortFilter.setSourceModel(&_ircListModel);
39 _sortFilter.setFilterCaseSensitivity(Qt::CaseInsensitive);
40 _sortFilter.setFilterKeyColumn(-1);
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 ui.channelListView->verticalHeader()->hide();
50 ui.channelListView->horizontalHeader()->setStretchLastSection(true);
52 ui.searchChannelsButton->setAutoDefault(false);
54 connect(ui.advancedModeLabel, SIGNAL(clicked()), this, SLOT(toggleMode()));
55 connect(ui.searchChannelsButton, SIGNAL(clicked()), this, SLOT(requestSearch()));
56 connect(ui.channelNameLineEdit, SIGNAL(returnPressed()), this, SLOT(requestSearch()));
57 connect(ui.filterLineEdit, SIGNAL(textChanged(QString)), &_sortFilter, SLOT(setFilterFixedString(QString)));
58 connect(Client::ircListHelper(), SIGNAL(channelListReceived(const NetworkId &, const QStringList &, QList<IrcListHelper::ChannelDescription>)),
59 this, SLOT(receiveChannelList(NetworkId , QStringList, QList<IrcListHelper::ChannelDescription>)));
60 connect(Client::ircListHelper(), SIGNAL(finishedListReported(const NetworkId &)), this, SLOT(reportFinishedList()));
61 connect(Client::ircListHelper(), SIGNAL(errorReported(const QString &)), this, SLOT(showError(const QString &)));
62 connect(ui.channelListView, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
64 setAdvancedMode(false);
66 showFilterLine(false);
70 void ChannelListDlg::setNetwork(NetworkId netId) {
75 _ircListModel.setChannelList();
76 showFilterLine(false);
79 void ChannelListDlg::requestSearch() {
80 _listFinished = false;
83 QStringList channelFilters;
84 channelFilters << ui.channelNameLineEdit->text().trimmed();
85 Client::ircListHelper()->requestChannelList(_netId, channelFilters);
88 void ChannelListDlg::receiveChannelList(const NetworkId &netId, const QStringList &channelFilters, const QList<IrcListHelper::ChannelDescription> &channelList) {
89 Q_UNUSED(channelFilters)
93 showFilterLine(!channelList.isEmpty());
94 _ircListModel.setChannelList(channelList);
95 enableQuery(_listFinished);
98 void ChannelListDlg::showFilterLine(bool show) {
99 ui.line->setVisible(show);
100 ui.filterLabel->setVisible(show);
101 ui.filterLineEdit->setVisible(show);
104 void ChannelListDlg::enableQuery(bool enable) {
105 ui.channelNameLineEdit->setEnabled(enable);
106 ui.searchChannelsButton->setEnabled(enable);
109 void ChannelListDlg::setAdvancedMode(bool advanced) {
110 _advancedMode = advanced;
112 #if QT_VERSION >= 0x040400
113 // FIXME: remove if macro when we depend on Qt 4.4
115 if(_simpleModeSpacer) {
116 ui.searchLayout->removeItem(_simpleModeSpacer);
117 delete _simpleModeSpacer;
118 _simpleModeSpacer = 0;
120 ui.advancedModeLabel->setPixmap(QPixmap(QString::fromUtf8(":/22x22/actions/oxygen/22x22/actions/edit-clear-locationbar-rtl.png")));
122 if(!_simpleModeSpacer) {
123 _simpleModeSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
124 ui.searchLayout->insertSpacerItem(0, _simpleModeSpacer);
126 ui.advancedModeLabel->setPixmap(QPixmap(QString::fromUtf8(":/22x22/actions/oxygen/22x22/actions/edit-clear.png")));
130 ui.channelNameLineEdit->clear();
131 ui.channelNameLineEdit->setVisible(advanced);
132 ui.searchPatternLabel->setVisible(advanced);
135 void ChannelListDlg::showErrors(bool show) {
137 ui.errorTextEdit->clear();
139 ui.errorLabel->setVisible(show);
140 ui.errorTextEdit->setVisible(show);
144 void ChannelListDlg::reportFinishedList() {
145 _listFinished = true;
148 void ChannelListDlg::showError(const QString &error) {
150 ui.errorTextEdit->moveCursor(QTextCursor::End);
151 ui.errorTextEdit->insertPlainText(error + "\n");
154 void ChannelListDlg::joinChannel(const QModelIndex &index) {
155 Client::instance()->userInput(BufferInfo::fakeStatusBuffer(_netId), QString("/JOIN %1").arg(index.sibling(index.row(), 0).data().toString()));