1 /***************************************************************************
2 * Copyright (C) 2005-2019 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "aliasesmodel.h"
24 #include <QStringList>
27 #include "signalproxy.h"
29 AliasesModel::AliasesModel(QObject* parent)
30 : QAbstractItemModel(parent)
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);
36 if (Client::isConnected())
39 emit modelReady(false);
42 QVariant AliasesModel::data(const QModelIndex& index, int role) const
47 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
52 switch (index.column()) {
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");
58 // To avoid overwhelming the user, organize things into a table
60 QTextStream tooltip(&strTooltip, QIODevice::WriteOnly);
61 tooltip << "<qt><style>.bold { font-weight: bold; } .italic { font-style: italic; }</style>";
63 // Function to add a row to the tooltip table
64 auto addRow = [&](const QString& key, const QString& value = QString(), bool condition = true) {
66 if (value.isEmpty()) {
67 tooltip << "<tr><td class='italic' align='left' colspan='2'>" << key << "</td></tr>";
70 tooltip << "<tr><td class='bold' align='left'>" << key << "</td><td>" << value << "</td></tr>";
75 tooltip << "<p class='bold'>" << tr("The string the shortcut will be expanded to") << "</p>";
77 tooltip << "<p class='bold' align='center'>" << tr("Special variables") << "</p>";
79 // Variable option table
80 tooltip << "<table cellspacing='5' cellpadding='0'>";
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"));
89 addRow(tr("Nickname parameter variables"));
91 tr("account of user identified by i'th parameter, or a '*' if logged out or "
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"));
96 tr("ident of user identified by i'th parameter if verified, or a '*' if unknown "
97 "or unverified (prefixed with '~')"));
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"));
106 tooltip << "</table>";
109 tooltip << "<p>" << tr("Multiple commands can be separated with semicolons") << "</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>")
124 case Qt::DisplayRole:
126 switch (index.column()) {
128 return aliasManager()[index.row()].name;
130 return aliasManager()[index.row()].expansion;
139 bool AliasesModel::setData(const QModelIndex& index, const QVariant& value, int role)
144 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
147 QString newValue = value.toString();
148 if (newValue.isEmpty())
151 switch (index.column()) {
153 if (aliasManager().contains(newValue)) {
157 cloneAliasManager()[index.row()].name = newValue;
161 cloneAliasManager()[index.row()].expansion = newValue;
168 void AliasesModel::newAlias()
170 QString newName("alias");
172 AliasManager& manager = cloneAliasManager();
173 while (manager.contains(newName)) {
175 newName = QString("alias%1").arg(i);
177 beginInsertRows(QModelIndex(), rowCount(), rowCount());
178 manager.addAlias(newName, "Expansion");
182 void AliasesModel::loadDefaults()
187 AliasManager& manager = cloneAliasManager();
189 if (!manager.isEmpty()) {
190 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
191 for (int i = rowCount() - 1; i >= 0; i--)
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);
204 void AliasesModel::removeAlias(int index)
206 if (index < 0 || index >= rowCount())
209 AliasManager& manager = cloneAliasManager();
210 beginRemoveRows(QModelIndex(), index, index);
211 manager.removeAt(index);
215 Qt::ItemFlags AliasesModel::flags(const QModelIndex& index) const
217 if (!index.isValid()) {
218 return Qt::ItemIsDropEnabled;
221 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
225 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
228 header << tr("Alias") << tr("Expansion");
230 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
231 return header[section];
236 QModelIndex AliasesModel::index(int row, int column, const QModelIndex& parent) const
239 if (row >= rowCount() || column >= columnCount())
242 return createIndex(row, column);
245 const AliasManager& AliasesModel::aliasManager() const
248 return _clonedAliasManager;
250 return *Client::aliasManager();
253 AliasManager& AliasesModel::aliasManager()
256 return _clonedAliasManager;
258 return *Client::aliasManager();
261 AliasManager& AliasesModel::cloneAliasManager()
263 if (!_configChanged) {
264 _clonedAliasManager = *Client::aliasManager();
265 _configChanged = true;
266 emit configChanged(true);
268 return _clonedAliasManager;
271 void AliasesModel::revert()
276 _configChanged = false;
277 emit configChanged(false);
282 void AliasesModel::commit()
287 Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
291 void AliasesModel::initDone()
296 emit modelReady(true);
299 void AliasesModel::clientConnected()
301 connect(Client::aliasManager(), &AliasManager::updated, this, &AliasesModel::revert);
302 if (Client::aliasManager()->isInitialized())
305 connect(Client::aliasManager(), &SyncableObject::initDone, this, &AliasesModel::initDone);
308 void AliasesModel::clientDisconnected()
311 _clonedAliasManager = ClientAliasManager();
315 emit modelReady(false);