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),
31 _configChanged(false),
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()));
38 if (Client::isConnected())
41 emit modelReady(false);
45 QVariant AliasesModel::data(const QModelIndex &index, int role) const
50 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
55 switch (index.column()) {
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");
62 // To avoid overwhelming the user, organize things into a table
64 QTextStream tooltip( &strTooltip, QIODevice::WriteOnly );
65 tooltip << "<qt><style>.bold { font-weight: bold; } .italic { font-style: italic; }</style>";
67 // Function to add a row to the tooltip table
69 const QString& key, const QString& value = QString(), bool condition = true) {
71 if (value.isEmpty()) {
72 tooltip << "<tr><td class='italic' align='left' colspan='2'>"
73 << key << "</td></tr>";
75 tooltip << "<tr><td class='bold' align='left'>"
76 << key << "</td><td>" << value << "</td></tr>";
81 tooltip << "<p class='bold'>"
82 << tr("The string the shortcut will be expanded to") << "</p>";
84 tooltip << "<p class='bold' align='center'>"
85 << tr("Special variables") << "</p>";
87 // Variable option table
88 tooltip << "<table cellspacing='5' cellpadding='0'>";
90 // Parameter variables
91 addRow(tr("Parameter variables"));
92 addRow("$i", tr("i'th parameter"));
93 addRow("$i..j", tr("i'th to j'th parameter separated by spaces"));
94 addRow("$i..", tr("all parameters from i on separated by spaces"));
97 addRow(tr("Nickname parameter variables"));
99 tr("account of user identified by i'th parameter, or a '*' if logged out or "
101 addRow("$i:hostname",
102 tr("hostname of user identified by i'th parameter, or a '*' if unknown"));
104 tr("ident of user identified by i'th parameter, or a '*' if unknown"));
106 tr("ident of user identified by i'th parameter if verified, or a '*' if unknown "
107 "or unverified (prefixed with '~')"));
110 addRow(tr("General variables"));
111 addRow("$0", tr("the whole string"));
112 addRow("$nick", tr("your current nickname"));
113 addRow("$channel", tr("the name of the selected channel"));
116 tooltip << "</table>";
120 << tr("Multiple commands can be separated with semicolons") << "</p>";
123 tooltip << QString("<p><span class='bold'>%1</span> %2<br />").arg(
124 tr("Example:"), tr("\"Test $1; Test $2; Test All $0\""));
125 tooltip << tr("...will be expanded to three separate messages \"Test 1\", \"Test 2\" "
126 "and \"Test All 1 2 3\" when called like <i>/test 1 2 3</i>")
136 case Qt::DisplayRole:
138 switch (index.column()) {
140 return aliasManager()[index.row()].name;
142 return aliasManager()[index.row()].expansion;
152 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role)
157 if (!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
160 QString newValue = value.toString();
161 if (newValue.isEmpty())
164 switch (index.column()) {
166 if (aliasManager().contains(newValue)) {
170 cloneAliasManager()[index.row()].name = newValue;
174 cloneAliasManager()[index.row()].expansion = newValue;
182 void AliasesModel::newAlias()
184 QString newName("alias");
186 AliasManager &manager = cloneAliasManager();
187 while (manager.contains(newName)) {
189 newName = QString("alias%1").arg(i);
191 beginInsertRows(QModelIndex(), rowCount(), rowCount());
192 manager.addAlias(newName, "Expansion");
197 void AliasesModel::loadDefaults()
202 AliasManager &manager = cloneAliasManager();
204 if (!manager.isEmpty()) {
205 beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
206 for (int i = rowCount() - 1; i >= 0; i--)
211 AliasManager::AliasList defaults = AliasManager::defaults();
212 beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
213 foreach(AliasManager::Alias alias, defaults) {
214 manager.addAlias(alias.name, alias.expansion);
220 void AliasesModel::removeAlias(int index)
222 if (index < 0 || index >= rowCount())
225 AliasManager &manager = cloneAliasManager();
226 beginRemoveRows(QModelIndex(), index, index);
227 manager.removeAt(index);
232 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const
234 if (!index.isValid()) {
235 return Qt::ItemIsDropEnabled;
238 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
243 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const
246 header << tr("Alias")
249 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
250 return header[section];
256 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const
259 if (row >= rowCount() || column >= columnCount())
260 return QModelIndex();
262 return createIndex(row, column);
266 const AliasManager &AliasesModel::aliasManager() const
269 return _clonedAliasManager;
271 return *Client::aliasManager();
275 AliasManager &AliasesModel::aliasManager()
278 return _clonedAliasManager;
280 return *Client::aliasManager();
284 AliasManager &AliasesModel::cloneAliasManager()
286 if (!_configChanged) {
287 _clonedAliasManager = *Client::aliasManager();
288 _configChanged = true;
289 emit configChanged(true);
291 return _clonedAliasManager;
295 void AliasesModel::revert()
300 _configChanged = false;
301 emit configChanged(false);
307 void AliasesModel::commit()
312 Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
317 void AliasesModel::initDone()
322 emit modelReady(true);
326 void AliasesModel::clientConnected()
328 connect(Client::aliasManager(), SIGNAL(updated()), SLOT(revert()));
329 if (Client::aliasManager()->isInitialized())
332 connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
336 void AliasesModel::clientDisconnected()
339 _clonedAliasManager = ClientAliasManager();
343 emit modelReady(false);