Finaly got rid of the synchronizers, making Quassel quite a bit more lightweight...
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by The Quassel Team                             *
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) any later version.                                   *
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 "networkinfo.h"
25 #include "signalproxy.h"
26 #include "ircchannel.h"
27
28 #include <QDebug>
29
30 IrcUser::IrcUser(const QString &hostmask, NetworkInfo *networkinfo)
31   : QObject(networkinfo),
32     _initialized(false),
33     _nick(nickFromMask(hostmask)),
34     _user(userFromMask(hostmask)),
35     _host(hostFromMask(hostmask)),
36     networkInfo(networkinfo)
37 {
38   updateObjectName();
39 }
40
41 IrcUser::~IrcUser() {
42 }
43
44 // ====================
45 //  PUBLIC:
46 // ====================
47 bool IrcUser::initialized() const {
48   return _initialized;
49 }
50
51 QString IrcUser::user() const {
52   return _user;
53 }
54
55 QString IrcUser::host() const {
56   return _host;
57 }
58
59 QString IrcUser::nick() const {
60   return _nick;
61 }
62
63 QString IrcUser::hostmask() const {
64   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
65 }
66
67 QString IrcUser::userModes() const {
68   return _userModes;
69 }
70
71 QStringList IrcUser::channels() const {
72   return _channels.toList();
73 }
74
75 // ====================
76 //  PUBLIC SLOTS:
77 // ====================
78 void IrcUser::setUser(const QString &user) {
79   if(!user.isEmpty() && _user != user) {
80     _user = user;
81     emit userSet(user);
82   }
83 }
84
85 void IrcUser::setHost(const QString &host) {
86   if(!host.isEmpty() && _host != host) {
87     _host = host;
88     emit hostSet(host);
89   }
90 }
91
92 void IrcUser::setNick(const QString &nick) {
93   if(!nick.isEmpty() && nick != _nick) {
94     QString oldnick(_nick);
95     _nick = nick;
96     updateObjectName();
97     emit nickSet(nick);
98   }
99 }
100
101 void IrcUser::updateObjectName() {
102   QString oldName(objectName());
103   setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
104   if(!oldName.isEmpty()) {
105     emit renameObject(oldName, objectName());
106   }
107 }
108
109
110 void IrcUser::updateHostmask(const QString &mask) {
111   if(mask == hostmask())
112     return;
113
114   QString user = userFromMask(mask);
115   QString host = hostFromMask(mask);
116   setUser(user);
117   setHost(host);
118 }
119
120 void IrcUser::joinChannel(const QString &channel) {
121   if(!_channels.contains(channel)) {
122     _channels.insert(channel);
123     networkInfo->newIrcChannel(channel)->join(this);
124     emit channelJoined(channel);
125   }
126 }
127
128 void IrcUser::partChannel(const QString &channel) {
129   if(_channels.contains(channel)) {
130     _channels.remove(channel);
131
132     Q_ASSERT(networkInfo->ircChannel(channel));
133     networkInfo->ircChannel(channel)->part(this);
134     
135     emit channelParted(channel);
136   }
137 }
138
139 void IrcUser::setUserModes(const QString &modes) {
140   _userModes = modes;
141   emit userModesSet(modes);
142 }
143
144 void IrcUser::addUserMode(const QString &mode) {
145   if(!_userModes.contains(mode)) {
146     _userModes += mode;
147     emit userModeAdded(mode);
148   }
149 }
150
151 void IrcUser::removeUserMode(const QString &mode) {
152   if(_userModes.contains(mode)) {
153     _userModes.remove(mode);
154     emit userModeRemoved(mode);
155   }
156 }
157
158 void IrcUser::initSetChannels(const QStringList channels) {
159   foreach(QString channel, channels) {
160     joinChannel(channel);
161   }
162 }
163
164 void IrcUser::setInitialized() {
165   _initialized = true;
166   emit initDone();
167 }
168