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 _connectionState(Disconnected),
43 _prefixModes(QString()),
48 setObjectName(QString::number(networkid.toInt()));
51 // I think this is unnecessary since IrcUsers have us as their daddy :)
54 // QHashIterator<QString, IrcUser *> ircuser(_ircUsers);
55 // while (ircuser.hasNext()) {
57 // delete ircuser.value();
59 // qDebug() << "Destroying net" << networkName() << networkId();
63 NetworkId Network::networkId() const {
67 SignalProxy *Network::proxy() const {
71 void Network::setProxy(SignalProxy *proxy) {
73 //proxy->synchronize(this); // we should to this explicitly from the outside!
76 bool Network::isMyNick(const QString &nick) const {
77 return (myNick().toLower() == nick.toLower());
80 bool Network::isMe(IrcUser *ircuser) const {
81 return (ircuser->nick().toLower() == myNick().toLower());
84 bool Network::isChannelName(const QString &channelname) const {
85 if(channelname.isEmpty())
88 if(supports("CHANTYPES"))
89 return support("CHANTYPES").contains(channelname[0]);
91 return QString("#&!+").contains(channelname[0]);
94 bool Network::isConnected() const {
98 //Network::ConnectionState Network::connectionState() const {
99 int Network::connectionState() const {
100 return _connectionState;
103 NetworkInfo Network::networkInfo() const {
105 info.networkName = networkName();
106 info.networkId = networkId();
107 info.identity = identity();
108 info.codecForEncoding = codecForEncoding();
109 info.codecForDecoding = codecForDecoding();
110 info.serverList = serverList();
114 void Network::setNetworkInfo(const NetworkInfo &info) {
115 // we don't set our ID!
116 if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
117 if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
118 if(!info.codecForEncoding.isEmpty() && info.codecForEncoding != codecForEncoding())
119 setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
120 if(!info.codecForDecoding.isEmpty() && info.codecForDecoding != codecForDecoding())
121 setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
122 if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
125 QString Network::prefixToMode(const QString &prefix) {
126 if(prefixes().contains(prefix))
127 return QString(prefixModes()[prefixes().indexOf(prefix)]);
132 QString Network::prefixToMode(const QCharRef &prefix) {
133 return prefixToMode(QString(prefix));
136 QString Network::modeToPrefix(const QString &mode) {
137 if(prefixModes().contains(mode))
138 return QString(prefixes()[prefixModes().indexOf(mode)]);
143 QString Network::modeToPrefix(const QCharRef &mode) {
144 return modeToPrefix(QString(mode));
147 QString Network::networkName() const {
151 QString Network::currentServer() const {
152 return _currentServer;
155 QString Network::myNick() const {
159 IdentityId Network::identity() const {
163 QStringList Network::nicks() const {
164 // we don't use _ircUsers.keys() since the keys may be
165 // not up to date after a nick change
167 foreach(IrcUser *ircuser, _ircUsers.values()) {
168 nicks << ircuser->nick();
173 QStringList Network::channels() const {
174 return _ircChannels.keys();
177 QVariantList Network::serverList() const {
181 QString Network::prefixes() {
182 if(_prefixes.isNull())
188 QString Network::prefixModes() {
189 if(_prefixModes.isNull())
195 bool Network::supports(const QString ¶m) const {
196 return _supports.contains(param);
199 QString Network::support(const QString ¶m) const {
200 QString support_ = param.toUpper();
201 if(_supports.contains(support_))
202 return _supports[support_];
207 IrcUser *Network::newIrcUser(const QString &hostmask) {
208 QString nick(nickFromMask(hostmask).toLower());
209 if(!_ircUsers.contains(nick)) {
210 IrcUser *ircuser = new IrcUser(hostmask, this);
211 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
212 //if(isInitialized())
213 // ircuser->setInitialized();
215 proxy()->synchronize(ircuser);
217 qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
219 connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
220 connect(ircuser, SIGNAL(initDone()), this, SLOT(ircUserInitDone()));
221 connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
222 _ircUsers[nick] = ircuser;
223 emit ircUserAdded(hostmask);
224 emit ircUserAdded(ircuser);
226 return _ircUsers[nick];
229 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
230 return newIrcUser(decodeString(hostmask));
233 void Network::removeIrcUser(IrcUser *ircuser) {
234 QString nick = _ircUsers.key(ircuser);
238 _ircUsers.remove(nick);
239 disconnect(ircuser, 0, this, 0);
240 emit ircUserRemoved(nick);
241 emit ircUserRemoved(ircuser);
242 ircuser->deleteLater();
245 void Network::removeIrcUser(QString nick) {
247 if((ircuser = ircUser(nick)) != 0)
248 removeIrcUser(ircuser);
251 IrcUser *Network::ircUser(QString nickname) const {
252 nickname = nickname.toLower();
253 if(_ircUsers.contains(nickname))
254 return _ircUsers[nickname];
259 IrcUser *Network::ircUser(const QByteArray &nickname) const {
260 return ircUser(decodeString(nickname));
263 QList<IrcUser *> Network::ircUsers() const {
264 return _ircUsers.values();
267 quint32 Network::ircUserCount() const {
268 return _ircUsers.count();
271 IrcChannel *Network::newIrcChannel(const QString &channelname) {
272 if(!_ircChannels.contains(channelname.toLower())) {
273 IrcChannel *channel = new IrcChannel(channelname, this);
274 // mark IrcUser as already initialized to keep the SignalProxy from requesting initData
275 //if(isInitialized())
276 // channel->setInitialized();
279 proxy()->synchronize(channel);
281 qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
283 connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
284 connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
285 _ircChannels[channelname.toLower()] = channel;
286 emit ircChannelAdded(channelname);
287 emit ircChannelAdded(channel);
289 return _ircChannels[channelname.toLower()];
292 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
293 return newIrcChannel(decodeString(channelname));
296 IrcChannel *Network::ircChannel(QString channelname) const {
297 channelname = channelname.toLower();
298 if(_ircChannels.contains(channelname))
299 return _ircChannels[channelname];
304 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
305 return ircChannel(decodeString(channelname));
309 QList<IrcChannel *> Network::ircChannels() const {
310 return _ircChannels.values();
313 quint32 Network::ircChannelCount() const {
314 return _ircChannels.count();
317 QByteArray Network::codecForEncoding() const {
318 if(_codecForEncoding) return _codecForEncoding->name();
322 void Network::setCodecForEncoding(const QByteArray &name) {
323 setCodecForEncoding(QTextCodec::codecForName(name));
326 void Network::setCodecForEncoding(QTextCodec *codec) {
327 _codecForEncoding = codec;
328 emit codecForEncodingSet(codecForEncoding());
331 QByteArray Network::codecForDecoding() const {
332 if(_codecForDecoding) return _codecForDecoding->name();
333 else return QByteArray();
336 void Network::setCodecForDecoding(const QByteArray &name) {
337 setCodecForDecoding(QTextCodec::codecForName(name));
340 void Network::setCodecForDecoding(QTextCodec *codec) {
341 _codecForDecoding = codec;
342 emit codecForDecodingSet(codecForDecoding());
345 QString Network::decodeString(const QByteArray &text) const {
346 return ::decodeString(text, _codecForDecoding);
349 QByteArray Network::encodeString(const QString string) const {
350 if(_codecForEncoding) {
351 return _codecForEncoding->fromUnicode(string);
353 return string.toAscii();
356 // ====================
358 // ====================
359 void Network::setNetworkName(const QString &networkName) {
360 _networkName = networkName;
361 emit networkNameSet(networkName);
364 void Network::setCurrentServer(const QString ¤tServer) {
365 _currentServer = currentServer;
366 emit currentServerSet(currentServer);
369 void Network::setConnected(bool connected) {
370 _connected = connected;
371 emit connectedSet(connected);
374 //void Network::setConnectionState(ConnectionState state) {
375 void Network::setConnectionState(int state) {
376 _connectionState = (ConnectionState)state;
377 //qDebug() << "netstate" << networkId() << networkName() << state;
378 emit connectionStateSet(state);
379 emit connectionStateSet(_connectionState);
382 void Network::setMyNick(const QString &nickname) {
384 emit myNickSet(nickname);
387 void Network::setIdentity(IdentityId id) {
389 emit identitySet(id);
392 void Network::setServerList(const QVariantList &serverList) {
393 _serverList = serverList;
394 emit serverListSet(serverList);
397 void Network::addSupport(const QString ¶m, const QString &value) {
398 if(!_supports.contains(param)) {
399 _supports[param] = value;
400 emit supportAdded(param, value);
404 void Network::removeSupport(const QString ¶m) {
405 if(_supports.contains(param)) {
406 _supports.remove(param);
407 emit supportRemoved(param);
411 QVariantMap Network::initSupports() const {
412 QVariantMap supports;
413 QHashIterator<QString, QString> iter(_supports);
414 while(iter.hasNext()) {
416 supports[iter.key()] = iter.value();
421 QVariantList Network::initServerList() const {
425 QStringList Network::initIrcUsers() const {
426 QStringList hostmasks;
427 foreach(IrcUser *ircuser, ircUsers()) {
428 hostmasks << ircuser->hostmask();
433 QStringList Network::initIrcChannels() const {
434 return _ircChannels.keys();
437 void Network::initSetSupports(const QVariantMap &supports) {
438 QMapIterator<QString, QVariant> iter(supports);
439 while(iter.hasNext()) {
441 addSupport(iter.key(), iter.value().toString());
445 void Network::initSetServerList(const QVariantList & serverList) {
446 setServerList(serverList);
449 void Network::initSetIrcUsers(const QStringList &hostmasks) {
450 if(!_ircUsers.empty())
452 foreach(QString hostmask, hostmasks) {
453 newIrcUser(hostmask);
457 void Network::initSetChannels(const QStringList &channels) {
458 if(!_ircChannels.empty())
460 foreach(QString channel, channels)
461 newIrcChannel(channel);
464 IrcUser *Network::updateNickFromMask(const QString &mask) {
465 QString nick(nickFromMask(mask).toLower());
468 if(_ircUsers.contains(nick)) {
469 ircuser = _ircUsers[nick];
470 ircuser->updateHostmask(mask);
472 ircuser = newIrcUser(mask);
477 void Network::ircUserNickChanged(QString newnick) {
478 QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
483 if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
485 if(myNick().toLower() == oldnick)
489 void Network::ircUserInitDone() {
490 IrcUser *ircuser = static_cast<IrcUser *>(sender());
492 emit ircUserInitDone(ircuser);
495 void Network::ircChannelInitDone() {
496 IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
497 Q_ASSERT(ircchannel);
498 emit ircChannelInitDone(ircchannel);
501 void Network::ircUserDestroyed() {
502 IrcUser *ircuser = static_cast<IrcUser *>(sender());
504 removeIrcUser(ircuser);
507 void Network::channelDestroyed() {
508 IrcChannel *channel = static_cast<IrcChannel *>(sender());
510 emit ircChannelRemoved(sender());
511 _ircChannels.remove(_ircChannels.key(channel));
514 void Network::requestConnect() const {
516 if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
518 if(connectionState() != Disconnected) {
519 qWarning() << "Requesting connect while not being disconnected!";
522 emit connectRequested(networkId()); // and this is for CoreSession :)
526 void Network::requestDisconnect() const {
528 if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
530 if(connectionState() == Disconnected) {
531 qWarning() << "Requesting disconnect while not being connected!";
534 emit disconnectRequested(networkId()); // and this is for CoreSession :)
538 void Network::emitConnectionError(const QString &errorMsg) {
539 emit connectionError(errorMsg);
542 // ====================
544 // ====================
545 void Network::determinePrefixes() {
546 // seems like we have to construct them first
547 QString PREFIX = support("PREFIX");
549 if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
550 _prefixes = PREFIX.section(")", 1);
551 _prefixModes = PREFIX.mid(1).section(")", 0, 0);
553 QString defaultPrefixes("~&@%+");
554 QString defaultPrefixModes("qaohv");
556 // we just assume that in PREFIX are only prefix chars stored
557 for(int i = 0; i < defaultPrefixes.size(); i++) {
558 if(PREFIX.contains(defaultPrefixes[i])) {
559 _prefixes += defaultPrefixes[i];
560 _prefixModes += defaultPrefixModes[i];
564 if(!_prefixes.isNull())
567 // well... our assumption was obviously wrong...
568 // check if it's only prefix modes
569 for(int i = 0; i < defaultPrefixes.size(); i++) {
570 if(PREFIX.contains(defaultPrefixModes[i])) {
571 _prefixes += defaultPrefixes[i];
572 _prefixModes += defaultPrefixModes[i];
575 // now we've done all we've could...
579 /************************************************************************
581 ************************************************************************/
583 bool NetworkInfo::operator==(const NetworkInfo &other) const {
584 if(networkId != other.networkId) return false;
585 if(networkName != other.networkName) return false;
586 if(identity != other.identity) return false;
587 if(codecForEncoding != other.codecForEncoding) return false;
588 if(codecForDecoding != other.codecForDecoding) return false;
589 if(serverList != other.serverList) return false;
593 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
594 return !(*this == other);
597 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
599 i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
600 i["NetworkName"] = info.networkName;
601 i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
602 i["CodecForEncoding"] = info.codecForEncoding;
603 i["CodecForDecoding"] = info.codecForDecoding;
604 i["ServerList"] = info.serverList;
609 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
612 info.networkId = i["NetworkId"].value<NetworkId>();
613 info.networkName = i["NetworkName"].toString();
614 info.identity = i["Identity"].value<IdentityId>();
615 info.codecForEncoding = i["CodecForEncoding"].toByteArray();
616 info.codecForDecoding = i["CodecForDecoding"].toByteArray();
617 info.serverList = i["ServerList"].toList();