a45eb7579af265024c3ee7a67f8d69e91327ef85
[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("Default Identity"));
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
87 bool Identity::isValid() const {
88   return (id() > 0);
89 }
90
91 bool Identity::initialized() const {
92   return _initialized;
93 }
94
95 void Identity::setInitialized() {
96   _initialized = true;
97 }
98
99 IdentityId Identity::id() const {
100   return _identityId;
101 }
102
103 QString Identity::identityName() const {
104   return _identityName;
105 }
106
107 QString Identity::realName() const {
108   return _realName;
109 }
110
111 QStringList Identity::nicks() const {
112   return _nicks;
113 }
114
115 QString Identity::awayNick() const {
116   return _awayNick;
117 }
118
119 bool Identity::awayNickEnabled() const {
120   return _awayNickEnabled;
121 }
122
123 QString Identity::awayReason() const {
124   return _awayReason;
125 }
126
127 bool Identity::awayReasonEnabled() const {
128   return _awayReasonEnabled;
129 }
130
131 QString Identity::returnMessage() const {
132   return _returnMessage;
133 }
134
135 bool Identity::returnMessageEnabled() const {
136   return _returnMessageEnabled;
137 }
138
139 bool Identity::autoAwayEnabled() const {
140   return _autoAwayEnabled;
141 }
142
143 int Identity::autoAwayTime() const {
144   return _autoAwayTime;
145 }
146
147 QString Identity::autoAwayReason() const {
148   return _autoAwayReason;
149 }
150
151 bool Identity::autoAwayReasonEnabled() const {
152   return _autoAwayReasonEnabled;
153 }
154
155 QString Identity::autoReturnMessage() const {
156   return _autoReturnMessage;
157 }
158
159 bool Identity::autoReturnMessageEnabled() const {
160   return _autoReturnMessageEnabled;
161 }
162
163 QString Identity::ident() const {
164   return _ident;
165 }
166
167 QString Identity::kickReason() const {
168   return _kickReason;
169 }
170
171 QString Identity::partReason() const
172 {return _partReason;}
173
174 QString Identity::quitReason() const {
175   return _quitReason;
176 }
177
178 /*** setters ***/
179
180 // NOTE: DO NOT USE ON SYNCHRONIZED OBJECTS!
181 void Identity::setId(IdentityId _id) {
182   _identityId = _id;
183   setObjectName(QString::number(id()));
184   //emit idSet(id);
185 }
186
187 void Identity::setIdentityName(const QString &identityName) {
188   _identityName = identityName;
189   emit identityNameSet(identityName);
190 }
191
192 void Identity::setRealName(const QString &realName) {
193   _realName = realName;
194   emit realNameSet(realName);
195 }
196
197 void Identity::setNicks(const QStringList &nicks) {
198   _nicks = nicks;
199   emit nicksSet(nicks);
200 }
201
202 void Identity::setAwayNick(const QString &nick) {
203   _awayNick = nick;
204   emit awayNickSet(nick);
205 }
206
207 void Identity::setAwayReason(const QString &reason) {
208   _awayReason = reason;
209   emit awayReasonSet(reason);
210 }
211
212 void Identity::setReturnMessage(const QString &message) {
213   _returnMessage = message;
214   emit returnMessageSet(message);
215 }
216
217 void Identity::setAwayNickEnabled(bool enabled) {
218   _awayNickEnabled = enabled;
219   emit awayNickEnabledSet(enabled);
220 }
221
222 void Identity::setAwayReasonEnabled(bool enabled) {
223   _awayReasonEnabled = enabled;
224   emit awayReasonEnabledSet(enabled);
225 }
226
227 void Identity::setReturnMessageEnabled(bool enabled) {
228   _returnMessageEnabled = enabled;
229   emit returnMessageEnabledSet(enabled);
230 }
231
232 void Identity::setAutoAwayEnabled(bool enabled) {
233   _autoAwayEnabled = enabled;
234   emit autoAwayEnabledSet(enabled);
235 }
236
237 void Identity::setAutoAwayTime(int time) {
238   _autoAwayTime = time;
239   emit autoAwayTimeSet(time);
240 }
241
242 void Identity::setAutoAwayReason(const QString & reason) {
243   _autoAwayReason = reason;
244   emit autoAwayReasonSet(reason);
245 }
246
247 void Identity::setAutoAwayReasonEnabled(bool enabled) {
248   _autoAwayReasonEnabled = enabled;
249   emit autoAwayReasonEnabledSet(enabled);
250 }
251
252 void Identity::setAutoReturnMessage(const QString & message) {
253   _autoReturnMessage = message;
254   emit autoReturnMessageSet(message);
255 }
256
257 void Identity::setAutoReturnMessageEnabled(bool enabled) {
258   _autoReturnMessageEnabled = enabled;
259   emit autoReturnMessageEnabledSet(enabled);
260 }
261
262 void Identity::setIdent(const QString & ident) {
263   _ident = ident;
264   emit identSet(ident);
265 }
266
267 void Identity::setKickReason(const QString & reason) {
268   _kickReason = reason;
269   emit kickReasonSet(reason);
270 }
271
272 void Identity::setPartReason(const QString & reason) {
273   _partReason = reason;
274   emit partReasonSet(reason);
275 }
276
277 void Identity::setQuitReason(const QString & reason) {
278   _quitReason = reason;
279   emit quitReasonSet(reason);
280 }
281
282 /***  ***/
283
284 void Identity::update(const Identity &other) {
285 for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
286     QMetaProperty metaProp = metaObject()->property(idx);
287     Q_ASSERT(metaProp.isValid());
288     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
289       setProperty(metaProp.name(), other.property(metaProp.name()));
290     }
291   }
292 }
293 #include <QDebug>
294 bool Identity::operator==(const Identity &other) {
295   for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
296     QMetaProperty metaProp = metaObject()->property(idx);
297     Q_ASSERT(metaProp.isValid());
298     QVariant v1 = this->property(metaProp.name());
299     QVariant v2 = other.property(metaProp.name()); //qDebug() << v1 << v2;
300     // QVariant cannot compare custom types, so we need to check for this case
301     if(QString(v1.typeName()) == "IdentityId") {
302       if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
303     } else {
304       if(v1 != v2) return false;
305     }
306   }
307   return true;
308 }
309
310 bool Identity::operator!=(const Identity &other) {
311   return !(*this == other);
312 }
313
314 ///////////////////////////////
315
316 QDataStream &operator<<(QDataStream &out, Identity id) {
317   out << id.toVariantMap();
318   return out;
319 }
320
321
322 QDataStream &operator>>(QDataStream &in, Identity &id) {
323   QVariantMap i;
324   in >> i;
325   id.fromVariantMap(i);
326   return in;
327 }
328
329