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>
28 # include <CoreServices/CoreServices.h>
29 # include "mac_utils.h"
33 # include <sys/types.h>
41 # define SECURITY_WIN32
42 # include <Security.h>
45 Identity::Identity(IdentityId id, QObject *parent)
46 : SyncableObject(parent),
53 Identity::Identity(const Identity &other, QObject *parent)
54 : SyncableObject(parent),
55 _identityId(other.id()),
56 _identityName(other.identityName()),
57 _realName(other.realName()),
58 _nicks(other.nicks()),
59 _awayNick(other.awayNick()),
60 _awayNickEnabled(other.awayNickEnabled()),
61 _awayReason(other.awayReason()),
62 _awayReasonEnabled(other.awayReasonEnabled()),
63 _autoAwayEnabled(other.autoAwayEnabled()),
64 _autoAwayTime(other.autoAwayTime()),
65 _autoAwayReason(other.autoAwayReason()),
66 _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
67 _detachAwayEnabled(other.detachAwayEnabled()),
68 _detachAwayReason(other.detachAwayReason()),
69 _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
70 _ident(other.ident()),
71 _kickReason(other.kickReason()),
72 _partReason(other.partReason()),
73 _quitReason(other.quitReason())
78 void Identity::init() {
79 setObjectName(QString::number(id().toInt()));
80 setAllowClientUpdates(true);
83 QString Identity::defaultNick() {
84 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
87 QString shortUserName = CFStringToQString(CSCopyUserName(true));
88 if(!shortUserName.isEmpty())
91 #elif defined(Q_OS_UNIX)
93 struct passwd *pwd = getpwuid(getuid());
95 userName = pwd->pw_name;
96 if(!userName.isEmpty())
99 #elif defined(Q_OS_WIN32)
101 DWORD bufCharCount = 128;
102 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
103 if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
104 QString nickName(infoBuf);
105 int lastBs = nickName.lastIndexOf('\\');
107 nickName = nickName.mid(lastBs + 1);
109 if(!nickName.isEmpty())
114 // cleaning forbidden characters from nick
115 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
120 QString Identity::defaultRealName() {
121 QString generalDefault = tr("Quassel IRC User");
124 return CFStringToQString(CSCopyUserName(false));
126 #elif defined(Q_OS_UNIX)
128 struct passwd *pwd = getpwuid(getuid());
130 realName = QString::fromUtf8(pwd->pw_gecos);
131 if(!realName.isEmpty())
134 return generalDefault;
136 #elif defined(Q_OS_WIN32)
138 DWORD bufCharCount = 128;
139 if(GetUserName(infoBuf, &bufCharCount))
140 return QString(infoBuf);
142 return generalDefault;
144 return generalDefault;
148 void Identity::setToDefaults() {
149 setIdentityName(tr("<empty>"));
150 setRealName(defaultRealName());
151 QStringList n = QStringList() << defaultNick();
154 setAwayNickEnabled(false);
155 setAwayReason(tr("Gone fishing."));
156 setAwayReasonEnabled(true);
157 setAutoAwayEnabled(false);
159 setAutoAwayReason(tr("Not here. No, really. not here!"));
160 setAutoAwayReasonEnabled(false);
161 setDetachAwayEnabled(false);
162 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
163 setDetachAwayReasonEnabled(false);
165 setKickReason(tr("Kindergarten is elsewhere!"));
166 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
167 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
172 void Identity::setId(IdentityId _id) {
175 renameObject(QString::number(id().toInt()));
178 void Identity::setIdentityName(const QString &identityName) {
179 _identityName = identityName;
180 emit identityNameSet(identityName);
183 void Identity::setRealName(const QString &realName) {
184 _realName = realName;
185 emit realNameSet(realName);
188 void Identity::setNicks(const QStringList &nicks) {
190 emit nicksSet(nicks);
193 void Identity::setAwayNick(const QString &nick) {
195 emit awayNickSet(nick);
198 void Identity::setAwayReason(const QString &reason) {
199 _awayReason = reason;
200 emit awayReasonSet(reason);
203 void Identity::setAwayNickEnabled(bool enabled) {
204 _awayNickEnabled = enabled;
205 emit awayNickEnabledSet(enabled);
208 void Identity::setAwayReasonEnabled(bool enabled) {
209 _awayReasonEnabled = enabled;
210 emit awayReasonEnabledSet(enabled);
213 void Identity::setAutoAwayEnabled(bool enabled) {
214 _autoAwayEnabled = enabled;
215 emit autoAwayEnabledSet(enabled);
218 void Identity::setAutoAwayTime(int time) {
219 _autoAwayTime = time;
220 emit autoAwayTimeSet(time);
223 void Identity::setAutoAwayReason(const QString &reason) {
224 _autoAwayReason = reason;
225 emit autoAwayReasonSet(reason);
228 void Identity::setAutoAwayReasonEnabled(bool enabled) {
229 _autoAwayReasonEnabled = enabled;
230 emit autoAwayReasonEnabledSet(enabled);
233 void Identity::setDetachAwayEnabled(bool enabled) {
234 _detachAwayEnabled = enabled;
235 emit detachAwayEnabledSet(enabled);
238 void Identity::setDetachAwayReason(const QString &reason) {
239 _detachAwayReason = reason;
240 emit detachAwayReasonSet(reason);
243 void Identity::setDetachAwayReasonEnabled(bool enabled) {
244 _detachAwayReasonEnabled = enabled;
245 emit detachAwayReasonEnabledSet(enabled);
248 void Identity::setIdent(const QString &ident) {
250 emit identSet(ident);
253 void Identity::setKickReason(const QString &reason) {
254 _kickReason = reason;
255 emit kickReasonSet(reason);
258 void Identity::setPartReason(const QString &reason) {
259 _partReason = reason;
260 emit partReasonSet(reason);
263 void Identity::setQuitReason(const QString &reason) {
264 _quitReason = reason;
265 emit quitReasonSet(reason);
270 void Identity::copyFrom(const Identity &other) {
271 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
272 QMetaProperty metaProp = staticMetaObject.property(idx);
273 Q_ASSERT(metaProp.isValid());
274 if(this->property(metaProp.name()) != other.property(metaProp.name())) {
275 setProperty(metaProp.name(), other.property(metaProp.name()));
280 bool Identity::operator==(const Identity &other) const {
281 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
282 QMetaProperty metaProp = staticMetaObject.property(idx);
283 Q_ASSERT(metaProp.isValid());
284 QVariant v1 = this->property(metaProp.name());
285 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
286 // QVariant cannot compare custom types, so we need to check for this special case
287 if(QString(v1.typeName()) == "IdentityId") {
288 if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
290 if(v1 != v2) return false;
296 bool Identity::operator!=(const Identity &other) const {
297 return !(*this == other);
300 ///////////////////////////////
302 QDataStream &operator<<(QDataStream &out, Identity id) {
303 out << id.toVariantMap();
308 QDataStream &operator>>(QDataStream &in, Identity &id) {
311 id.fromVariantMap(i);