Providing proper defaults for nick and realname on Mac OS using CoreServices
[quassel.git] / src / common / identity.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "identity.h"
22
23 #include <QMetaProperty>
24 #include <QVariantMap>
25
26 #ifdef Q_OS_MAC
27 #  include <CoreServices/CoreServices.h>
28 #  include "mac_utils.h"
29 #endif
30
31 Identity::Identity(IdentityId id, QObject *parent)
32   : SyncableObject(parent),
33     _identityId(id)
34 {
35   init();
36   setToDefaults();
37 }
38
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())
60 {
61   init();
62 }
63
64 void Identity::init() {
65   setObjectName(QString::number(id().toInt()));
66   setAllowClientUpdates(true);
67 }
68
69 QString Identity::defaultNick() {
70 #ifdef Q_OS_MAC
71   return CFStringToQString(CSCopyUserName(true));
72 #else
73   return QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
74 #endif
75 }
76
77 QString Identity::defaultRealName() {
78 #ifdef Q_OS_MAC
79   return CFStringToQString(CSCopyUserName(false));
80 #else
81   return tr("Quassel IRC User");
82 #endif
83 }
84
85 void Identity::setToDefaults() {
86   setIdentityName(tr("<empty>"));
87   setRealName(defaultRealName());
88   QStringList n;
89   n << defaultNick();
90   setNicks(n);
91   setAwayNick("");
92   setAwayNickEnabled(false);
93   setAwayReason(tr("Gone fishing."));
94   setAwayReasonEnabled(true);
95   setAutoAwayEnabled(false);
96   setAutoAwayTime(10);
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);
102   setIdent("quassel");
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."));
106 }
107
108 /*** setters ***/
109
110 void Identity::setId(IdentityId _id) {
111   _identityId = _id;
112   emit idSet(_id);
113   renameObject(QString::number(id().toInt()));
114 }
115
116 void Identity::setIdentityName(const QString &identityName) {
117   _identityName = identityName;
118   emit identityNameSet(identityName);
119 }
120
121 void Identity::setRealName(const QString &realName) {
122   _realName = realName;
123   emit realNameSet(realName);
124 }
125
126 void Identity::setNicks(const QStringList &nicks) {
127   _nicks = nicks;
128   emit nicksSet(nicks);
129 }
130
131 void Identity::setAwayNick(const QString &nick) {
132   _awayNick = nick;
133   emit awayNickSet(nick);
134 }
135
136 void Identity::setAwayReason(const QString &reason) {
137   _awayReason = reason;
138   emit awayReasonSet(reason);
139 }
140
141 void Identity::setAwayNickEnabled(bool enabled) {
142   _awayNickEnabled = enabled;
143   emit awayNickEnabledSet(enabled);
144 }
145
146 void Identity::setAwayReasonEnabled(bool enabled) {
147   _awayReasonEnabled = enabled;
148   emit awayReasonEnabledSet(enabled);
149 }
150
151 void Identity::setAutoAwayEnabled(bool enabled) {
152   _autoAwayEnabled = enabled;
153   emit autoAwayEnabledSet(enabled);
154 }
155
156 void Identity::setAutoAwayTime(int time) {
157   _autoAwayTime = time;
158   emit autoAwayTimeSet(time);
159 }
160
161 void Identity::setAutoAwayReason(const QString &reason) {
162   _autoAwayReason = reason;
163   emit autoAwayReasonSet(reason);
164 }
165
166 void Identity::setAutoAwayReasonEnabled(bool enabled) {
167   _autoAwayReasonEnabled = enabled;
168   emit autoAwayReasonEnabledSet(enabled);
169 }
170
171 void Identity::setDetachAwayEnabled(bool enabled) {
172   _detachAwayEnabled = enabled;
173   emit detachAwayEnabledSet(enabled);
174 }
175
176 void Identity::setDetachAwayReason(const QString &reason) {
177   _detachAwayReason = reason;
178   emit detachAwayReasonSet(reason);
179 }
180
181 void Identity::setDetachAwayReasonEnabled(bool enabled) {
182   _detachAwayReasonEnabled = enabled;
183   emit detachAwayReasonEnabledSet(enabled);
184 }
185
186 void Identity::setIdent(const QString &ident) {
187   _ident = ident;
188   emit identSet(ident);
189 }
190
191 void Identity::setKickReason(const QString &reason) {
192   _kickReason = reason;
193   emit kickReasonSet(reason);
194 }
195
196 void Identity::setPartReason(const QString &reason) {
197   _partReason = reason;
198   emit partReasonSet(reason);
199 }
200
201 void Identity::setQuitReason(const QString &reason) {
202   _quitReason = reason;
203   emit quitReasonSet(reason);
204 }
205
206 /***  ***/
207
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()));
214     }
215   }
216 }
217
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;
227     } else {
228       if(v1 != v2) return false;
229     }
230   }
231   return true;
232 }
233
234 bool Identity::operator!=(const Identity &other) const {
235   return !(*this == other);
236 }
237
238 ///////////////////////////////
239
240 QDataStream &operator<<(QDataStream &out, Identity id) {
241   out << id.toVariantMap();
242   return out;
243 }
244
245
246 QDataStream &operator>>(QDataStream &in, Identity &id) {
247   QVariantMap i;
248   in >> i;
249   id.fromVariantMap(i);
250   return in;
251 }
252