Providing proper defaults for nick and realname on Mac OS using CoreServices
authorMarcus Eggenberger <egs@quassel-irc.org>
Sat, 7 Feb 2009 16:07:29 +0000 (17:07 +0100)
committerMarcus Eggenberger <egs@quassel-irc.org>
Sat, 7 Feb 2009 16:07:29 +0000 (17:07 +0100)
src/common/CMakeLists.txt
src/common/identity.cpp
src/common/identity.h
src/common/mac_utils.cpp [new file with mode: 0644]
src/common/mac_utils.h [new file with mode: 0644]

index dc790b6..fae257d 100644 (file)
@@ -56,9 +56,17 @@ set(HEADERS ${MOC_HDRS}
     types.h
     util.h)
 
+if(APPLE)
+  set(SOURCES ${SOURCES} mac_utils.cpp)
+  set(HEADERS ${HEADERS} mac_utils.h)
+endif(APPLE)
+
 qt4_wrap_cpp(MOC ${MOC_HDRS})
 
 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR})  # for version.inc and version.gen
 set_directory_properties(PROPERTIES ADDITIONAL_MAKE_CLEAN_FILES version.gen)
 
 add_library(mod_common STATIC ${SOURCES} ${MOC})
+if(APPLE)
+  target_link_libraries(mod_common "-framework CoreServices" "-framework CoreFoundation")
+endif(APPLE)
\ No newline at end of file
index 4eadff9..31b8a4a 100644 (file)
  *   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
 
 Identity::Identity(IdentityId id, QObject *parent)
   : SyncableObject(parent),
@@ -61,11 +66,27 @@ void Identity::init() {
   setAllowClientUpdates(true);
 }
 
+QString Identity::defaultNick() {
+#ifdef Q_OS_MAC
+  return CFStringToQString(CSCopyUserName(true));
+#else
+  return QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
+#endif
+}
+
+QString Identity::defaultRealName() {
+#ifdef Q_OS_MAC
+  return CFStringToQString(CSCopyUserName(false));
+#else
+  return tr("Quassel IRC User");
+#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);
index a5fc9cd..3e9a560 100644 (file)
@@ -147,6 +147,8 @@ private:
   QString _ident, _kickReason, _partReason, _quitReason;
 
   void init();
+  QString defaultNick();
+  QString defaultRealName();
 
   friend QDataStream &operator>>(QDataStream &in, Identity &identity);
 };
diff --git a/src/common/mac_utils.cpp b/src/common/mac_utils.cpp
new file mode 100644 (file)
index 0000000..c76c71c
--- /dev/null
@@ -0,0 +1,37 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#include "mac_utils.h"
+
+#include <QVarLengthArray>
+
+QString CFStringToQString(CFStringRef str) {
+  if(!str)
+    return QString();
+
+  CFIndex length = CFStringGetLength(str);
+  const UniChar *chars = CFStringGetCharactersPtr(str);
+  if(chars)
+    return QString(reinterpret_cast<const QChar *>(chars), length);
+
+  QVarLengthArray<UniChar> buffer(length);
+  CFStringGetCharacters(str, CFRangeMake(0, length), buffer.data());
+  return QString(reinterpret_cast<const QChar *>(buffer.constData()), length);
+}
diff --git a/src/common/mac_utils.h b/src/common/mac_utils.h
new file mode 100644 (file)
index 0000000..78bd58c
--- /dev/null
@@ -0,0 +1,29 @@
+/***************************************************************************
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
+ *   devel@quassel-irc.org                                                 *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) version 3.                                           *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   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.             *
+ ***************************************************************************/
+
+#ifndef MAC_UTILS_H
+#define MAC_UTILS_H
+
+#include <QString>
+#include <CoreFoundation/CoreFoundation.h>
+
+QString CFStringToQString(CFStringRef str);
+
+#endif //MAC_UTILS_H