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 INIT_SYNCABLE_OBJECT(Identity)
46 Identity::Identity(IdentityId id, QObject *parent)
47 : SyncableObject(parent),
54 Identity::Identity(const Identity &other, QObject *parent)
55 : SyncableObject(parent),
56 _identityId(other.id()),
57 _identityName(other.identityName()),
58 _realName(other.realName()),
59 _nicks(other.nicks()),
60 _awayNick(other.awayNick()),
61 _awayNickEnabled(other.awayNickEnabled()),
62 _awayReason(other.awayReason()),
63 _awayReasonEnabled(other.awayReasonEnabled()),
64 _autoAwayEnabled(other.autoAwayEnabled()),
65 _autoAwayTime(other.autoAwayTime()),
66 _autoAwayReason(other.autoAwayReason()),
67 _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
68 _detachAwayEnabled(other.detachAwayEnabled()),
69 _detachAwayReason(other.detachAwayReason()),
70 _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
71 _ident(other.ident()),
72 _kickReason(other.kickReason()),
73 _partReason(other.partReason()),
74 _quitReason(other.quitReason())
79 void Identity::init() {
80 setObjectName(QString::number(id().toInt()));
81 setAllowClientUpdates(true);
84 QString Identity::defaultNick() {
85 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
88 QString shortUserName = CFStringToQString(CSCopyUserName(true));
89 if(!shortUserName.isEmpty())
92 #elif defined(Q_OS_UNIX)
94 struct passwd *pwd = getpwuid(getuid());
96 userName = pwd->pw_name;
97 if(!userName.isEmpty())
100 #elif defined(Q_OS_WIN32)
102 DWORD bufCharCount = 128;
103 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
104 if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
105 QString nickName(infoBuf);
106 int lastBs = nickName.lastIndexOf('\\');
108 nickName = nickName.mid(lastBs + 1);
110 if(!nickName.isEmpty())
115 // cleaning forbidden characters from nick
116 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
121 QString Identity::defaultRealName() {
122 QString generalDefault = tr("Quassel IRC User");
125 return CFStringToQString(CSCopyUserName(false));
127 #elif defined(Q_OS_UNIX)
129 struct passwd *pwd = getpwuid(getuid());
131 realName = QString::fromUtf8(pwd->pw_gecos);
132 if(!realName.isEmpty())
135 return generalDefault;
137 #elif defined(Q_OS_WIN32)
139 DWORD bufCharCount = 128;
140 if(GetUserName(infoBuf, &bufCharCount))
141 return QString(infoBuf);
143 return generalDefault;
145 return generalDefault;
149 void Identity::setToDefaults() {
150 setIdentityName(tr("<empty>"));
151 setRealName(defaultRealName());
152 QStringList n = QStringList() << defaultNick();
155 setAwayNickEnabled(false);
156 setAwayReason(tr("Gone fishing."));
157 setAwayReasonEnabled(true);
158 setAutoAwayEnabled(false);
160 setAutoAwayReason(tr("Not here. No, really. not here!"));
161 setAutoAwayReasonEnabled(false);
162 setDetachAwayEnabled(false);
163 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
164 setDetachAwayReasonEnabled(false);
166 setKickReason(tr("Kindergarten is elsewhere!"));
167 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
168 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
173 void Identity::setId(IdentityId _id) {
177 renameObject(QString::number(id().toInt()));
180 void Identity::setIdentityName(const QString &identityName) {
181 _identityName = identityName;
182 SYNC(ARG(identityName))
185 void Identity::setRealName(const QString &realName) {
186 _realName = realName;
190 void Identity::setNicks(const QStringList &nicks) {
193 emit nicksSet(nicks);
196 void Identity::setAwayNick(const QString &nick) {
201 void Identity::setAwayReason(const QString &reason) {
202 _awayReason = reason;
206 void Identity::setAwayNickEnabled(bool enabled) {
207 _awayNickEnabled = enabled;
211 void Identity::setAwayReasonEnabled(bool enabled) {
212 _awayReasonEnabled = enabled;
216 void Identity::setAutoAwayEnabled(bool enabled) {
217 _autoAwayEnabled = enabled;
221 void Identity::setAutoAwayTime(int time) {
222 _autoAwayTime = time;
226 void Identity::setAutoAwayReason(const QString &reason) {
227 _autoAwayReason = reason;
231 void Identity::setAutoAwayReasonEnabled(bool enabled) {
232 _autoAwayReasonEnabled = enabled;
236 void Identity::setDetachAwayEnabled(bool enabled) {
237 _detachAwayEnabled = enabled;
241 void Identity::setDetachAwayReason(const QString &reason) {
242 _detachAwayReason = reason;
246 void Identity::setDetachAwayReasonEnabled(bool enabled) {
247 _detachAwayReasonEnabled = enabled;
251 void Identity::setIdent(const QString &ident) {
256 void Identity::setKickReason(const QString &reason) {
257 _kickReason = reason;
261 void Identity::setPartReason(const QString &reason) {
262 _partReason = reason;
266 void Identity::setQuitReason(const QString &reason) {
267 _quitReason = reason;
273 void Identity::copyFrom(const Identity &other) {
274 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
275 QMetaProperty metaProp = staticMetaObject.property(idx);
276 Q_ASSERT(metaProp.isValid());
277 if(this->property(metaProp.name()) != other.property(metaProp.name())) {
278 setProperty(metaProp.name(), other.property(metaProp.name()));
283 bool Identity::operator==(const Identity &other) const {
284 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
285 QMetaProperty metaProp = staticMetaObject.property(idx);
286 Q_ASSERT(metaProp.isValid());
287 QVariant v1 = this->property(metaProp.name());
288 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
289 // QVariant cannot compare custom types, so we need to check for this special case
290 if(QString(v1.typeName()) == "IdentityId") {
291 if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
293 if(v1 != v2) return false;
299 bool Identity::operator!=(const Identity &other) const {
300 return !(*this == other);
303 ///////////////////////////////
305 QDataStream &operator<<(QDataStream &out, Identity id) {
306 out << id.toVariantMap();
311 QDataStream &operator>>(QDataStream &in, Identity &id) {
314 id.fromVariantMap(i);
319 INIT_SYNCABLE_OBJECT(CertManager)