Make custom highlights work again
[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         "<b>special variables:</b><br />"
56         " - <b>$i</b> represenents the i'th parameter.<br />"
57         " - <b>$i:hostname</b> represents the hostname of the user identified by the i'th parameter or a * if unknown.<br />"
58         " - <b>$0</b> the whole string.<br />"
59         " - <b>$currentnick</b> your current nickname<br />"
60         " - <b>$channelname</b> the name of the selected channel<br /><br />"
61         "Multiple commands can be separated with semicolons<br /><br />"
62         "<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";
63     default:
64       return QVariant();
65     }
66   case Qt::DisplayRole:
67   case Qt::EditRole:
68     switch(index.column()) {
69     case 0:
70       return aliasManager()[index.row()].name;
71     case 1:
72       return aliasManager()[index.row()].expansion;
73     default:
74       return QVariant();
75     }
76   default:
77     return QVariant();
78   }
79 }
80
81 bool AliasesModel::setData(const QModelIndex &index, const QVariant &value, int role) {
82   if(!index.isValid() || index.row() >= rowCount() || index.column() >= columnCount() || role != Qt::EditRole)
83     return false;
84
85   QString newValue = value.toString();
86   if(newValue.isEmpty())
87     return false;
88   
89   switch(index.column()) {
90   case 0:
91     if(aliasManager().contains(newValue)) {
92       return false;
93     } else {
94       cloneAliasManager()[index.row()].name = newValue;
95       return true;
96     }
97   case 1:
98     cloneAliasManager()[index.row()].expansion = newValue;
99     return true;
100   default:
101     return false;
102   }
103 }
104
105 void AliasesModel::newAlias() {
106   QString newName("alias");
107   int i = 0;
108   AliasManager &manager = cloneAliasManager();
109   while(manager.contains(newName)) {
110     i++;
111     newName = QString("alias%1").arg(i);
112   }
113   beginInsertRows(QModelIndex(), rowCount(), rowCount());
114   manager.addAlias(newName, "Expansion");
115   endInsertRows();
116 }
117
118 void AliasesModel::removeAlias(int index) {
119   if(index < 0 || index >= rowCount())
120     return;
121
122   AliasManager &manager = cloneAliasManager();  
123   beginRemoveRows(QModelIndex(), index, index);
124   manager.removeAt(index);
125   endRemoveRows();
126 }
127
128 Qt::ItemFlags AliasesModel::flags(const QModelIndex &index) const {
129   if(!index.isValid()) {
130     return Qt::ItemIsDropEnabled;
131   } else {
132     return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsEditable;
133   }
134 }
135
136
137 QVariant AliasesModel::headerData(int section, Qt::Orientation orientation, int role) const {
138   QStringList header;
139   header << tr("Alias")
140          << tr("Expansion");
141   
142   if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
143     return header[section];
144
145   return QVariant();
146 }
147
148 QModelIndex AliasesModel::index(int row, int column, const QModelIndex &parent) const {
149   Q_UNUSED(parent);
150   if(row >= rowCount() || column >= columnCount())
151     return QModelIndex();
152
153   return createIndex(row, column);
154 }
155
156
157 const AliasManager &AliasesModel::aliasManager() const {
158   if(_configChanged)
159     return _clonedAliasManager;
160   else
161     return _aliasManager;
162 }
163
164 AliasManager &AliasesModel::aliasManager() {
165   if(_configChanged)
166     return _clonedAliasManager;
167   else
168     return _aliasManager;
169 }
170
171 AliasManager &AliasesModel::cloneAliasManager() {
172   if(!_configChanged) {
173     _clonedAliasManager = _aliasManager;
174     _configChanged = true;
175     emit configChanged(true);
176   }
177   return _clonedAliasManager;
178 }
179
180 void AliasesModel::revert() {
181   if(!_configChanged)
182     return;
183   
184   _configChanged = false;
185   emit configChanged(false);
186   reset();
187 }
188
189 void AliasesModel::commit() {
190   if(!_configChanged)
191     return;
192
193   _aliasManager.requestUpdate(_clonedAliasManager.toVariantMap());
194   revert();
195 }  
196
197 void AliasesModel::initDone() {
198   reset();
199   emit modelReady(true);
200 }
201
202 void AliasesModel::clientConnected() {
203   _aliasManager = AliasManager();
204   Client::signalProxy()->synchronize(&_aliasManager);
205   connect(&_aliasManager, SIGNAL(initDone()), this, SLOT(initDone()));
206   connect(&_aliasManager, SIGNAL(updated(const QVariantMap &)), this, SLOT(revert()));
207 }
208
209 void AliasesModel::clientDisconnected() {
210   // clear alias managers
211   _aliasManager = AliasManager();
212   _clonedAliasManager = AliasManager();
213   reset();
214   emit modelReady(false);
215 }