1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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);
45 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");
94 switch(index.column()) {
96 return ignoreListManager()[index.row()].isActive;
98 return ignoreListManager()[index.row()].type;
100 return ignoreListManager()[index.row()].ignoreRule;
109 bool IgnoreListModel::setData(const QModelIndex &index, const QVariant &value, int role) {
113 if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
116 QVariant newValue = value;
117 if(newValue.isNull())
120 switch(index.column()) {
122 cloneIgnoreListManager()[index.row()].isActive = newValue.toBool();
125 cloneIgnoreListManager()[index.row()].type = (IgnoreListManager::IgnoreType)newValue.toInt();
128 if(ignoreListManager().contains(newValue.toString())) {
131 cloneIgnoreListManager()[index.row()].ignoreRule = newValue.toString();
139 bool IgnoreListModel::newIgnoreRule(const IgnoreListManager::IgnoreListItem &item) {
140 IgnoreListManager &manager = cloneIgnoreListManager();
141 if(manager.contains(item.ignoreRule))
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);
151 void IgnoreListModel::loadDefaults() {
155 IgnoreListManager &manager = cloneIgnoreListManager();
157 if(!manager.isEmpty()) {
158 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
159 for(int i = rowCount() - 1; i >= 0; i--)
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);
172 void IgnoreListModel::removeIgnoreRule(int index) {
173 if(index < 0 || index >= rowCount())
176 IgnoreListManager &manager = cloneIgnoreListManager();
177 beginRemoveRows(QModelIndex(), index, index);
178 manager.removeAt(index);
182 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const {
183 if(!index.isValid()) {
184 return Qt::ItemIsDropEnabled;
186 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
191 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const {
193 header << tr("Enabled")
195 << tr("Ignore Rule");
197 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
198 return header[section];
203 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const {
205 if(row >= rowCount() || column >= columnCount())
206 return QModelIndex();
208 return createIndex(row, column);
212 const IgnoreListManager &IgnoreListModel::ignoreListManager() const {
214 return _clonedIgnoreListManager;
216 return *Client::ignoreListManager();
219 IgnoreListManager &IgnoreListModel::ignoreListManager() {
221 return _clonedIgnoreListManager;
223 return *Client::ignoreListManager();
226 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager() {
227 if(!_configChanged) {
228 _clonedIgnoreListManager = *Client::ignoreListManager();
229 _configChanged = true;
230 emit configChanged(true);
232 return _clonedIgnoreListManager;
235 void IgnoreListModel::revert() {
239 _configChanged = false;
240 emit configChanged(false);
244 void IgnoreListModel::commit() {
248 Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
252 void IgnoreListModel::initDone() {
255 emit modelReady(true);
258 void IgnoreListModel::clientConnected() {
259 connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
260 if(Client::ignoreListManager()->isInitialized())
263 connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
266 void IgnoreListModel::clientDisconnected() {
268 _clonedIgnoreListManager = ClientIgnoreListManager();
271 emit modelReady(false);
274 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const {
275 return ignoreListManager()[row];
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));
283 const QModelIndex IgnoreListModel::indexOf(const QString &rule) {
284 return createIndex(ignoreListManager().indexOf(rule), 2);