more default nick and realname improvements
[quassel.git] / src / common / identity.cpp
index aee2e4a..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"
+#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),
@@ -61,11 +73,52 @@ void Identity::init() {
   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("<empty>"));
-  setRealName(tr("Quassel IRC User"));
+  setRealName(defaultRealName());
   QStringList n;
-  n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+  n << defaultNick();
   setNicks(n);
   setAwayNick("");
   setAwayNickEnabled(false);
@@ -184,7 +237,7 @@ void Identity::setQuitReason(const QString &reason) {
 
 /***  ***/
 
-void Identity::update(const Identity &other) {
+void Identity::copyFrom(const Identity &other) {
   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
     QMetaProperty metaProp = staticMetaObject.property(idx);
     Q_ASSERT(metaProp.isValid());