416414d571b1493c2c5e5a11ad563bf1c35cb2a7
[quassel.git] / src / qtui / settingspages / aliasesmodel.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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     _modelReady(false)
33 {
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()));
37
38   if(Client::isConnected())
39     clientConnected();
40   else
41     emit modelReady(false);
42 }
43
44 QVariant AliasesModel::data(const QModelIndex &index, int role) const {
45   if(!_modelReady)
46     return QVariant();
47
48   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount())
49     return QVariant();
50
51   switch(role) {
52   case Qt::ToolTipRole:
53     switch(index.column()) {
54     case 0:
55       return "<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";
58     case 1:
59       return "<b>The string the shortcut will be expanded to</b><br />"
60         "<b>special variables:</b><br />"
61         " - <b>$i</b> represents the i'th parameter.<br />"
62         " - <b>$i..j</b> represents the i'th to j'th parameter separated by spaces.<br />"
63         " - <b>$i..</b> represents all parameters from i on separated by spaces.<br />"
64         " - <b>$i:hostname</b> represents the hostname of the user identified by the i'th parameter or a * if unknown.<br />"
65         " - <b>$0</b> the whole string.<br />"
66         " - <b>$nick</b> your current nickname<br />"
67         " - <b>$channel</b> the name of the selected channel<br /><br />"
68         "Multiple commands can be separated with semicolons<br /><br />"
69         "<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";
70     default:
71       return QVariant();
72     }
73   case Qt::DisplayRole:
74   case Qt::EditRole:
75     switch(index.column()) {
76     case 0:
77       return aliasManager()[index.row()].name;
78     case 1:
79       return aliasManager()[index.row()].expansion;
80     default:
81       return QVariant();
82     }
83   default:
84     return QVariant();
85   }
86 }
87
88 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
89   if(!_modelReady)
90     return false;
91
92   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
93     return false;
94
95   QString newValue = value.toString();
96   if(newValue.isEmpty())
97     return false;
98
99   switch(index.column()) {
100   case 0:
101     if(aliasManager().contains(newValue)) {
102       return false;
103     } else {
104       cloneAliasManager()[index.row()].name = newValue;
105       return true;
106     }
107   case 1:
108     cloneAliasManager()[index.row()].expansion = newValue;
109     return true;
110   default:
111     return false;
112   }
113 }
114
115 void AliasesModel::newAlias() {
116   QString newName("alias");
117   int i = 0;
118   AliasManager &manager = cloneAliasManager();
119   while(manager.contains(newName)) {
120     i++;
121     newName = QString("alias%1").arg(i);
122   }
123   beginInsertRows(QModelIndex(), rowCount(), rowCount());
124   manager.addAlias(newName, "Expansion");
125   endInsertRows();
126 }
127
128 void AliasesModel::loadDefaults() {
129   if(!_modelReady)
130     return;
131
132   AliasManager &manager = cloneAliasManager();
133
134   if(!manager.isEmpty()) {
135     beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
136     for(int i = rowCount() - 1; i >= 0; i--)
137       manager.removeAt(i);
138     endRemoveRows();
139   }
140
141   AliasManager::AliasList defaults = AliasManager::defaults();
142   beginInsertRows(QModelIndex(), 0, defaults.count() - 1);
143   foreach(AliasManager::Alias alias, defaults) {
144     manager.addAlias(alias.name, alias.expansion);
145   }
146   endInsertRows();
147 }
148
149 void AliasesModel::removeAlias(int index) {
150   if(index < 0 || index >= rowCount())
151     return;
152
153   AliasManager &manager = cloneAliasManager();  
154   beginRemoveRows(QModelIndex(), index, index);
155   manager.removeAt(index);
156   endRemoveRows();
157 }
158
159 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
160   if(!index.isValid()) {
161     return Qt::ItemIsDropEnabled;
162   } else {
163     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
164   }
165 }
166
167
168 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
169   QStringList header;
170   header << tr("Alias")
171          << tr("Expansion");
172
173   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
174     return header[section];
175
176   return QVariant();
177 }
178
179 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
180   Q_UNUSED(parent);
181   if(row >= rowCount() || column >= columnCount())
182     return QModelIndex();
183
184   return createIndex(row, column);
185 }
186
187
188 const AliasManager &AliasesModel::aliasManager() const {
189   if(_configChanged)
190     return _clonedAliasManager;
191   else
192     return *Client::aliasManager();
193 }
194
195 AliasManager &AliasesModel::aliasManager() {
196   if(_configChanged)
197     return _clonedAliasManager;
198   else
199     return *Client::aliasManager();
200 }
201
202 AliasManager &AliasesModel::cloneAliasManager() {
203   if(!_configChanged) {
204     _clonedAliasManager = *Client::aliasManager();
205     _configChanged = true;
206     emit configChanged(true);
207   }
208   return _clonedAliasManager;
209 }
210
211 void AliasesModel::revert() {
212   if(!_configChanged)
213     return;
214
215   _configChanged = false;
216   emit configChanged(false);
217   reset();
218 }
219
220 void AliasesModel::commit() {
221   if(!_configChanged)
222     return;
223
224   Client::aliasManager()->requestUpdate(_clonedAliasManager.toVariantMap());
225   revert();
226 }
227
228 void AliasesModel::initDone() {
229   _modelReady = true;
230   reset();
231   emit modelReady(true);
232 }
233
234 void AliasesModel::clientConnected() {
235   connect(Client::aliasManager(), SIGNAL(updated(QVariantMap)), SLOT(revert()));
236   if(Client::aliasManager()->isInitialized())
237     initDone();
238   else
239     connect(Client::aliasManager(), SIGNAL(initDone()), SLOT(initDone()));
240 }
241
242 void AliasesModel::clientDisconnected() {
243   // clear
244   _clonedAliasManager = ClientAliasManager();
245   _modelReady = false;
246   reset();
247   emit modelReady(false);
248 }