Fix slots for new syncobj
[quassel.git] / src / qtui / settingspages / ignorelistmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "ignorelistmodel.h"
22
23 #include <QDebug>
24 #include <QStringList>
25 #include <QPushButton>
26
27 #include "client.h"
28 #include "signalproxy.h"
29
30 IgnoreListModel::IgnoreListModel(QObject *parent)
31   : QAbstractItemModel(parent),
32     _configChanged(false),
33     _modelReady(false)
34 {
35   // we need this signal for future connects to reset the data;
36   connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
37   connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
38
39   if(Client::isConnected())
40     clientConnected();
41   else
42     emit modelReady(false);
43 }
44
45 QVariant IgnoreListModel::data(const QModelIndex &index, int role) const {
46   if(!_modelReady)
47     return QVariant();
48
49   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
50     return QVariant();
51
52   switch(role) {
53   case Qt::ToolTipRole:
54     switch(index.column()) {
55       /*
56     case 0: return "<b>Type:</b><br />"
57   "<i><u>BySender:</u></i><br />"
58     "The ignore rule is matched against the <i>nick!ident@host.mask</i> sender-string.<br />"
59   "<i><u>ByMessage:</u></i><br />"
60     "The ignore rule is matched against the message content.";
61     case 1:
62       return "<b>Strictness:</b><br />"
63   "<i><u>Dynamic:</u></i><br />"
64         "Messages are hidden but still get stored in the database.<br />Deactivate or delete an ignore rule to show the messages again<br />"
65   "<i><u>Permanent:</u></i><br />"
66          "Messages are never stored or shown anywhere.";
67   */
68     case 0:
69       return tr("<b>Enable / Disable:</b><br />"
70                 "Only enabled rules are filtered.<br />"
71                 "For dynamic rules, disabling actually shows the filtered messages again");
72     case 2:
73       return tr("<b>Ignore rule:</b><br />"
74                 "Depending on the type of the rule, the text is matched against either:<br /><br />"
75                 "- <u>the message content:</u><br />"
76                 "<i>Example:<i><br />"
77                 "    \"*foobar*\" matches any text containing the word \"foobar\"<br /><br />"
78                 "- <u>the sender string <i>nick!ident@host.name<i></u><br />"
79                 "<i>Example:</i><br />"
80                 "    \"*@foobar.com\" matches any sender from host foobar.com<br />"
81                 "    \"stupid!.+\" (RegEx) matches any sender with nickname \"stupid\" from any host<br />");
82     default:
83       return QVariant();
84     }
85   case Qt::DisplayRole:
86     switch(index.column()) {
87     case 1:
88       if(ignoreListManager()[index.row()].type == IgnoreListManager::SenderIgnore)
89         return tr("By Sender");
90       else
91         return tr("By Message");
92     }
93   case Qt::EditRole:
94     switch(index.column()) {
95     case 0:
96       return ignoreListManager()[index.row()].isActive;
97     case 1:
98       return ignoreListManager()[index.row()].type;
99     case 2:
100       return ignoreListManager()[index.row()].ignoreRule;
101     default:
102       return QVariant();
103     }
104   default:
105     return QVariant();
106   }
107 }
108
109 bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
110   if(!_modelReady)
111     return false;
112
113   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
114     return false;
115
116   QVariant newValue = value;
117   if(newValue.isNull())
118     return false;
119
120   switch(index.column()) {
121   case 0:
122     cloneIgnoreListManager()[index.row()].isActive = newValue.toBool();
123     return true;
124   case 1:
125     cloneIgnoreListManager()[index.row()].type = (IgnoreListManager::IgnoreType)newValue.toInt();
126     return true;
127   case 2:
128     if(ignoreListManager().contains(newValue.toString())) {
129       return false;
130     } else {
131       cloneIgnoreListManager()[index.row()].ignoreRule = newValue.toString();
132       return true;
133     }
134   default:
135     return false;
136   }
137 }
138
139 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item) {
140   IgnoreListManager &manager = cloneIgnoreListManager();
141   if(manager.contains(item.ignoreRule))
142     return false;
143   beginInsertRows(QModelIndex(), rowCount(), rowCount());
144   manager.addIgnoreListItem(item);
145   endInsertRows();
146   return true;
147 }
148
149 void IgnoreListModel::loadDefaults() {
150   /*if(!_modelReady)
151     return;
152
153   IgnoreListManager &manager = cloneIgnoreListManager();
154
155   if(!manager.isEmpty()) {
156     beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
157     for(int i = rowCount() - 1; i >= 0; i--)
158       manager.removeAt(i);
159     endRemoveRows();
160   }
161
162   IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
163   beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
164   foreach(IgnoreListManager::IgnoreListItem item, defaults) {
165     manager.addIgnoreListItem(item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule);
166   }
167   endInsertRows();*/
168 }
169
170 void IgnoreListModel::removeIgnoreRule(int index) {
171   if(index < 0 || index >= rowCount())
172     return;
173
174   IgnoreListManager &manager = cloneIgnoreListManager();
175   beginRemoveRows(QModelIndex(), index, index);
176   manager.removeAt(index);
177   endRemoveRows();
178 }
179
180 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const {
181   if(!index.isValid()) {
182     return Qt::ItemIsDropEnabled;
183   } else {
184     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
185   }
186 }
187
188
189 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const {
190   QStringList header;
191   header << tr("Enabled")
192          << tr("Type")
193          << tr("Ignore Rule");
194
195   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
196     return header[section];
197
198   return QVariant();
199 }
200
201 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const {
202   Q_UNUSED(parent);
203   if(row >= rowCount() || column >= columnCount())
204     return QModelIndex();
205
206   return createIndex(row, column);
207 }
208
209
210 const IgnoreListManager &IgnoreListModel::ignoreListManager() const {
211   if(_configChanged)
212     return _clonedIgnoreListManager;
213   else
214     return *Client::ignoreListManager();
215 }
216
217 IgnoreListManager &IgnoreListModel::ignoreListManager() {
218   if(_configChanged)
219     return _clonedIgnoreListManager;
220   else
221     return *Client::ignoreListManager();
222 }
223
224 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager() {
225   if(!_configChanged) {
226     _clonedIgnoreListManager = *Client::ignoreListManager();
227     _configChanged = true;
228     emit configChanged(true);
229   }
230   return _clonedIgnoreListManager;
231 }
232
233 void IgnoreListModel::revert() {
234   if(!_configChanged)
235     return;
236
237   _configChanged = false;
238   emit configChanged(false);
239   reset();
240 }
241
242 void IgnoreListModel::commit() {
243   if(!_configChanged)
244     return;
245
246   Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
247   revert();
248 }
249
250 void IgnoreListModel::initDone() {
251   _modelReady = true;
252   reset();
253   emit modelReady(true);
254 }
255
256 void IgnoreListModel::clientConnected() {
257   connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
258   if(Client::ignoreListManager()->isInitialized())
259     initDone();
260   else
261     connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
262 }
263
264 void IgnoreListModel::clientDisconnected() {
265   // clear
266   _clonedIgnoreListManager = ClientIgnoreListManager();
267   _modelReady = false;
268   reset();
269   emit modelReady(false);
270 }
271
272 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const {
273   return ignoreListManager()[row];
274 }
275 // FIXME use QModelIndex?
276 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item) {
277   cloneIgnoreListManager()[row] = item;
278   emit dataChanged(createIndex(row, 0), createIndex(row, 2));
279 }