X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fidentity.cpp;h=40b0b2b6000034c78f7ef9e09896f732e369a7e8;hp=b25b5eb0ed3e65c3524cee50e7c9a5884d2b40ee;hb=9ffb7c204ea6d658dafdb626f50230d4b890008c;hpb=8699dd758516d0ded076811e8ea656adc95e69d0 diff --git a/src/common/identity.cpp b/src/common/identity.cpp index b25b5eb0..40b0b2b6 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,115 +18,229 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "identity.h" + #include #include -#include "identity.h" +#ifdef Q_OS_MAC +# include +# include "mac_utils.h" +#endif + +#ifdef Q_OS_WIN32 +# include +# include +# define SECURITY_WIN32 +# include +#endif -Identity::Identity(IdentityId id, QObject *parent) : QObject(parent), _identityId(id) { +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()), - _awayReason(other.awayReason()), - _returnMessage(other.returnMessage()) { +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 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_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-z\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_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("")); + 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.")); - setReturnMessage(tr("Brought fish.")); + 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.")); +} + +/*** setters ***/ +void Identity::setId(IdentityId _id) { + _identityId = _id; + emit idSet(_id); + renameObject(QString::number(id().toInt())); } -bool Identity::initialized() const { - return _initialized; +void Identity::setIdentityName(const QString &identityName) { + _identityName = identityName; + emit identityNameSet(identityName); } -void Identity::setInitialized() { - _initialized = true; +void Identity::setRealName(const QString &realName) { + _realName = realName; + emit realNameSet(realName); } -IdentityId Identity::id() const { - return _identityId; +void Identity::setNicks(const QStringList &nicks) { + _nicks = nicks; + emit nicksSet(nicks); } -QString Identity::identityName() const { - return _identityName; +void Identity::setAwayNick(const QString &nick) { + _awayNick = nick; + emit awayNickSet(nick); } -QString Identity::realName() const { - return _realName; +void Identity::setAwayReason(const QString &reason) { + _awayReason = reason; + emit awayReasonSet(reason); } -QStringList Identity::nicks() const { - return _nicks; +void Identity::setAwayNickEnabled(bool enabled) { + _awayNickEnabled = enabled; + emit awayNickEnabledSet(enabled); } -QString Identity::awayNick() const { - return _awayNick; +void Identity::setAwayReasonEnabled(bool enabled) { + _awayReasonEnabled = enabled; + emit awayReasonEnabledSet(enabled); } -QString Identity::awayReason() const { - return _awayReason; +void Identity::setAutoAwayEnabled(bool enabled) { + _autoAwayEnabled = enabled; + emit autoAwayEnabledSet(enabled); } -QString Identity::returnMessage() const { - return _returnMessage; +void Identity::setAutoAwayTime(int time) { + _autoAwayTime = time; + emit autoAwayTimeSet(time); } -////////////////////// +void Identity::setAutoAwayReason(const QString &reason) { + _autoAwayReason = reason; + emit autoAwayReasonSet(reason); +} -void Identity::setIdentityName(const QString &identityName) { - _identityName = identityName; - emit identityNameSet(identityName); +void Identity::setAutoAwayReasonEnabled(bool enabled) { + _autoAwayReasonEnabled = enabled; + emit autoAwayReasonEnabledSet(enabled); } -void Identity::setRealName(const QString &realName) { - _realName = realName; - emit realNameSet(realName); +void Identity::setDetachAwayEnabled(bool enabled) { + _detachAwayEnabled = enabled; + emit detachAwayEnabledSet(enabled); } -void Identity::setNicks(const QStringList &nicks) { - _nicks = nicks; - emit nicksSet(nicks); +void Identity::setDetachAwayReason(const QString &reason) { + _detachAwayReason = reason; + emit detachAwayReasonSet(reason); } -void Identity::setAwayNick(const QString &nick) { - _awayNick = nick; - emit awayNickSet(nick); +void Identity::setDetachAwayReasonEnabled(bool enabled) { + _detachAwayReasonEnabled = enabled; + emit detachAwayReasonEnabledSet(enabled); } -void Identity::setAwayReason(const QString &reason) { - _awayReason = reason; - emit awayReasonSet(reason); +void Identity::setIdent(const QString &ident) { + _ident = ident; + emit identSet(ident); } -void Identity::setReturnMessage(const QString &message) { - _returnMessage = message; - emit returnMessageSet(message); +void Identity::setKickReason(const QString &reason) { + _kickReason = reason; + emit kickReasonSet(reason); } -void Identity::update(const Identity &other) { - for(int idx = 0; idx < metaObject()->propertyCount(); idx++) { - QMetaProperty metaProp = metaObject()->property(metaObject()->propertyOffset() + idx); +void Identity::setPartReason(const QString &reason) { + _partReason = reason; + emit partReasonSet(reason); +} + +void Identity::setQuitReason(const QString &reason) { + _quitReason = reason; + emit quitReasonSet(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())); @@ -134,31 +248,38 @@ void Identity::update(const Identity &other) { } } +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() != v2.value()) return false; + } else { + if(v1 != v2) return false; + } + } + return true; +} + +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["AwayReason"] = id.awayReason(); - i["ReturnMessage"] = id.returnMessage(); - 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.setAwayReason(i["AwayReason"].toString()); - id.setReturnMessage(i["ReturnMessage"].toString()); + id.fromVariantMap(i); return in; } +