modernize: Use raw string literals instead of escaped strings
[quassel.git] / src / common / identity.cpp
index c1858e8..9d885e0 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-09 by the Quassel Project                          *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   You should have received a copy of the GNU General Public License     *
  *   along with this program; if not, write to the                         *
  *   Free Software Foundation, Inc.,                                       *
- *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
  ***************************************************************************/
 
 #include "identity.h"
 
 #include <QMetaProperty>
 #include <QVariantMap>
+#include <QString>
 
 #ifdef Q_OS_MAC
 #  include <CoreServices/CoreServices.h>
@@ -34,7 +35,7 @@
 #  include <unistd.h>
 #endif
 
-#ifdef Q_OS_WIN32
+#ifdef Q_OS_WIN
 #  include <windows.h>
 #  include <Winbase.h>
 #  define SECURITY_WIN32
 #endif
 
 Identity::Identity(IdentityId id, QObject *parent)
-  : SyncableObject(parent),
+    : SyncableObject(parent),
     _identityId(id)
 {
-  init();
-  setToDefaults();
+    init();
+    setToDefaults();
 }
 
+
 Identity::Identity(const Identity &other, QObject *parent)
-  : SyncableObject(parent),
+    : SyncableObject(parent),
     _identityId(other.id()),
     _identityName(other.identityName()),
     _realName(other.realName()),
@@ -71,241 +73,318 @@ Identity::Identity(const Identity &other, QObject *parent)
     _partReason(other.partReason()),
     _quitReason(other.quitReason())
 {
-  init();
+    init();
+}
+
+
+#ifdef Q_OS_WIN
+#ifdef UNICODE
+QString tcharToQString(TCHAR *tchar)
+{
+    return QString::fromUtf16(reinterpret_cast<ushort *>(tchar));
 }
 
