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"
34 # define SECURITY_WIN32
35 # include <Security.h>
38 Identity::Identity(IdentityId id, QObject *parent)
39 : SyncableObject(parent),
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())
71 void Identity::init() {
72 setObjectName(QString::number(id().toInt()));
73 setAllowClientUpdates(true);
76 QString Identity::defaultNick() {
77 QString generalDefault = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
79 return CFStringToQString(CSCopyUserName(true));
80 #elif defined(Q_OS_WIN32)
82 DWORD bufCharCount = 128;
83 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
84 if(!GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount))
85 return generalDefault;
87 QString nickName(infoBuf);
88 int lastBs = nickName.lastIndexOf('\\');
90 nickName = nickName.mid(lastBs + 1);
92 if(nickName.isEmpty())
93 return generalDefault;
97 return generalDefault;
101 QString Identity::defaultRealName() {
102 QString generalDefault = tr("Quassel IRC User");
104 return CFStringToQString(CSCopyUserName(false));
105 #elif defined(Q_OS_WIN32)
107 DWORD bufCharCount = 128;
108 if(GetUserName(infoBuf, &bufCharCount))
109 return QString(infoBuf);
111 return generalDefault;
113 return generalDefault;
117 void Identity::setToDefaults() {
118 setIdentityName(tr("<empty>"));
119 setRealName(defaultRealName());
124 setAwayNickEnabled(false);
125 setAwayReason(tr("Gone fishing."));
126 setAwayReasonEnabled(true);
127 setAutoAwayEnabled(false);
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);
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."));
142 void Identity::setId(IdentityId _id) {
145 renameObject(QString::number(id().toInt()));
148 void Identity::setIdentityName(const QString &identityName) {
149 _identityName = identityName;
150 emit identityNameSet(identityName);
153 void Identity::setRealName(const QString &realName) {
154 _realName = realName;
155 emit realNameSet(realName);
158 void Identity::setNicks(const QStringList &nicks) {
160 emit nicksSet(nicks);
163 void Identity::setAwayNick(const QString &nick) {
165 emit awayNickSet(nick);
168 void Identity::setAwayReason(const QString &reason) {
169 _awayReason = reason;
170 emit awayReasonSet(reason);
173 void Identity::setAwayNickEnabled(bool enabled) {
174 _awayNickEnabled = enabled;
175 emit awayNickEnabledSet(enabled);
178 void Identity::setAwayReasonEnabled(bool enabled) {
179 _awayReasonEnabled = enabled;
180 emit awayReasonEnabledSet(enabled);
183 void Identity::setAutoAwayEnabled(bool enabled) {
184 _autoAwayEnabled = enabled;
185 emit autoAwayEnabledSet(enabled);
188 void Identity::setAutoAwayTime(int time) {
189 _autoAwayTime = time;
190 emit autoAwayTimeSet(time);
193 void Identity::setAutoAwayReason(const QString &reason) {
194 _autoAwayReason = reason;
195 emit autoAwayReasonSet(reason);
198 void Identity::setAutoAwayReasonEnabled(bool enabled) {
199 _autoAwayReasonEnabled = enabled;
200 emit autoAwayReasonEnabledSet(enabled);
203 void Identity::setDetachAwayEnabled(bool enabled) {
204 _detachAwayEnabled = enabled;
205 emit detachAwayEnabledSet(enabled);
208 void Identity::setDetachAwayReason(const QString &reason) {
209 _detachAwayReason = reason;
210 emit detachAwayReasonSet(reason);
213 void Identity::setDetachAwayReasonEnabled(bool enabled) {
214 _detachAwayReasonEnabled = enabled;
215 emit detachAwayReasonEnabledSet(enabled);
218 void Identity::setIdent(const QString &ident) {
220 emit identSet(ident);
223 void Identity::setKickReason(const QString &reason) {
224 _kickReason = reason;
225 emit kickReasonSet(reason);
228 void Identity::setPartReason(const QString &reason) {
229 _partReason = reason;
230 emit partReasonSet(reason);
233 void Identity::setQuitReason(const QString &reason) {
234 _quitReason = reason;
235 emit quitReasonSet(reason);
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()));
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;
260 if(v1 != v2) return false;
266 bool Identity::operator!=(const Identity &other) const {
267 return !(*this == other);
270 ///////////////////////////////
272 QDataStream &operator<<(QDataStream &out, Identity id) {
273 out << id.toVariantMap();
278 QDataStream &operator>>(QDataStream &in, Identity &id) {
281 id.fromVariantMap(i);