This is it, the long-awaited huge commit with the new Network handling.
[quassel.git] / src / common / ircuser.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 "ircuser.h"
22 #include "util.h"
23
24 #include "network.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
27
28 #include <QTextCodec>
29 #include <QDebug>
30
31 IrcUser::IrcUser(const QString &hostmask, Network *network)
32   : SyncableObject(network),
33     _initialized(false),
34     _nick(nickFromMask(hostmask)),
35     _user(userFromMask(hostmask)),
36     _host(hostFromMask(hostmask)),
37     _realName(),
38     _awayMessage(),
39     _away(false),
40     _server(),
41     _idleTime(QDateTime::currentDateTime()),
42     _ircOperator(),
43     _network(network),
44     _codecForEncoding(0),
45     _codecForDecoding(0)
46 {
47   updateObjectName();
48 }
49
50 IrcUser::~IrcUser() {
51   //qDebug() << nick() << "destroyed.";
52 }
53
54 // ====================
55 //  PUBLIC:
56 // ====================
57
58 QString IrcUser::user() const {
59   return _user;
60 }
61
62 QString IrcUser::host() const {
63   return _host;
64 }
65
66 QString IrcUser::nick() const {
67   return _nick;
68 }
69
70 QString IrcUser::realName() const {
71   return _realName;
72 }
73
74 QString IrcUser::hostmask() const {
75   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
76 }
77
78 bool IrcUser::isAway() const {
79   return _away;
80 }
81
82 QString IrcUser::awayMessage() const {
83   return _awayMessage;
84 }
85
86 QString IrcUser::server() const {
87   return _server;
88 }
89
90 QDateTime IrcUser::idleTime() const {
91   return _idleTime;
92 }
93
94 QString IrcUser::ircOperator() const {
95   return _ircOperator;
96 }
97
98 QString IrcUser::userModes() const {
99   return _userModes;
100 }
101
102 QStringList IrcUser::channels() const {
103   QStringList chanList;
104   IrcChannel *channel;
105   foreach(channel, _channels) {
106     chanList << channel->name();
107   }
108   return chanList;
109 }
110
111 Network* IrcUser::network() const {
112   return _network;
113 }
114
115 QTextCodec *IrcUser::codecForEncoding() const {
116   return _codecForEncoding;
117 }
118
119 void IrcUser::setCodecForEncoding(const QString &name) {
120   setCodecForEncoding(QTextCodec::codecForName(name.toAscii()));
121 }
122
123 void IrcUser::setCodecForEncoding(QTextCodec *codec) {
124   _codecForEncoding = codec;
125 }
126
127 QTextCodec *IrcUser::codecForDecoding() const {
128   return _codecForDecoding;
129 }
130
131 void IrcUser::setCodecForDecoding(const QString &name) {
132   setCodecForDecoding(QTextCodec::codecForName(name.toAscii()));
133 }
134
135 void IrcUser::setCodecForDecoding(QTextCodec *codec) {
136   _codecForDecoding = codec;
137 }
138
139 QString IrcUser::decodeString(const QByteArray &text) const {
140   if(!codecForDecoding()) return network()->decodeString(text);
141   return ::decodeString(text, codecForDecoding());
142 }
143
144 QByteArray IrcUser::encodeString(const QString string) const {
145   if(codecForEncoding()) {
146     return codecForEncoding()->fromUnicode(string);
147   }
148   return network()->encodeString(string);
149 }
150
151 // ====================
152 //  PUBLIC SLOTS:
153 // ====================
154 void IrcUser::setUser(const QString &user) {
155   if(!user.isEmpty() && _user != user) {
156     _user = user;
157     emit userSet(user);
158   }
159 }
160
161 void IrcUser::setRealName(const QString &realName) {
162   if (!realName.isEmpty() && _realName != realName) {
163     _realName = realName;
164     emit realNameSet(realName);
165   }
166 }
167
168 void IrcUser::setAway(const bool &away) {
169   if(away != _away) {
170     _away = away;
171     emit awaySet(away);
172   }
173 }
174
175 void IrcUser::setAwayMessage(const QString &awayMessage) {
176   if(!awayMessage.isEmpty() && _awayMessage != awayMessage) {
177     _awayMessage = awayMessage;
178     emit awayMessageSet(awayMessage);
179   }
180 }
181
182 void IrcUser::setIdleTime(const QDateTime &idleTime) {
183   if(idleTime.isValid() && _idleTime != idleTime) {
184     _idleTime = idleTime;
185     emit idleTimeSet(idleTime);
186   }
187 }
188
189 void IrcUser::setServer(const QString &server) {
190   if(!server.isEmpty() && _server != server) {
191     _server = server;
192     emit serverSet(server);
193   }
194 }
195
196 void IrcUser::setIrcOperator(const QString &ircOperator) {
197   if(!ircOperator.isEmpty() && _ircOperator != ircOperator) {
198     _ircOperator = ircOperator;
199     emit ircOperatorSet(ircOperator);
200   }
201 }
202
203 void IrcUser::setHost(const QString &host) {
204   if(!host.isEmpty() && _host != host) {
205     _host = host;
206     emit hostSet(host);
207   }
208 }
209
210 void IrcUser::setNick(const QString &nick) {
211   if(!nick.isEmpty() && nick != _nick) {
212     _nick = nick;
213     updateObjectName();
214     emit nickSet(nick);
215   }
216 }
217
218 void IrcUser::updateObjectName() {
219   QString newName = QString::number(network()->networkId().toInt()) + "/" + _nick;
220   QString oldName = objectName();
221   if(oldName != newName) {
222     setObjectName(newName);
223     emit renameObject(oldName, newName);
224   }
225 }
226
227 void IrcUser::updateHostmask(const QString &mask) {
228   if(mask == hostmask())
229     return;
230
231   QString user = userFromMask(mask);
232   QString host = hostFromMask(mask);
233   setUser(user);
234   setHost(host);
235 }
236
237 void IrcUser::joinChannel(IrcChannel *channel) {
238   Q_ASSERT(channel);
239   if(!_channels.contains(channel)) {
240     _channels.insert(channel);
241     channel->join(this);
242     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
243     emit channelJoined(channel->name());
244   }
245 }
246
247 void IrcUser::joinChannel(const QString &channelname) {
248   joinChannel(network()->newIrcChannel(channelname));
249 }
250
251 void IrcUser::partChannel(IrcChannel *channel) {
252   if(_channels.contains(channel)) {
253     _channels.remove(channel);
254     disconnect(channel, 0, this, 0);
255     channel->part(this);
256     emit channelParted(channel->name());
257   }
258 }
259
260 void IrcUser::partChannel(const QString &channelname) {
261   IrcChannel *channel = network()->ircChannel(channelname);
262   if(channel == 0) {
263     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
264   } else {
265     partChannel(channel);
266   }
267 }
268
269 void IrcUser::channelDestroyed() {
270   // private slot!
271   IrcChannel *channel = static_cast<IrcChannel*>(sender());
272   Q_ASSERT(channel);
273   if(_channels.contains(channel)) {
274     _channels.remove(channel);
275     disconnect(channel, 0, this, 0);
276   }
277 }
278
279 void IrcUser::setUserModes(const QString &modes) {
280   _userModes = modes;
281   emit userModesSet(modes);
282 }
283
284 void IrcUser::addUserMode(const QString &mode) {
285   if(!_userModes.contains(mode)) {
286     _userModes += mode;
287     emit userModeAdded(mode);
288   }
289 }
290
291 void IrcUser::removeUserMode(const QString &mode) {
292   if(_userModes.contains(mode)) {
293     _userModes.remove(mode);
294     emit userModeRemoved(mode);
295   }
296 }
297
298 void IrcUser::initSetChannels(const QStringList channels) {
299   foreach(QString channel, channels) {
300     joinChannel(channel);
301   }
302 }
303