Added a function decodeString() to util.{h|cpp} that takes a QByteArray with raw...
[quassel.git] / src / client / buffertreemodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 <QColor>  // FIXME Dependency on QtGui!
22
23 #include "clientproxy.h"
24 #include "buffertreemodel.h"
25
26 /*****************************************
27 *  Fancy Buffer Items
28 *****************************************/
29 BufferTreeItem::BufferTreeItem(Buffer *buffer, TreeItem *parent) : TreeItem(parent) {
30   buf = buffer;
31   activity = Buffer::NoActivity;
32 }
33
34 void BufferTreeItem::setActivity(const Buffer::ActivityLevel &level) {
35   activity = level;
36 }
37
38 QString BufferTreeItem::text(int column) const {
39   switch(column) {
40     case 0:
41       return buf->displayName();
42     case 1:
43       return buf->networkName();
44     default:
45       return QString();
46   }
47 }
48
49 QColor BufferTreeItem::foreground(int /*column*/) const {
50   // for the time beeing we ignore the column :)
51   if(activity & Buffer::Highlight) {
52     return QColor(Qt::red);
53   } else if(activity & Buffer::NewMessage) {
54     return QColor(Qt::darkYellow);
55   } else if(activity & Buffer::OtherActivity) {
56     return QColor(Qt::darkGreen);
57   } else {
58     if(buf->isActive())
59       return QColor(Qt::black);
60     else
61       return QColor(Qt::gray);
62   }
63 }
64
65
66 QVariant BufferTreeItem::data(int column, int role) const {
67   switch(role) {
68     case Qt::DisplayRole:
69       return text(column);
70     case Qt::ForegroundRole:
71       return foreground(column);
72     case BufferTreeModel::BufferNameRole:
73       return buf->bufferName();
74     case BufferTreeModel::BufferTypeRole:
75       return buf->bufferType();
76     case BufferTreeModel::BufferActiveRole:
77       return buf->isActive();
78     case BufferTreeModel::BufferIdRole:
79       return buf->bufferId().uid();
80     default:
81       return QVariant();
82   }
83 }
84
85 /*****************************************
86  * BufferTreeModel
87  *****************************************/
88 BufferTreeModel::BufferTreeModel(QObject *parent) : TreeModel(BufferTreeModel::defaultHeader(), parent) {
89   connect(this, SIGNAL(fakeUserInput(BufferId, QString)),
90           ClientProxy::instance(), SLOT(gsUserInput(BufferId, QString)));
91 }
92
93 QList<QVariant >BufferTreeModel::defaultHeader() {
94   QList<QVariant> data;
95   data << tr("Buffer") << tr("Network");
96   return data;
97 }
98
99
100 Qt::ItemFlags BufferTreeModel::flags(const QModelIndex &index) const {
101   if(!index.isValid())
102     return Qt::ItemIsDropEnabled;
103     //return 0;
104
105   // I think this is pretty ugly..
106   if(isBufferIndex(index)) {
107     Buffer *buffer = getBufferByIndex(index);
108     if(buffer->bufferType() == Buffer::QueryBuffer)
109       return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
110     else
111       return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
112   } else {
113     return Qt::ItemIsEnabled | Qt::ItemIsDropEnabled; 
114   }
115 }
116
117 bool BufferTreeModel::isBufferIndex(const QModelIndex &index) const {
118   return parent(index) != QModelIndex();
119 }
120
121 Buffer *BufferTreeModel::getBufferByIndex(const QModelIndex &index) const {
122   BufferTreeItem *item = static_cast<BufferTreeItem *>(index.internalPointer());
123   return item->buffer();
124 }
125
126 QModelIndex BufferTreeModel::getOrCreateNetworkItemIndex(Buffer *buffer) {
127   QString net = buffer->networkName();
128   
129   if(networkItem.contains(net)) {
130     return index(networkItem[net]->row(), 0);
131   } else {
132     QList<QVariant> data;
133     data << net << "";
134     
135     int nextRow = rootItem->childCount();
136     
137     beginInsertRows(QModelIndex(), nextRow, nextRow);
138     rootItem->appendChild(new TreeItem(data, rootItem));
139     endInsertRows();
140     
141     networkItem[net] = rootItem->child(nextRow);
142     return index(nextRow, 0);
143   }
144 }
145
146 QModelIndex BufferTreeModel::getOrCreateBufferItemIndex(Buffer *buffer) {
147   QModelIndex networkItemIndex = getOrCreateNetworkItemIndex(buffer);
148   
149   if(bufferItem.contains(buffer)) {
150     return index(bufferItem[buffer]->row(), 0, networkItemIndex);
151   } else {
152     // first we determine the parent of the new Item
153     TreeItem *networkItem = static_cast<TreeItem*>(networkItemIndex.internalPointer());
154
155     int nextRow = networkItem->childCount();
156
157     beginInsertRows(networkItemIndex, nextRow, nextRow);
158     networkItem->appendChild(new BufferTreeItem(buffer, networkItem));
159     endInsertRows();
160
161     bufferItem[buffer] = static_cast<BufferTreeItem *>(networkItem->child(nextRow));
162     return index(nextRow, 0, networkItemIndex);
163   }
164 }
165
166 QStringList BufferTreeModel::mimeTypes() const {
167   QStringList types;
168   types << "application/Quassel/BufferItem/row"
169     << "application/Quassel/BufferItem/network"
170     << "application/Quassel/BufferItem/bufferId";
171   return types;
172 }
173
174 QMimeData *BufferTreeModel::mimeData(const QModelIndexList &indexes) const {
175   QMimeData *mimeData = new QMimeData();
176
177   QModelIndex index = indexes.first();
178   
179   mimeData->setData("application/Quassel/BufferItem/row", QByteArray::number(index.row()));
180   mimeData->setData("application/Quassel/BufferItem/network", getBufferByIndex(index)->networkName().toUtf8());
181   mimeData->setData("application/Quassel/BufferItem/bufferId", QByteArray::number(getBufferByIndex(index)->bufferId().uid()));
182   return mimeData;
183 }
184
185 bool BufferTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction /*action*/, int /*row*/, int /*column*/, const QModelIndex &parent) {
186   if(!(data->hasFormat("application/Quassel/BufferItem/row")
187        && data->hasFormat("application/Quassel/BufferItem/network")
188        && data->hasFormat("application/Quassel/BufferItem/bufferId")))
189     return false; // whatever the drop is... it's not a buffer...
190        
191   int sourcerow = data->data("application/Quassel/BufferItem/row").toInt();
192   QString network = QString::fromUtf8(data->data("application/Quassel/BufferItem/network"));
193   
194   Q_ASSERT(networkItem.contains(network));
195
196   if(parent == QModelIndex()) // can't be a query...
197     return false;
198   
199   Buffer *sourceBuffer = static_cast<BufferTreeItem *>(networkItem[network]->child(sourcerow))->buffer();
200   Buffer *targetBuffer = getBufferByIndex(parent);
201
202   if(!(sourceBuffer->bufferType() & targetBuffer->bufferType() & Buffer::QueryBuffer)) // only queries can be merged
203     return false;
204   
205   if(sourceBuffer == targetBuffer) // we won't merge with ourself :)
206     return false;
207     
208   /*
209   if(QMessageBox::warning(static_cast<QWidget *>(QObject::parent()),
210                           tr("Merge Buffers?"),
211                           tr("Do you really want to merge the following Buffers?<br />%1.%2<br />%3.%4").arg(sourceBuffer->networkName()).arg(sourceBuffer->bufferName()).arg(targetBuffer->networkName()).arg(targetBuffer->bufferName()),
212                           QMessageBox::Yes|QMessageBox::No) == QMessageBox::No)
213     return false;
214
215   */
216   qDebug() << "merging" << sourceBuffer->bufferName() << "with" << targetBuffer->bufferName();
217   bufferItem.remove(getBufferByIndex(parent));
218   removeRow(parent.row(), BufferTreeModel::parent(parent));
219   
220   return true;
221 }
222
223 void BufferTreeModel::bufferUpdated(Buffer *buffer) {
224   QModelIndex itemindex = getOrCreateBufferItemIndex(buffer);
225   emit invalidateFilter();
226   emit dataChanged(itemindex, itemindex);
227 }
228
229 // This Slot indicates that the user has selected a different buffer in the gui
230 void BufferTreeModel::changeCurrent(const QModelIndex &current, const QModelIndex &/*previous*/) {
231   if(isBufferIndex(current)) {
232     currentBuffer = getBufferByIndex(current);
233     bufferActivity(Buffer::NoActivity, currentBuffer);
234     emit bufferSelected(currentBuffer);
235     emit selectionChanged(current);
236   }
237 }
238
239 // we received a double click on a buffer, so we're going to join it
240 void BufferTreeModel::doubleClickReceived(const QModelIndex &clicked) {
241   if(isBufferIndex(clicked)) {
242     Buffer *buffer = getBufferByIndex(clicked);
243     if(!buffer->isStatusBuffer()) 
244       emit fakeUserInput(buffer->bufferId(), QString("/join " + buffer->bufferName()));
245   }
246 }
247
248 void BufferTreeModel::bufferActivity(Buffer::ActivityLevel level, Buffer *buffer) {
249   if(bufferItem.contains(buffer) and buffer != currentBuffer)
250     bufferItem[buffer]->setActivity(level);
251   else
252     bufferItem[buffer]->setActivity(Buffer::NoActivity);
253   bufferUpdated(buffer);
254 }
255
256 void BufferTreeModel::selectBuffer(Buffer *buffer) {
257   QModelIndex index = getOrCreateBufferItemIndex(buffer);
258   emit selectionChanged(index);
259 }