+#ifdef Q_OS_MAC
+# include <CoreServices/CoreServices.h>
+
+# include "mac_utils.h"
+#endif
+
+#ifdef Q_OS_UNIX
+# include <pwd.h>
+# include <sys/types.h>
+# include <unistd.h>
+#endif
+
+#ifdef Q_OS_WIN
+# include <windows.h>
+# include <Winbase.h>
+# define SECURITY_WIN32
+# include <Security.h>
+#endif
+
+Identity::Identity(IdentityId id, QObject* parent)
+ : SyncableObject(parent)
+ , _identityId(id)
+{
+ init();
+ setToDefaults();
+}
+
+Identity::Identity(const Identity& other, QObject* parent)
+ : SyncableObject(parent)
+ , _identityId(other.id())
+ , _identityName(other.identityName())
+ , _realName(other.realName())
+ , _nicks(other.nicks())
+ , _awayNick(other.awayNick())
+ , _awayNickEnabled(other.awayNickEnabled())
+ , _awayReason(other.awayReason())
+ , _awayReasonEnabled(other.awayReasonEnabled())
+ , _autoAwayEnabled(other.autoAwayEnabled())
+ , _autoAwayTime(other.autoAwayTime())
+ , _autoAwayReason(other.autoAwayReason())
+ , _autoAwayReasonEnabled(other.autoAwayReasonEnabled())
+ , _detachAwayEnabled(other.detachAwayEnabled())
+ , _detachAwayReason(other.detachAwayReason())
+ , _detachAwayReasonEnabled(other.detachAwayReasonEnabled())
+ , _ident(other.ident())
+ , _kickReason(other.kickReason())
+ , _partReason(other.partReason())
+ , _quitReason(other.quitReason())
+{
+ init();
+}
+
+#ifdef Q_OS_WIN
+# ifdef UNICODE
+QString tcharToQString(TCHAR* tchar)
+{
+ return QString::fromUtf16(reinterpret_cast<ushort*>(tchar));
+}
+
+# else
+QString tcharToQString(TCHAR* tchar)
+{
+ return QString::fromLocal8Bit(tchar);
+}
+
+# endif
+
+#endif
+void Identity::init()
+{
+ setObjectName(QString::number(id().toInt()));
+ setAllowClientUpdates(true);
+}
+
+QString Identity::defaultNick()
+{
+ QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+
+#ifdef Q_OS_MAC
+ QString shortUserName = CFStringToQString(CSCopyUserName(true));
+ if (!shortUserName.isEmpty())
+ nick = shortUserName;
+
+#elif defined(Q_OS_UNIX)
+ QString userName;
+ struct passwd* pwd = getpwuid(getuid());
+ if (pwd)
+ userName = pwd->pw_name;
+ if (!userName.isEmpty())
+ nick = userName;
+
+#elif defined(Q_OS_WIN)
+ TCHAR infoBuf[128];
+ DWORD bufCharCount = 128;
+ // if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
+ if (GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
+ QString nickName(tcharToQString(infoBuf));
+ int lastBs = nickName.lastIndexOf('\\');
+ if (lastBs != -1) {
+ nickName = nickName.mid(lastBs + 1);
+ }
+ if (!nickName.isEmpty())
+ nick = nickName;
+ }
+#endif
+
+ // cleaning forbidden characters from nick
+ QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])")); // NOLINT(modernize-raw-string-literal)
+ nick.remove(rx);
+ return nick;
+}
+
+QString Identity::defaultRealName()
+{
+ QString generalDefault = tr("Quassel IRC User");
+
+#ifdef Q_OS_MAC
+ return CFStringToQString(CSCopyUserName(false));
+
+#elif defined(Q_OS_UNIX)
+ QString realName;
+ struct passwd* pwd = getpwuid(getuid());
+ if (pwd)
+ realName = QString::fromUtf8(pwd->pw_gecos);
+ if (!realName.isEmpty())
+ return realName;
+ else
+ return generalDefault;
+
+#elif defined(Q_OS_WIN)
+ TCHAR infoBuf[128];
+ DWORD bufCharCount = 128;
+ if (GetUserName(infoBuf, &bufCharCount))
+ return tcharToQString(infoBuf);
+ else
+ return generalDefault;
+#else
+ return generalDefault;
+#endif
+}