1 /***************************************************************************
2 * Copyright (C) 2005-2016 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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),
55 Identity::Identity(const Identity &other, QObject *parent)
56 : SyncableObject(parent),
57 _identityId(other.id()),
58 _identityName(other.identityName()),
59 _realName(other.realName()),
60 _nicks(other.nicks()),
61 _awayNick(other.awayNick()),
62 _awayNickEnabled(other.awayNickEnabled()),
63 _awayReason(other.awayReason()),
64 _awayReasonEnabled(other.awayReasonEnabled()),
65 _autoAwayEnabled(other.autoAwayEnabled()),
66 _autoAwayTime(other.autoAwayTime()),
67 _autoAwayReason(other.autoAwayReason()),
68 _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
69 _detachAwayEnabled(other.detachAwayEnabled()),
70 _detachAwayReason(other.detachAwayReason()),
71 _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
72 _ident(other.ident()),
73 _kickReason(other.kickReason()),
74 _partReason(other.partReason()),
75 _quitReason(other.quitReason())
83 QString tcharToQString(TCHAR *tchar)
85 return QString::fromUtf16(reinterpret_cast<ushort *>(tchar));
90 QString tcharToQString(TCHAR *tchar)
92 return QString::fromLocal8Bit(tchar);
101 setObjectName(QString::number(id().toInt()));
102 setAllowClientUpdates(true);
106 QString Identity::defaultNick()
108 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
111 QString shortUserName = CFStringToQString(CSCopyUserName(true));
112 if (!shortUserName.isEmpty())
113 nick = shortUserName;
115 #elif defined(Q_OS_UNIX)
117 struct passwd *pwd = getpwuid(getuid());
119 userName = pwd->pw_name;
120 if (!userName.isEmpty())
123 #elif defined(Q_OS_WIN)
125 DWORD bufCharCount = 128;
126 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
127 if (GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
128 QString nickName(tcharToQString(infoBuf));
129 int lastBs = nickName.lastIndexOf('\\');
131 nickName = nickName.mid(lastBs + 1);
133 if (!nickName.isEmpty())
138 // cleaning forbidden characters from nick
139 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
145 QString Identity::defaultRealName()
147 QString generalDefault = tr("Quassel IRC User");
150 return CFStringToQString(CSCopyUserName(false));
152 #elif defined(Q_OS_UNIX)
154 struct passwd *pwd = getpwuid(getuid());
156 realName = QString::fromUtf8(pwd->pw_gecos);
157 if (!realName.isEmpty())
160 return generalDefault;
162 #elif defined(Q_OS_WIN)
164 DWORD bufCharCount = 128;
165 if (GetUserName(infoBuf, &bufCharCount))
166 return tcharToQString(infoBuf);
168 return generalDefault;
170 return generalDefault;
175 void Identity::setToDefaults()
177 setIdentityName(tr("<empty>"));
178 setRealName(defaultRealName());
179 QStringList n = QStringList() << defaultNick();
182 setAwayNickEnabled(false);
183 setAwayReason(tr("Gone fishing."));
184 setAwayReasonEnabled(true);
185 setAutoAwayEnabled(false);
187 setAutoAwayReason(tr("Not here. No, really. not here!"));
188 setAutoAwayReasonEnabled(false);
189 setDetachAwayEnabled(false);
190 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
191 setDetachAwayReasonEnabled(false);
193 setKickReason(tr("Kindergarten is elsewhere!"));
194 setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
195 setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
201 void Identity::setId(IdentityId _id)
206 renameObject(QString::number(id().toInt()));
210 void Identity::setIdentityName(const QString &identityName)
212 _identityName = identityName;
213 SYNC(ARG(identityName))
217 void Identity::setRealName(const QString &realName)
219 _realName = realName;
224 void Identity::setNicks(const QStringList &nicks)
228 emit nicksSet(nicks);
232 void Identity::setAwayNick(const QString &nick)
239 void Identity::setAwayReason(const QString &reason)
241 _awayReason = reason;
246 void Identity::setAwayNickEnabled(bool enabled)
248 _awayNickEnabled = enabled;
253 void Identity::setAwayReasonEnabled(bool enabled)
255 _awayReasonEnabled = enabled;
260 void Identity::setAutoAwayEnabled(bool enabled)
262 _autoAwayEnabled = enabled;
267 void Identity::setAutoAwayTime(int time)
269 _autoAwayTime = time;
274 void Identity::setAutoAwayReason(const QString &reason)
276 _autoAwayReason = reason;
281 void Identity::setAutoAwayReasonEnabled(bool enabled)
283 _autoAwayReasonEnabled = enabled;
288 void Identity::setDetachAwayEnabled(bool enabled)
290 _detachAwayEnabled = enabled;
295 void Identity::setDetachAwayReason(const QString &reason)
297 _detachAwayReason = reason;
302 void Identity::setDetachAwayReasonEnabled(bool enabled)
304 _detachAwayReasonEnabled = enabled;
309 void Identity::setIdent(const QString &ident)
316 void Identity::setKickReason(const QString &reason)
318 _kickReason = reason;
323 void Identity::setPartReason(const QString &reason)
325 _partReason = reason;
330 void Identity::setQuitReason(const QString &reason)
332 _quitReason = reason;
339 void Identity::copyFrom(const Identity &other)
341 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
342 QMetaProperty metaProp = staticMetaObject.property(idx);
343 Q_ASSERT(metaProp.isValid());
344 if (this->property(metaProp.name()) != other.property(metaProp.name())) {
345 setProperty(metaProp.name(), other.property(metaProp.name()));
351 bool Identity::operator==(const Identity &other) const
353 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
354 QMetaProperty metaProp = staticMetaObject.property(idx);
355 Q_ASSERT(metaProp.isValid());
356 QVariant v1 = this->property(metaProp.name());
357 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
358 // QVariant cannot compare custom types, so we need to check for this special case
359 if (QString(v1.typeName()) == "IdentityId") {
360 if (v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
363 if (v1 != v2) return false;
370 bool Identity::operator!=(const Identity &other) const
372 return !(*this == other);
376 ///////////////////////////////
378 QDataStream &operator<<(QDataStream &out, Identity id)
380 out << id.toVariantMap();
385 QDataStream &operator>>(QDataStream &in, Identity &id)
389 id.fromVariantMap(i);
395 INIT_SYNCABLE_OBJECT(CertManager)