Make BufferInfo qDebug()able as per EgS' request.
[quassel.git] / src / common / ircuser.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC 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) 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 "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   //qDebug() << nick() << "destroyed.";
43 }
44
45 // ====================
46 //  PUBLIC:
47 // ====================
48 bool IrcUser::initialized() const {
49   return _initialized;
50 }
51
52 QString IrcUser::user() const {
53   return _user;
54 }
55
56 QString IrcUser::host() const {
57   return _host;
58 }
59
60 QString IrcUser::nick() const {
61   return _nick;
62 }
63
64 QString IrcUser::hostmask() const {
65   return QString("%1!%2@%3").arg(nick()).arg(user()).arg(host());
66 }
67
68 QString IrcUser::userModes() const {
69   return _userModes;
70 }
71
72 QStringList IrcUser::channels() const {
73   QStringList chanList;
74   IrcChannel *channel;
75   foreach(channel, _channels) {
76     chanList << channel->name();
77   }
78   return chanList;
79 }
80
81 // ====================
82 //  PUBLIC SLOTS:
83 // ====================
84 void IrcUser::setUser(const QString &user) {
85   if(!user.isEmpty() && _user != user) {
86     _user = user;
87     emit userSet(user);
88   }
89 }
90
91 void IrcUser::setHost(const QString &host) {
92   if(!host.isEmpty() && _host != host) {
93     _host = host;
94     emit hostSet(host);
95   }
96 }
97
98 void IrcUser::setNick(const QString &nick) {
99   if(!nick.isEmpty() && nick != _nick) {
100     QString oldnick(_nick);
101     _nick = nick;
102     updateObjectName();
103     emit nickSet(nick);
104   }
105 }
106
107 void IrcUser::updateObjectName() {
108   QString oldName(objectName());
109   setObjectName(QString::number(networkInfo->networkId()) + "/" + _nick);
110   if(!oldName.isEmpty()) {
111     emit renameObject(oldName, objectName());
112   }
113 }
114
115
116 void IrcUser::updateHostmask(const QString &mask) {
117   if(mask == hostmask())
118     return;
119
120   QString user = userFromMask(mask);
121   QString host = hostFromMask(mask);
122   setUser(user);
123   setHost(host);
124 }
125
126 void IrcUser::joinChannel(IrcChannel *channel) {
127   Q_ASSERT(channel);
128   if(!_channels.contains(channel)) {
129     channel->join(this);
130     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
131     _channels.insert(channel);
132     emit channelJoined(channel->name());
133   }
134 }
135
136 void IrcUser::joinChannel(const QString &channelname) {
137   joinChannel(networkInfo->newIrcChannel(channelname));
138 }
139
140 void IrcUser::partChannel(IrcChannel *channel) {
141   if(_channels.contains(channel)) {
142     _channels.remove(channel);
143     disconnect(channel, 0, this, 0);
144     channel->part(this);
145     emit channelParted(channel->name());
146   }
147 }
148
149 void IrcUser::partChannel(const QString &channelname) {
150   IrcChannel *channel = networkInfo->ircChannel(channelname);
151   if(channel == 0) {
152     qWarning() << "IrcUser::partChannel(): received part for unknown Channel" << channelname;
153   } else {
154     partChannel(channel);
155   }
156 }
157
158 void IrcUser::channelDestroyed() {
159   // private slot!
160   IrcChannel *channel = static_cast<IrcChannel*>(sender());
161   Q_ASSERT(channel);
162   if(_channels.contains(channel)) {
163     _channels.remove(channel);
164     disconnect(channel, 0, this, 0);
165   }
166 }
167
168 void IrcUser::setUserModes(const QString &modes) {
169   _userModes = modes;
170   emit userModesSet(modes);
171 }
172
173 void IrcUser::addUserMode(const QString &mode) {
174   if(!_userModes.contains(mode)) {
175     _userModes += mode;
176     emit userModeAdded(mode);
177   }
178 }
179
180 void IrcUser::removeUserMode(const QString &mode) {
181   if(_userModes.contains(mode)) {
182     _userModes.remove(mode);
183     emit userModeRemoved(mode);
184   }
185 }
186
187 void IrcUser::initSetChannels(const QStringList channels) {
188   foreach(QString channel, channels) {
189     joinChannel(channel);
190   }
191 }
192
193 void IrcUser::setInitialized() {
194   _initialized = true;
195   emit initDone();
196 }
197