aee2e4a8116fd19e047eb8d7d099faeb205c1f7a
[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)
27   : SyncableObject(parent),
28     _identityId(id)
29 {
30   init();
31   setToDefaults();
32 }
33
34 Identity::Identity(const Identity &other, QObject *parent)
35   : SyncableObject(parent),
36     _identityId(other.id()),
37     _identityName(other.identityName()),
38     _realName(other.realName()),
39     _nicks(other.nicks()),
40     _awayNick(other.awayNick()),
41     _awayNickEnabled(other.awayNickEnabled()),
42     _awayReason(other.awayReason()),
43     _awayReasonEnabled(other.awayReasonEnabled()),
44     _autoAwayEnabled(other.autoAwayEnabled()),
45     _autoAwayTime(other.autoAwayTime()),
46     _autoAwayReason(other.autoAwayReason()),
47     _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
48     _detachAwayEnabled(other.detachAwayEnabled()),
49     _detachAwayReason(other.detachAwayReason()),
50     _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
51     _ident(other.ident()),
52     _kickReason(other.kickReason()),
53     _partReason(other.partReason()),
54     _quitReason(other.quitReason())
55 {
56   init();
57 }
58
59 void Identity::init() {
60   setObjectName(QString::number(id().toInt()));
61   setAllowClientUpdates(true);
62 }
63
64 void Identity::setToDefaults() {
65   setIdentityName(tr("<empty>"));
66   setRealName(tr("Quassel IRC User"));
67   QStringList n;
68   n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
69   setNicks(n);
70   setAwayNick("");
71   setAwayNickEnabled(false);
72   setAwayReason(tr("Gone fishing."));
73   setAwayReasonEnabled(true);
74   setAutoAwayEnabled(false);
75   setAutoAwayTime(10);
76   setAutoAwayReason(tr("Not here. No, really. not here!"));
77   setAutoAwayReasonEnabled(false);
78   setDetachAwayEnabled(false);
79   setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
80   setDetachAwayReasonEnabled(false);
81   setIdent("quassel");
82   setKickReason(tr("Kindergarten is elsewhere!"));
83   setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
84   setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
85 }
86
87 /*** setters ***/
88
89 void Identity::setId(IdentityId _id) {
90   _identityId = _id;
91   emit idSet(_id);
92   renameObject(QString::number(id().toInt()));
93 }
94
95 void Identity::setIdentityName(const QString &identityName) {
96   _identityName = identityName;
97   emit identityNameSet(identityName);
98 }
99
100 void Identity::setRealName(const QString &realName) {
101   _realName = realName;
102   emit realNameSet(realName);
103 }
104
105 void Identity::setNicks(const QStringList &nicks) {
106   _nicks = nicks;
107   emit nicksSet(nicks);
108 }
109
110 void Identity::setAwayNick(const QString &nick) {
111   _awayNick = nick;
112   emit awayNickSet(nick);
113 }
114
115 void Identity::setAwayReason(const QString &reason) {
116   _awayReason = reason;
117   emit awayReasonSet(reason);
118 }
119
120 void Identity::setAwayNickEnabled(bool enabled) {
121   _awayNickEnabled = enabled;
122   emit awayNickEnabledSet(enabled);
123 }
124
125 void Identity::setAwayReasonEnabled(bool enabled) {
126   _awayReasonEnabled = enabled;
127   emit awayReasonEnabledSet(enabled);
128 }
129
130 void Identity::setAutoAwayEnabled(bool enabled) {
131   _autoAwayEnabled = enabled;
132   emit autoAwayEnabledSet(enabled);
133 }
134
135 void Identity::setAutoAwayTime(int time) {
136   _autoAwayTime = time;
137   emit autoAwayTimeSet(time);
138 }
139
140 void Identity::setAutoAwayReason(const QString &reason) {
141   _autoAwayReason = reason;
142   emit autoAwayReasonSet(reason);
143 }
144
145 void Identity::setAutoAwayReasonEnabled(bool enabled) {
146   _autoAwayReasonEnabled = enabled;
147   emit autoAwayReasonEnabledSet(enabled);
148 }
149
150 void Identity::setDetachAwayEnabled(bool enabled) {
151   _detachAwayEnabled = enabled;
152   emit detachAwayEnabledSet(enabled);
153 }
154
155 void Identity::setDetachAwayReason(const QString &reason) {
156   _detachAwayReason = reason;
157   emit detachAwayReasonSet(reason);
158 }
159
160 void Identity::setDetachAwayReasonEnabled(bool enabled) {
161   _detachAwayReasonEnabled = enabled;
162   emit detachAwayReasonEnabledSet(enabled);
163 }
164
165 void Identity::setIdent(const QString &ident) {
166   _ident = ident;
167   emit identSet(ident);
168 }
169
170 void Identity::setKickReason(const QString &reason) {
171   _kickReason = reason;
172   emit kickReasonSet(reason);
173 }
174
175 void Identity::setPartReason(const QString &reason) {
176   _partReason = reason;
177   emit partReasonSet(reason);
178 }
179
180 void Identity::setQuitReason(const QString &reason) {
181   _quitReason = reason;
182   emit quitReasonSet(reason);
183 }
184
185 /***  ***/
186
187 void Identity::update(const Identity &other) {
188   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
189     QMetaProperty metaProp = staticMetaObject.property(idx);
190     Q_ASSERT(metaProp.isValid());
191     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
192       setProperty(metaProp.name(), other.property(metaProp.name()));
193     }
194   }
195 }
196
197 bool Identity::operator==(const Identity &other) const {
198   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
199     QMetaProperty metaProp = staticMetaObject.property(idx);
200     Q_ASSERT(metaProp.isValid());
201     QVariant v1 = this->property(metaProp.name());
202     QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
203     // QVariant cannot compare custom types, so we need to check for this special case
204     if(QString(v1.typeName()) == "IdentityId") {
205       if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
206     } else {
207       if(v1 != v2) return false;
208     }
209   }
210   return true;
211 }
212
213 bool Identity::operator!=(const Identity &other) const {
214   return !(*this == other);
215 }
216
217 ///////////////////////////////
218
219 QDataStream &operator<<(QDataStream &out, Identity id) {
220   out << id.toVariantMap();
221   return out;
222 }
223
224
225 QDataStream &operator>>(QDataStream &in, Identity &id) {
226   QVariantMap i;
227   in >> i;
228   id.fromVariantMap(i);
229   return in;
230 }
231