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