Use login name as default nick on Unix, fixes #516
[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-z\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 #ifdef Q_OS_MAC
119   return CFStringToQString(CSCopyUserName(false));
120 #elif defined(Q_OS_WIN32)
121   TCHAR  infoBuf[128];
122   DWORD  bufCharCount = 128;
123   if(GetUserName(infoBuf, &bufCharCount))
124     return QString(infoBuf);
125   else
126     return generalDefault;
127 #else
128   return generalDefault;
129 #endif
130 }
131
132 void Identity::setToDefaults() {
133   setIdentityName(tr("<empty>"));
134   setRealName(defaultRealName());
135   QStringList n;
136   n << defaultNick() << defaultNick() + "_" << defaultNick() + "__";
137   setNicks(n);
138   setAwayNick("");
139   setAwayNickEnabled(false);
140   setAwayReason(tr("Gone fishing."));
141   setAwayReasonEnabled(true);
142   setAutoAwayEnabled(false);
143   setAutoAwayTime(10);
144   setAutoAwayReason(tr("Not here. No, really. not here!"));
145   setAutoAwayReasonEnabled(false);
146   setDetachAwayEnabled(false);
147   setDetachAwayReason(tr("All Quassel clients vanished from the face of the earth..."));
148   setDetachAwayReasonEnabled(false);
149   setIdent("quassel");
150   setKickReason(tr("Kindergarten is elsewhere!"));
151   setPartReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
152   setQuitReason(tr("http://quassel-irc.org - Chat comfortably. Anywhere."));
153 }
154
155 /*** setters ***/
156
157 void Identity::setId(IdentityId _id) {
158   _identityId = _id;
159   emit idSet(_id);
160   renameObject(QString::number(id().toInt()));
161 }
162
163 void Identity::setIdentityName(const QString &identityName) {
164   _identityName = identityName;
165   emit identityNameSet(identityName);
166 }
167
168 void Identity::setRealName(const QString &realName) {
169   _realName = realName;
170   emit realNameSet(realName);
171 }
172
173 void Identity::setNicks(const QStringList &nicks) {
174   _nicks = nicks;
175   emit nicksSet(nicks);
176 }
177
178 void Identity::setAwayNick(const QString &nick) {
179   _awayNick = nick;
180   emit awayNickSet(nick);
181 }
182
183 void Identity::setAwayReason(const QString &reason) {
184   _awayReason = reason;
185   emit awayReasonSet(reason);
186 }
187
188 void Identity::setAwayNickEnabled(bool enabled) {
189   _awayNickEnabled = enabled;
190   emit awayNickEnabledSet(enabled);
191 }
192
193 void Identity::setAwayReasonEnabled(bool enabled) {
194   _awayReasonEnabled = enabled;
195   emit awayReasonEnabledSet(enabled);
196 }
197
198 void Identity::setAutoAwayEnabled(bool enabled) {
199   _autoAwayEnabled = enabled;
200   emit autoAwayEnabledSet(enabled);
201 }
202
203 void Identity::setAutoAwayTime(int time) {
204   _autoAwayTime = time;
205   emit autoAwayTimeSet(time);
206 }
207
208 void Identity::setAutoAwayReason(const QString &reason) {
209   _autoAwayReason = reason;
210   emit autoAwayReasonSet(reason);
211 }
212
213 void Identity::setAutoAwayReasonEnabled(bool enabled) {
214   _autoAwayReasonEnabled = enabled;
215   emit autoAwayReasonEnabledSet(enabled);
216 }
217
218 void Identity::setDetachAwayEnabled(bool enabled) {
219   _detachAwayEnabled = enabled;
220   emit detachAwayEnabledSet(enabled);
221 }
222
223 void Identity::setDetachAwayReason(const QString &reason) {
224   _detachAwayReason = reason;
225   emit detachAwayReasonSet(reason);
226 }
227
228 void Identity::setDetachAwayReasonEnabled(bool enabled) {
229   _detachAwayReasonEnabled = enabled;
230   emit detachAwayReasonEnabledSet(enabled);
231 }
232
233 void Identity::setIdent(const QString &ident) {
234   _ident = ident;
235   emit identSet(ident);
236 }
237
238 void Identity::setKickReason(const QString &reason) {
239   _kickReason = reason;
240   emit kickReasonSet(reason);
241 }
242
243 void Identity::setPartReason(const QString &reason) {
244   _partReason = reason;
245   emit partReasonSet(reason);
246 }
247
248 void Identity::setQuitReason(const QString &reason) {
249   _quitReason = reason;
250   emit quitReasonSet(reason);
251 }
252
253 /***  ***/
254
255 void Identity::copyFrom(const Identity &other) {
256   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
257     QMetaProperty metaProp = staticMetaObject.property(idx);
258     Q_ASSERT(metaProp.isValid());
259     if(this->property(metaProp.name()) != other.property(metaProp.name())) {
260       setProperty(metaProp.name(), other.property(metaProp.name()));
261     }
262   }
263 }
264
265 bool Identity::operator==(const Identity &other) const {
266   for(int idx = staticMetaObject.propertyOffset(); idx < staticMetaObject.propertyCount(); idx++) {
267     QMetaProperty metaProp = staticMetaObject.property(idx);
268     Q_ASSERT(metaProp.isValid());
269     QVariant v1 = this->property(metaProp.name());
270     QVariant v2 = other.property(metaProp.name()); // qDebug() << v1 << v2;
271     // QVariant cannot compare custom types, so we need to check for this special case
272     if(QString(v1.typeName()) == "IdentityId") {
273       if(v1.value<IdentityId>() != v2.value<IdentityId>()) return false;
274     } else {
275       if(v1 != v2) return false;
276     }
277   }
278   return true;
279 }
280
281 bool Identity::operator!=(const Identity &other) const {
282   return !(*this == other);
283 }
284
285 ///////////////////////////////
286
287 QDataStream &operator<<(QDataStream &out, Identity id) {
288   out << id.toVariantMap();
289   return out;
290 }
291
292
293 QDataStream &operator>>(QDataStream &in, Identity &id) {
294   QVariantMap i;
295   in >> i;
296   id.fromVariantMap(i);
297   return in;
298 }
299