Fixed those nasty "Client::updateLastSeen(): Unknown buffer $bufferId" messages.
[quassel.git] / src / uisupport / bufferview.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "client.h"
22 #include "buffersyncer.h"
23 #include "bufferview.h"
24 #include "networkmodel.h"
25 #include "network.h"
26
27 #include "uisettings.h"
28
29 /*****************************************
30 * The TreeView showing the Buffers
31 *****************************************/
32 // Please be carefull when reimplementing methods which are used to inform the view about changes to the data
33 // to be on the safe side: call QTreeView's method aswell
34 BufferView::BufferView(QWidget *parent) : QTreeView(parent) {
35   setContextMenuPolicy(Qt::CustomContextMenu);
36
37   connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
38           this, SLOT(showContextMenu(const QPoint &)));
39 }
40
41 void BufferView::init() {
42   setIndentation(10);
43   header()->setContextMenuPolicy(Qt::ActionsContextMenu);
44   hideColumn(1);
45   hideColumn(2);
46   expandAll();
47
48   setAnimated(true);
49
50 #ifndef QT_NO_DRAGANDDROP
51   setDragEnabled(true);
52   setAcceptDrops(true);
53   setDropIndicatorShown(true);
54 #endif
55
56   setSortingEnabled(true);
57   sortByColumn(0, Qt::AscendingOrder);
58   connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(joinChannel(QModelIndex)));
59 }
60
61 void BufferView::setFilteredModel(QAbstractItemModel *model, BufferViewFilter::Modes mode, QList<NetworkId> nets) {
62   BufferViewFilter *filter = new BufferViewFilter(model, mode, nets);
63   setModel(filter);
64   connect(this, SIGNAL(removeBuffer(const QModelIndex &)), filter, SLOT(removeBuffer(const QModelIndex &)));
65 }
66
67 void BufferView::setModel(QAbstractItemModel *model) {
68   delete selectionModel();
69   QTreeView::setModel(model);
70   init();
71
72   // remove old Actions
73   QList<QAction *> oldactions = header()->actions();
74   foreach(QAction *action, oldactions) {
75     header()->removeAction(action);
76     action->deleteLater();
77   }
78
79   QString sectionName;
80   QAction *showSection;
81   for(int i = 1; i < model->columnCount(); i++) {
82     sectionName = (model->headerData(i, Qt::Horizontal, Qt::DisplayRole)).toString();
83     showSection = new QAction(sectionName, header());
84     showSection->setCheckable(true);
85     showSection->setChecked(!isColumnHidden(i));
86     showSection->setProperty("column", i);
87     connect(showSection, SIGNAL(toggled(bool)), this, SLOT(toggleHeader(bool)));
88     header()->addAction(showSection);
89   }
90   
91 }
92
93 void BufferView::joinChannel(const QModelIndex &index) {
94   BufferInfo::Type bufferType = (BufferInfo::Type)index.data(NetworkModel::BufferTypeRole).value<int>();
95
96   if(bufferType != BufferInfo::ChannelBuffer)
97     return;
98
99   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
100   
101   Client::userInput(bufferInfo, QString("/JOIN %1").arg(bufferInfo.bufferName()));
102 }
103
104 void BufferView::keyPressEvent(QKeyEvent *event) {
105   if(event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete) {
106     event->accept();
107     QModelIndex index = selectionModel()->selectedIndexes().first();
108     if(index.isValid()) {
109       emit removeBuffer(index);
110     }
111   }
112   QTreeView::keyPressEvent(event);
113 }
114
115 // ensure that newly inserted network nodes are expanded per default
116 void BufferView::rowsInserted(const QModelIndex & parent, int start, int end) {
117   QTreeView::rowsInserted(parent, start, end);
118   if(model()->rowCount(parent) == 1 && parent != QModelIndex()) {
119     // without updating the parent the expand will have no effect... Qt Bug?
120     update(parent); 
121     expand(parent);
122   }
123 }
124
125 void BufferView::toggleHeader(bool checked) {
126   QAction *action = qobject_cast<QAction *>(sender());
127   header()->setSectionHidden((action->property("column")).toInt(), !checked);
128 }
129
130 void BufferView::showContextMenu(const QPoint &pos) {
131   QModelIndex index = indexAt(pos);
132   if(!index.isValid()) return;
133   QMenu contextMenu(this);
134   QAction *connectNetAction = new QAction(tr("Connect"), this);
135   QAction *disconnectNetAction = new QAction(tr("Disconnect"), this);
136
137   QAction *joinBufferAction = new QAction(tr("Join"), this);
138   QAction *partBufferAction = new QAction(tr("Part"), this);
139   QAction *removeBufferAction = new QAction(tr("Delete buffer"), this);
140
141   QMenu *hideEventsMenu = new QMenu(tr("Hide Events"), this);
142   QAction *hideJoinAction = hideEventsMenu->addAction(tr("Join Events"));
143   QAction *hidePartAction = hideEventsMenu->addAction(tr("Part Events"));
144   QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
145   QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
146   QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
147   hideJoinAction->setCheckable(true);
148   hidePartAction->setCheckable(true);
149   hideKillAction->setCheckable(true);
150   hideQuitAction->setCheckable(true);
151   hideModeAction->setCheckable(true);
152   hideJoinAction->setEnabled(false);
153   hidePartAction->setEnabled(false);
154   hideKillAction->setEnabled(false);
155   hideQuitAction->setEnabled(false);
156   hideModeAction->setEnabled(false);
157
158   QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
159   ignoreListAction->setEnabled(false);
160   QAction *whoBufferAction = new QAction(tr("WHO"), this);
161
162   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
163     if(index.data(NetworkModel::ItemActiveRole).toBool()) {
164       contextMenu.addAction(disconnectNetAction);
165     } else {
166       contextMenu.addAction(connectNetAction);
167     }
168   }
169
170   BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
171   QString channelname = index.sibling(index.row(), 0).data().toString();
172
173   if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
174     if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
175     contextMenu.addAction(joinBufferAction);
176     contextMenu.addAction(partBufferAction);
177     contextMenu.addAction(removeBufferAction);
178     contextMenu.addMenu(hideEventsMenu);
179     contextMenu.addAction(ignoreListAction);
180     contextMenu.addAction(whoBufferAction);
181
182     if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
183       if(index.data(NetworkModel::ItemActiveRole).toBool()) {
184         removeBufferAction->setEnabled(false);
185         removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
186         joinBufferAction->setVisible(false);
187       } else {
188         partBufferAction->setVisible(false);
189       }
190     } else {
191       joinBufferAction->setVisible(false);
192       partBufferAction->setVisible(false);
193     }
194   }
195
196   QAction *result = contextMenu.exec(QCursor::pos());
197   if(result == connectNetAction || result == disconnectNetAction) {
198     const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
199     if(!network) return;
200     if(network->connectionState() == Network::Disconnected) 
201       network->requestConnect();
202     else 
203       network->requestDisconnect();
204   } else
205   if(result == joinBufferAction) {
206     Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
207   } else
208   if(result == partBufferAction) {
209     Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
210   } else
211   if(result == removeBufferAction) {
212     int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
213                                     tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
214                                        "data, from the core's database!").arg(bufferInfo.bufferName()),
215                                         QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
216     if(res == QMessageBox::Yes) {
217       Client::removeBuffer(bufferInfo.bufferId());
218     } 
219   } else 
220   if(result == whoBufferAction) {
221     Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));
222   }
223 }
224
225 void BufferView::wheelEvent(QWheelEvent* event)
226 {
227   UiSettings s;
228   if(s.value("MouseWheelChangesBuffers",QVariant(true)).toBool()) {
229     int rowDelta = ( event->delta() > 0 ) ? -1 : 1;
230     QModelIndex currentIndex = selectionModel()->currentIndex();
231     QModelIndex resultingIndex;
232     if( model()->hasIndex(  currentIndex.row() + rowDelta, currentIndex.column(), currentIndex.parent() ) )
233     {
234         resultingIndex = currentIndex.sibling( currentIndex.row() + rowDelta, currentIndex.column() );
235     }
236     else //if we scroll into a the parent node...
237     {
238         QModelIndex parent = currentIndex.parent();
239         QModelIndex aunt = parent.sibling( parent.row() + rowDelta, parent.column() );
240         if( rowDelta == -1 )
241             resultingIndex = aunt.child( model()->rowCount( aunt ) - 1, 0 );
242         else
243             resultingIndex = aunt.child( 0, 0 );
244         if( !resultingIndex.isValid() )
245             return;
246     }
247     selectionModel()->setCurrentIndex( resultingIndex, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
248     selectionModel()->select( resultingIndex, QItemSelectionModel::ClearAndSelect );
249   } else {
250     QAbstractScrollArea::wheelEvent(event);
251   }
252 }
253