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 = 1; 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("Part Events"));
142 QAction *hideKillAction = hideEventsMenu->addAction(tr("Kill Events"));
143 QAction *hideQuitAction = hideEventsMenu->addAction(tr("Quit Events"));
144 QAction *hideModeAction = hideEventsMenu->addAction(tr("Mode Events"));
145 hideJoinAction->setCheckable(true);
146 hidePartAction->setCheckable(true);
147 hideKillAction->setCheckable(true);
148 hideQuitAction->setCheckable(true);
149 hideModeAction->setCheckable(true);
150 hideJoinAction->setEnabled(false);
151 hidePartAction->setEnabled(false);
152 hideKillAction->setEnabled(false);
153 hideQuitAction->setEnabled(false);
154 hideModeAction->setEnabled(false);
156 QAction *ignoreListAction = new QAction(tr("Ignore list"), this);
157 ignoreListAction->setEnabled(false);
158 QAction *whoBufferAction = new QAction(tr("WHO"), this);
160 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::NetworkItemType) {
161 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
162 contextMenu.addAction(disconnectNetAction);
164 contextMenu.addAction(connectNetAction);
168 BufferInfo bufferInfo = index.data(NetworkModel::BufferInfoRole).value<BufferInfo>();
169 QString channelname = index.sibling(index.row(), 0).data().toString();
171 if(index.data(NetworkModel::ItemTypeRole) == NetworkModel::BufferItemType) {
172 if(bufferInfo.type() != BufferInfo::ChannelBuffer && bufferInfo.type() != BufferInfo::QueryBuffer) return;
173 contextMenu.addAction(joinBufferAction);
174 contextMenu.addAction(partBufferAction);
175 contextMenu.addAction(removeBufferAction);
176 contextMenu.addMenu(hideEventsMenu);
177 contextMenu.addAction(ignoreListAction);
178 contextMenu.addAction(whoBufferAction);
180 if(bufferInfo.type() == BufferInfo::ChannelBuffer) {
181 if(index.data(NetworkModel::ItemActiveRole).toBool()) {
182 removeBufferAction->setEnabled(false);
183 removeBufferAction->setToolTip("To delete the buffer, part the channel first.");
184 joinBufferAction->setVisible(false);
186 partBufferAction->setVisible(false);
189 joinBufferAction->setVisible(false);
190 partBufferAction->setVisible(false);
194 QAction *result = contextMenu.exec(QCursor::pos());
195 if(result == connectNetAction || result == disconnectNetAction) {
196 const Network *network = Client::network(index.data(NetworkModel::NetworkIdRole).value<NetworkId>());
198 if(network->connectionState() == Network::Disconnected)
199 network->requestConnect();
201 network->requestDisconnect();
203 if(result == joinBufferAction) {
204 Client::instance()->userInput(bufferInfo, QString("/JOIN %1").arg(channelname));
206 if(result == partBufferAction) {
207 Client::instance()->userInput(bufferInfo, QString("/PART %1").arg(channelname));
209 if(result == removeBufferAction) {
210 int res = QMessageBox::question(this, tr("Remove buffer permanently?"),
211 tr("Do you want to delete the buffer \"%1\" permanently? This will delete all related data, including all backlog "
212 "data, from the core's database!").arg(bufferInfo.bufferName()),
213 QMessageBox::Yes|QMessageBox::No, QMessageBox::No);
214 if(res == QMessageBox::Yes) {
215 Client::removeBuffer(bufferInfo.bufferId());
218 if(result == whoBufferAction) {
219 Client::instance()->userInput(bufferInfo, QString("/WHO %1").arg(channelname));