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