X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fidentity.cpp;h=0569178ade8b86b0fbc150c2cbe322d3d64ecb1b;hp=eff50215611f2f3df17cc68f5cedf75f8226d19a;hb=9f461bad6c890d3fe92d89ebf1b6a3f313d7e8ef;hpb=5d05ab030c24ff57b0e5d00d9d66869c2f934a49 diff --git a/src/common/identity.cpp b/src/common/identity.cpp index eff50215..0569178a 100644 --- a/src/common/identity.cpp +++ b/src/common/identity.cpp @@ -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 * @@ -18,10 +18,29 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "identity.h" + #include #include - -#include "identity.h" +#include + +#ifdef Q_OS_MAC +# include +# include "mac_utils.h" +#endif + +#ifdef Q_OS_UNIX +# include +# include +# include +#endif + +#ifdef Q_OS_WIN32 +# include +# include +# define SECURITY_WIN32 +# include +#endif Identity::Identity(IdentityId id, QObject *parent) : SyncableObject(parent), @@ -61,11 +80,75 @@ void Identity::init() { 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_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); + } + 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; +} + +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_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("")); - setRealName(tr("Quassel IRC User")); - QStringList n; - n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks + setRealName(defaultRealName()); + QStringList n = QStringList() << defaultNick(); setNicks(n); setAwayNick(""); setAwayNickEnabled(false); @@ -86,10 +169,10 @@ void Identity::setToDefaults() { /*** setters ***/ -// NOTE: DO NOT USE ON SYNCHRONIZED OBJECTS! void Identity::setId(IdentityId _id) { _identityId = _id; - setObjectName(QString::number(id().toInt())); + emit idSet(_id); + renameObject(QString::number(id().toInt())); } void Identity::setIdentityName(const QString &identityName) { @@ -184,9 +267,9 @@ void Identity::setQuitReason(const QString &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())); @@ -194,9 +277,9 @@ for(int idx = metaObject()->propertyOffset(); idx < metaObject()->propertyCount( } } -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; @@ -210,7 +293,7 @@ bool Identity::operator==(const Identity &other) { return true; } -bool Identity::operator!=(const Identity &other) { +bool Identity::operator!=(const Identity &other) const { return !(*this == other); } @@ -228,3 +311,4 @@ QDataStream &operator>>(QDataStream &in, Identity &id) { id.fromVariantMap(i); return in; } +