1 /***************************************************************************
2 * Copyright (C) 2005-09 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 ***************************************************************************/
23 #include <QMetaProperty>
24 #include <QVariantMap>
27 # include <CoreServices/CoreServices.h>
28 # include "mac_utils.h"
31 Identity::Identity(IdentityId id, QObject *parent)
32 : SyncableObject(parent),
39 Identity::Identity(const Identity &other, QObject *parent)
40 : SyncableObject(parent),
41 _identityId(other.id()),
42 _identityName(other.identityName()),
43 _realName(other.realName()),
44 _nicks(other.nicks()),
45 _awayNick(other.awayNick()),
46 _awayNickEnabled(other.awayNickEnabled()),
47 _awayReason(other.awayReason()),
48 _awayReasonEnabled(other.awayReasonEnabled()),
49 _autoAwayEnabled(other.autoAwayEnabled()),
50 _autoAwayTime(other.autoAwayTime()),
51 _autoAwayReason(other.autoAwayReason()),
52 _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
53 _detachAwayEnabled(other.detachAwayEnabled()),
54 _detachAwayReason(other.detachAwayReason()),
55 _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
56 _ident(other.ident()),
57 _kickReason(other.kickReason()),
58 _partReason(other.partReason()),
59 _quitReason(other.quitReason())
64 void Identity::init() {
65 setObjectName(QString::number(id().toInt()));
66 setAllowClientUpdates(true);
69 QString Identity::defaultNick() {
71 return CFStringToQString(CSCopyUserName(true));
73 return QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
77 QString Identity::defaultRealName() {
79 return CFStringToQString(CSCopyUserName(false));
81 return tr("Quassel IRC User");
85 void Identity::setToDefaults() {
86 setIdentityName(tr("<empty>"));
87 setRealName(defaultRealName());
92 setAwayNickEnabled(false);
93 setAwayReason(tr("Gone fishing."));
94 setAwayReasonEnabled(true);
95 setAutoAwayEnabled(false);
97 setAutoAwayReason(tr("Not here. No, really. not here!"));
98 setAutoAwayReasonEnabled(false);
99 setDetachAwayEnabled(false);
100 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
101 setDetachAwayReasonEnabled(false);
103 setKickReason(tr("Kindergarten is elsewhere!"));
104 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
105 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
110 void Identity::setId(IdentityId _id) {
113 renameObject(QString::number(id().toInt()));
116 void Identity::setIdentityName(const QString &identityName) {
117 _identityName = identityName;
118 emit identityNameSet(identityName);
121 void Identity::setRealName(const QString &realName) {
122 _realName = realName;
123 emit realNameSet(realName);
126 void Identity::setNicks(const QStringList &nicks) {
128 emit nicksSet(nicks);
131 void Identity::setAwayNick(const QString &nick) {
133 emit awayNickSet(nick);
136 void Identity::setAwayReason(const QString &reason) {
137 _awayReason = reason;
138 emit awayReasonSet(reason);
141 void Identity::setAwayNickEnabled(bool enabled) {
142 _awayNickEnabled = enabled;
143 emit awayNickEnabledSet(enabled);
146 void Identity::setAwayReasonEnabled(bool enabled) {
147 _awayReasonEnabled = enabled;
148 emit awayReasonEnabledSet(enabled);
151 void Identity::setAutoAwayEnabled(bool enabled) {
152 _autoAwayEnabled = enabled;
153 emit autoAwayEnabledSet(enabled);
156 void Identity::setAutoAwayTime(int time) {
157 _autoAwayTime = time;
158 emit autoAwayTimeSet(time);
161 void Identity::setAutoAwayReason(const QString &reason) {
162 _autoAwayReason = reason;
163 emit autoAwayReasonSet(reason);
166 void Identity::setAutoAwayReasonEnabled(bool enabled) {
167 _autoAwayReasonEnabled = enabled;
168 emit autoAwayReasonEnabledSet(enabled);
171 void Identity::setDetachAwayEnabled(bool enabled) {
172 _detachAwayEnabled = enabled;
173 emit detachAwayEnabledSet(enabled);
176 void Identity::setDetachAwayReason(const QString &reason) {
177 _detachAwayReason = reason;
178 emit detachAwayReasonSet(reason);
181 void Identity::setDetachAwayReasonEnabled(bool enabled) {
182 _detachAwayReasonEnabled = enabled;
183 emit detachAwayReasonEnabledSet(enabled);
186 void Identity::setIdent(const QString &ident) {
188 emit identSet(ident);
191 void Identity::setKickReason(const QString &reason) {
192 _kickReason = reason;
193 emit kickReasonSet(reason);
196 void Identity::setPartReason(const QString &reason) {
197 _partReason = reason;
198 emit partReasonSet(reason);
201 void Identity::setQuitReason(const QString &reason) {
202 _quitReason = reason;
203 emit quitReasonSet(reason);
208 void Identity::copyFrom(const Identity &other) {
209 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
210 QMetaProperty metaProp = staticMetaObject.property(idx);
211 Q_ASSERT(metaProp.isValid());
212 if(this->property(metaProp.name()) != other.property(metaProp.name())) {
213 setProperty(metaProp.name(), other.property(metaProp.name()));
218 bool Identity::operator==(const Identity &other) const {
219 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
220 QMetaProperty metaProp = staticMetaObject.property(idx);
221 Q_ASSERT(metaProp.isValid());
222 QVariant v1 = this->property(metaProp.name());
223 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
224 // QVariant cannot compare custom types, so we need to check for this special case
225 if(QString(v1.typeName()) == "IdentityId") {
226 if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
228 if(v1 != v2) return false;
234 bool Identity::operator!=(const Identity &other) const {
235 return !(*this == other);
238 ///////////////////////////////
240 QDataStream &operator<<(QDataStream &out, Identity id) {
241 out << id.toVariantMap();
246 QDataStream &operator>>(QDataStream &in, Identity &id) {
249 id.fromVariantMap(i);