1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QMetaProperty>
22 #include <QVariantMap>
26 Identity::Identity(IdentityId id, QObject *parent) : SyncableObject(parent), _identityId(id) {
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())
56 void Identity::init() {
57 setObjectName(QString::number(id().toInt()));
58 setAllowClientUpdates(true);
61 void Identity::setToDefaults() {
62 setIdentityName(tr("<empty>"));
63 setRealName(tr("Quassel IRC User"));
65 n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
68 setAwayNickEnabled(false);
69 setAwayReason(tr("Gone fishing."));
70 setAwayReasonEnabled(true);
71 setAutoAwayEnabled(false);
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);
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."));
84 bool Identity::isValid() const {
85 return (id().toInt() > 0);
88 IdentityId Identity::id() const {
92 QString Identity::identityName() const {
96 QString Identity::realName() const {
100 QStringList Identity::nicks() const {
104 QString Identity::awayNick() const {
108 bool Identity::awayNickEnabled() const {
109 return _awayNickEnabled;
112 QString Identity::awayReason() const {
116 bool Identity::awayReasonEnabled() const {
117 return _awayReasonEnabled;
120 bool Identity::autoAwayEnabled() const {
121 return _autoAwayEnabled;
124 int Identity::autoAwayTime() const {
125 return _autoAwayTime;
128 QString Identity::autoAwayReason() const {
129 return _autoAwayReason;
132 bool Identity::autoAwayReasonEnabled() const {
133 return _autoAwayReasonEnabled;
136 bool Identity::detachAwayEnabled() const {
137 return _detachAwayEnabled;
140 QString Identity::detachAwayReason() const {
141 return _detachAwayReason;
144 bool Identity::detachAwayReasonEnabled() const {
145 return _detachAwayReasonEnabled;
148 QString Identity::ident() const {
152 QString Identity::kickReason() const {
156 QString Identity::partReason() const
157 {return _partReason;}
159 QString Identity::quitReason() const {
165 // NOTE: DO NOT USE ON SYNCHRONIZED OBJECTS!
166 void Identity::setId(IdentityId _id) {
168 setObjectName(QString::number(id().toInt()));
172 void Identity::setIdentityName(const QString &identityName) {
173 _identityName = identityName;
174 emit identityNameSet(identityName);
177 void Identity::setRealName(const QString &realName) {
178 _realName = realName;
179 emit realNameSet(realName);
182 void Identity::setNicks(const QStringList &nicks) {
184 emit nicksSet(nicks);
187 void Identity::setAwayNick(const QString &nick) {
189 emit awayNickSet(nick);
192 void Identity::setAwayReason(const QString &reason) {
193 _awayReason = reason;
194 emit awayReasonSet(reason);
197 void Identity::setAwayNickEnabled(bool enabled) {
198 _awayNickEnabled = enabled;
199 emit awayNickEnabledSet(enabled);
202 void Identity::setAwayReasonEnabled(bool enabled) {
203 _awayReasonEnabled = enabled;
204 emit awayReasonEnabledSet(enabled);
207 void Identity::setAutoAwayEnabled(bool enabled) {
208 _autoAwayEnabled = enabled;
209 emit autoAwayEnabledSet(enabled);
212 void Identity::setAutoAwayTime(int time) {
213 _autoAwayTime = time;
214 emit autoAwayTimeSet(time);
217 void Identity::setAutoAwayReason(const QString & reason) {
218 _autoAwayReason = reason;
219 emit autoAwayReasonSet(reason);
222 void Identity::setAutoAwayReasonEnabled(bool enabled) {
223 _autoAwayReasonEnabled = enabled;
224 emit autoAwayReasonEnabledSet(enabled);
227 void Identity::setDetachAwayEnabled(bool enabled) {
228 _detachAwayEnabled = enabled;
229 emit detachAwayEnabledSet(enabled);
232 void Identity::setDetachAwayReason(const QString & reason) {
233 _detachAwayReason = reason;
234 emit detachAwayReasonSet(reason);
237 void Identity::setDetachAwayReasonEnabled(bool enabled) {
238 _detachAwayReasonEnabled = enabled;
239 emit detachAwayReasonEnabledSet(enabled);
242 void Identity::setIdent(const QString & ident) {
244 emit identSet(ident);
247 void Identity::setKickReason(const QString & reason) {
248 _kickReason = reason;
249 emit kickReasonSet(reason);
252 void Identity::setPartReason(const QString & reason) {
253 _partReason = reason;
254 emit partReasonSet(reason);
257 void Identity::setQuitReason(const QString & reason) {
258 _quitReason = reason;
259 emit quitReasonSet(reason);
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()));
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;
284 if(v1 != v2) return false;
290 bool Identity::operator!=(const Identity &other) {
291 return !(*this == other);
294 ///////////////////////////////
296 QDataStream &operator<<(QDataStream &out, Identity id) {
297 out << id.toVariantMap();
302 QDataStream &operator>>(QDataStream &in, Identity &id) {
305 id.fromVariantMap(i);