Add a property disableDecoration to BufferViewConfig
[quassel.git] / src / qtui / settingspages / aliasesmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include "aliasesmodel.h"
22
23 #include <QDebug>
24 #include <QStringList>
25
26 #include "client.h"
27 #include "signalproxy.h"
28
29 AliasesModel::AliasesModel(QObject *parent)
30   : QAbstractItemModel(parent),
31     _configChanged(false)
32 {
33   // we need this signal for future connects to reset the data;
34   connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
35   connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
36   if(Client::isConnected())
37     clientConnected();
38   else
39     emit modelReady(false);
40 }
41
42 QVariant AliasesModel::data(const QModelIndex &index, int role) const {
43   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
44     return QVariant();
45
46   switch(role) {
47   case Qt::ToolTipRole:
48     switch(index.column()) {
49     case 0:
50       return "<b>The shortcut for the alias</b><br />"
51         "It can be used as a regular slash command.<br /><br />"
52         "<b>Example:</b> \"foo\" can be used per /foo";
53     case 1:
54       return "<b>The string the shortcut will be expanded to</b><br />"
55         "<b>special variables:</b><br />"
56         " - <b>$i</b> represenents the i'th parameter.<br />"
57         " - <b>$i..j</b> represenents the i'th to j'th parameter separated by spaces.<br />"
58         " - <b>$i..</b> represenents all parameters from i on separated by spaces.<br />"
59         " - <b>$i:hostname</b> represents the hostname of the user identified by the i'th parameter or a * if unknown.<br />"
60         " - <b>$0</b> the whole string.<br />"
61         " - <b>$currentnick</b> your current nickname<br />"
62         " - <b>$channelname</b> the name of the selected channel<br /><br />"
63         "Multiple commands can be separated with semicolons<br /><br />"
64         "<b>Example:</b> \"Test $1; Test $2; Test All $0\" will be expanded to three separate messages \"Test 1\", \"Test 2\" and \"Test All 1 2 3\" when called like /test 1 2 3";
65     default:
66       return QVariant();
67     }
68   case Qt::DisplayRole:
69   case Qt::EditRole:
70     switch(index.column()) {
71     case 0:
72       return aliasManager()[index.row()].name;
73     case 1:
74       return aliasManager()[index.row()].expansion;
75     default:
76       return QVariant();
77     }
78   default:
79     return QVariant();
80   }
81 }
82
83 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
84   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
85     return false;
86
87   QString newValue = value.toString();
88   if(newValue.isEmpty())
89     return false;
90   
91   switch(index.column()) {
92   case 0:
93     if(aliasManager().contains(newValue)) {
94       return false;
95     } else {
96       cloneAliasManager()[index.row()].name = newValue;
97       return true;
98     }
99   case 1:
100     cloneAliasManager()[index.row()].expansion = newValue;
101     return true;
102   default:
103     return false;
104   }
105 }
106
107 void AliasesModel::newAlias() {
108   QString newName("alias");
109   int i = 0;
110   AliasManager &manager = cloneAliasManager();
111   while(manager.contains(newName)) {
112     i++;
113     newName = QString("alias%1").arg(i);
114   }
115   beginInsertRows(QModelIndex(), rowCount(), rowCount());
116   manager.addAlias(newName, "Expansion");
117   endInsertRows();
118 }
119
120 void AliasesModel::loadDefaults() {
121   AliasManager &manager = cloneAliasManager();
122
123   if(!manager.isEmpty()) {
124     beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
125     for(int i = rowCount() - 1; i >= 0; i--)
126       manager.removeAt(i);
127     endRemoveRows();
128   }
129
130   AliasManager::AliasList defaults = AliasManager::defaults();
131   beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
132   foreach(AliasManager::Alias alias, defaults) {
133     manager.addAlias(alias.name, alias.expansion);
134   }
135   endInsertRows();
136 }
137
138 void AliasesModel::removeAlias(int index) {
139   if(index < 0 || index >= rowCount())
140     return;
141
142   AliasManager &manager = cloneAliasManager();  
143   beginRemoveRows(QModelIndex(), index, index);
144   manager.removeAt(index);
145   endRemoveRows();
146 }
147
148 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
149   if(!index.isValid()) {
150     return Qt::ItemIsDropEnabled;
151   } else {
152     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
153   }
154 }
155
156
157 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
158   QStringList header;
159   header << tr("Alias")
160          << tr("Expansion");
161   
162   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
163     return header[section];
164
165   return QVariant();
166 }
167
168 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
169   Q_UNUSED(parent);
170   if(row >= rowCount() || column >= columnCount())
171     return QModelIndex();
172
173   return createIndex(row, column);
174 }
175
176
177 const AliasManager &AliasesModel::aliasManager() const {
178   if(_configChanged)
179     return _clonedAliasManager;
180   else
181     return _aliasManager;
182 }
183
184 AliasManager &AliasesModel::aliasManager() {
185   if(_configChanged)
186     return _clonedAliasManager;
187   else
188     return _aliasManager;
189 }
190
191 AliasManager &AliasesModel::cloneAliasManager() {
192   if(!_configChanged) {
193     _clonedAliasManager = _aliasManager;
194     _configChanged = true;
195     emit configChanged(true);
196   }
197   return _clonedAliasManager;
198 }
199
200 void AliasesModel::revert() {
201   if(!_configChanged)
202     return;
203   
204   _configChanged = false;
205   emit configChanged(false);
206   reset();
207 }
208
209 void AliasesModel::commit() {
210   if(!_configChanged)
211     return;
212
213   _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap());
214   revert();
215 }  
216
217 void AliasesModel::initDone() {
218   reset();
219   emit modelReady(true);
220 }
221
222 void AliasesModel::clientConnected() {
223   _aliasManager = AliasManager();
224   Client::signalProxy()->synchronize(&_aliasManager);
225   connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone()));
226   connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert()));
227 }
228
229 void AliasesModel::clientDisconnected() {
230   // clear alias managers
231   _aliasManager = AliasManager();
232   _clonedAliasManager = AliasManager();
233   reset();
234   emit modelReady(false);
235 }