Various stuff, cosmetic fixes, fiddling with IrcUsers and NetworkInfos in Buffers.
[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 IrcChannel::~IrcChannel() {
44
45 }
46
47 // ====================
48 //  PUBLIC:
49 // ====================
50 bool IrcChannel::isKnownUser(IrcUser *ircuser) const {
51   bool isknown = true;
52
53   if(ircuser == 0) {
54     qWarning() << "Channel" << name() << "received IrcUser Nullpointer!";
55     isknown = false;
56   }
57   
58   if(!_userModes.contains(ircuser) && ircuser) {
59     qWarning() << "Channel" << name() << "received data for unknown User" << ircuser->nick();
60     isknown = false;
61   }
62   
63   return isknown;
64 }
65
66 bool IrcChannel::isValidChannelUserMode(const QString &mode) const {
67   bool isvalid = true;
68   if(mode.size() > 1) {
69     qWarning() << "Channel" << name() << "received Channel User Mode which is longer then 1 Char:" << mode;
70     isvalid = false;
71   }
72   return isvalid;
73 }
74
75 bool IrcChannel::initialized() const {
76   return _initialized;
77 }
78
79 QString IrcChannel::name() const {
80   return _name;
81 }
82
83 QString IrcChannel::topic() const {
84   return _topic;
85 }
86
87 QList<IrcUser *> IrcChannel::ircUsers() const {
88   return _userModes.keys();
89 }
90
91 QString IrcChannel::userMode(IrcUser *ircuser) const {
92   if(_userModes.contains(ircuser))
93     return _userModes[ircuser];
94   else
95     return QString();
96 }
97
98 QString IrcChannel::userMode(const QString &nick) const {
99   return userMode(networkInfo->ircUser(nick));
100 }
101
102 // ====================
103 //  PUBLIC SLOTS:
104 // ====================
105 void IrcChannel::setTopic(const QString &topic) {
106   _topic = topic;
107   emit topicSet(topic);
108 }
109
110 void IrcChannel::join(IrcUser *ircuser) {
111   if(!_userModes.contains(ircuser) && ircuser) {
112     _userModes[ircuser] = QString();
113     ircuser->joinChannel(name());
114     connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
115     // if you wonder why there is no counterpart to ircUserJoined:
116     // the joines are propagted by the ircuser. the signal ircUserJoined is only for convenience
117     emit ircUserJoined(ircuser);
118   }
119 }
120
121 void IrcChannel::join(const QString &nick) {
122   join(networkInfo->ircUser(nick));
123 }
124
125 void IrcChannel::part(IrcUser *ircuser) {
126   if(isKnownUser(ircuser)) {
127     _userModes.remove(ircuser);
128     ircuser->partChannel(name());
129     // if you wonder why there is no counterpart to ircUserParted:
130     // the joines are propagted by the ircuser. the signal ircUserParted is only for convenience
131     emit ircUserParted(ircuser);
132   }
133 }
134
135 void IrcChannel::part(const QString &nick) {
136   part(networkInfo->ircUser(nick));
137 }
138
139 // SET USER MODE
140 void IrcChannel::setUserModes(IrcUser *ircuser, const QString &modes) {
141   if(isKnownUser(ircuser)) {
142     _userModes[ircuser] = modes;
143     emit userModesSet(ircuser->nick(), modes);
144   }
145 }
146
147 void IrcChannel::setUserModes(const QString &nick, const QString &modes) {
148   setUserModes(networkInfo->ircUser(nick), modes);
149 }
150
151 // ADD USER MODE
152 void IrcChannel::addUserMode(IrcUser *ircuser, const QString &mode) {
153   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
154     return;
155   
156   if(!_userModes[ircuser].contains(mode)) {
157     _userModes[ircuser] += mode;
158     emit userModeAdded(ircuser->nick(), mode);
159   }
160
161 }
162
163 void IrcChannel::addUserMode(const QString &nick, const QString &mode) {
164   addUserMode(networkInfo->ircUser(nick), mode);
165 }
166
167
168 // REMOVE USER MODE
169 void IrcChannel::removeUserMode(IrcUser *ircuser, const QString &mode) {
170   if(!isKnownUser(ircuser) || !isValidChannelUserMode(mode))
171     return;
172
173   if(_userModes[ircuser].contains(mode)) {
174     _userModes[ircuser].remove(mode);
175     emit userModeRemoved(ircuser->nick(), mode);
176   }
177
178 }
179
180 void IrcChannel::removeUserMode(const QString &nick, const QString &mode) {
181   removeUserMode(networkInfo->ircUser(nick), mode);
182 }
183
184 // INIT SET USER MODES
185 QVariantMap IrcChannel::initUserModes() const {
186   QVariantMap usermodes;
187   QHash<IrcUser *, QString>::const_iterator iter = _userModes.constBegin();
188   while(iter != _userModes.constEnd()) {
189     usermodes[iter.key()->nick()] = iter.value();
190     iter++;
191   }
192   return usermodes;
193 }
194
195 void IrcChannel::initSetUserModes(const QVariantMap &usermodes) {
196   QMapIterator<QString, QVariant> iter(usermodes);
197   while(iter.hasNext()) {
198     iter.next();
199     setUserModes(iter.key(), iter.value().toString());
200   }
201 }
202
203 void IrcChannel::ircUserDestroyed() {
204   IrcUser *ircUser = static_cast<IrcUser *>(sender());
205   Q_ASSERT(ircUser);
206   _userModes.remove(ircUser);
207 }
208
209 void IrcChannel::setInitialized() {
210   _initialized = true;
211   emit initDone();
212 }
213