1 /***************************************************************************
2 * Copyright (C) 2005-2015 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),
32 _configChanged(false),
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()));
39 if (Client::isConnected())
42 emit modelReady(false);
46 QVariant IgnoreListModel::data(const QModelIndex &index, int role) const
51 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
56 switch (index.column()) {
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.";
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.";
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");
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 />");
88 switch (index.column()) {
90 if (ignoreListManager()[index.row()].type == IgnoreListManager::SenderIgnore)
91 return tr("By Sender");
93 return tr("By Message");
96 switch (index.column()) {
98 return ignoreListManager()[index.row()].isActive;
100 return ignoreListManager()[index.row()].type;
102 return ignoreListManager()[index.row()].ignoreRule;
112 bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role)
117 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
120 QVariant newValue = value;
121 if (newValue.isNull())
124 switch (index.column()) {
126 cloneIgnoreListManager()[index.row()].isActive = newValue.toBool();
129 cloneIgnoreListManager()[index.row()].type = (IgnoreListManager::IgnoreType)newValue.toInt();
132 if (ignoreListManager().contains(newValue.toString())) {
136 cloneIgnoreListManager()[index.row()].ignoreRule = newValue.toString();
145 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item)
147 IgnoreListManager &manager = cloneIgnoreListManager();
148 if (manager.contains(item.ignoreRule))
150 beginInsertRows(QModelIndex(), rowCount(), rowCount());
151 // manager.addIgnoreListItem(item);
152 manager.addIgnoreListItem(item.type, item.ignoreRule, item.isRegEx, item.strictness, item.scope,
153 item.scopeRule, item.isActive);
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.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule);
182 void IgnoreListModel::removeIgnoreRule(int index)
184 if (index < 0 || index >= rowCount())
187 IgnoreListManager &manager = cloneIgnoreListManager();
188 beginRemoveRows(QModelIndex(), index, index);
189 manager.removeAt(index);
194 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const
196 if (!index.isValid()) {
197 return Qt::ItemIsDropEnabled;
200 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
205 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const
208 header << tr("Enabled")
210 << tr("Ignore Rule");
212 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
213 return header[section];
219 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const
222 if (row >= rowCount() || column >= columnCount())
223 return QModelIndex();
225 return createIndex(row, column);
229 const IgnoreListManager &IgnoreListModel::ignoreListManager() const
232 return _clonedIgnoreListManager;
234 return *Client::ignoreListManager();
238 IgnoreListManager &IgnoreListModel::ignoreListManager()
241 return _clonedIgnoreListManager;
243 return *Client::ignoreListManager();
247 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager()
249 if (!_configChanged) {
250 _clonedIgnoreListManager = *Client::ignoreListManager();
251 _configChanged = true;
252 emit configChanged(true);
254 return _clonedIgnoreListManager;
258 void IgnoreListModel::revert()
263 _configChanged = false;
264 emit configChanged(false);
270 void IgnoreListModel::commit()
275 Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
280 void IgnoreListModel::initDone()
285 emit modelReady(true);
289 void IgnoreListModel::clientConnected()
291 connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
292 if (Client::ignoreListManager()->isInitialized())
295 connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
299 void IgnoreListModel::clientDisconnected()
302 _clonedIgnoreListManager = ClientIgnoreListManager();
306 emit modelReady(false);
310 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const
312 return ignoreListManager()[row];
316 // FIXME use QModelIndex?
317 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item)
319 cloneIgnoreListManager()[row] = item;
320 emit dataChanged(createIndex(row, 0), createIndex(row, 2));
324 const QModelIndex IgnoreListModel::indexOf(const QString &rule)
326 return createIndex(ignoreListManager().indexOf(rule), 2);