b25b5eb0ed3e65c3524cee50e7c9a5884d2b40ee
[quassel.git] / src / common / identity.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QMetaProperty>
22 #include <QVariantMap>
23
24 #include "identity.h"
25
26 Identity::Identity(IdentityId id, QObject *parent) : QObject(parent), _identityId(id) {
27   init();
28   setToDefaults();
29 }
30
31 Identity::Identity(const Identity &other, QObject *parent) : QObject(parent),
32             _identityId(other.id()),
33             _identityName(other.identityName()),
34             _realName(other.realName()),
35             _nicks(other.nicks()),
36             _awayNick(other.awayNick()),
37             _awayReason(other.awayReason()),
38             _returnMessage(other.returnMessage()) {
39   init();
40 }
41
42 void Identity::init() {
43   _initialized = false;
44   setObjectName(QString::number(id()));
45 }
46
47 void Identity::setToDefaults() {
48   setIdentityName(tr("Default Identity"));
49   setRealName(tr("Quassel IRC User"));
50   QStringList n;
51   n << QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
52   setNicks(n);
53   setAwayNick("");
54   setAwayReason(tr("Gone fishing."));
55   setReturnMessage(tr("Brought fish."));
56
57 }
58
59 bool Identity::initialized() const {
60   return _initialized;
61 }
62
63 void Identity::setInitialized() {
64   _initialized = true;
65 }
66
67 IdentityId Identity::id() const {
68   return _identityId;
69 }
70
71 QString Identity::identityName() const {
72   return _identityName;
73 }
74
75 QString Identity::realName() const {
76   return _realName;
77 }
78
79 QStringList Identity::nicks() const {
80   return _nicks;
81 }
82
83 QString Identity::awayNick() const {
84   return _awayNick;
85 }
86
87 QString Identity::awayReason() const {
88   return _awayReason;
89 }
90
91 QString Identity::returnMessage() const {
92   return _returnMessage;
93 }
94
95 //////////////////////
96
97 void Identity::setIdentityName(const QString &identityName) {
98   _identityName = identityName;
99   emit identityNameSet(identityName);
100 }
101
102 void Identity::setRealName(const QString &realName) {
103   _realName = realName;
104   emit realNameSet(realName);
105 }
106
107 void Identity::setNicks(const QStringList &nicks) {
108   _nicks = nicks;
109   emit nicksSet(nicks);
110 }
111
112 void Identity::setAwayNick(const QString &nick) {
113   _awayNick = nick;
114   emit awayNickSet(nick);
115 }
116
117 void Identity::setAwayReason(const QString &reason) {
118   _awayReason = reason;
119   emit awayReasonSet(reason);
120 }
121
122 void Identity::setReturnMessage(const QString &message) {
123   _returnMessage = message;
124   emit returnMessageSet(message);
125 }
126
127 void Identity::update(const Identity &other) {
128   for(int idx = 0; idx < metaObject()->propertyCount(); idx++) {
129     QMetaProperty metaProp = metaObject()->property(metaObject()->propertyOffset() + idx);
130     Q_ASSERT(metaProp.isValid());
131     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
132       setProperty(metaProp.name(), other.property(metaProp.name()));
133     }
134   }
135 }
136
137 ///////////////////////////////
138
139 // we use a hash, so we can easily extend identities without breaking saved ones
140 QDataStream &operator<<(QDataStream &out, const Identity &id) {
141   QVariantMap i;
142   i["IdentityId"] = id.id();
143   i["IdentityName"] = id.identityName();
144   i["RealName"] = id.realName();
145   i["Nicks"] = id.nicks();
146   i["AwayNick"] = id.awayNick();
147   i["AwayReason"] = id.awayReason();
148   i["ReturnMessage"] = id.returnMessage();
149   out << i;
150   return out;
151 }
152
153 QDataStream &operator>>(QDataStream &in, Identity &id) {
154   QVariantMap i;
155   in >> i;
156   id._identityId = i["IdentityId"].toUInt();
157   id.setIdentityName(i["IdentityName"].toString());
158   id.setRealName(i["RealName"].toString());
159   id.setNicks(i["Nicks"].toStringList());
160   id.setAwayNick(i["AwayNick"].toString());
161   id.setAwayReason(i["AwayReason"].toString());
162   id.setReturnMessage(i["ReturnMessage"].toString());
163   return in;
164 }