1 /***************************************************************************
2 * Copyright (C) 2005-2019 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>
25 #include <QVariantMap>
28 # include <CoreServices/CoreServices.h>
30 # include "mac_utils.h"
35 # include <sys/types.h>
42 # define SECURITY_WIN32
43 # include <Security.h>
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)
83 return QString::fromUtf16(reinterpret_cast<ushort*>(tchar));
87 QString tcharToQString(TCHAR* tchar)
89 return QString::fromLocal8Bit(tchar);
97 setObjectName(QString::number(id().toInt()));
98 setAllowClientUpdates(true);
101 QString Identity::defaultNick()
103 QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
106 QString shortUserName = CFStringToQString(CSCopyUserName(true));
107 if (!shortUserName.isEmpty())
108 nick = shortUserName;
110 #elif defined(Q_OS_UNIX)
112 struct passwd* pwd = getpwuid(getuid());
114 userName = pwd->pw_name;
115 if (!userName.isEmpty())
118 #elif defined(Q_OS_WIN)
120 DWORD bufCharCount = 128;
121 // if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
122 if (GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
123 QString nickName(tcharToQString(infoBuf));
124 int lastBs = nickName.lastIndexOf('\\');
126 nickName = nickName.mid(lastBs + 1);
128 if (!nickName.isEmpty())
133 // cleaning forbidden characters from nick
134 QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])")); // NOLINT(modernize-raw-string-literal)
139 QString Identity::defaultRealName()
141 QString generalDefault = tr("Quassel IRC User");
144 return CFStringToQString(CSCopyUserName(false));
146 #elif defined(Q_OS_UNIX)
148 struct passwd* pwd = getpwuid(getuid());
150 realName = QString::fromUtf8(pwd->pw_gecos);
151 if (!realName.isEmpty())
154 return generalDefault;
156 #elif defined(Q_OS_WIN)
158 DWORD bufCharCount = 128;
159 if (GetUserName(infoBuf, &bufCharCount))
160 return tcharToQString(infoBuf);
162 return generalDefault;
164 return generalDefault;
168 void Identity::setToDefaults()
170 setIdentityName(tr("<empty>"));
171 setRealName(defaultRealName());
172 QStringList n = QStringList() << defaultNick();
175 setAwayNickEnabled(false);
176 setAwayReason(tr("Gone fishing."));
177 setAwayReasonEnabled(true);
178 setAutoAwayEnabled(false);
180 setAutoAwayReason(tr("Not here. No, really. not here!"));
181 setAutoAwayReasonEnabled(false);
182 setDetachAwayEnabled(false);
183 setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
184 setDetachAwayReasonEnabled(false);
186 setKickReason(tr("Kindergarten is elsewhere!"));
187 setPartReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
188 setQuitReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
193 void Identity::setId(IdentityId _id)
198 setObjectName(QString::number(id().toInt()));
201 void Identity::setIdentityName(const QString& identityName)
203 _identityName = identityName;
204 SYNC(ARG(identityName))
207 void Identity::setRealName(const QString& realName)
209 _realName = realName;
213 void Identity::setNicks(const QStringList& nicks)
217 emit nicksSet(nicks);
220 void Identity::setAwayNick(const QString& nick)
226 void Identity::setAwayReason(const QString& reason)
228 _awayReason = reason;
232 void Identity::setAwayNickEnabled(bool enabled)
234 _awayNickEnabled = enabled;
238 void Identity::setAwayReasonEnabled(bool enabled)
240 _awayReasonEnabled = enabled;
244 void Identity::setAutoAwayEnabled(bool enabled)
246 _autoAwayEnabled = enabled;
250 void Identity::setAutoAwayTime(int time)
252 _autoAwayTime = time;
256 void Identity::setAutoAwayReason(const QString& reason)
258 _autoAwayReason = reason;
262 void Identity::setAutoAwayReasonEnabled(bool enabled)
264 _autoAwayReasonEnabled = enabled;
268 void Identity::setDetachAwayEnabled(bool enabled)
270 _detachAwayEnabled = enabled;
274 void Identity::setDetachAwayReason(const QString& reason)
276 _detachAwayReason = reason;
280 void Identity::setDetachAwayReasonEnabled(bool enabled)
282 _detachAwayReasonEnabled = enabled;
286 void Identity::setIdent(const QString& ident)
292 void Identity::setKickReason(const QString& reason)
294 _kickReason = reason;
298 void Identity::setPartReason(const QString& reason)
300 _partReason = reason;
304 void Identity::setQuitReason(const QString& reason)
306 _quitReason = reason;
312 void Identity::copyFrom(const Identity& other)
314 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
315 QMetaProperty metaProp = staticMetaObject.property(idx);
316 Q_ASSERT(metaProp.isValid());
317 if (this->property(metaProp.name()) != other.property(metaProp.name())) {
318 setProperty(metaProp.name(), other.property(metaProp.name()));
323 bool Identity::operator==(const Identity& other) const
325 for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
326 QMetaProperty metaProp = staticMetaObject.property(idx);
327 Q_ASSERT(metaProp.isValid());
328 QVariant v1 = this->property(metaProp.name());
329 QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
330 // QVariant cannot compare custom types, so we need to check for this special case
331 if (QString(v1.typeName()) == "IdentityId") {
332 if (v1.value<IdentityId>() != v2.value<IdentityId>())
343 bool Identity::operator!=(const Identity& other) const
345 return !(*this == other);
348 ///////////////////////////////
350 QDataStream& operator<<(QDataStream& out, Identity id)
352 out << id.toVariantMap();
356 QDataStream& operator>>(QDataStream& in, Identity& id)
360 id.fromVariantMap(i);