1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
22 #include "signalproxy.h"
24 #include "ircchannel.h"
31 // ====================
33 // ====================
34 Network::Network(const NetworkId &networkid, QObject *parent) : SyncableObject(parent),
35 _networkId(networkid),
38 _networkName(QString("<not initialized>")),
39 _currentServer(QString()),
41 _prefixModes(QString()),
46 setObjectName(QString::number(networkid));
49 // I think this is unnecessary since IrcUsers have us as their daddy :)
50 //Network::~Network() {
51 // QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
52 // while (ircuser.hasNext()) {
54 // delete ircuser.value();
58 NetworkId Network::networkId() const {
62 SignalProxy *Network::proxy() const {
66 void Network::setProxy(SignalProxy *proxy) {
68 //proxy->synchronize(this); // we should to this explicitly from the outside!
71 bool Network::isMyNick(const QString &nick) const {
72 return (myNick().toLower() == nick.toLower());
75 bool Network::isMe(IrcUser *ircuser) const {
76 return (ircuser->nick().toLower() == myNick().toLower());
79 bool Network::isChannelName(const QString &channelname) const {
80 if(channelname.isEmpty())
83 if(supports("CHANTYPES"))
84 return support("CHANTYPES").contains(channelname[0]);
86 return QString("#&!+").contains(channelname[0]);
89 QString Network::prefixToMode(const QString &prefix) {
90 if(prefixes().contains(prefix))
91 return QString(prefixModes()[prefixes().indexOf(prefix)]);
96 QString Network::prefixToMode(const QCharRef &prefix) {
97 return prefixToMode(QString(prefix));
100 QString Network::modeToPrefix(const QString &mode) {
101 if(prefixModes().contains(mode))
102 return QString(prefixes()[prefixModes().indexOf(mode)]);
107 QString Network::modeToPrefix(const QCharRef &mode) {
108 return modeToPrefix(QString(mode));
111 QString Network::networkName() const {
115 QString Network::currentServer() const {
116 return _currentServer;
119 QString Network::myNick() const {
123 IdentityId Network::identity() const {
127 QStringList Network::nicks() const {
128 // we don't use _ircUsers.keys() since the keys may be
129 // not up to date after a nick change
131 foreach(IrcUser *ircuser, _ircUsers.values()) {
132 nicks << ircuser->nick();
137 QStringList Network::channels() const {
138 return _ircChannels.keys();
141 QList<QVariantMap> Network::serverList() const {
145 QString Network::prefixes() {
146 if(_prefixes.isNull())
152 QString Network::prefixModes() {
153 if(_prefixModes.isNull())
159 bool Network::supports(const QString ¶m) const {
160 return _supports.contains(param);
163 QString Network::support(const QString ¶m) const {
164 QString support_ = param.toUpper();
165 if(_supports.contains(support_))
166 return _supports[support_];
171 IrcUser *Network::newIrcUser(const QString &hostmask) {
172 QString nick(nickFromMask(hostmask).toLower());
173 if(!_ircUsers.contains(nick)) {
174 IrcUser *ircuser = new IrcUser(hostmask, this);
175 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
176 //if(isInitialized())
177 // ircuser->setInitialized();
179 proxy()->synchronize(ircuser);
181 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
183 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
184 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
185 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
186 _ircUsers[nick] = ircuser;
187 emit ircUserAdded(hostmask);
188 emit ircUserAdded(ircuser);
190 return _ircUsers[nick];
193 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
194 return newIrcUser(decodeString(hostmask));
197 void Network::removeIrcUser(IrcUser *ircuser) {
198 QString nick = _ircUsers.key(ircuser);
202 _ircUsers.remove(nick);
203 emit ircUserRemoved(nick);
204 emit ircUserRemoved(ircuser);
205 ircuser->deleteLater();
208 void Network::removeIrcUser(QString nick) {
210 if((ircuser = ircUser(nick)) != 0)
211 removeIrcUser(ircuser);
214 IrcUser *Network::ircUser(QString nickname) const {
215 nickname = nickname.toLower();
216 if(_ircUsers.contains(nickname))
217 return _ircUsers[nickname];
222 IrcUser *Network::ircUser(const QByteArray &nickname) const {
223 return ircUser(decodeString(nickname));
226 QList<IrcUser *> Network::ircUsers() const {
227 return _ircUsers.values();
230 quint32 Network::ircUserCount() const {
231 return _ircUsers.count();
234 IrcChannel *Network::newIrcChannel(const QString &channelname) {
235 if(!_ircChannels.contains(channelname.toLower())) {
236 IrcChannel *channel = new IrcChannel(channelname, this);
237 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
238 //if(isInitialized())
239 // channel->setInitialized();
242 proxy()->synchronize(channel);
244 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
246 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
247 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
248 _ircChannels[channelname.toLower()] = channel;
249 emit ircChannelAdded(channelname);
250 emit ircChannelAdded(channel);
252 return _ircChannels[channelname.toLower()];
255 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
256 return newIrcChannel(decodeString(channelname));
259 IrcChannel *Network::ircChannel(QString channelname) {
260 channelname = channelname.toLower();
261 if(_ircChannels.contains(channelname))
262 return _ircChannels[channelname];
267 IrcChannel *Network::ircChannel(const QByteArray &channelname) {
268 return ircChannel(decodeString(channelname));
272 QList<IrcChannel *> Network::ircChannels() const {
273 return _ircChannels.values();
276 quint32 Network::ircChannelCount() const {
277 return _ircChannels.count();
280 QByteArray Network::codecForEncoding() const {
281 if(_codecForEncoding) return _codecForEncoding->name();
285 void Network::setCodecForEncoding(const QByteArray &name) {
286 setCodecForEncoding(QTextCodec::codecForName(name));
289 void Network::setCodecForEncoding(QTextCodec *codec) {
290 _codecForEncoding = codec;
293 QByteArray Network::codecForDecoding() const {
294 if(_codecForDecoding) return _codecForDecoding->name();
295 else return QByteArray();
298 void Network::setCodecForDecoding(const QByteArray &name) {
299 setCodecForDecoding(QTextCodec::codecForName(name));
302 void Network::setCodecForDecoding(QTextCodec *codec) {
303 _codecForDecoding = codec;
306 QString Network::decodeString(const QByteArray &text) const {
307 return ::decodeString(text, _codecForDecoding);
310 QByteArray Network::encodeString(const QString string) const {
311 if(_codecForEncoding) {
312 return _codecForEncoding->fromUnicode(string);
314 return string.toAscii();
317 // ====================
319 // ====================
320 void Network::setNetworkName(const QString &networkName) {
321 _networkName = networkName;
322 emit networkNameSet(networkName);
325 void Network::setCurrentServer(const QString ¤tServer) {
326 _currentServer = currentServer;
327 emit currentServerSet(currentServer);
330 void Network::setMyNick(const QString &nickname) {
332 emit myNickSet(nickname);
335 void Network::setIdentity(IdentityId id) {
337 emit identitySet(id);
340 void Network::setServerList(const QList<QVariantMap> &serverList) {
341 _serverList = serverList;
342 emit serverListSet(serverList);
345 void Network::addSupport(const QString ¶m, const QString &value) {
346 if(!_supports.contains(param)) {
347 _supports[param] = value;
348 emit supportAdded(param, value);
352 void Network::removeSupport(const QString ¶m) {
353 if(_supports.contains(param)) {
354 _supports.remove(param);
355 emit supportRemoved(param);
359 QVariantMap Network::initSupports() const {
360 QVariantMap supports;
361 QHashIterator<QString, QString> iter(_supports);
362 while(iter.hasNext()) {
364 supports[iter.key()] = iter.value();
369 QVariantList Network::initServerList() const {
370 QList<QVariant> list;
371 foreach(QVariantMap serverdata, serverList()) list << QVariant(serverdata);
375 QStringList Network::initIrcUsers() const {
376 QStringList hostmasks;
377 foreach(IrcUser *ircuser, ircUsers()) {
378 hostmasks << ircuser->hostmask();
383 QStringList Network::initIrcChannels() const {
384 return _ircChannels.keys();
387 void Network::initSetSupports(const QVariantMap &supports) {
388 QMapIterator<QString, QVariant> iter(supports);
389 while(iter.hasNext()) {
391 addSupport(iter.key(), iter.value().toString());
395 void Network::initSetServerList(const QVariantList & serverList) {
396 QList<QVariantMap> slist;
397 foreach(QVariant v, serverList) slist << v.toMap();
398 setServerList(slist);
401 void Network::initSetIrcUsers(const QStringList &hostmasks) {
402 if(!_ircUsers.empty())
404 foreach(QString hostmask, hostmasks) {
405 newIrcUser(hostmask);
409 void Network::initSetChannels(const QStringList &channels) {
410 if(!_ircChannels.empty())
412 foreach(QString channel, channels)
413 newIrcChannel(channel);
416 IrcUser *Network::updateNickFromMask(const QString &mask) {
417 QString nick(nickFromMask(mask).toLower());
420 if(_ircUsers.contains(nick)) {
421 ircuser = _ircUsers[nick];
422 ircuser->updateHostmask(mask);
424 ircuser = newIrcUser(mask);
429 void Network::ircUserNickChanged(QString newnick) {
430 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
435 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
437 if(myNick().toLower() == oldnick)
441 void Network::ircUserInitDone() {
442 IrcUser *ircuser = static_cast<IrcUser *>(sender());
444 emit ircUserInitDone(ircuser);
447 void Network::ircChannelInitDone() {
448 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
449 Q_ASSERT(ircchannel);
450 emit ircChannelInitDone(ircchannel);
453 void Network::ircUserDestroyed() {
454 IrcUser *ircuser = static_cast<IrcUser *>(sender());
456 removeIrcUser(ircuser);
459 void Network::channelDestroyed() {
460 IrcChannel *channel = static_cast<IrcChannel *>(sender());
462 emit ircChannelRemoved(sender());
463 _ircChannels.remove(_ircChannels.key(channel));
466 void Network::requestConnect() {
468 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
469 else emit connectRequested(networkId()); // and this is for CoreSession :)
472 // ====================
474 // ====================
475 void Network::determinePrefixes() {
476 // seems like we have to construct them first
477 QString PREFIX = support("PREFIX");
479 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
480 _prefixes = PREFIX.section(")", 1);
481 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
483 QString defaultPrefixes("~&@%+");
484 QString defaultPrefixModes("qaohv");
486 // we just assume that in PREFIX are only prefix chars stored
487 for(int i = 0; i < defaultPrefixes.size(); i++) {
488 if(PREFIX.contains(defaultPrefixes[i])) {
489 _prefixes += defaultPrefixes[i];
490 _prefixModes += defaultPrefixModes[i];
494 if(!_prefixes.isNull())
497 // well... our assumption was obviously wrong...
498 // check if it's only prefix modes
499 for(int i = 0; i < defaultPrefixes.size(); i++) {
500 if(PREFIX.contains(defaultPrefixModes[i])) {
501 _prefixes += defaultPrefixes[i];
502 _prefixModes += defaultPrefixModes[i];
505 // now we've done all we've could...