Added ModelPropertyMapper which allows to keep track of /current/ changes in the...
[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   setObjectName(QString::number(networkInfo->networkId()) + "/" + IrcUser::hostmask());
39 }
40
41 // ====================
42 //  PUBLIC:
43 // ====================
44 bool IrcUser::initialized() const {
45   return _initialized;
46 }
47
48 QString IrcUser::user() const {
49   return _user;
50 }
51
52 QString IrcUser::host() const {
53   return _host;
54 }
55
56 QString IrcUser::nick() const {
57   return _nick;
58 }
59
60 QString IrcUser::hostmask() const {
61   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
62 }
63
64 QString IrcUser::userModes() const {
65   return _userModes;
66 }
67
68 QStringList IrcUser::channels() const {
69   return _channels.toList();
70 }
71
72 void IrcUser::updateObjectName() {
73   setObjectName(QString::number(networkInfo->networkId()) + "/" +  hostmask());
74   emit objectNameSet();
75 }
76
77 // ====================
78 //  PUBLIC SLOTS:
79 // ====================
80 void IrcUser::setUser(const QString &user) {
81   if(!user.isEmpty() && _user != user) {
82     _user = user;
83     emit userSet(user);
84
85     setObjectName(hostmask());
86     emit objectNameSet();
87   }
88 }
89
90 void IrcUser::setHost(const QString &host) {
91   if(!host.isEmpty() && _host != host) {
92     _host = host;
93     emit hostSet(host);
94     updateObjectName();
95   }
96 }
97
98 void IrcUser::setNick(const QString &nick) {
99   if(!nick.isEmpty() && nick != _nick) {
100     QString oldnick(_nick);
101     _nick = nick;
102     emit nickSet(nick);
103     updateObjectName();
104   }
105 }
106
107 void IrcUser::updateHostmask(const QString &mask) {
108   if(mask == hostmask())
109     return;
110
111   QString user = userFromMask(mask);
112   QString host = hostFromMask(mask);
113
114   // we only need to check user and hostmask.
115   // nick can't have changed since we're identifying IrcUsers by nick
116
117   // we don't use setUser and setHost here.
118   // though this is unpretty code duplication this saves us one emit objectNameSet()
119   // the second one would be erroneous
120   
121   if(!user.isEmpty() && _user != user) {
122     _user = user;
123   }
124
125   if(!host.isEmpty() && _host != host) {
126     _host = host;
127   }
128
129   emit hostmaskUpdated(mask);
130   updateObjectName();
131 }
132
133 void IrcUser::joinChannel(const QString &channel) {
134   if(!_channels.contains(channel)) {
135     _channels.insert(channel);
136     networkInfo->newIrcChannel(channel)->join(this);
137     emit channelJoined(channel);
138   }
139 }
140
141 void IrcUser::partChannel(const QString &channel) {
142   if(_channels.contains(channel)) {
143     _channels.remove(channel);
144
145     Q_ASSERT(networkInfo->ircChannel(channel));
146     networkInfo->ircChannel(channel)->part(this);
147     
148     emit channelParted(channel);
149   }
150 }
151
152 void IrcUser::setUserModes(const QString &modes) {
153   _userModes = modes;
154   emit userModesSet(modes);
155 }
156
157 void IrcUser::addUserMode(const QString &mode) {
158   if(!_userModes.contains(mode)) {
159     _userModes += mode;
160     emit userModeAdded(mode);
161   }
162 }
163
164 void IrcUser::removeUserMode(const QString &mode) {
165   if(_userModes.contains(mode)) {
166     _userModes.remove(mode);
167     emit userModeRemoved(mode);
168   }
169 }
170
171 void IrcUser::initSetChannels(const QStringList channels) {
172   foreach(QString channel, channels) {
173     joinChannel(channel);
174   }
175 }
176
177 void IrcUser::setInitialized() {
178   _initialized = true;
179   emit initDone();
180 }
181