We now have a current svn snapshot of libqxt in our contrib dir, and
[quassel.git] / src / common / ircchannel.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 "ircchannel.h"
22
23 #include "networkinfo.h"
24 #include "signalproxy.h"
25 #include "ircuser.h"
26
27 #include <QMapIterator>
28 #include <QHashIterator>
29
30 #include <QDebug>
31
32
33 IrcChannel::IrcChannel(const QString &channelname, NetworkInfo *networkinfo) 
34   : QObject(networkinfo),
35     _initialized(false),
36     _name(channelname),
37     _topic(QString()),
38     networkInfo(networkinfo)
39 {
40   setObjectName(QString::number(networkInfo->networkId()) + "/" +  channelname);
41 }
42
43 // ====================
44 //  PUBLIC:
45 // ====================
46 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
47   bool isknown = true;
48
49   if(ircuser == 0) {
50     qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
51     isknown = false;
52   }
53   
54   if(!_userModes.contains(ircuser) && ircuser) {
55     qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
56     isknown = false;
57   }
58   
59   return isknown;
60 }
61
62 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
63   bool isvalid = true;
64   if(mode.size() > 1) {
65     qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
66     isvalid = false;
67   }
68   return isvalid;
69 }
70
71 bool IrcChannel::initialized() const {
72   return _initialized;
73 }
74
75 QString IrcChannel::name() const {
76   return _name;
77 }
78
79 QString IrcChannel::topic() const {
80   return _topic;
81 }
82
83 QList<IrcUser *> IrcChannel::ircUsers() const {
84   return _userModes.keys();
85 }
86
87 QString IrcChannel::userMode(IrcUser *ircuser) const {
88   if(_userModes.contains(ircuser))
89     return _userModes[ircuser];
90   else
91     return QString();
92 }
93
94 QString IrcChannel::userMode(const QString &nick) const {
95   return userMode(networkInfo->ircUser(nick));
96 }
97
98 // ====================
99 //  PUBLIC SLOTS:
100 // ====================
101 void IrcChannel::setTopic(const QString &topic) {
102   _topic = topic;
103   emit topicSet(topic);
104 }
105
106 void IrcChannel::join(IrcUser *ircuser) {
107   if(!_userModes.contains(ircuser) && ircuser) {
108     _userModes[ircuser] = QString();
109     ircuser->joinChannel(name());
110     // no emit here since the join is propagated by IrcUser
111   }
112 }
113
114 void IrcChannel::join(const QString &nick) {
115   join(networkInfo->ircUser(nick));
116 }
117
118 void IrcChannel::part(IrcUser *ircuser) {
119   if(isKnownUser(ircuser)) {
120     _userModes.remove(ircuser);
121     ircuser->partChannel(name());
122     // no emit here since the part is propagated by IrcUser
123   }
124 }
125
126 void IrcChannel::part(const QString &nick) {
127   part(networkInfo->ircUser(nick));
128 }
129
130 // SET USER MODE
131 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
132   if(isKnownUser(ircuser)) {
133     _userModes[ircuser] = modes;
134     emit userModesSet(ircuser->nick(), modes);
135   }
136 }
137
138 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
139   setUserModes(networkInfo->ircUser(nick), modes);
140 }
141
142 // ADD USER MODE
143 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
144   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
145     return;
146   
147   if(!_userModes[ircuser].contains(mode)) {
148     _userModes[ircuser] += mode;
149     emit userModeAdded(ircuser->nick(), mode);
150   }
151
152 }
153
154 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
155   addUserMode(networkInfo->ircUser(nick), mode);
156 }
157
158
159 // REMOVE USER MODE
160 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
161   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
162     return;
163
164   if(_userModes[ircuser].contains(mode)) {
165     _userModes[ircuser].remove(mode);
166     emit userModeRemoved(ircuser->nick(), mode);
167   }
168
169 }
170
171 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
172   removeUserMode(networkInfo->ircUser(nick), mode);
173 }
174
175 // INIT SET USER MODES
176 QVariantMap IrcChannel::initUserModes() const {
177   QVariantMap usermodes;
178   QHashIterator<IrcUser *, QString> iter(_userModes);
179   while(iter.hasNext()) {
180     iter.next();
181     usermodes[iter.key()->nick()] = iter.value();
182   }
183   return usermodes;
184 }
185
186 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
187   QMapIterator<QString, QVariant> iter(usermodes);
188   while(iter.hasNext()) {
189     iter.next();
190     setUserModes(iter.key(), iter.value().toString());
191   }
192 }
193
194 void IrcChannel::ircUserDestroyed() {
195   IrcUser *ircUser = static_cast<IrcUser *>(sender());
196   Q_ASSERT(ircUser);
197   part(ircUser);
198 }
199
200 void IrcChannel::setInitialized() {
201   _initialized = true;
202   emit initDone();
203 }
204