1 /***************************************************************************
2 * Copyright (C) 2005-08 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include "aliasesmodel.h"
24 #include <QStringList>
27 #include "signalproxy.h"
29 AliasesModel::AliasesModel(QObject *parent)
30 : QAbstractItemModel(parent),
33 // _aliasManager.addAlias("a", "aa");
34 // _aliasManager.addAlias("b", "bb");
35 // _aliasManager.addAlias("c", "cc");
36 // _aliasManager.addAlias("d", "dd");
38 // we need this signal for future connects to reset the data;
39 connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
40 if(Client::isConnected())
44 QVariant AliasesModel::data(const QModelIndex &index, int role) const {
45 if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
50 switch(index.column()) {
52 return "<b>The shortcut for the alias</b><br />"
53 "It can be used as a regular slash command.<br /><br />"
54 "<b>Example:</b> \"foo\" can be used per /foo";
56 return "<b>The string the shortcut will be expanded to</b><br />"
57 "$i represenents the i'th parameter. $0 the whole string.<br />"
58 "Multiple commands can be separated with semicolons<br /><br />"
59 "<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";
64 switch(index.column()) {
66 return aliasManager()[index.row()].name;
68 return aliasManager()[index.row()].expansion;
77 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
78 if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
81 QString newValue = value.toString();
82 if(newValue.isEmpty())
85 switch(index.column()) {
87 if(aliasManager().contains(newValue)) {
90 cloneAliasManager()[index.row()].name = newValue;
94 cloneAliasManager()[index.row()].expansion = newValue;
101 void AliasesModel::newAlias() {
102 QString newName("alias");
104 AliasManager &manager = cloneAliasManager();
105 while(manager.contains(newName)) {
107 newName = QString("alias%1").arg(i);
109 beginInsertRows(QModelIndex(), rowCount(), rowCount());
110 manager.addAlias(newName, "Expansion");
114 void AliasesModel::removeAlias(int index) {
115 if(index < 0 || index >= rowCount())
118 AliasManager &manager = cloneAliasManager();
119 beginRemoveRows(QModelIndex(), index, index);
120 manager.removeAt(index);
124 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
125 if(!index.isValid()) {
126 return Qt::ItemIsDropEnabled;
128 return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
133 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
135 header << tr("Alias")
138 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
139 return header[section];
144 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
146 if(row >= rowCount() || column >= columnCount())
147 return QModelIndex();
149 return createIndex(row, column);
153 const AliasManager &AliasesModel::aliasManager() const {
155 return _clonedAliasManager;
157 return _aliasManager;
160 AliasManager &AliasesModel::aliasManager() {
162 return _clonedAliasManager;
164 return _aliasManager;
167 AliasManager &AliasesModel::cloneAliasManager() {
168 if(!_configChanged) {
169 _clonedAliasManager = _aliasManager;
170 _configChanged = true;
171 emit configChanged(true);
173 return _clonedAliasManager;
176 void AliasesModel::revert() {
180 _configChanged = false;
181 emit configChanged(false);
185 void AliasesModel::commit() {
189 _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap());
193 void AliasesModel::initDone() {
198 void AliasesModel::clientConnected() {
199 _aliasManager = AliasManager();
200 Client::signalProxy()->synchronize(&_aliasManager);
201 connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone()));
202 connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert()));