_Almost_ finished the identity dialog (which is still not used and not visible yet...
[quassel.git] / src / common / identity.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 <QMetaProperty>
22 #include <QVariantMap>
23
24 #include "identity.h"
25
26 Identity::Identity(IdentityId id, QObject *parent) : SyncableObject(parent), _identityId(id) {
27   init();
28   setToDefaults();
29 }
30
31 Identity::Identity(const Identity &other, QObject *parent) : SyncableObject(parent),
32             _identityId(other.id()),
33             _identityName(other.identityName()),
34             _realName(other.realName()),
35             _nicks(other.nicks()),
36             _awayNick(other.awayNick()),
37             _awayNickEnabled(other.awayNickEnabled()),
38             _awayReason(other.awayReason()),
39             _awayReasonEnabled(other.awayReasonEnabled()),
40             _returnMessage(other.returnMessage()),
41             _returnMessageEnabled(other.returnMessageEnabled()),
42             _autoAwayEnabled(other.autoAwayEnabled()),
43             _autoAwayTime(other.autoAwayTime()),
44             _autoAwayReason(other.autoAwayReason()),
45             _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
46             _autoReturnMessage(other.autoReturnMessage()),
47             _autoReturnMessageEnabled(other.autoReturnMessageEnabled()),
48             _ident(other.ident()),
49             _kickReason(other.kickReason()),
50             _partReason(other.partReason()),
51             _quitReason(other.quitReason())
52
53 {
54   init();
55 }
56
57 void Identity::init() {
58   _initialized = false;
59   setObjectName(QString::number(id()));
60 }
61
62 void Identity::setToDefaults() {
63   setIdentityName(tr("<empty>"));
64   setRealName(tr("Quassel IRC User"));
65   QStringList n;
66   n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
67   setNicks(n);
68   setAwayNick("");
69   setAwayNickEnabled(false);
70   setAwayReason(tr("Gone fishing."));
71   setAwayReasonEnabled(true);
72   setReturnMessage(tr("Brought fish."));
73   setReturnMessageEnabled(false);
74   setAutoAwayEnabled(false);
75   setAutoAwayTime(10);
76   setAutoAwayReason(tr("Not here. No, really. not here!"));
77   setAutoAwayReasonEnabled(false);
78   setAutoReturnMessage(tr("Back in action again!"));
79   setAutoReturnMessageEnabled(false);
80   setIdent("quassel");
81   setKickReason(tr("Kindergarten is elsewhere!"));
82   setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
83   setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
84 }
85
86 bool Identity::isValid() const {
87   return (id() > 0);
88 }
89
90 bool Identity::initialized() const {
91   return _initialized;
92 }
93
94 void Identity::setInitialized() {
95   _initialized = true;
96 }
97
98 IdentityId Identity::id() const {
99   return _identityId;
100 }
101
102 QString Identity::identityName() const {
103   return _identityName;
104 }
105
106 QString Identity::realName() const {
107   return _realName;
108 }
109
110 QStringList Identity::nicks() const {
111   return _nicks;
112 }
113
114 QString Identity::awayNick() const {
115   return _awayNick;
116 }
117
118 bool Identity::awayNickEnabled() const {
119   return _awayNickEnabled;
120 }
121
122 QString Identity::awayReason() const {
123   return _awayReason;
124 }
125
126 bool Identity::awayReasonEnabled() const {
127   return _awayReasonEnabled;
128 }
129
130 QString Identity::returnMessage() const {
131   return _returnMessage;
132 }
133
134 bool Identity::returnMessageEnabled() const {
135   return _returnMessageEnabled;
136 }
137
138 bool Identity::autoAwayEnabled() const {
139   return _autoAwayEnabled;
140 }
141
142 int Identity::autoAwayTime() const {
143   return _autoAwayTime;
144 }
145
146 QString Identity::autoAwayReason() const {
147   return _autoAwayReason;
148 }
149
150 bool Identity::autoAwayReasonEnabled() const {
151   return _autoAwayReasonEnabled;
152 }
153
154 QString Identity::autoReturnMessage() const {
155   return _autoReturnMessage;
156 }
157
158 bool Identity::autoReturnMessageEnabled() const {
159   return _autoReturnMessageEnabled;
160 }
161
162 QString Identity::ident() const {
163   return _ident;
164 }
165
166 QString Identity::kickReason() const {
167   return _kickReason;
168 }
169
170 QString Identity::partReason() const
171 {return _partReason;}
172
173 QString Identity::quitReason() const {
174   return _quitReason;
175 }
176
177 /*** setters ***/
178
179 // NOTE: DO NOT USE ON SYNCHRONIZED OBJECTS!
180 void Identity::setId(IdentityId _id) {
181   _identityId = _id;
182   setObjectName(QString::number(id()));
183   //emit idSet(id);
184 }
185
186 void Identity::setIdentityName(const QString &identityName) {
187   _identityName = identityName;
188   emit identityNameSet(identityName);
189 }
190
191 void Identity::setRealName(const QString &realName) {
192   _realName = realName;
193   emit realNameSet(realName);
194 }
195
196 void Identity::setNicks(const QStringList &nicks) {
197   _nicks = nicks;
198   emit nicksSet(nicks);
199 }
200
201 void Identity::setAwayNick(const QString &nick) {
202   _awayNick = nick;
203   emit awayNickSet(nick);
204 }
205
206 void Identity::setAwayReason(const QString &reason) {
207   _awayReason = reason;
208   emit awayReasonSet(reason);
209 }
210
211 void Identity::setReturnMessage(const QString &message) {
212   _returnMessage = message;
213   emit returnMessageSet(message);
214 }
215
216 void Identity::setAwayNickEnabled(bool enabled) {
217   _awayNickEnabled = enabled;
218   emit awayNickEnabledSet(enabled);
219 }
220
221 void Identity::setAwayReasonEnabled(bool enabled) {
222   _awayReasonEnabled = enabled;
223   emit awayReasonEnabledSet(enabled);
224 }
225
226 void Identity::setReturnMessageEnabled(bool enabled) {
227   _returnMessageEnabled = enabled;
228   emit returnMessageEnabledSet(enabled);
229 }
230
231 void Identity::setAutoAwayEnabled(bool enabled) {
232   _autoAwayEnabled = enabled;
233   emit autoAwayEnabledSet(enabled);
234 }
235
236 void Identity::setAutoAwayTime(int time) {
237   _autoAwayTime = time;
238   emit autoAwayTimeSet(time);
239 }
240
241 void Identity::setAutoAwayReason(const QString & reason) {
242   _autoAwayReason = reason;
243   emit autoAwayReasonSet(reason);
244 }
245
246 void Identity::setAutoAwayReasonEnabled(bool enabled) {
247   _autoAwayReasonEnabled = enabled;
248   emit autoAwayReasonEnabledSet(enabled);
249 }
250
251 void Identity::setAutoReturnMessage(const QString & message) {
252   _autoReturnMessage = message;
253   emit autoReturnMessageSet(message);
254 }
255
256 void Identity::setAutoReturnMessageEnabled(bool enabled) {
257   _autoReturnMessageEnabled = enabled;
258   emit autoReturnMessageEnabledSet(enabled);
259 }
260
261 void Identity::setIdent(const QString & ident) {
262   _ident = ident;
263   emit identSet(ident);
264 }
265
266 void Identity::setKickReason(const QString & reason) {
267   _kickReason = reason;
268   emit kickReasonSet(reason);
269 }
270
271 void Identity::setPartReason(const QString & reason) {
272   _partReason = reason;
273   emit partReasonSet(reason);
274 }
275
276 void Identity::setQuitReason(const QString & reason) {
277   _quitReason = reason;
278   emit quitReasonSet(reason);
279 }
280
281 /***  ***/
282
283 void Identity::update(const Identity &other) {
284 for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
285     QMetaProperty metaProp = metaObject()->property(idx);
286     Q_ASSERT(metaProp.isValid());
287     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
288       setProperty(metaProp.name(), other.property(metaProp.name()));
289     }
290   }
291 }
292 #include <QDebug>
293 bool Identity::operator==(const Identity &other) {
294   for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
295     QMetaProperty metaProp = metaObject()->property(idx);
296     Q_ASSERT(metaProp.isValid());
297     QVariant v1 = this->property(metaProp.name());
298     QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
299     // QVariant cannot compare custom types, so we need to check for this special case
300     if(QString(v1.typeName()) == "IdentityId") {
301       if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
302     } else {
303       if(v1 != v2) return false;
304     }
305   }
306   return true;
307 }
308
309 bool Identity::operator!=(const Identity &other) {
310   return !(*this == other);
311 }
312
313 ///////////////////////////////
314
315 QDataStream &operator<<(QDataStream &out, Identity id) {
316   out << id.toVariantMap();
317   return out;
318 }
319
320
321 QDataStream &operator>>(QDataStream &in, Identity &id) {
322   QVariantMap i;
323   in >> i;
324   id.fromVariantMap(i);
325   return in;
326 }
327
328