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