b6b44bf3723671d6a165ff230fb1d3135cbc0a0a
[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   manager.addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope,
146                             item.scopeRule, item.isActive);
147   endInsertRows();
148   return true;
149 }
150
151 void IgnoreListModel::loadDefaults() {
152   /*if(!_modelReady)
153     return;
154
155   IgnoreListManager &manager = cloneIgnoreListManager();
156
157   if(!manager.isEmpty()) {
158     beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
159     for(int i = rowCount() - 1; i >= 0; i--)
160       manager.removeAt(i);
161     endRemoveRows();
162   }
163
164   IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
165   beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
166   foreach(IgnoreListManager::IgnoreListItem item, defaults) {
167     manager.addIgnoreListItem(item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule);
168   }
169   endInsertRows();*/
170 }
171
172 void IgnoreListModel::removeIgnoreRule(int index) {
173   if(index < 0 || index >= rowCount())
174     return;
175
176   IgnoreListManager &manager = cloneIgnoreListManager();
177   beginRemoveRows(QModelIndex(), index, index);
178   manager.removeAt(index);
179   endRemoveRows();
180 }
181
182 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const {
183   if(!index.isValid()) {
184     return Qt::ItemIsDropEnabled;
185   } else {
186     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
187   }
188 }
189
190
191 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const {
192   QStringList header;
193   header << tr("Enabled")
194          << tr("Type")
195          << tr("Ignore Rule");
196
197   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
198     return header[section];
199
200   return QVariant();
201 }
202
203 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const {
204   Q_UNUSED(parent);
205   if(row >= rowCount() || column >= columnCount())
206     return QModelIndex();
207
208   return createIndex(row, column);
209 }
210
211
212 const IgnoreListManager &IgnoreListModel::ignoreListManager() const {
213   if(_configChanged)
214     return _clonedIgnoreListManager;
215   else
216     return *Client::ignoreListManager();
217 }
218
219 IgnoreListManager &IgnoreListModel::ignoreListManager() {
220   if(_configChanged)
221     return _clonedIgnoreListManager;
222   else
223     return *Client::ignoreListManager();
224 }
225
226 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager() {
227   if(!_configChanged) {
228     _clonedIgnoreListManager = *Client::ignoreListManager();
229     _configChanged = true;
230     emit configChanged(true);
231   }
232   return _clonedIgnoreListManager;
233 }
234
235 void IgnoreListModel::revert() {
236   if(!_configChanged)
237     return;
238
239   _configChanged = false;
240   emit configChanged(false);
241   reset();
242 }
243
244 void IgnoreListModel::commit() {
245   if(!_configChanged)
246     return;
247
248   Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
249   revert();
250 }
251
252 void IgnoreListModel::initDone() {
253   _modelReady = true;
254   reset();
255   emit modelReady(true);
256 }
257
258 void IgnoreListModel::clientConnected() {
259   connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
260   if(Client::ignoreListManager()->isInitialized())
261     initDone();
262   else
263     connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
264 }
265
266 void IgnoreListModel::clientDisconnected() {
267   // clear
268   _clonedIgnoreListManager = ClientIgnoreListManager();
269   _modelReady = false;
270   reset();
271   emit modelReady(false);
272 }
273
274 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const {
275   return ignoreListManager()[row];
276 }
277 // FIXME use QModelIndex?
278 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item) {
279   cloneIgnoreListManager()[row] = item;
280   emit dataChanged(createIndex(row, 0), createIndex(row, 2));
281 }
282
283 const QModelIndex IgnoreListModel::indexOf(const QString &rule) {
284   return createIndex(ignoreListManager().indexOf(rule), 2);
285 }