src: Yearly copyright bump
[quassel.git] / src / qtui / settingspages / ignorelistmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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
46 QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
47 {
48     if (!_modelReady)
49         return QVariant();
50
51     if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
52         return QVariant();
53
54     switch (role) {
55     case Qt::ToolTipRole:
56         switch (index.column()) {
57         /*
58       case 0: return "<b>Type:</b><br />"
59     "<i><u>BySender:</u></i><br />"
60       "The ignore rule is matched against the <i>nick!ident@host.mask</i> sender-string.<br />"
61     "<i><u>ByMessage:</u></i><br />"
62       "The ignore rule is matched against the message content.";
63       case 1:
64         return "<b>Strictness:</b><br />"
65     "<i><u>Dynamic:</u></i><br />"
66           "Messages are hidden but still get stored in the database.<br />Deactivate or delete an ignore rule to show the messages again<br />"
67     "<i><u>Permanent:</u></i><br />"
68            "Messages are never stored or shown anywhere.";
69     */
70         case 0:
71             return tr("<b>Enable / Disable:</b><br />"
72                       "Only enabled rules are filtered.<br />"
73                       "For dynamic rules, disabling actually shows the filtered messages again");
74         case 2:
75             return tr("<b>Ignore rule:</b><br />"
76                       "Depending on the type of the rule, the text is matched against either:<br /><br />"
77                       "- <u>the message content:</u><br />"
78                       "<i>Example:<i><br />"
79                       "    \"*foobar*\" matches any text containing the word \"foobar\"<br /><br />"
80                       "- <u>the sender string <i>nick!ident@host.name<i></u><br />"
81                       "<i>Example:</i><br />"
82                       "    \"*@foobar.com\" matches any sender from host foobar.com<br />"
83                       "    \"stupid!.+\" (RegEx) matches any sender with nickname \"stupid\" from any host<br />");
84         default:
85             return QVariant();
86         }
87     case Qt::DisplayRole:
88         switch (index.column()) {
89         case 1:
90             if (ignoreListManager()[index.row()].type() == IgnoreListManager::SenderIgnore)
91                 return tr("By Sender");
92             else
93                 return tr("By Message");
94         }
95         // Intentional fallthrough
96     case Qt::EditRole:
97         switch (index.column()) {
98         case 0:
99             return ignoreListManager()[index.row()].isEnabled();
100         case 1:
101             return ignoreListManager()[index.row()].type();
102         case 2:
103             return ignoreListManager()[index.row()].contents();
104         default:
105             return QVariant();
106         }
107     default:
108         return QVariant();
109     }
110 }
111
112
113 bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role)
114 {
115     if (!_modelReady)
116         return false;
117
118     if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
119         return false;
120
121     QVariant newValue = value;
122     if (newValue.isNull())
123         return false;
124
125     switch (index.column()) {
126     case 0:
127         cloneIgnoreListManager()[index.row()].setIsEnabled(newValue.toBool());
128         return true;
129     case 1:
130         cloneIgnoreListManager()[index.row()].setType(
131                     (IgnoreListManager::IgnoreType)newValue.toInt());
132         return true;
133     case 2:
134         if (ignoreListManager().contains(newValue.toString())) {
135             return false;
136         }
137         else {
138             cloneIgnoreListManager()[index.row()].setContents(newValue.toString());
139             return true;
140         }
141     default:
142         return false;
143     }
144 }
145
146
147 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item)
148 {
149     IgnoreListManager &manager = cloneIgnoreListManager();
150     if (manager.contains(item.contents()))
151         return false;
152     beginInsertRows(QModelIndex(), rowCount(), rowCount());
153     // manager.addIgnoreListItem(item);
154     manager.addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(),
155                               item.scope(), item.scopeRule(), item.isEnabled());
156     endInsertRows();
157     return true;
158 }
159
160
161 void IgnoreListModel::loadDefaults()
162 {
163     /*if(!_modelReady)
164       return;
165
166     IgnoreListManager &manager = cloneIgnoreListManager();
167
168     if(!manager.isEmpty()) {
169       beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
170       for(int i = rowCount() - 1; i >= 0; i--)
171         manager.removeAt(i);
172       endRemoveRows();
173     }
174
175     IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
176     beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
177     foreach(IgnoreListManager::IgnoreListItem item, defaults) {
178       manager.addIgnoreListItem(item.contents(), item.isRegEx(), item.strictness(), item.scope(),
179                                 item.scopeRule());
180     }
181     endInsertRows();*/
182 }
183
184
185 void IgnoreListModel::removeIgnoreRule(int index)
186 {
187     if (index < 0 || index >= rowCount())
188         return;
189
190     IgnoreListManager &manager = cloneIgnoreListManager();
191     beginRemoveRows(QModelIndex(), index, index);
192     manager.removeAt(index);
193     endRemoveRows();
194 }
195
196
197 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const
198 {
199     if (!index.isValid()) {
200         return Qt::ItemIsDropEnabled;
201     }
202     else {
203         return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
204     }
205 }
206
207
208 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const
209 {
210     QStringList header;
211     header << tr("Enabled")
212            << tr("Type")
213            << tr("Ignore Rule");
214
215     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
216         return header[section];
217
218     return QVariant();
219 }
220
221
222 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const
223 {
224     Q_UNUSED(parent);
225     if (row >= rowCount() || column >= columnCount())
226         return QModelIndex();
227
228     return createIndex(row, column);
229 }
230
231
232 const IgnoreListManager &IgnoreListModel::ignoreListManager() const
233 {
234     if (_configChanged)
235         return _clonedIgnoreListManager;
236     else
237         return *Client::ignoreListManager();
238 }
239
240
241 IgnoreListManager &IgnoreListModel::ignoreListManager()
242 {
243     if (_configChanged)
244         return _clonedIgnoreListManager;
245     else
246         return *Client::ignoreListManager();
247 }
248
249
250 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager()
251 {
252     if (!_configChanged) {
253         _clonedIgnoreListManager = *Client::ignoreListManager();
254         _configChanged = true;
255         emit configChanged(true);
256     }
257     return _clonedIgnoreListManager;
258 }
259
260
261 void IgnoreListModel::revert()
262 {
263     if (!_configChanged)
264         return;
265
266     _configChanged = false;
267     emit configChanged(false);
268     beginResetModel();
269     endResetModel();
270 }
271
272
273 void IgnoreListModel::commit()
274 {
275     if (!_configChanged)
276         return;
277
278     Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
279     revert();
280 }
281
282
283 void IgnoreListModel::initDone()
284 {
285     _modelReady = true;
286     beginResetModel();
287     endResetModel();
288     emit modelReady(true);
289 }
290
291
292 void IgnoreListModel::clientConnected()
293 {
294     connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
295     if (Client::ignoreListManager()->isInitialized())
296         initDone();
297     else
298         connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
299 }
300
301
302 void IgnoreListModel::clientDisconnected()
303 {
304     // clear
305     _clonedIgnoreListManager = ClientIgnoreListManager();
306     _modelReady = false;
307     beginResetModel();
308     endResetModel();
309     emit modelReady(false);
310 }
311
312
313 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const
314 {
315     return ignoreListManager()[row];
316 }
317
318
319 // FIXME use QModelIndex?
320 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item)
321 {
322     cloneIgnoreListManager()[row] = item;
323     emit dataChanged(createIndex(row, 0), createIndex(row, 2));
324 }
325
326
327 const QModelIndex IgnoreListModel::indexOf(const QString &rule)
328 {
329     return createIndex(ignoreListManager().indexOf(rule), 2);
330 }