1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "ignorelistmodel.h"
24 #include <QStringList>
25 #include <QPushButton>
28 #include "signalproxy.h"
30 IgnoreListModel::IgnoreListModel(QObject *parent)
31 : QAbstractItemModel(parent)
33 // we need this signal for future connects to reset the data;
34 connect(Client::instance(), &Client::connected, this, &IgnoreListModel::clientConnected);
35 connect(Client::instance(), &Client::disconnected, this, &IgnoreListModel::clientDisconnected);
37 if (Client::isConnected())
40 emit modelReady(false);
44 QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
49 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
54 switch (index.column()) {
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.";
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.";
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");
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 />");
86 switch (index.column()) {
88 if (ignoreListManager()[index.row()].type() == IgnoreListManager::SenderIgnore)
89 return tr("By Sender");
91 return tr("By Message");
93 // Intentional fallthrough
95 switch (index.column()) {
97 return ignoreListManager()[index.row()].isEnabled();
99 return ignoreListManager()[index.row()].type();
101 return ignoreListManager()[index.row()].contents();
111 bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role)
116 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
119 QVariant newValue = value;
120 if (newValue.isNull())
123 switch (index.column()) {
125 cloneIgnoreListManager()[index.row()].setIsEnabled(newValue.toBool());
128 cloneIgnoreListManager()[index.row()].setType(
129 (IgnoreListManager::IgnoreType)newValue.toInt());
132 if (ignoreListManager().contains(newValue.toString())) {
136 cloneIgnoreListManager()[index.row()].setContents(newValue.toString());
145 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item)
147 IgnoreListManager &manager = cloneIgnoreListManager();
148 if (manager.contains(item.contents()))
150 beginInsertRows(QModelIndex(), rowCount(), rowCount());
151 // manager.addIgnoreListItem(item);
152 manager.addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(),
153 item.scope(), item.scopeRule(), item.isEnabled());
159 void IgnoreListModel::loadDefaults()
164 IgnoreListManager &manager = cloneIgnoreListManager();
166 if(!manager.isEmpty()) {
167 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
168 for(int i = rowCount() - 1; i >= 0; i--)
173 IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
174 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
175 foreach(IgnoreListManager::IgnoreListItem item, defaults) {
176 manager.addIgnoreListItem(item.contents(), item.isRegEx(), item.strictness(), item.scope(),
183 void IgnoreListModel::removeIgnoreRule(int index)
185 if (index < 0 || index >= rowCount())
188 IgnoreListManager &manager = cloneIgnoreListManager();
189 beginRemoveRows(QModelIndex(), index, index);
190 manager.removeAt(index);
195 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const
197 if (!index.isValid()) {
198 return Qt::ItemIsDropEnabled;
201 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
206 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const
209 header << tr("Enabled")
211 << tr("Ignore Rule");
213 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
214 return header[section];
220 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const
223 if (row >= rowCount() || column >= columnCount())
226 return createIndex(row, column);
230 const IgnoreListManager &IgnoreListModel::ignoreListManager() const
233 return _clonedIgnoreListManager;
235 return *Client::ignoreListManager();
239 IgnoreListManager &IgnoreListModel::ignoreListManager()
242 return _clonedIgnoreListManager;
244 return *Client::ignoreListManager();
248 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager()
250 if (!_configChanged) {
251 _clonedIgnoreListManager = *Client::ignoreListManager();
252 _configChanged = true;
253 emit configChanged(true);
255 return _clonedIgnoreListManager;
259 void IgnoreListModel::revert()
264 _configChanged = false;
265 emit configChanged(false);
271 void IgnoreListModel::commit()
276 Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
281 void IgnoreListModel::initDone()
286 emit modelReady(true);
290 void IgnoreListModel::clientConnected()
292 connect(Client::ignoreListManager(), &IgnoreListManager::updated, this, &IgnoreListModel::revert);
293 if (Client::ignoreListManager()->isInitialized())
296 connect(Client::ignoreListManager(), &SyncableObject::initDone, this, &IgnoreListModel::initDone);
300 void IgnoreListModel::clientDisconnected()
303 _clonedIgnoreListManager = ClientIgnoreListManager();
307 emit modelReady(false);
311 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const
313 return ignoreListManager()[row];
317 // FIXME use QModelIndex?
318 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item)
320 cloneIgnoreListManager()[row] = item;
321 emit dataChanged(createIndex(row, 0), createIndex(row, 2));
325 const QModelIndex IgnoreListModel::indexOf(const QString &rule)
327 return createIndex(ignoreListManager().indexOf(rule), 2);