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