Support $i:ident in aliases
[quassel.git] / src / qtui / settingspages / aliasesmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 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     _modelReady(false)
33 {
34     // we need this signal for future connects to reset the data;
35     connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
36     connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
37
38     if (Client::isConnected())
39         clientConnected();
40     else
41         emit modelReady(false);
42 }
43
44
45 QVariant AliasesModel::data(const QModelIndex &index, int role) const
46 {
47     if (!_modelReady)
48         return QVariant();
49
50     if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
51         return QVariant();
52
53     switch (role) {
54     case Qt::ToolTipRole:
55         switch (index.column()) {
56         case 0:
57             return tr("<b>The shortcut for the alias</b><br />"
58                       "It can be used as a regular slash command.<br /><br />"
59                       "<b>Example:</b> \"foo\" can be used per /foo");
60         case 1:
61             return tr("<b>The string the shortcut will be expanded to</b><br />"
62                       "<b>special variables:</b><br />"
63                       " - <b>$i</b> represents the i'th parameter.<br />"
64                       " - <b>$i..j</b> represents the i'th to j'th parameter separated by spaces.<br />"
65                       " - <b>$i..</b> represents all parameters from i on separated by spaces.<br />"
66                       " - <b>$i:hostname</b> represents the hostname of the user identified by the i'th parameter or a * if unknown.<br />"
67                       " - <b>$i:ident</b> represents the ident of the user identified by the i'th parameter or a * if unknown.<br />"
68                       " - <b>$0</b> the whole string.<br />"
69                       " - <b>$nick</b> your current nickname<br />"
70                       " - <b>$channel</b> the name of the selected channel<br /><br />"
71                       "Multiple commands can be separated with semicolons<br /><br />"
72                       "<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");
73         default:
74             return QVariant();
75         }
76     case Qt::DisplayRole:
77     case Qt::EditRole:
78         switch (index.column()) {
79         case 0:
80             return aliasManager()[index.row()].name;
81         case 1:
82             return aliasManager()[index.row()].expansion;
83         default:
84             return QVariant();
85         }
86     default:
87         return QVariant();
88     }
89 }
90
91
92 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role)
93 {
94     if (!_modelReady)
95         return false;
96
97     if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
98         return false;
99
100     QString newValue = value.toString();
101     if (newValue.isEmpty())
102         return false;
103
104     switch (index.column()) {
105     case 0:
106         if (aliasManager().contains(newValue)) {
107             return false;
108         }
109         else {
110             cloneAliasManager()[index.row()].name = newValue;
111             return true;
112         }
113     case 1:
114         cloneAliasManager()[index.row()].expansion = newValue;
115         return true;
116     default:
117         return false;
118     }
119 }
120
121
122 void AliasesModel::newAlias()
123 {
124     QString newName("alias");
125     int i = 0;
126     AliasManager &manager = cloneAliasManager();
127     while (manager.contains(newName)) {
128         i++;
129         newName = QString("alias%1").arg(i);
130     }
131     beginInsertRows(QModelIndex(), rowCount(), rowCount());
132     manager.addAlias(newName, "Expansion");
133     endInsertRows();
134 }
135
136
137 void AliasesModel::loadDefaults()
138 {
139     if (!_modelReady)
140         return;
141
142     AliasManager &manager = cloneAliasManager();
143
144     if (!manager.isEmpty()) {
145         beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
146         for (int i = rowCount() - 1; i >= 0; i--)
147             manager.removeAt(i);
148         endRemoveRows();
149     }
150
151     AliasManager::AliasList defaults = AliasManager::defaults();
152     beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
153     foreach(AliasManager::Alias alias, defaults) {
154         manager.addAlias(alias.name, alias.expansion);
155     }
156     endInsertRows();
157 }
158
159
160 void AliasesModel::removeAlias(int index)
161 {
162     if (index < 0 || index >= rowCount())
163         return;
164
165     AliasManager &manager = cloneAliasManager();
166     beginRemoveRows(QModelIndex(), index, index);
167     manager.removeAt(index);
168     endRemoveRows();
169 }
170
171
172 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const
173 {
174     if (!index.isValid()) {
175         return Qt::ItemIsDropEnabled;
176     }
177     else {
178         return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
179     }
180 }
181
182
183 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
184 {
185     QStringList header;
186     header << tr("Alias")
187            << tr("Expansion");
188
189     if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
190         return header[section];
191
192     return QVariant();
193 }
194
195
196 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const
197 {
198     Q_UNUSED(parent);
199     if (row >= rowCount() || column >= columnCount())
200         return QModelIndex();
201
202     return createIndex(row, column);
203 }
204
205
206 const AliasManager &AliasesModel::aliasManager() const
207 {
208     if (_configChanged)
209         return _clonedAliasManager;
210     else
211         return *Client::aliasManager();
212 }
213
214
215 AliasManager &AliasesModel::aliasManager()
216 {
217     if (_configChanged)
218         return _clonedAliasManager;
219     else
220         return *Client::aliasManager();
221 }
222
223
224 AliasManager &AliasesModel::cloneAliasManager()
225 {
226     if (!_configChanged) {
227         _clonedAliasManager = *Client::aliasManager();
228         _configChanged = true;
229         emit configChanged(true);
230     }
231     return _clonedAliasManager;
232 }
233
234
235 void AliasesModel::revert()
236 {
237     if (!_configChanged)
238         return;
239
240     _configChanged = false;
241     emit configChanged(false);
242     beginResetModel();
243     endResetModel();
244 }
245
246
247 void AliasesModel::commit()
248 {
249     if (!_configChanged)
250         return;
251
252     Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
253     revert();
254 }
255
256
257 void AliasesModel::initDone()
258 {
259     _modelReady = true;
260     beginResetModel();
261     endResetModel();
262     emit modelReady(true);
263 }
264
265
266 void AliasesModel::clientConnected()
267 {
268     connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert()));
269     if (Client::aliasManager()->isInitialized())
270         initDone();
271     else
272         connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
273 }
274
275
276 void AliasesModel::clientDisconnected()
277 {
278     // clear
279     _clonedAliasManager = ClientAliasManager();
280     _modelReady = false;
281     beginResetModel();
282     endResetModel();
283     emit modelReady(false);
284 }