1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 <QPushButton>
25 #include <QStringList>
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);
43 QVariant IgnoreListModel::data(const QModelIndex& index, int role) const
48 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
53 switch (index.column()) {
55 case 0: return "<b>Type:</b><br />"
56 "<i><u>BySender:</u></i><br />"
57 "The ignore rule is matched against the <i>nick!ident@host.mask</i> sender-string.<br />"
58 "<i><u>ByMessage:</u></i><br />"
59 "The ignore rule is matched against the message content.";
61 return "<b>Strictness:</b><br />"
62 "<i><u>Dynamic:</u></i><br />"
63 "Messages are hidden but still get stored in the database.<br />Deactivate or delete an ignore rule to show the messages again<br />"
64 "<i><u>Permanent:</u></i><br />"
65 "Messages are never stored or shown anywhere.";
68 return tr("<b>Enable / Disable:</b><br />"
69 "Only enabled rules are filtered.<br />"
70 "For dynamic rules, disabling actually shows the filtered messages again");
72 return tr("<b>Ignore rule:</b><br />"
73 "Depending on the type of the rule, the text is matched against either:<br /><br />"
74 "- <u>the message content:</u><br />"
75 "<i>Example:<i><br />"
76 " \"*foobar*\" matches any text containing the word \"foobar\"<br /><br />"
77 "- <u>the sender string <i>nick!ident@host.name<i></u><br />"
78 "<i>Example:</i><br />"
79 " \"*@foobar.com\" matches any sender from host foobar.com<br />"
80 " \"stupid!.+\" (RegEx) matches any sender with nickname \"stupid\" from any host<br />");
85 switch (index.column()) {
87 if (ignoreListManager()[index.row()].type() == IgnoreListManager::SenderIgnore)
88 return tr("By Sender");
90 return tr("By Message");
92 // Intentional fallthrough
94 switch (index.column()) {
96 return ignoreListManager()[index.row()].isEnabled();
98 return ignoreListManager()[index.row()].type();
100 return ignoreListManager()[index.row()].contents();
109 bool IgnoreListModel::setData(const QModelIndex& index, const QVariant& value, int role)
114 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
117 QVariant newValue = value;
118 if (newValue.isNull())
121 switch (index.column()) {
123 cloneIgnoreListManager()[index.row()].setIsEnabled(newValue.toBool());
126 cloneIgnoreListManager()[index.row()].setType((IgnoreListManager::IgnoreType)newValue.toInt());
129 if (ignoreListManager().contains(newValue.toString())) {
133 cloneIgnoreListManager()[index.row()].setContents(newValue.toString());
141 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem& item)
143 IgnoreListManager& manager = cloneIgnoreListManager();
144 if (manager.contains(item.contents()))
146 beginInsertRows(QModelIndex(), rowCount(), rowCount());
147 // manager.addIgnoreListItem(item);
148 manager.addIgnoreListItem(item.type(), item.contents(), item.isRegEx(), item.strictness(), item.scope(), item.scopeRule(), item.isEnabled());
153 void IgnoreListModel::loadDefaults()
158 IgnoreListManager &manager = cloneIgnoreListManager();
160 if(!manager.isEmpty()) {
161 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
162 for(int i = rowCount() - 1; i >= 0; i--)
167 IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
168 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
169 foreach(IgnoreListManager::IgnoreListItem item, defaults) {
170 manager.addIgnoreListItem(item.contents(), item.isRegEx(), item.strictness(), item.scope(),
176 void IgnoreListModel::removeIgnoreRule(int index)
178 if (index < 0 || index >= rowCount())
181 IgnoreListManager& manager = cloneIgnoreListManager();
182 beginRemoveRows(QModelIndex(), index, index);
183 manager.removeAt(index);
187 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex& index) const
189 if (!index.isValid()) {
190 return Qt::ItemIsDropEnabled;
193 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
197 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const
200 header << tr("Enabled") << tr("Type") << tr("Ignore Rule");
202 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
203 return header[section];
208 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex& parent) const
211 if (row >= rowCount() || column >= columnCount())
214 return createIndex(row, column);
217 const IgnoreListManager& IgnoreListModel::ignoreListManager() const
220 return _clonedIgnoreListManager;
222 return *Client::ignoreListManager();
225 IgnoreListManager& IgnoreListModel::ignoreListManager()
228 return _clonedIgnoreListManager;
230 return *Client::ignoreListManager();
233 IgnoreListManager& IgnoreListModel::cloneIgnoreListManager()
235 if (!_configChanged) {
236 _clonedIgnoreListManager = *Client::ignoreListManager();
237 _configChanged = true;
238 emit configChanged(true);
240 return _clonedIgnoreListManager;
243 void IgnoreListModel::revert()
248 _configChanged = false;
249 emit configChanged(false);
254 void IgnoreListModel::commit()
259 Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
263 void IgnoreListModel::initDone()
268 emit modelReady(true);
271 void IgnoreListModel::clientConnected()
273 connect(Client::ignoreListManager(), &IgnoreListManager::updated, this, &IgnoreListModel::revert);
274 if (Client::ignoreListManager()->isInitialized())
277 connect(Client::ignoreListManager(), &SyncableObject::initDone, this, &IgnoreListModel::initDone);
280 void IgnoreListModel::clientDisconnected()
283 _clonedIgnoreListManager = ClientIgnoreListManager();
287 emit modelReady(false);
290 const IgnoreListManager::IgnoreListItem& IgnoreListModel::ignoreListItemAt(int row) const
292 return ignoreListManager()[row];
295 // FIXME use QModelIndex?
296 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem& item)
298 cloneIgnoreListManager()[row] = item;
299 emit dataChanged(createIndex(row, 0), createIndex(row, 2));
302 const QModelIndex IgnoreListModel::indexOf(const QString& rule)
304 return createIndex(ignoreListManager().indexOf(rule), 2);