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 ***************************************************************************/
22 #include "buffersyncer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
27 /*****************************************
28 * The TreeView showing the Buffers
29 *****************************************/
30 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
31 // to be on the safe side: call QTreeView's method aswell
32 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
33 setContextMenuPolicy(Qt::CustomContextMenu);
35 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
36 this, SLOT(showContextMenu(const QPoint &)));
39 void BufferView::init() {
41 header()->setContextMenuPolicy(Qt::ActionsContextMenu);
48 #ifndef QT_NO_DRAGANDDROP
51 setDropIndicatorShown(true);
54 setSortingEnabled(true);
55 sortByColumn(0, Qt::AscendingOrder);
56 connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
59 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
60 BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
62 connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
65 void BufferView::setModel(QAbstractItemModel *model) {
66 delete selectionModel();
67 QTreeView::setModel(model);
71 QList<QAction *> oldactions = header()->actions();
72 foreach(QAction *action, oldactions) {
73 header()->removeAction(action);
74 action->deleteLater();
79 for(int i = 0; i < model->columnCount(); i++) {
80 sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
81 showSection = new QAction(sectionName, header());
82 showSection->setCheckable(true);
83 showSection->setChecked(!isColumnHidden(i));
84 showSection->setProperty("column", i);
85 connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
86 header()->addAction(showSection);
91 void BufferView::joinChannel(const QModelIndex &index) {
92 BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
94 if(bufferType != BufferInfo::ChannelBuffer)
97 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
99 Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
102 void BufferView::keyPressEvent(QKeyEvent *event) {
103 if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
105 QModelIndex index = selectionModel()->selectedIndexes().first();
106 if(index.isValid()) {
107 emit removeBuffer(index);
110 QTreeView::keyPressEvent(event);
113 // ensure that newly inserted network nodes are expanded per default
114 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
115 QTreeView::rowsInserted(parent, start, end);
116 if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
117 // without updating the parent the expand will have no effect... Qt Bug?
123 void BufferView::toggleHeader(bool checked) {
124 QAction *action = qobject_cast<QAction *>(sender());
125 header()->setSectionHidden((action->property("column")).toInt(), !checked);
128 void BufferView::showContextMenu(const QPoint &pos) {
129 QModelIndex index = indexAt(pos);
130 if(!index.isValid()) return;
131 QMenu contextMenu(this);
132 QAction *connectNetAction = new QAction(tr("Connect"), this);
133 QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
135 QAction *joinBufferAction = new QAction(tr("Join"), this);
136 QAction *partBufferAction = new QAction(tr("Part"), this);
137 QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
139 QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
140 QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
141 QAction *hidePartAction = hideEventsMenu->addAction(tr("PartEvents"));
142 QAction *hideQuitAction = hideEventsMenu->addAction(tr("QuitEvents"));
143 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
144 hideJoinAction->setCheckable(true);
145 hidePartAction->setCheckable(true);
146 hideQuitAction->setCheckable(true);
147 hideModeAction->setCheckable(true);
148 hideJoinAction->setEnabled(false);
149 hidePartAction->setEnabled(false);
150 hideQuitAction->setEnabled(false);
151 hideModeAction->setEnabled(false);
153 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
154 ignoreListAction->setEnabled(false);
155 QAction *whoBufferAction = new QAction(tr("WHO"), this);
157 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
158 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
159 contextMenu.addAction(disconnectNetAction);
161 contextMenu.addAction(connectNetAction);
165 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
166 QString channelname = index.sibling(index.row(), 0).data().toString();
168 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
169 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
170 contextMenu.addAction(joinBufferAction);
171 contextMenu.addAction(partBufferAction);
172 contextMenu.addAction(removeBufferAction);
173 contextMenu.addMenu(hideEventsMenu);
174 contextMenu.addAction(ignoreListAction);
175 contextMenu.addAction(whoBufferAction);
177 if(bufferInfo.type() == BufferInfo::ChannelBuffer && index.data(NetworkModel::ItemActiveRole).toBool()) {
178 removeBufferAction->setEnabled(false);
179 joinBufferAction->setVisible(false);
181 partBufferAction->setVisible(false);
185 QAction *result = contextMenu.exec(QCursor::pos());
186 if(result == connectNetAction || result == disconnectNetAction) {
187 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
189 if(network->connectionState() == Network::Disconnected)
190 network->requestConnect();
192 network->requestDisconnect();
194 if(result == joinBufferAction) {
195 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
197 if(result == partBufferAction) {
198 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
200 if(result == removeBufferAction) {
201 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
202 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
203 "data, from the core's database!").arg(bufferInfo.bufferName()),
204 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
205 if(res == QMessageBox::Yes) {
206 Client::bufferSyncer()->requestRemoveBuffer(bufferInfo.bufferId());
209 if(result == whoBufferAction) {
210 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));