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 nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
79 QString shortUserName = CFStringToQString(CSCopyUserName(true));
80 if(!shortUserName.isEmpty())
82 #elif defined(Q_OS_WIN32)
84 DWORD bufCharCount = 128;
85 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
86 if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
87 QString nickName(infoBuf);
88 int lastBs = nickName.lastIndexOf('\\');
90 nickName = nickName.mid(lastBs + 1);
92 if(!nickName.isEmpty())
96 // cleaning forbidden characters from nick
97 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z\x5b-\x60\x7b-\x7d])"));
102 QString Identity::defaultRealName() {
103 QString generalDefault = tr("Quassel IRC User");
105 return CFStringToQString(CSCopyUserName(false));
106 #elif defined(Q_OS_WIN32)
108 DWORD bufCharCount = 128;
109 if(GetUserName(infoBuf, &bufCharCount))
110 return QString(infoBuf);
112 return generalDefault;
114 return generalDefault;
118 void Identity::setToDefaults() {
119 setIdentityName(tr("<empty>"));
120 setRealName(defaultRealName());
125 setAwayNickEnabled(false);
126 setAwayReason(tr("Gone fishing."));
127 setAwayReasonEnabled(true);
128 setAutoAwayEnabled(false);
130 setAutoAwayReason(tr("Not here. No, really. not here!"));
131 setAutoAwayReasonEnabled(false);
132 setDetachAwayEnabled(false);
133 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
134 setDetachAwayReasonEnabled(false);
136 setKickReason(tr("Kindergarten is elsewhere!"));
137 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
138 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
143 void Identity::setId(IdentityId _id) {
146 renameObject(QString::number(id().toInt()));
149 void Identity::setIdentityName(const QString &identityName) {
150 _identityName = identityName;
151 emit identityNameSet(identityName);
154 void Identity::setRealName(const QString &realName) {
155 _realName = realName;
156 emit realNameSet(realName);
159 void Identity::setNicks(const QStringList &nicks) {
161 emit nicksSet(nicks);
164 void Identity::setAwayNick(const QString &nick) {
166 emit awayNickSet(nick);
169 void Identity::setAwayReason(const QString &reason) {
170 _awayReason = reason;
171 emit awayReasonSet(reason);
174 void Identity::setAwayNickEnabled(bool enabled) {
175 _awayNickEnabled = enabled;
176 emit awayNickEnabledSet(enabled);
179 void Identity::setAwayReasonEnabled(bool enabled) {
180 _awayReasonEnabled = enabled;
181 emit awayReasonEnabledSet(enabled);
184 void Identity::setAutoAwayEnabled(bool enabled) {
185 _autoAwayEnabled = enabled;
186 emit autoAwayEnabledSet(enabled);
189 void Identity::setAutoAwayTime(int time) {
190 _autoAwayTime = time;
191 emit autoAwayTimeSet(time);
194 void Identity::setAutoAwayReason(const QString &reason) {
195 _autoAwayReason = reason;
196 emit autoAwayReasonSet(reason);
199 void Identity::setAutoAwayReasonEnabled(bool enabled) {
200 _autoAwayReasonEnabled = enabled;
201 emit autoAwayReasonEnabledSet(enabled);
204 void Identity::setDetachAwayEnabled(bool enabled) {
205 _detachAwayEnabled = enabled;
206 emit detachAwayEnabledSet(enabled);
209 void Identity::setDetachAwayReason(const QString &reason) {
210 _detachAwayReason = reason;
211 emit detachAwayReasonSet(reason);
214 void Identity::setDetachAwayReasonEnabled(bool enabled) {
215 _detachAwayReasonEnabled = enabled;
216 emit detachAwayReasonEnabledSet(enabled);
219 void Identity::setIdent(const QString &ident) {
221 emit identSet(ident);
224 void Identity::setKickReason(const QString &reason) {
225 _kickReason = reason;
226 emit kickReasonSet(reason);
229 void Identity::setPartReason(const QString &reason) {
230 _partReason = reason;
231 emit partReasonSet(reason);
234 void Identity::setQuitReason(const QString &reason) {
235 _quitReason = reason;
236 emit quitReasonSet(reason);
241 void Identity::copyFrom(const Identity &other) {
242 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
243 QMetaProperty metaProp = staticMetaObject.property(idx);
244 Q_ASSERT(metaProp.isValid());
245 if(this->property(metaProp.name()) != other.property(metaProp.name())) {
246 setProperty(metaProp.name(), other.property(metaProp.name()));
251 bool Identity::operator==(const Identity &other) const {
252 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
253 QMetaProperty metaProp = staticMetaObject.property(idx);
254 Q_ASSERT(metaProp.isValid());
255 QVariant v1 = this->property(metaProp.name());
256 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
257 // QVariant cannot compare custom types, so we need to check for this special case
258 if(QString(v1.typeName()) == "IdentityId") {
259 if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
261 if(v1 != v2) return false;
267 bool Identity::operator!=(const Identity &other) const {
268 return !(*this == other);
271 ///////////////////////////////
273 QDataStream &operator<<(QDataStream &out, Identity id) {
274 out << id.toVariantMap();
279 QDataStream &operator>>(QDataStream &in, Identity &id) {
282 id.fromVariantMap(i);