-void Identity::init() {
-  setObjectName(QString::number(id().toInt()));
-  setAllowClientUpdates(true);
+
+#else
+QString tcharToQString(TCHAR *tchar)
+{
+    return QString::fromLocal8Bit(tchar);
 }
 
-QString Identity::defaultNick() {
-  QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+
+#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;
+    QString shortUserName = CFStringToQString(CSCopyUserName(true));
+    if (!shortUserName.isEmpty())
+        nick = shortUserName;
 
 #elif defined(Q_OS_UNIX)
-  QString userName = getlogin();
-  if(!userName.isEmpty())
-    nick = userName;
-
-#elif defined(Q_OS_WIN32)
-  TCHAR  infoBuf[128];
-  DWORD  bufCharCount = 128;
-  //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
-  if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
-    QString nickName(infoBuf);
-    int lastBs = nickName.lastIndexOf('\\');
-    if(lastBs != -1) {
-      nickName = nickName.mid(lastBs + 1);
+    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;
     }
-    if(!nickName.isEmpty())
-      nick = nickName;
-  }
 #endif
 
-  // cleaning forbidden characters from nick
-  QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
-  nick.remove(rx);
-  return nick;
+    // 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");
+
+QString Identity::defaultRealName()
+{
+    QString generalDefault = tr("Quassel IRC User");
 
 #ifdef Q_OS_MAC
-  return CFStringToQString(CSCopyUserName(false));
+    return CFStringToQString(CSCopyUserName(false));
 
 #elif defined(Q_OS_UNIX)
-  QString realName;
-  struct passwd *pwd = getpwuid(getuid());
-  if(pwd)
-    realName = pwd->pw_gecos;
-  if(!realName.isEmpty())
-    return realName;
-  else
-    return generalDefault;
-
-#elif defined(Q_OS_WIN32)
-  TCHAR  infoBuf[128];
-  DWORD  bufCharCount = 128;
-  if(GetUserName(infoBuf, &bufCharCount))
-    return QString(infoBuf);
-  else
-    return generalDefault;
+    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;
+    return generalDefault;
 #endif
 }
 
-void Identity::setToDefaults() {
-  setIdentityName(tr("<empty>"));
-  setRealName(defaultRealName());
-  QStringList n;
-  n << defaultNick() << defaultNick() + "_" << defaultNick() + "__";
-  setNicks(n);
-  setAwayNick("");
-  setAwayNickEnabled(false);
-  setAwayReason(tr("Gone fishing."));
-  setAwayReasonEnabled(true);
-  setAutoAwayEnabled(false);
-  setAutoAwayTime(10);
-  setAutoAwayReason(tr("Not here. No, really. not here!"));
-  setAutoAwayReasonEnabled(false);
-  setDetachAwayEnabled(false);
-  setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
-  setDetachAwayReasonEnabled(false);
-  setIdent("quassel");
-  setKickReason(tr("Kindergarten is elsewhere!"));
-  setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
-  setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
+
+void Identity::setToDefaults()
+{
+    setIdentityName(tr("<empty>"));
+    setRealName(defaultRealName());
+    QStringList n = QStringList() << defaultNick();
+    setNicks(n);
+    setAwayNick("");
+    setAwayNickEnabled(false);
+    setAwayReason(tr("Gone fishing."));
+    setAwayReasonEnabled(true);
+    setAutoAwayEnabled(false);
+    setAutoAwayTime(10);
+    setAutoAwayReason(tr("Not here. No, really. not here!"));
+    setAutoAwayReasonEnabled(false);
+    setDetachAwayEnabled(false);
+    setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
+    setDetachAwayReasonEnabled(false);
+    setIdent("quassel");
+    setKickReason(tr("Kindergarten is elsewhere!"));
+    setPartReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
+    setQuitReason(tr("https://quassel-irc.org - Chat comfortably. Anywhere."));
 }
 
+
 /*** setters ***/
 
-void Identity::setId(IdentityId _id) {
-  _identityId = _id;
-  emit idSet(_id);
-  renameObject(QString::number(id().toInt()));
+void Identity::setId(IdentityId _id)
+{
+    _identityId = _id;
+    SYNC(ARG(_id))
+    emit idSet(_id);
+    renameObject(QString::number(id().toInt()));
 }
 
-void Identity::setIdentityName(const QString &identityName) {
-  _identityName = identityName;
-  emit identityNameSet(identityName);
+
+void Identity::setIdentityName(const QString &identityName)
+{
+    _identityName = identityName;
+    SYNC(ARG(identityName))
 }
 
-void Identity::setRealName(const QString &realName) {
-  _realName = realName;
-  emit realNameSet(realName);
+
+void Identity::setRealName(const QString &realName)
+{
+    _realName = realName;
+    SYNC(ARG(realName))
 }
 
-void Identity::setNicks(const QStringList &nicks) {
-  _nicks = nicks;
-  emit nicksSet(nicks);
+
+void Identity::setNicks(const QStringList &nicks)
+{
+    _nicks = nicks;
+    SYNC(ARG(nicks))
+    emit nicksSet(nicks);
 }
 
-void Identity::setAwayNick(const QString &nick) {
-  _awayNick = nick;
-  emit awayNickSet(nick);
+
+void Identity::setAwayNick(const QString &nick)
+{
+    _awayNick = nick;
+    SYNC(ARG(nick))
 }
 
-void Identity::setAwayReason(const QString &reason) {
-  _awayReason = reason;
-  emit awayReasonSet(reason);
+
+void Identity::setAwayReason(const QString &reason)
+{
+    _awayReason = reason;
+    SYNC(ARG(reason))
 }
 
-void Identity::setAwayNickEnabled(bool enabled) {
-  _awayNickEnabled = enabled;
-  emit awayNickEnabledSet(enabled);
+
+void Identity::setAwayNickEnabled(bool enabled)
+{
+    _awayNickEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setAwayReasonEnabled(bool enabled) {
-  _awayReasonEnabled = enabled;
-  emit awayReasonEnabledSet(enabled);
+
+void Identity::setAwayReasonEnabled(bool enabled)
+{
+    _awayReasonEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setAutoAwayEnabled(bool enabled) {
-  _autoAwayEnabled = enabled;
-  emit autoAwayEnabledSet(enabled);
+
+void Identity::setAutoAwayEnabled(bool enabled)
+{
+    _autoAwayEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setAutoAwayTime(int time) {
-  _autoAwayTime = time;
-  emit autoAwayTimeSet(time);
+
+void Identity::setAutoAwayTime(int time)
+{
+    _autoAwayTime = time;
+    SYNC(ARG(time))
 }
 
-void Identity::setAutoAwayReason(const QString &reason) {
-  _autoAwayReason = reason;
-  emit autoAwayReasonSet(reason);
+
+void Identity::setAutoAwayReason(const QString &reason)
+{
+    _autoAwayReason = reason;
+    SYNC(ARG(reason))
 }
 
-void Identity::setAutoAwayReasonEnabled(bool enabled) {
-  _autoAwayReasonEnabled = enabled;
-  emit autoAwayReasonEnabledSet(enabled);
+
+void Identity::setAutoAwayReasonEnabled(bool enabled)
+{
+    _autoAwayReasonEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setDetachAwayEnabled(bool enabled) {
-  _detachAwayEnabled = enabled;
-  emit detachAwayEnabledSet(enabled);
+
+void Identity::setDetachAwayEnabled(bool enabled)
+{
+    _detachAwayEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setDetachAwayReason(const QString &reason) {
-  _detachAwayReason = reason;
-  emit detachAwayReasonSet(reason);
+
+void Identity::setDetachAwayReason(const QString &reason)
+{
+    _detachAwayReason = reason;
+    SYNC(ARG(reason))
 }
 
-void Identity::setDetachAwayReasonEnabled(bool enabled) {
-  _detachAwayReasonEnabled = enabled;
-  emit detachAwayReasonEnabledSet(enabled);
+
+void Identity::setDetachAwayReasonEnabled(bool enabled)
+{
+    _detachAwayReasonEnabled = enabled;
+    SYNC(ARG(enabled))
 }
 
-void Identity::setIdent(const QString &ident) {
-  _ident = ident;
-  emit identSet(ident);
+
+void Identity::setIdent(const QString &ident)
+{
+    _ident = ident;
+    SYNC(ARG(ident))
 }
 
-void Identity::setKickReason(const QString &reason) {
-  _kickReason = reason;
-  emit kickReasonSet(reason);
+
+void Identity::setKickReason(const QString &reason)
+{
+    _kickReason = reason;
+    SYNC(ARG(reason))
 }
 
-void Identity::setPartReason(const QString &reason) {
-  _partReason = reason;
-  emit partReasonSet(reason);
+
+void Identity::setPartReason(const QString &reason)
+{
+    _partReason = reason;
+    SYNC(ARG(reason))
 }
 
-void Identity::setQuitReason(const QString &reason) {
-  _quitReason = reason;
-  emit quitReasonSet(reason);
+
+void Identity::setQuitReason(const QString &reason)
+{
+    _quitReason = reason;
+    SYNC(ARG(reason))
 }
 
+
 /***  ***/
 
-void Identity::copyFrom(const Identity &other) {
-  for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
-    QMetaProperty metaProp = staticMetaObject.property(idx);
-    Q_ASSERT(metaProp.isValid());
-    if(this->property(metaProp.name()) != other.property(metaProp.name())) {
-      setProperty(metaProp.name(), other.property(metaProp.name()));
+void Identity::copyFrom(const Identity &other)
+{
+    for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
+        QMetaProperty metaProp = staticMetaObject.property(idx);
+        Q_ASSERT(metaProp.isValid());
+        if (this->property(metaProp.name()) != other.property(metaProp.name())) {
+            setProperty(metaProp.name(), other.property(metaProp.name()));
+        }
     }
-  }
-}
-
-bool Identity::operator==(const Identity &other) const {
-  for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
-    QMetaProperty metaProp = staticMetaObject.property(idx);
-    Q_ASSERT(metaProp.isValid());
-    QVariant v1 = this->property(metaProp.name());
-    QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
-    // QVariant cannot compare custom types, so we need to check for this special case
-    if(QString(v1.typeName()) == "IdentityId") {
-      if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
-    } else {
-      if(v1 != v2) return false;
+}
+
+
+bool Identity::operator==(const Identity &other) const
+{
+    for (int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
+        QMetaProperty metaProp = staticMetaObject.property(idx);
+        Q_ASSERT(metaProp.isValid());
+        QVariant v1 = this->property(metaProp.name());
+        QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
+        // QVariant cannot compare custom types, so we need to check for this special case
+        if (QString(v1.typeName()) == "IdentityId") {
+            if (v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
+        }
+        else {
+            if (v1 != v2) return false;
+        }
     }
-  }
-  return true;
+    return true;
 }
 
-bool Identity::operator!=(const Identity &other) const {
-  return !(*this == other);
+
+bool Identity::operator!=(const Identity &other) const
+{
+    return !(*this == other);
 }
 
+
 ///////////////////////////////
 
-QDataStream &operator<<(QDataStream &out, Identity id) {
-  out << id.toVariantMap();
-  return out;
+QDataStream &operator<<(QDataStream &out, Identity id)
+{
+    out << id.toVariantMap();
+    return out;
 }
 
 
-QDataStream &operator>>(QDataStream &in, Identity &id) {
-  QVariantMap i;
-  in >> i;
-  id.fromVariantMap(i);
-  return in;
+QDataStream &operator>>(QDataStream &in, Identity &id)
+{
+    QVariantMap i;
+    in >> i;
+    id.fromVariantMap(i);
+    return in;
 }
-