no longer adding nick alternatives with trailing underscores to the default nicks
[quassel.git] / src / common / identity.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 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 "identity.h"
22
23 #include <QMetaProperty>
24 #include <QVariantMap>
25
26 #ifdef Q_OS_MAC
27 #  include <CoreServices/CoreServices.h>
28 #  include "mac_utils.h"
29 #endif
30
31 #ifdef Q_OS_UNIX
32 #  include <sys/types.h>
33 #  include <pwd.h>
34 #  include <unistd.h>
35 #endif
36
37 #ifdef Q_OS_WIN32
38 #  include <windows.h>
39 #  include <Winbase.h>
40 #  define SECURITY_WIN32
41 #  include <Security.h>
42 #endif
43
44 Identity::Identity(IdentityId id, QObject *parent)
45   : SyncableObject(parent),
46     _identityId(id)
47 {
48   init();
49   setToDefaults();
50 }
51
52 Identity::Identity(const Identity &other, QObject *parent)
53   : SyncableObject(parent),
54     _identityId(other.id()),
55     _identityName(other.identityName()),
56     _realName(other.realName()),
57     _nicks(other.nicks()),
58     _awayNick(other.awayNick()),
59     _awayNickEnabled(other.awayNickEnabled()),
60     _awayReason(other.awayReason()),
61     _awayReasonEnabled(other.awayReasonEnabled()),
62     _autoAwayEnabled(other.autoAwayEnabled()),
63     _autoAwayTime(other.autoAwayTime()),
64     _autoAwayReason(other.autoAwayReason()),
65     _autoAwayReasonEnabled(other.autoAwayReasonEnabled()),
66     _detachAwayEnabled(other.detachAwayEnabled()),
67     _detachAwayReason(other.detachAwayReason()),
68     _detachAwayReasonEnabled(other.detachAwayReasonEnabled()),
69     _ident(other.ident()),
70     _kickReason(other.kickReason()),
71     _partReason(other.partReason()),
72     _quitReason(other.quitReason())
73 {
74   init();
75 }
76
77 void Identity::init() {
78   setObjectName(QString::number(id().toInt()));
79   setAllowClientUpdates(true);
80 }
81
82 QString Identity::defaultNick() {
83   QString nick = QString("quassel%1").arg(qrand() & 0xff); // FIXME provide more sensible default nicks
84
85 #ifdef Q_OS_MAC
86   QString shortUserName = CFStringToQString(CSCopyUserName(true));
87   if(!shortUserName.isEmpty())
88     nick = shortUserName;
89
90 #elif defined(Q_OS_UNIX)
91   QString userName = getlogin();
92   if(!userName.isEmpty())
93     nick = userName;
94
95 #elif defined(Q_OS_WIN32)
96   TCHAR  infoBuf[128];
97   DWORD  bufCharCount = 128;
98   //if(GetUserNameEx(/* NameSamCompatible */ 1, infoBuf, &bufCharCount))
99   if(GetUserNameEx(NameSamCompatible, infoBuf, &bufCharCount)) {
100     QString nickName(infoBuf);
101     int lastBs = nickName.lastIndexOf('\\');
102     if(lastBs != -1) {
103       nickName = nickName.mid(lastBs + 1);
104     }
105     if(!nickName.isEmpty())
106       nick = nickName;
107   }
108 #endif
109
110   // cleaning forbidden characters from nick
111   QRegExp rx(QString("(^[\\d-]+|[^A-Za-z0-9\x5b-\x60\x7b-\x7d])"));
112   nick.remove(rx);
113   return nick;
114 }
115
116 QString Identity::defaultRealName() {
117   QString generalDefault = tr("Quassel IRC User");
118
119 #ifdef Q_OS_MAC
120   return CFStringToQString(CSCopyUserName(false));
121
122 #elif defined(Q_OS_UNIX)
123   QString realName;
124   struct passwd *pwd = getpwuid(getuid());
125   if(pwd)
126     realName = pwd->pw_gecos;
127   if(!realName.isEmpty())
128     return realName;
129   else
130     return generalDefault;
131
132 #elif defined(Q_OS_WIN32)
133   TCHAR  infoBuf[128];
134   DWORD  bufCharCount = 128;
135   if(GetUserName(infoBuf, &bufCharCount))
136     return QString(infoBuf);
137   else
138     return generalDefault;
139 #else
140   return generalDefault;
141 #endif
142 }
143
144 void Identity::setToDefaults() {
145   setIdentityName(tr("<empty>"));
146   setRealName(defaultRealName());
147   QStringList n = QStringList() << defaultNick();
148   setNicks(n);
149   setAwayNick("");
150   setAwayNickEnabled(false);
151   setAwayReason(tr("Gone fishing."));
152   setAwayReasonEnabled(true);
153   setAutoAwayEnabled(false);
154   setAutoAwayTime(10);
155   setAutoAwayReason(tr("Not here. No, really. not here!"));
156   setAutoAwayReasonEnabled(false);
157   setDetachAwayEnabled(false);
158   setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
159   setDetachAwayReasonEnabled(false);
160   setIdent("quassel");
161   setKickReason(tr("Kindergarten is elsewhere!"));
162   setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
163   setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
164 }
165
166 /*** setters ***/
167
168 void Identity::setId(IdentityId _id) {
169   _identityId = _id;
170   emit idSet(_id);
171   renameObject(QString::number(id().toInt()));
172 }
173
174 void Identity::setIdentityName(const QString &identityName) {
175   _identityName = identityName;
176   emit identityNameSet(identityName);
177 }
178
179 void Identity::setRealName(const QString &realName) {
180   _realName = realName;
181   emit realNameSet(realName);
182 }
183
184 void Identity::setNicks(const QStringList &nicks) {
185   _nicks = nicks;
186   emit nicksSet(nicks);
187 }
188
189 void Identity::setAwayNick(const QString &nick) {
190   _awayNick = nick;
191   emit awayNickSet(nick);
192 }
193
194 void Identity::setAwayReason(const QString &reason) {
195   _awayReason = reason;
196   emit awayReasonSet(reason);
197 }
198
199 void Identity::setAwayNickEnabled(bool enabled) {
200   _awayNickEnabled = enabled;
201   emit awayNickEnabledSet(enabled);
202 }
203
204 void Identity::setAwayReasonEnabled(bool enabled) {
205   _awayReasonEnabled = enabled;
206   emit awayReasonEnabledSet(enabled);
207 }
208
209 void Identity::setAutoAwayEnabled(bool enabled) {
210   _autoAwayEnabled = enabled;
211   emit autoAwayEnabledSet(enabled);
212 }
213
214 void Identity::setAutoAwayTime(int time) {
215   _autoAwayTime = time;
216   emit autoAwayTimeSet(time);
217 }
218
219 void Identity::setAutoAwayReason(const QString &reason) {
220   _autoAwayReason = reason;
221   emit autoAwayReasonSet(reason);
222 }
223
224 void Identity::setAutoAwayReasonEnabled(bool enabled) {
225   _autoAwayReasonEnabled = enabled;
226   emit autoAwayReasonEnabledSet(enabled);
227 }
228
229 void Identity::setDetachAwayEnabled(bool enabled) {
230   _detachAwayEnabled = enabled;
231   emit detachAwayEnabledSet(enabled);
232 }
233
234 void Identity::setDetachAwayReason(const QString &reason) {
235   _detachAwayReason = reason;
236   emit detachAwayReasonSet(reason);
237 }
238
239 void Identity::setDetachAwayReasonEnabled(bool enabled) {
240   _detachAwayReasonEnabled = enabled;
241   emit detachAwayReasonEnabledSet(enabled);
242 }
243
244 void Identity::setIdent(const QString &ident) {
245   _ident = ident;
246   emit identSet(ident);
247 }
248
249 void Identity::setKickReason(const QString &reason) {
250   _kickReason = reason;
251   emit kickReasonSet(reason);
252 }
253
254 void Identity::setPartReason(const QString &reason) {
255   _partReason = reason;
256   emit partReasonSet(reason);
257 }
258
259 void Identity::setQuitReason(const QString &reason) {
260   _quitReason = reason;
261   emit quitReasonSet(reason);
262 }
263
264 /***  ***/
265
266 void Identity::copyFrom(const Identity &other) {
267   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
268     QMetaProperty metaProp = staticMetaObject.property(idx);
269     Q_ASSERT(metaProp.isValid());
270     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
271       setProperty(metaProp.name(), other.property(metaProp.name()));
272     }
273   }
274 }
275
276 bool Identity::operator==(const Identity &other) const {
277   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
278     QMetaProperty metaProp = staticMetaObject.property(idx);
279     Q_ASSERT(metaProp.isValid());
280     QVariant v1 = this->property(metaProp.name());
281     QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
282     // QVariant cannot compare custom types, so we need to check for this special case
283     if(QString(v1.typeName()) == "IdentityId") {
284       if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
285     } else {
286       if(v1 != v2) return false;
287     }
288   }
289   return true;
290 }
291
292 bool Identity::operator!=(const Identity &other) const {
293   return !(*this == other);
294 }
295
296 ///////////////////////////////
297
298 QDataStream &operator<<(QDataStream &out, Identity id) {
299   out << id.toVariantMap();
300   return out;
301 }
302
303
304 QDataStream &operator>>(QDataStream &in, Identity &id) {
305   QVariantMap i;
306   in >> i;
307   id.fromVariantMap(i);
308   return in;
309 }
310