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