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);
149 void IgnoreListModel::loadDefaults() {
153 IgnoreListManager &manager = cloneIgnoreListManager();
155 if(!manager.isEmpty()) {
156 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
157 for(int i = rowCount() - 1; i >= 0; i--)
162 IgnoreListManager::IgnoreList defaults = IgnoreListModel::defaults();
163 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
164 foreach(IgnoreListManager::IgnoreListItem item, defaults) {
165 manager.addIgnoreListItem(item.ignoreRule, item.isRegEx, item.strictness, item.scope, item.scopeRule);
170 void IgnoreListModel::removeIgnoreRule(int index) {
171 if(index < 0 || index >= rowCount())
174 IgnoreListManager &manager = cloneIgnoreListManager();
175 beginRemoveRows(QModelIndex(), index, index);
176 manager.removeAt(index);
180 Qt::ItemFlags IgnoreListModel::flags(const QModelIndex &index) const {
181 if(!index.isValid()) {
182 return Qt::ItemIsDropEnabled;
184 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
189 QVariant IgnoreListModel::headerData(int section, Qt::Orientation orientation, int role) const {
191 header << tr("Enabled")
193 << tr("Ignore Rule");
195 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
196 return header[section];
201 QModelIndex IgnoreListModel::index(int row, int column, const QModelIndex &parent) const {
203 if(row >= rowCount() || column >= columnCount())
204 return QModelIndex();
206 return createIndex(row, column);
210 const IgnoreListManager &IgnoreListModel::ignoreListManager() const {
212 return _clonedIgnoreListManager;
214 return *Client::ignoreListManager();
217 IgnoreListManager &IgnoreListModel::ignoreListManager() {
219 return _clonedIgnoreListManager;
221 return *Client::ignoreListManager();
224 IgnoreListManager &IgnoreListModel::cloneIgnoreListManager() {
225 if(!_configChanged) {
226 _clonedIgnoreListManager = *Client::ignoreListManager();
227 _configChanged = true;
228 emit configChanged(true);
230 return _clonedIgnoreListManager;
233 void IgnoreListModel::revert() {
237 _configChanged = false;
238 emit configChanged(false);
242 void IgnoreListModel::commit() {
246 Client::ignoreListManager()->requestUpdate(_clonedIgnoreListManager.toVariantMap());
250 void IgnoreListModel::initDone() {
253 emit modelReady(true);
256 void IgnoreListModel::clientConnected() {
257 connect(Client::ignoreListManager(), SIGNAL(updated()), SLOT(revert()));
258 if(Client::ignoreListManager()->isInitialized())
261 connect(Client::ignoreListManager(), SIGNAL(initDone()), SLOT(initDone()));
264 void IgnoreListModel::clientDisconnected() {
266 _clonedIgnoreListManager = ClientIgnoreListManager();
269 emit modelReady(false);
272 const IgnoreListManager::IgnoreListItem &IgnoreListModel::ignoreListItemAt(int row) const {
273 return ignoreListManager()[row];
275 // FIXME use QModelIndex?
276 void IgnoreListModel::setIgnoreListItemAt(int row, const IgnoreListManager::IgnoreListItem &item) {
277 cloneIgnoreListManager()[row] = item;
278 emit dataChanged(createIndex(row, 0), createIndex(row, 2));