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