more default nick and realname improvements
[quassel.git] / src / common / identity.cpp
index c2fdeaf..aafb1fa 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   Copyright (C) 2005-09 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include "identity.h"
+
 #include <QMetaProperty>
 #include <QVariantMap>
 
-#include "identity.h"
-
-Identity::Identity(IdentityId id, QObject *parent) : QObject(parent), _identityId(id) {
+#ifdef Q_OS_MAC
+#  include <CoreServices/CoreServices.h>
+#  include "mac_utils.h"
+#endif
+
+#ifdef Q_OS_WIN32
+#  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) : QObject(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()),
-            _returnMessage(other.returnMessage()),
-            _returnMessageEnabled(other.returnMessageEnabled()),
-            _autoAwayEnabled(other.autoAwayEnabled()),
-            _autoAwayTime(other.autoAwayTime()),
-            _autoAwayReason(other.autoAwayReason()),
-            _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
-            _autoReturnMessage(other.autoReturnMessage()),
-            _autoReturnMessageEnabled(other.autoReturnMessageEnabled()),
-            _ident(other.ident()),
-            _kickReason(other.kickReason()),
-            _partReason(other.partReason()),
-            _quitReason(other.quitReason())
-
+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();
 }
 
 void Identity::init() {
-  _initialized = false;
-  setObjectName(QString::number(id()));
+  setObjectName(QString::number(id().toInt()));
+  setAllowClientUpdates(true);
+}
+
+QString Identity::defaultNick() {
+  QString generalDefault = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+#ifdef Q_OS_MAC
+  return CFStringToQString(CSCopyUserName(true));
+#elif defined(Q_OS_WIN32)
+  TCHAR  infoBuf[128];
+  DWORD  bufCharCount = 128;
+  //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
+  if(!GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount))
+    return generalDefault;
+
+  QString nickName(infoBuf);
+  int lastBs = nickName.lastIndexOf('\\');
+  if(lastBs != -1) {
+    nickName = nickName.mid(lastBs + 1);
+  }
+  if(nickName.isEmpty())
+    return generalDefault;
+  else
+    return nickName;
+#else
+  return generalDefault;
+#endif
+}
+
+QString Identity::defaultRealName() {
+  QString generalDefault = tr("Quassel IRC User");
+#ifdef Q_OS_MAC
+  return CFStringToQString(CSCopyUserName(false));
+#elif defined(Q_OS_WIN32)
+  TCHAR  infoBuf[128];
+  DWORD  bufCharCount = 128;
+  if(GetUserName(infoBuf, &bufCharCount))
+    return QString(infoBuf);
+  else
+    return generalDefault;
+#else
+  return generalDefault;
+#endif
 }
 
 void Identity::setToDefaults() {
-  setIdentityName(tr("Default Identity"));
-  setRealName(tr("Quassel IRC User"));
+  setIdentityName(tr("<empty>"));
+  setRealName(defaultRealName());
   QStringList n;
-  n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+  n << defaultNick();
   setNicks(n);
   setAwayNick("");
   setAwayNickEnabled(false);
   setAwayReason(tr("Gone fishing."));
   setAwayReasonEnabled(true);
-  setReturnMessage(tr("Brought fish."));
-  setReturnMessageEnabled(false);
   setAutoAwayEnabled(false);
   setAutoAwayTime(10);
   setAutoAwayReason(tr("Not here. No, really. not here!"));
   setAutoAwayReasonEnabled(false);
-  setAutoReturnMessage(tr("Back in action again!"));
-  setAutoReturnMessageEnabled(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."));
-
-}
-
-bool Identity::isValid() const {
-  return (id() > 0);
-}
-
-bool Identity::initialized() const {
-  return _initialized;
-}
-
-void Identity::setInitialized() {
-  _initialized = true;
-}
-
-IdentityId Identity::id() const {
-  return _identityId;
-}
-
-QString Identity::identityName() const {
-  return _identityName;
-}
-
-QString Identity::realName() const {
-  return _realName;
-}
-
-QStringList Identity::nicks() const {
-  return _nicks;
-}
-
-QString Identity::awayNick() const {
-  return _awayNick;
-}
-
-bool Identity::awayNickEnabled() const {
-  return _awayNickEnabled;
-}
-
-QString Identity::awayReason() const {
-  return _awayReason;
-}
-
-bool Identity::awayReasonEnabled() const {
-  return _awayReasonEnabled;
-}
-
-QString Identity::returnMessage() const {
-  return _returnMessage;
-}
-
-bool Identity::returnMessageEnabled() const {
-  return _returnMessageEnabled;
-}
-
-bool Identity::autoAwayEnabled() const {
-  return _autoAwayEnabled;
-}
-
-int Identity::autoAwayTime() const {
-  return _autoAwayTime;
-}
-
-QString Identity::autoAwayReason() const {
-  return _autoAwayReason;
-}
-
-bool Identity::autoAwayReasonEnabled() const {
-  return _autoAwayReasonEnabled;
-}
-
-QString Identity::autoReturnMessage() const {
-  return _autoReturnMessage;
-}
-
-bool Identity::autoReturnMessageEnabled() const {
-  return _autoReturnMessageEnabled;
-}
-
-QString Identity::ident() const {
-  return _ident;
-}
-
-QString Identity::kickReason() const {
-  return _kickReason;
-}
-
-QString Identity::partReason() const
-{return _partReason;}
-
-QString Identity::quitReason() const {
-  return _quitReason;
 }
 
 /*** setters ***/
 
-// NOTE: DO NOT USE ON SYNCHRONIZED OBJECTS!
 void Identity::setId(IdentityId _id) {
   _identityId = _id;
-  setObjectName(QString::number(id()));
-  //emit idSet(id);
+  emit idSet(_id);
+  renameObject(QString::number(id().toInt()));
 }
 
 void Identity::setIdentityName(const QString &identityName) {
@@ -209,11 +170,6 @@ void Identity::setAwayReason(const QString &reason) {
   emit awayReasonSet(reason);
 }
 
-void Identity::setReturnMessage(const QString &message) {
-  _returnMessage = message;
-  emit returnMessageSet(message);
-}
-
 void Identity::setAwayNickEnabled(bool enabled) {
   _awayNickEnabled = enabled;
   emit awayNickEnabledSet(enabled);
@@ -224,11 +180,6 @@ void Identity::setAwayReasonEnabled(bool enabled) {
   emit awayReasonEnabledSet(enabled);
 }
 
-void Identity::setReturnMessageEnabled(bool enabled) {
-  _returnMessageEnabled = enabled;
-  emit returnMessageEnabledSet(enabled);
-}
-
 void Identity::setAutoAwayEnabled(bool enabled) {
   _autoAwayEnabled = enabled;
   emit autoAwayEnabledSet(enabled);
@@ -239,7 +190,7 @@ void Identity::setAutoAwayTime(int time) {
   emit autoAwayTimeSet(time);
 }
 
-void Identity::setAutoAwayReason(const QString & reason) {
+void Identity::setAutoAwayReason(const QString &reason) {
   _autoAwayReason = reason;
   emit autoAwayReasonSet(reason);
 }
@@ -249,55 +200,60 @@ void Identity::setAutoAwayReasonEnabled(bool enabled) {
   emit autoAwayReasonEnabledSet(enabled);
 }
 
-void Identity::setAutoReturnMessage(const QString & message) {
-  _autoReturnMessage = message;
-  emit autoReturnMessageSet(message);
+void Identity::setDetachAwayEnabled(bool enabled) {
+  _detachAwayEnabled = enabled;
+  emit detachAwayEnabledSet(enabled);
+}
+
+void Identity::setDetachAwayReason(const QString &reason) {
+  _detachAwayReason = reason;
+  emit detachAwayReasonSet(reason);
 }
 
-void Identity::setAutoReturnMessageEnabled(bool enabled) {
-  _autoReturnMessageEnabled = enabled;
-  emit autoReturnMessageEnabledSet(enabled);
+void Identity::setDetachAwayReasonEnabled(bool enabled) {
+  _detachAwayReasonEnabled = enabled;
+  emit detachAwayReasonEnabledSet(enabled);
 }
 
-void Identity::setIdent(const QString & ident) {
+void Identity::setIdent(const QString &ident) {
   _ident = ident;
   emit identSet(ident);
 }
 
-void Identity::setKickReason(const QString & reason) {
+void Identity::setKickReason(const QString &reason) {
   _kickReason = reason;
   emit kickReasonSet(reason);
 }
 
-void Identity::setPartReason(const QString & reason) {
+void Identity::setPartReason(const QString &reason) {
   _partReason = reason;
   emit partReasonSet(reason);
 }
 
-void Identity::setQuitReason(const QString & reason) {
+void Identity::setQuitReason(const QString &reason) {
   _quitReason = reason;
   emit quitReasonSet(reason);
 }
 
 /***  ***/
 
-void Identity::update(const Identity &other) {
-for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
-    QMetaProperty metaProp = metaObject()->property(idx);
+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()));
     }
   }
 }
-#include <QDebug>
-bool Identity::operator==(const Identity &other) {
-  for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount(); idx++) {
-    QMetaProperty metaProp = metaObject()->property(idx);
+
+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 case
+    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 {
@@ -307,62 +263,22 @@ bool Identity::operator==(const Identity &other) {
   return true;
 }
 
-bool Identity::operator!=(const Identity &other) {
+bool Identity::operator!=(const Identity &other) const {
   return !(*this == other);
 }
 
 ///////////////////////////////
 
-// we use a hash, so we can easily extend identities without breaking saved ones
-QDataStream &operator<<(QDataStream &out, const Identity &id) {
-  QVariantMap i;
-  i["IdentityId"] = id.id();
-  i["IdentityName"] = id.identityName();
-  i["RealName"] = id.realName();
-  i["Nicks"] = id.nicks();
-  i["AwayNick"] = id.awayNick();
-  i["AwayNickEnabled"] = id.awayNickEnabled();
-  i["AwayReason"] = id.awayReason();
-  i["AwayReasonEnabled"] = id.awayReasonEnabled();
-  i["ReturnMessage"] = id.returnMessage();
-  i["ReturnMessageEnabled"] = id.returnMessageEnabled();
-  i["AutoAwayEnabled"] = id.autoAwayEnabled();
-  i["AutoAwayTime"] = id.autoAwayTime();
-  i["AutoAwayReason"] = id.autoAwayReason();
-  i["AutoAwayReasonEnabled"] = id.autoAwayReasonEnabled();
-  i["AutoReturnMessage"] = id.autoReturnMessage();
-  i["AutoReturnMessageEnabled"] = id.autoReturnMessageEnabled();
-  i["Ident"] = id.ident();
-  i["KickReason"] = id.kickReason();
-  i["PartReason"] = id.partReason();
-  i["QuitReason"] = id.quitReason();
-  out << i;
+QDataStream &operator<<(QDataStream &out, Identity id) {
+  out << id.toVariantMap();
   return out;
 }
 
+
 QDataStream &operator>>(QDataStream &in, Identity &id) {
   QVariantMap i;
   in >> i;
-  id._identityId = i["IdentityId"].toUInt();
-  id.setIdentityName(i["IdentityName"].toString());
-  id.setRealName(i["RealName"].toString());
-  id.setNicks(i["Nicks"].toStringList());
-  id.setAwayNick(i["AwayNick"].toString());
-  id.setAwayNickEnabled(i["AwayNickEnabled"].toBool());
-  id.setAwayReason(i["AwayReason"].toString());
-  id.setAwayReasonEnabled(i["AwayReasonEnabled"].toBool());
-  id.setReturnMessage(i["ReturnMessage"].toString());
-  id.setReturnMessageEnabled(i["ReturnMessageEnabled"].toBool());
-  id.setAutoAwayEnabled(i["AutoAwayEnabled"].toBool());
-  id.setAutoAwayTime(i["AutoAwayTime"].toInt());
-  id.setAutoAwayReason(i["AutoAwayReason"].toString());
-  id.setAutoAwayReasonEnabled(i["AutoAwayReasonEnabled"].toBool());
-  id.setAutoReturnMessage(i["AutoReturnMessage"].toString());
-  id.setAutoReturnMessageEnabled(i["AutoReturnMessageEnabled"].toBool());
-  id.setIdent(i["Ident"].toString());
-  id.setKickReason(i["KickReason"].toString());
-  id.setPartReason(i["PartReason"].toString());
-  id.setQuitReason(i["QuitReason"].toString());
+  id.fromVariantMap(i);
   return in;
 }