8df227910d1e4ca6f095c30c41cc58cfe0591504
[quassel.git] / src / qtui / settingspages / aliasesmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "aliasesmodel.h"
22
23 #include <QDebug>
24 #include <QStringList>
25
26 #include "client.h"
27 #include "signalproxy.h"
28
29 AliasesModel::AliasesModel(QObject *parent)
30   : QAbstractItemModel(parent),
31     _configChanged(false)
32 {
33   // we need this signal for future connects to reset the data;
34   connect(Client::instance(), SIGNAL(connected()), this, SLOT(clientConnected()));
35   connect(Client::instance(), SIGNAL(disconnected()), this, SLOT(clientDisconnected()));
36   if(Client::isConnected())
37     clientConnected();
38   else
39     emit modelReady(false);
40 }
41
42 QVariant AliasesModel::data(const QModelIndex &index, int role) const {
43   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
44     return QVariant();
45
46   switch(role) {
47   case Qt::ToolTipRole:
48     switch(index.column()) {
49     case 0:
50       return "<b>The shortcut for the alias</b><br />"
51         "It can be used as a regular slash command.<br /><br />"
52         "<b>Example:</b> \"foo\" can be used per /foo";
53     case 1:
54       return "<b>The string the shortcut will be expanded to</b><br />"
55         "$i represenents the i'th parameter. $0 the whole string.<br />"
56         "Multiple commands can be separated with semicolons<br /><br />"
57         "<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";
58     default:
59       return QVariant();
60     }
61   case Qt::DisplayRole:
62   case Qt::EditRole:
63     switch(index.column()) {
64     case 0:
65       return aliasManager()[index.row()].name;
66     case 1:
67       return aliasManager()[index.row()].expansion;
68     default:
69       return QVariant();
70     }
71   default:
72     return QVariant();
73   }
74 }
75
76 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
77   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
78     return false;
79
80   QString newValue = value.toString();
81   if(newValue.isEmpty())
82     return false;
83   
84   switch(index.column()) {
85   case 0:
86     if(aliasManager().contains(newValue)) {
87       return false;
88     } else {
89       cloneAliasManager()[index.row()].name = newValue;
90       return true;
91     }
92   case 1:
93     cloneAliasManager()[index.row()].expansion = newValue;
94     return true;
95   default:
96     return false;
97   }
98 }
99
100 void AliasesModel::newAlias() {
101   QString newName("alias");
102   int i = 0;
103   AliasManager &manager = cloneAliasManager();
104   while(manager.contains(newName)) {
105     i++;
106     newName = QString("alias%1").arg(i);
107   }
108   beginInsertRows(QModelIndex(), rowCount(), rowCount());
109   manager.addAlias(newName, "Expansion");
110   endInsertRows();
111 }
112
113 void AliasesModel::removeAlias(int index) {
114   if(index < 0 || index >= rowCount())
115     return;
116
117   AliasManager &manager = cloneAliasManager();  
118   beginRemoveRows(QModelIndex(), index, index);
119   manager.removeAt(index);
120   endRemoveRows();
121 }
122
123 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
124   if(!index.isValid()) {
125     return Qt::ItemIsDropEnabled;
126   } else {
127     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
128   }
129 }
130
131
132 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
133   QStringList header;
134   header << tr("Alias")
135          << tr("Expansion");
136   
137   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
138     return header[section];
139
140   return QVariant();
141 }
142
143 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
144   Q_UNUSED(parent);
145   if(row >= rowCount() || column >= columnCount())
146     return QModelIndex();
147
148   return createIndex(row, column);
149 }
150
151
152 const AliasManager &AliasesModel::aliasManager() const {
153   if(_configChanged)
154     return _clonedAliasManager;
155   else
156     return _aliasManager;
157 }
158
159 AliasManager &AliasesModel::aliasManager() {
160   if(_configChanged)
161     return _clonedAliasManager;
162   else
163     return _aliasManager;
164 }
165
166 AliasManager &AliasesModel::cloneAliasManager() {
167   if(!_configChanged) {
168     _clonedAliasManager = _aliasManager;
169     _configChanged = true;
170     emit configChanged(true);
171   }
172   return _clonedAliasManager;
173 }
174
175 void AliasesModel::revert() {
176   if(!_configChanged)
177     return;
178   
179   _configChanged = false;
180   emit configChanged(false);
181   reset();
182 }
183
184 void AliasesModel::commit() {
185   if(!_configChanged)
186     return;
187
188   _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap());
189   revert();
190 }  
191
192 void AliasesModel::initDone() {
193   reset();
194   emit modelReady(true);
195 }
196
197 void AliasesModel::clientConnected() {
198   _aliasManager = AliasManager();
199   Client::signalProxy()->synchronize(&_aliasManager);
200   connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone()));
201   connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert()));
202 }
203
204 void AliasesModel::clientDisconnected() {
205   // clear alias managers
206   _aliasManager = AliasManager();
207   _clonedAliasManager = AliasManager();
208   reset();
209   emit modelReady(false);
210 }