1 /***************************************************************************
2 * Copyright (C) 2005-2020 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
AliasesModel(QObject * parent)29 AliasesModel::AliasesModel(QObject* parent)
30 : QAbstractItemModel(parent)
31 {
32 // we need this signal for future connects to reset the data;
33 connect(Client::instance(), &Client::connected, this, &AliasesModel::clientConnected);
34 connect(Client::instance(), &Client::disconnected, this, &AliasesModel::clientDisconnected);
35
36 if (Client::isConnected())
37 clientConnected();
38 else
39 emit modelReady(false);
40 }
41
data(const QModelIndex & index,int role) const42 QVariant AliasesModel::data(const QModelIndex& index, int role) const
43 {
44 if (!_modelReady)
45 return QVariant();
46
47 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
48 return QVariant();
49
50 switch (role) {
51 case Qt::ToolTipRole:
52 switch (index.column()) {
53 case 0:
54 return tr("<b>The shortcut for the alias</b><br />"
55 "It can be used as a regular slash command.<br /><br />"
56 "<b>Example:</b> \"foo\" can be used per /foo");
57 case 1: {
58 // To avoid overwhelming the user, organize things into a table
59 QString strTooltip;
60 QTextStream tooltip(&strTooltip, QIODevice::WriteOnly);
61 tooltip << "<qt><style>.bold { font-weight: bold; } .italic { font-style: italic; }</style>";
62
63 // Function to add a row to the tooltip table
64 auto addRow = [&](const QString& key, const QString& value = QString(), bool condition = true) {
65 if (condition) {
66 if (value.isEmpty()) {
67 tooltip << "<tr><td class='italic' align='left' colspan='2'>" << key << "</td></tr>";
68 }
69 else {
70 tooltip << "<tr><td class='bold' align='left'>" << key << "</td><td>" << value << "</td></tr>";
71 }
72 }
73 };
74
75 tooltip << "<p class='bold'>" << tr("The string the shortcut will be expanded to") << "</p>";
76
77 tooltip << "<p class='bold' align='center'>" << tr("Special variables") << "</p>";
78
79 // Variable option table
80 tooltip << "<table cellspacing='5' cellpadding='0'>";
81
82 // Parameter variables
83 addRow(tr("Parameter variables"));
84 addRow("$i", tr("i'th parameter"));
85 addRow("$i..j", tr("i'th to j'th parameter separated by spaces"));
86 addRow("$i..", tr("all parameters from i on separated by spaces"));
87
88 // IrcUser handling
89 addRow(tr("Nickname parameter variables"));
90 addRow("$i:account",
91 tr("account of user identified by i'th parameter, or a '*' if logged out or "
92 "unknown"));
93 addRow("$i:hostname", tr("hostname of user identified by i'th parameter, or a '*' if unknown"));
94 addRow("$i:ident", tr("ident of user identified by i'th parameter, or a '*' if unknown"));
95 addRow("$i:identd",
96 tr("ident of user identified by i'th parameter if verified, or a '*' if unknown "
97 "or unverified (prefixed with '~')"));
98
99 // General variables
100 addRow(tr("General variables"));
101 addRow("$0", tr("the whole string"));
102 addRow("$nick", tr("your current nickname"));
103 addRow("$channel", tr("the name of the selected channel"));
104
105 // End table
106 tooltip << "</table>";
107
108 // Example header
109 tooltip << "<p>" << tr("Multiple commands can be separated with semicolons") << "</p>";
110 // Example
111 tooltip << "<p>";
112 tooltip << QString("<p><span class='bold'>%1</span> %2<br />").arg(tr("Example:"), tr("\"Test $1; Test $2; Test All $0\""));
113 tooltip << tr("...will be expanded to three separate messages \"Test 1\", \"Test 2\" "
114 "and \"Test All 1 2 3\" when called like <i>/test 1 2 3</i>")
115 << "</p>";
116
117 // End tooltip
118 tooltip << "</qt>";
119 return strTooltip;
120 }
121 default:
122 return QVariant();
123 }
124 case Qt::DisplayRole:
125 case Qt::EditRole:
126 switch (index.column()) {
127 case 0:
128 return aliasManager()[index.row()].name;
129 case 1:
130 return aliasManager()[index.row()].expansion;
131 default:
132 return QVariant();
133 }
134 default:
135 return QVariant();
136 }
137 }
138
setData(const QModelIndex & index,const QVariant & value,int role)139 bool AliasesModel::setData(const QModelIndex& index, const QVariant& value, int role)
140 {
141 if (!_modelReady)
142 return false;
143
144 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
145 return false;
146
147 QString newValue = value.toString();
148 if (newValue.isEmpty())
149 return false;
150
151 switch (index.column()) {
152 case 0:
153 if (aliasManager().contains(newValue)) {
154 return false;
155 }
156 else {
157 cloneAliasManager()[index.row()].name = newValue;
158 return true;
159 }
160 case 1:
161 cloneAliasManager()[index.row()].expansion = newValue;
162 return true;
163 default:
164 return false;
165 }
166 }
167
newAlias()168 void AliasesModel::newAlias()
169 {
170 QString newName("alias");
171 int i = 0;
172 AliasManager& manager = cloneAliasManager();
173 while (manager.contains(newName)) {
174 i++;
175 newName = QString("alias%1").arg(i);
176 }
177 beginInsertRows(QModelIndex(), rowCount(), rowCount());
178 manager.addAlias(newName, "Expansion");
179 endInsertRows();
180 }
181
loadDefaults()182 void AliasesModel::loadDefaults()
183 {
184 if (!_modelReady)
185 return;
186
187 AliasManager& manager = cloneAliasManager();
188
189 if (!manager.isEmpty()) {
190 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
191 for (int i = rowCount() - 1; i >= 0; i--)
192 manager.removeAt(i);
193 endRemoveRows();
194 }
195
196 AliasManager::AliasList defaults = AliasManager::defaults();
197 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
198 foreach (AliasManager::Alias alias, defaults) {
199 manager.addAlias(alias.name, alias.expansion);
200 }
201 endInsertRows();
202 }
203
removeAlias(int index)204 void AliasesModel::removeAlias(int index)
205 {
206 if (index < 0 || index >= rowCount())
207 return;
208
209 AliasManager& manager = cloneAliasManager();
210 beginRemoveRows(QModelIndex(), index, index);
211 manager.removeAt(index);
212 endRemoveRows();
213 }
214
flags(const QModelIndex & index) const215 Qt::ItemFlags AliasesModel::flags(const QModelIndex& index) const
216 {
217 if (!index.isValid()) {
218 return Qt::ItemIsDropEnabled;
219 }
220 else {
221 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
222 }
223 }
224
headerData(int section,Qt::Orientation orientation,int role) const225 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
226 {
227 QStringList header;
228 header << tr("Alias") << tr("Expansion");
229
230 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
231 return header[section];
232
233 return QVariant();
234 }
235
index(int row,int column,const QModelIndex & parent) const236 QModelIndex AliasesModel::index(int row, int column, const QModelIndex& parent) const
237 {
238 Q_UNUSED(parent);
239 if (row >= rowCount() || column >= columnCount())
240 return {};
241
242 return createIndex(row, column);
243 }
244
aliasManager() const245 const AliasManager& AliasesModel::aliasManager() const
246 {
247 return _clonedAliasManager ? *_clonedAliasManager : *Client::aliasManager();
248 }
249
aliasManager()250 AliasManager& AliasesModel::aliasManager()
251 {
252 return _clonedAliasManager ? *_clonedAliasManager : *Client::aliasManager();
253 }
254
cloneAliasManager()255 AliasManager& AliasesModel::cloneAliasManager()
256 {
257 if (!_clonedAliasManager) {
258 _clonedAliasManager = std::make_unique<ClientAliasManager>();
259 _clonedAliasManager->fromVariantMap(Client::aliasManager()->toVariantMap());
260 emit configChanged(true);
261 }
262 return *_clonedAliasManager;
263 }
264
revert()265 void AliasesModel::revert()
266 {
267 if (!_clonedAliasManager)
268 return;
269
270 beginResetModel();
271 _clonedAliasManager.reset();
272 endResetModel();
273 emit configChanged(false);
274 }
275
commit()276 void AliasesModel::commit()
277 {
278 if (!_clonedAliasManager)
279 return;
280
281 Client::aliasManager()->requestUpdate(_clonedAliasManager->toVariantMap());
282 revert();
283 }
284
initDone()285 void AliasesModel::initDone()
286 {
287 _modelReady = true;
288 beginResetModel();
289 endResetModel();
290 emit modelReady(true);
291 }
292
clientConnected()293 void AliasesModel::clientConnected()
294 {
295 connect(Client::aliasManager(), &AliasManager::updated, this, &AliasesModel::revert);
296 if (Client::aliasManager()->isInitialized())
297 initDone();
298 else
299 connect(Client::aliasManager(), &SyncableObject::initDone, this, &AliasesModel::initDone);
300 }
301
clientDisconnected()302 void AliasesModel::clientDisconnected()
303 {
304 _modelReady = false;
305 beginResetModel();
306 _clonedAliasManager.reset();
307 endResetModel();
308 emit modelReady(false);
309 }
310