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