1 /***************************************************************************
2 * Copyright (C) 2005-2018 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 Identity::Identity(IdentityId id, QObject *parent)
46 : 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())
82 QString tcharToQString(TCHAR *tchar)
84 return QString::fromUtf16(reinterpret_cast<ushort *>(tchar));
89 QString tcharToQString(TCHAR *tchar)
91 return QString::fromLocal8Bit(tchar);
100 setObjectName(QString::number(id().toInt()));
101 setAllowClientUpdates(true);
105 QString Identity::defaultNick()
107 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
110 QString shortUserName = CFStringToQString(CSCopyUserName(true));
111 if (!shortUserName.isEmpty())
112 nick = shortUserName;
114 #elif defined(Q_OS_UNIX)
116 struct passwd *pwd = getpwuid(getuid());
118 userName = pwd->pw_name;
119 if (!userName.isEmpty())
122 #elif defined(Q_OS_WIN)
124 DWORD bufCharCount = 128;
125 //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
126 if (GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
127 QString nickName(tcharToQString(infoBuf));
128 int lastBs = nickName.lastIndexOf('\\');
130 nickName = nickName.mid(lastBs + 1);
132 if (!nickName.isEmpty())
137 // cleaning forbidden characters from nick
138 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
144 QString Identity::defaultRealName()
146 QString generalDefault = tr("Quassel IRC User");
149 return CFStringToQString(CSCopyUserName(false));
151 #elif defined(Q_OS_UNIX)
153 struct passwd *pwd = getpwuid(getuid());
155 realName = QString::fromUtf8(pwd->pw_gecos);
156 if (!realName.isEmpty())
159 return generalDefault;
161 #elif defined(Q_OS_WIN)
163 DWORD bufCharCount = 128;
164 if (GetUserName(infoBuf, &bufCharCount))
165 return tcharToQString(infoBuf);
167 return generalDefault;
169 return generalDefault;
174 void Identity::setToDefaults()
176 setIdentityName(tr("<empty>"));
177 setRealName(defaultRealName());
178 QStringList n = QStringList() << defaultNick();
181 setAwayNickEnabled(false);
182 setAwayReason(tr("Gone fishing."));
183 setAwayReasonEnabled(true);
184 setAutoAwayEnabled(false);
186 setAutoAwayReason(tr("Not here. No, really. not here!"));
187 setAutoAwayReasonEnabled(false);
188 setDetachAwayEnabled(false);
189 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
190 setDetachAwayReasonEnabled(false);
192 setKickReason(tr("Kindergarten is elsewhere!"));
193 setPartReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
194 setQuitReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
200 void Identity::setId(IdentityId _id)
205 renameObject(QString::number(id().toInt()));
209 void Identity::setIdentityName(const QString &identityName)
211 _identityName = identityName;
212 SYNC(ARG(identityName))
216 void Identity::setRealName(const QString &realName)
218 _realName = realName;
223 void Identity::setNicks(const QStringList &nicks)
227 emit nicksSet(nicks);
231 void Identity::setAwayNick(const QString &nick)
238 void Identity::setAwayReason(const QString &reason)
240 _awayReason = reason;
245 void Identity::setAwayNickEnabled(bool enabled)
247 _awayNickEnabled = enabled;
252 void Identity::setAwayReasonEnabled(bool enabled)
254 _awayReasonEnabled = enabled;
259 void Identity::setAutoAwayEnabled(bool enabled)
261 _autoAwayEnabled = enabled;
266 void Identity::setAutoAwayTime(int time)
268 _autoAwayTime = time;
273 void Identity::setAutoAwayReason(const QString &reason)
275 _autoAwayReason = reason;
280 void Identity::setAutoAwayReasonEnabled(bool enabled)
282 _autoAwayReasonEnabled = enabled;
287 void Identity::setDetachAwayEnabled(bool enabled)
289 _detachAwayEnabled = enabled;
294 void Identity::setDetachAwayReason(const QString &reason)
296 _detachAwayReason = reason;
301 void Identity::setDetachAwayReasonEnabled(bool enabled)
303 _detachAwayReasonEnabled = enabled;
308 void Identity::setIdent(const QString &ident)
315 void Identity::setKickReason(const QString &reason)
317 _kickReason = reason;
322 void Identity::setPartReason(const QString &reason)
324 _partReason = reason;
329 void Identity::setQuitReason(const QString &reason)
331 _quitReason = reason;
338 void Identity::copyFrom(const Identity &other)
340 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
341 QMetaProperty metaProp = staticMetaObject.property(idx);
342 Q_ASSERT(metaProp.isValid());
343 if (this->property(metaProp.name()) != other.property(metaProp.name())) {
344 setProperty(metaProp.name(), other.property(metaProp.name()));
350 bool Identity::operator==(const Identity &other) const
352 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
353 QMetaProperty metaProp = staticMetaObject.property(idx);
354 Q_ASSERT(metaProp.isValid());
355 QVariant v1 = this->property(metaProp.name());
356 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
357 // QVariant cannot compare custom types, so we need to check for this special case
358 if (QString(v1.typeName()) == "IdentityId") {
359 if (v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
362 if (v1 != v2) return false;
369 bool Identity::operator!=(const Identity &other) const
371 return !(*this == other);
375 ///////////////////////////////
377 QDataStream &operator<<(QDataStream &out, Identity id)
379 out << id.toVariantMap();
384 QDataStream &operator>>(QDataStream &in, Identity &id)
388 id.fromVariantMap(i);