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())
81 QString tcharToQString(TCHAR *tchar){
82 return QString::fromUtf16( reinterpret_cast<ushort*>(tchar));
85 QString tcharToQString(TCHAR *tchar){
86 return QString::fromLocal8Bit(tchar);
91 void Identity::init() {
92 setObjectName(QString::number(id().toInt()));
93 setAllowClientUpdates(true);
96 QString Identity::defaultNick() {
97 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
100 QString shortUserName = CFStringToQString(CSCopyUserName(true));
101 if(!shortUserName.isEmpty())
102 nick = shortUserName;
104 #elif defined(Q_OS_UNIX)
106 struct passwd *pwd = getpwuid(getuid());
108 userName = pwd->pw_name;
109 if(!userName.isEmpty())
112 #elif defined(Q_OS_WIN32)
114 DWORD bufCharCount = 128;
115 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
116 if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
117 QString nickName(tcharToQString(infoBuf));
118 int lastBs = nickName.lastIndexOf('\\');
120 nickName = nickName.mid(lastBs + 1);
122 if(!nickName.isEmpty())
127 // cleaning forbidden characters from nick
128 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
133 QString Identity::defaultRealName() {
134 QString generalDefault = tr("Quassel IRC User");
137 return CFStringToQString(CSCopyUserName(false));
139 #elif defined(Q_OS_UNIX)
141 struct passwd *pwd = getpwuid(getuid());
143 realName = QString::fromUtf8(pwd->pw_gecos);
144 if(!realName.isEmpty())
147 return generalDefault;
149 #elif defined(Q_OS_WIN32)
151 DWORD bufCharCount = 128;
152 if(GetUserName(infoBuf, &bufCharCount))
153 return tcharToQString(infoBuf);
155 return generalDefault;
157 return generalDefault;
161 void Identity::setToDefaults() {
162 setIdentityName(tr("<empty>"));
163 setRealName(defaultRealName());
164 QStringList n = QStringList() << defaultNick();
167 setAwayNickEnabled(false);
168 setAwayReason(tr("Gone fishing."));
169 setAwayReasonEnabled(true);
170 setAutoAwayEnabled(false);
172 setAutoAwayReason(tr("Not here. No, really. not here!"));
173 setAutoAwayReasonEnabled(false);
174 setDetachAwayEnabled(false);
175 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
176 setDetachAwayReasonEnabled(false);
178 setKickReason(tr("Kindergarten is elsewhere!"));
179 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
180 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
185 void Identity::setId(IdentityId _id) {
189 renameObject(QString::number(id().toInt()));
192 void Identity::setIdentityName(const QString &identityName) {
193 _identityName = identityName;
194 SYNC(ARG(identityName))
197 void Identity::setRealName(const QString &realName) {
198 _realName = realName;
202 void Identity::setNicks(const QStringList &nicks) {
205 emit nicksSet(nicks);
208 void Identity::setAwayNick(const QString &nick) {
213 void Identity::setAwayReason(const QString &reason) {
214 _awayReason = reason;
218 void Identity::setAwayNickEnabled(bool enabled) {
219 _awayNickEnabled = enabled;
223 void Identity::setAwayReasonEnabled(bool enabled) {
224 _awayReasonEnabled = enabled;
228 void Identity::setAutoAwayEnabled(bool enabled) {
229 _autoAwayEnabled = enabled;
233 void Identity::setAutoAwayTime(int time) {
234 _autoAwayTime = time;
238 void Identity::setAutoAwayReason(const QString &reason) {
239 _autoAwayReason = reason;
243 void Identity::setAutoAwayReasonEnabled(bool enabled) {
244 _autoAwayReasonEnabled = enabled;
248 void Identity::setDetachAwayEnabled(bool enabled) {
249 _detachAwayEnabled = enabled;
253 void Identity::setDetachAwayReason(const QString &reason) {
254 _detachAwayReason = reason;
258 void Identity::setDetachAwayReasonEnabled(bool enabled) {
259 _detachAwayReasonEnabled = enabled;
263 void Identity::setIdent(const QString &ident) {
268 void Identity::setKickReason(const QString &reason) {
269 _kickReason = reason;
273 void Identity::setPartReason(const QString &reason) {
274 _partReason = reason;
278 void Identity::setQuitReason(const QString &reason) {
279 _quitReason = reason;
285 void Identity::copyFrom(const Identity &other) {
286 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
287 QMetaProperty metaProp = staticMetaObject.property(idx);
288 Q_ASSERT(metaProp.isValid());
289 if(this->property(metaProp.name()) != other.property(metaProp.name())) {
290 setProperty(metaProp.name(), other.property(metaProp.name()));
295 bool Identity::operator==(const Identity &other) const {
296 for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
297 QMetaProperty metaProp = staticMetaObject.property(idx);
298 Q_ASSERT(metaProp.isValid());
299 QVariant v1 = this->property(metaProp.name());
300 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
301 // QVariant cannot compare custom types, so we need to check for this special case
302 if(QString(v1.typeName()) == "IdentityId") {
303 if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
305 if(v1 != v2) return false;
311 bool Identity::operator!=(const Identity &other) const {
312 return !(*this == other);
315 ///////////////////////////////
317 QDataStream &operator<<(QDataStream &out, Identity id) {
318 out << id.toVariantMap();
323 QDataStream &operator>>(QDataStream &in, Identity &id) {
326 id.fromVariantMap(i);
331 INIT_SYNCABLE_OBJECT(CertManager)