1 /***************************************************************************
2 * Copyright (C) 2005-2018 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(), SIGNAL(connected()), this, SLOT(clientConnected()));
34 connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
36 if (Client::isConnected())
39 emit modelReady(false);
43 QVariant AliasesModel::data(const QModelIndex &index, int role) const
48 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
53 switch (index.column()) {
55 return tr("<b>The shortcut for the alias</b><br />"
56 "It can be used as a regular slash command.<br /><br />"
57 "<b>Example:</b> \"foo\" can be used per /foo");
60 // To avoid overwhelming the user, organize things into a table
62 QTextStream tooltip( &strTooltip, QIODevice::WriteOnly );
63 tooltip << "<qt><style>.bold { font-weight: bold; } .italic { font-style: italic; }</style>";
65 // Function to add a row to the tooltip table
67 const QString& key, const QString& value = QString(), bool condition = true) {
69 if (value.isEmpty()) {
70 tooltip << "<tr><td class='italic' align='left' colspan='2'>"
71 << key << "</td></tr>";
73 tooltip << "<tr><td class='bold' align='left'>"
74 << key << "</td><td>" << value << "</td></tr>";
79 tooltip << "<p class='bold'>"
80 << tr("The string the shortcut will be expanded to") << "</p>";
82 tooltip << "<p class='bold' align='center'>"
83 << tr("Special variables") << "</p>";
85 // Variable option table
86 tooltip << "<table cellspacing='5' cellpadding='0'>";
88 // Parameter variables
89 addRow(tr("Parameter variables"));
90 addRow("$i", tr("i'th parameter"));
91 addRow("$i..j", tr("i'th to j'th parameter separated by spaces"));
92 addRow("$i..", tr("all parameters from i on separated by spaces"));
95 addRow(tr("Nickname parameter variables"));
97 tr("account of user identified by i'th parameter, or a '*' if logged out or "
100 tr("hostname of user identified by i'th parameter, or a '*' if unknown"));
102 tr("ident of user identified by i'th parameter, or a '*' if unknown"));
104 tr("ident of user identified by i'th parameter if verified, or a '*' if unknown "
105 "or unverified (prefixed with '~')"));
108 addRow(tr("General variables"));
109 addRow("$0", tr("the whole string"));
110 addRow("$nick", tr("your current nickname"));
111 addRow("$channel", tr("the name of the selected channel"));
114 tooltip << "</table>";
118 << tr("Multiple commands can be separated with semicolons") << "</p>";
121 tooltip << QString("<p><span class='bold'>%1</span> %2<br />").arg(
122 tr("Example:"), tr("\"Test $1; Test $2; Test All $0\""));
123 tooltip << tr("...will be expanded to three separate messages \"Test 1\", \"Test 2\" "
124 "and \"Test All 1 2 3\" when called like <i>/test 1 2 3</i>")
134 case Qt::DisplayRole:
136 switch (index.column()) {
138 return aliasManager()[index.row()].name;
140 return aliasManager()[index.row()].expansion;
150 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role)
155 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
158 QString newValue = value.toString();
159 if (newValue.isEmpty())
162 switch (index.column()) {
164 if (aliasManager().contains(newValue)) {
168 cloneAliasManager()[index.row()].name = newValue;
172 cloneAliasManager()[index.row()].expansion = newValue;
180 void AliasesModel::newAlias()
182 QString newName("alias");
184 AliasManager &manager = cloneAliasManager();
185 while (manager.contains(newName)) {
187 newName = QString("alias%1").arg(i);
189 beginInsertRows(QModelIndex(), rowCount(), rowCount());
190 manager.addAlias(newName, "Expansion");
195 void AliasesModel::loadDefaults()
200 AliasManager &manager = cloneAliasManager();
202 if (!manager.isEmpty()) {
203 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
204 for (int i = rowCount() - 1; i >= 0; i--)
209 AliasManager::AliasList defaults = AliasManager::defaults();
210 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
211 foreach(AliasManager::Alias alias, defaults) {
212 manager.addAlias(alias.name, alias.expansion);
218 void AliasesModel::removeAlias(int index)
220 if (index < 0 || index >= rowCount())
223 AliasManager &manager = cloneAliasManager();
224 beginRemoveRows(QModelIndex(), index, index);
225 manager.removeAt(index);
230 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const
232 if (!index.isValid()) {
233 return Qt::ItemIsDropEnabled;
236 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
241 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
244 header << tr("Alias")
247 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
248 return header[section];
254 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const
257 if (row >= rowCount() || column >= columnCount())
258 return QModelIndex();
260 return createIndex(row, column);
264 const AliasManager &AliasesModel::aliasManager() const
267 return _clonedAliasManager;
269 return *Client::aliasManager();
273 AliasManager &AliasesModel::aliasManager()
276 return _clonedAliasManager;
278 return *Client::aliasManager();
282 AliasManager &AliasesModel::cloneAliasManager()
284 if (!_configChanged) {
285 _clonedAliasManager = *Client::aliasManager();
286 _configChanged = true;
287 emit configChanged(true);
289 return _clonedAliasManager;
293 void AliasesModel::revert()
298 _configChanged = false;
299 emit configChanged(false);
305 void AliasesModel::commit()
310 Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
315 void AliasesModel::initDone()
320 emit modelReady(true);
324 void AliasesModel::clientConnected()
326 connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert()));
327 if (Client::aliasManager()->isInitialized())
330 connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
334 void AliasesModel::clientDisconnected()
337 _clonedAliasManager = ClientAliasManager();
341 emit modelReady(false);