df2a8376788417225175b6854ea61040be960cf0
[quassel.git] / src / common / network.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
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) version 3.                                           *
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 #include "network.h"
21
22 #include <QDebug>
23 #include <QTextCodec>
24
25 #include "util.h"
26
27 QTextCodec *Network::_defaultCodecForServer = 0;
28 QTextCodec *Network::_defaultCodecForEncoding = 0;
29 QTextCodec *Network::_defaultCodecForDecoding = 0;
30
31 Network::Server Network::Server::fromVariant(const QVariant &variant) {
32   QVariantMap serverMap = variant.toMap();
33   return Server(serverMap["Host"].toString(), serverMap["Port"].toUInt(), serverMap["Password"].toString(), serverMap["UseSSL"].toBool());
34 }
35
36 QVariant Network::Server::toVariant() const {
37   QVariantMap serverMap;
38   serverMap["Host"] = host;
39   serverMap["Port"] = port;
40   serverMap["Password"] = password;
41   serverMap["UseSSL"] = useSsl;
42   return QVariant::fromValue<QVariantMap>(serverMap);
43 }
44
45 // ====================
46 //  Public:
47 // ====================
48 Network::Network(const NetworkId &networkid, QObject *parent)
49   : SyncableObject(parent),
50     _proxy(0),
51     _networkId(networkid),
52     _identity(0),
53     _myNick(QString()),
54     _latency(0),
55     _networkName(QString("<not initialized>")),
56     _currentServer(QString()),
57     _connected(false),
58     _connectionState(Disconnected),
59     _prefixes(QString()),
60     _prefixModes(QString()),
61     _useRandomServer(false),
62     _useAutoIdentify(false),
63     _useAutoReconnect(false),
64     _autoReconnectInterval(60),
65     _autoReconnectRetries(10),
66     _unlimitedReconnectRetries(false),
67     _codecForServer(0),
68     _codecForEncoding(0),
69     _codecForDecoding(0),
70     _autoAwayActive(false)
71 {
72   setObjectName(QString::number(networkid.toInt()));
73 }
74
75 Network::~Network() {
76   emit aboutToBeDestroyed();
77 }
78
79 bool Network::isChannelName(const QString &channelname) const {
80   if(channelname.isEmpty())
81     return false;
82
83   if(supports("CHANTYPES"))
84     return support("CHANTYPES").contains(channelname[0]);
85   else
86     return QString("#&!+").contains(channelname[0]);
87 }
88
89 NetworkInfo Network::networkInfo() const {
90   NetworkInfo info;
91   info.networkName = networkName();
92   info.networkId = networkId();
93   info.identity = identity();
94   info.codecForServer = codecForServer();
95   info.codecForEncoding = codecForEncoding();
96   info.codecForDecoding = codecForDecoding();
97   info.serverList = variantServerList();
98   info.useRandomServer = useRandomServer();
99   info.perform = perform();
100   info.useAutoIdentify = useAutoIdentify();
101   info.autoIdentifyService = autoIdentifyService();
102   info.autoIdentifyPassword = autoIdentifyPassword();
103   info.useAutoReconnect = useAutoReconnect();
104   info.autoReconnectInterval = autoReconnectInterval();
105   info.autoReconnectRetries = autoReconnectRetries();
106   info.unlimitedReconnectRetries = unlimitedReconnectRetries();
107   info.rejoinChannels = rejoinChannels();
108   return info;
109 }
110
111 void Network::setNetworkInfo(const NetworkInfo &info) {
112   // we don't set our ID!
113   if(!info.networkName.isEmpty() && info.networkName != networkName()) setNetworkName(info.networkName);
114   if(info.identity > 0 && info.identity != identity()) setIdentity(info.identity);
115   if(info.codecForServer != codecForServer()) setCodecForServer(QTextCodec::codecForName(info.codecForServer));
116   if(info.codecForEncoding != codecForEncoding()) setCodecForEncoding(QTextCodec::codecForName(info.codecForEncoding));
117   if(info.codecForDecoding != codecForDecoding()) setCodecForDecoding(QTextCodec::codecForName(info.codecForDecoding));
118   if(info.serverList.count()) setServerList(info.serverList); // FIXME compare components
119   if(info.useRandomServer != useRandomServer()) setUseRandomServer(info.useRandomServer);
120   if(info.perform != perform()) setPerform(info.perform);
121   if(info.useAutoIdentify != useAutoIdentify()) setUseAutoIdentify(info.useAutoIdentify);
122   if(info.autoIdentifyService != autoIdentifyService()) setAutoIdentifyService(info.autoIdentifyService);
123   if(info.autoIdentifyPassword != autoIdentifyPassword()) setAutoIdentifyPassword(info.autoIdentifyPassword);
124   if(info.useAutoReconnect != useAutoReconnect()) setUseAutoReconnect(info.useAutoReconnect);
125   if(info.autoReconnectInterval != autoReconnectInterval()) setAutoReconnectInterval(info.autoReconnectInterval);
126   if(info.autoReconnectRetries != autoReconnectRetries()) setAutoReconnectRetries(info.autoReconnectRetries);
127   if(info.unlimitedReconnectRetries != unlimitedReconnectRetries()) setUnlimitedReconnectRetries(info.unlimitedReconnectRetries);
128   if(info.rejoinChannels != rejoinChannels()) setRejoinChannels(info.rejoinChannels);
129 }
130
131 QString Network::prefixToMode(const QString &prefix) {
132   if(prefixes().contains(prefix))
133     return QString(prefixModes()[prefixes().indexOf(prefix)]);
134   else
135     return QString();
136 }
137
138 QString Network::modeToPrefix(const QString &mode) {
139   if(prefixModes().contains(mode))
140     return QString(prefixes()[prefixModes().indexOf(mode)]);
141   else
142     return QString();
143 }
144
145 QStringList Network::nicks() const {
146   // we don't use _ircUsers.keys() since the keys may be
147   // not up to date after a nick change
148   QStringList nicks;
149   foreach(IrcUser *ircuser, _ircUsers.values()) {
150     nicks << ircuser->nick();
151   }
152   return nicks;
153 }
154
155 QVariantList Network::variantServerList() const {
156   QVariantList servers;
157   ServerList::const_iterator serverIter = _serverList.constBegin();
158   while(serverIter != _serverList.constEnd()) {
159     servers << serverIter->toVariant();
160     serverIter++;
161   }
162   return servers;
163 }
164
165 QString Network::prefixes() {
166   if(_prefixes.isNull())
167     determinePrefixes();
168
169   return _prefixes;
170 }
171
172 QString Network::prefixModes() {
173   if(_prefixModes.isNull())
174     determinePrefixes();
175
176   return _prefixModes;
177 }
178
179 // example Unreal IRCD: CHANMODES=beI,kfL,lj,psmntirRcOAQKVCuzNSMTG
180 Network::ChannelModeType Network::channelModeType(const QString &mode) {
181   if(mode.isEmpty())
182     return NOT_A_CHANMODE;
183
184   QString chanmodes = support("CHANMODES");
185   if(chanmodes.isEmpty())
186     return NOT_A_CHANMODE;
187
188   ChannelModeType modeType = A_CHANMODE;
189   for(int i = 0; i < chanmodes.count(); i++) {
190     if(chanmodes[i] == mode[0])
191       break;
192     else if(chanmodes[i] == ',')
193       modeType = (ChannelModeType)(modeType << 1);
194   }
195   if(modeType > D_CHANMODE) {
196     qWarning() << "Network" << networkId() << "supplied invalid CHANMODES:" << chanmodes;
197     modeType = NOT_A_CHANMODE;
198   }
199   return modeType;
200 }
201
202 QString Network::support(const QString &param) const {
203   QString support_ = param.toUpper();
204   if(_supports.contains(support_))
205     return _supports[support_];
206   else
207     return QString();
208 }
209
210 IrcUser *Network::newIrcUser(const QString &hostmask, const QVariantMap &initData) {
211   QString nick(nickFromMask(hostmask).toLower());
212   if(!_ircUsers.contains(nick)) {
213     IrcUser *ircuser = new IrcUser(hostmask, this);
214     if(!initData.isEmpty()) {
215       ircuser->fromVariantMap(initData);
216       ircuser->setInitialized();
217     }
218
219     if(proxy())
220       proxy()->synchronize(ircuser);
221     else
222       qWarning() << "unable to synchronize new IrcUser" << hostmask << "forgot to call Network::setProxy(SignalProxy *)?";
223
224     connect(ircuser, SIGNAL(nickSet(QString)), this, SLOT(ircUserNickChanged(QString)));
225
226     _ircUsers[nick] = ircuser;
227
228     emit ircUserAdded(hostmask);
229     emit ircUserAdded(ircuser);
230   }
231
232   return _ircUsers[nick];
233 }
234
235 IrcUser *Network::ircUser(QString nickname) const {
236   nickname = nickname.toLower();
237   if(_ircUsers.contains(nickname))
238     return _ircUsers[nickname];
239   else
240     return 0;
241 }
242
243 void Network::removeIrcUser(IrcUser *ircuser) {
244   QString nick = _ircUsers.key(ircuser);
245   if(nick.isNull())
246     return;
247
248   _ircUsers.remove(nick);
249   disconnect(ircuser, 0, this, 0);
250   ircuser->deleteLater();
251 }
252
253 void Network::removeIrcChannel(IrcChannel *channel) {
254   QString chanName = _ircChannels.key(channel);
255   if(chanName.isNull())
256     return;
257
258   _ircChannels.remove(chanName);
259   disconnect(channel, 0, this, 0);
260   channel->deleteLater();
261 }
262
263 void Network::removeChansAndUsers() {
264   QList<IrcUser *> users = ircUsers();
265   _ircUsers.clear();
266   QList<IrcChannel *> channels = ircChannels();
267   _ircChannels.clear();
268
269   foreach(IrcChannel *channel, channels) {
270     proxy()->detachObject(channel);
271     disconnect(channel, 0, this, 0);
272   }
273   foreach(IrcUser *user, users) {
274     proxy()->detachObject(user);
275     disconnect(user, 0, this, 0);
276   }
277
278   // the second loop is needed because quit can have sideffects
279   foreach(IrcUser *user, users) {
280     user->quit();
281   }
282
283   qDeleteAll(users);
284   qDeleteAll(channels);
285 }
286
287 IrcChannel *Network::newIrcChannel(const QString &channelname, const QVariantMap &initData) {
288   if(!_ircChannels.contains(channelname.toLower())) {
289     IrcChannel *channel = ircChannelFactory(channelname);
290     if(!initData.isEmpty()) {
291       channel->fromVariantMap(initData);
292       channel->setInitialized();
293     }
294
295     if(proxy())
296       proxy()->synchronize(channel);
297     else
298       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
299
300     _ircChannels[channelname.toLower()] = channel;
301
302     emit ircChannelAdded(channelname);
303     emit ircChannelAdded(channel);
304   }
305   return _ircChannels[channelname.toLower()];
306 }
307
308 IrcChannel *Network::ircChannel(QString channelname) const {
309   channelname = channelname.toLower();
310   if(_ircChannels.contains(channelname))
311     return _ircChannels[channelname];
312   else
313     return 0;
314 }
315
316 QByteArray Network::defaultCodecForServer() {
317   if(_defaultCodecForServer)
318     return _defaultCodecForServer->name();
319   return QByteArray();
320 }
321
322 void Network::setDefaultCodecForServer(const QByteArray &name) {
323   _defaultCodecForServer = QTextCodec::codecForName(name);
324 }
325
326 QByteArray Network::defaultCodecForEncoding() {
327   if(_defaultCodecForEncoding)
328     return _defaultCodecForEncoding->name();
329   return QByteArray();
330 }
331
332 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
333   _defaultCodecForEncoding = QTextCodec::codecForName(name);
334 }
335
336 QByteArray Network::defaultCodecForDecoding() {
337   if(_defaultCodecForDecoding)
338     return _defaultCodecForDecoding->name();
339   return QByteArray();
340 }
341
342 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
343   _defaultCodecForDecoding = QTextCodec::codecForName(name);
344 }
345
346 QByteArray Network::codecForServer() const {
347   if(_codecForServer)
348     return _codecForServer->name();
349   return QByteArray();
350 }
351
352 void Network::setCodecForServer(const QByteArray &name) {
353   setCodecForServer(QTextCodec::codecForName(name));
354 }
355
356 void Network::setCodecForServer(QTextCodec *codec) {
357   _codecForServer = codec;
358   emit codecForServerSet(codecForServer());
359 }
360
361 QByteArray Network::codecForEncoding() const {
362   if(_codecForEncoding)
363     return _codecForEncoding->name();
364   return QByteArray();
365 }
366
367 void Network::setCodecForEncoding(const QByteArray &name) {
368   setCodecForEncoding(QTextCodec::codecForName(name));
369 }
370
371 void Network::setCodecForEncoding(QTextCodec *codec) {
372   _codecForEncoding = codec;
373   emit codecForEncodingSet(codecForEncoding());
374 }
375
376 QByteArray Network::codecForDecoding() const {
377   if(_codecForDecoding)
378     return _codecForDecoding->name();
379   else return QByteArray();
380 }
381
382 void Network::setCodecForDecoding(const QByteArray &name) {
383   setCodecForDecoding(QTextCodec::codecForName(name));
384 }
385
386 void Network::setCodecForDecoding(QTextCodec *codec) {
387   _codecForDecoding = codec;
388   emit codecForDecodingSet(codecForDecoding());
389 }
390
391 // FIXME use server encoding if appropriate
392 QString Network::decodeString(const QByteArray &text) const {
393   if(_codecForDecoding)
394     return ::decodeString(text, _codecForDecoding);
395   else return ::decodeString(text, _defaultCodecForDecoding);
396 }
397
398 QByteArray Network::encodeString(const QString &string) const {
399   if(_codecForEncoding) {
400     return _codecForEncoding->fromUnicode(string);
401   }
402   if(_defaultCodecForEncoding) {
403     return _defaultCodecForEncoding->fromUnicode(string);
404   }
405   return string.toAscii();
406 }
407
408 QString Network::decodeServerString(const QByteArray &text) const {
409   if(_codecForServer)
410     return ::decodeString(text, _codecForServer);
411   else
412     return ::decodeString(text, _defaultCodecForServer);
413 }
414
415 QByteArray Network::encodeServerString(const QString &string) const {
416   if(_codecForServer) {
417     return _codecForServer->fromUnicode(string);
418   }
419   if(_defaultCodecForServer) {
420     return _defaultCodecForServer->fromUnicode(string);
421   }
422   return string.toAscii();
423 }
424
425 // ====================
426 //  Public Slots:
427 // ====================
428 void Network::setNetworkName(const QString &networkName) {
429   _networkName = networkName;
430   emit networkNameSet(networkName);
431 }
432
433 void Network::setCurrentServer(const QString &currentServer) {
434   _currentServer = currentServer;
435   emit currentServerSet(currentServer);
436 }
437
438 void Network::setConnected(bool connected) {
439   if(_connected == connected)
440     return;
441
442   _connected = connected;
443   if(!connected) {
444     setMyNick(QString());
445     setCurrentServer(QString());
446     removeChansAndUsers();
447   }
448   emit connectedSet(connected);
449 }
450
451 //void Network::setConnectionState(ConnectionState state) {
452 void Network::setConnectionState(int state) {
453   _connectionState = (ConnectionState)state;
454   //qDebug() << "netstate" << networkId() << networkName() << state;
455   emit connectionStateSet(state);
456   emit connectionStateSet(_connectionState);
457 }
458
459 void Network::setMyNick(const QString &nickname) {
460   _myNick = nickname;
461   if(!_myNick.isEmpty() && !ircUser(myNick())) {
462     newIrcUser(myNick());
463   }
464   emit myNickSet(nickname);
465 }
466
467 void Network::setLatency(int latency) {
468   if(_latency == latency)
469     return;
470   _latency = latency;
471   emit latencySet(latency);
472 }
473
474 void Network::setIdentity(IdentityId id) {
475   _identity = id;
476   emit identitySet(id);
477 }
478
479 void Network::setServerList(const QVariantList &serverList) {
480   _serverList.clear();
481   foreach(QVariant variant, serverList) {
482     _serverList << Server::fromVariant(variant);
483   }
484   emit serverListSet(serverList);
485 }
486
487 void Network::setUseRandomServer(bool use) {
488   _useRandomServer = use;
489   emit useRandomServerSet(use);
490 }
491
492 void Network::setPerform(const QStringList &perform) {
493   _perform = perform;
494   emit performSet(perform);
495 }
496
497 void Network::setUseAutoIdentify(bool use) {
498   _useAutoIdentify = use;
499   emit useAutoIdentifySet(use);
500 }
501
502 void Network::setAutoIdentifyService(const QString &service) {
503   _autoIdentifyService = service;
504   emit autoIdentifyServiceSet(service);
505 }
506
507 void Network::setAutoIdentifyPassword(const QString &password) {
508   _autoIdentifyPassword = password;
509   emit autoIdentifyPasswordSet(password);
510 }
511
512 void Network::setUseAutoReconnect(bool use) {
513   _useAutoReconnect = use;
514   emit useAutoReconnectSet(use);
515 }
516
517 void Network::setAutoReconnectInterval(quint32 interval) {
518   _autoReconnectInterval = interval;
519   emit autoReconnectIntervalSet(interval);
520 }
521
522 void Network::setAutoReconnectRetries(quint16 retries) {
523   _autoReconnectRetries = retries;
524   emit autoReconnectRetriesSet(retries);
525 }
526
527 void Network::setUnlimitedReconnectRetries(bool unlimited) {
528   _unlimitedReconnectRetries = unlimited;
529   emit unlimitedReconnectRetriesSet(unlimited);
530 }
531
532 void Network::setRejoinChannels(bool rejoin) {
533   _rejoinChannels = rejoin;
534   emit rejoinChannelsSet(rejoin);
535 }
536
537 void Network::addSupport(const QString &param, const QString &value) {
538   if(!_supports.contains(param)) {
539     _supports[param] = value;
540     emit supportAdded(param, value);
541   }
542 }
543
544 void Network::removeSupport(const QString &param) {
545   if(_supports.contains(param)) {
546     _supports.remove(param);
547     emit supportRemoved(param);
548   }
549 }
550
551 QVariantMap Network::initSupports() const {
552   QVariantMap supports;
553   QHashIterator<QString, QString> iter(_supports);
554   while(iter.hasNext()) {
555     iter.next();
556     supports[iter.key()] = iter.value();
557   }
558   return supports;
559 }
560
561 QVariantMap Network::initIrcUsersAndChannels() const {
562   QVariantMap usersAndChannels;
563   QVariantMap users;
564   QVariantMap channels;
565
566   QHash<QString, IrcUser *>::const_iterator userIter = _ircUsers.constBegin();
567   QHash<QString, IrcUser *>::const_iterator userIterEnd = _ircUsers.constEnd();
568   while(userIter != userIterEnd) {
569     users[userIter.value()->hostmask()] = userIter.value()->toVariantMap();
570     userIter++;
571   }
572   usersAndChannels["users"] = users;
573
574   QHash<QString, IrcChannel *>::const_iterator channelIter = _ircChannels.constBegin();
575   QHash<QString, IrcChannel *>::const_iterator channelIterEnd = _ircChannels.constEnd();
576   while(channelIter != channelIterEnd) {
577     channels[channelIter.value()->name()] = channelIter.value()->toVariantMap();
578     channelIter++;
579   }
580   usersAndChannels["channels"] = channels;
581
582   return usersAndChannels;
583 }
584
585 void Network::initSetIrcUsersAndChannels(const QVariantMap &usersAndChannels) {
586   Q_ASSERT(proxy());
587   if(isInitialized()) {
588     qWarning() << "Network" << networkId() << "received init data for users and channels allthough there allready are known users or channels!";
589     return;
590   }
591
592   QVariantMap users = usersAndChannels.value("users").toMap();
593   QVariantMap::const_iterator userIter = users.constBegin();
594   QVariantMap::const_iterator userIterEnd = users.constEnd();
595   while(userIter != userIterEnd) {
596     newIrcUser(userIter.key(), userIter.value().toMap());
597     userIter++;
598   }
599
600   QVariantMap channels = usersAndChannels.value("channels").toMap();
601   QVariantMap::const_iterator channelIter = channels.constBegin();
602   QVariantMap::const_iterator channelIterEnd = channels.constEnd();
603   while(channelIter != channelIterEnd) {
604     newIrcChannel(channelIter.key(), channelIter.value().toMap());
605     channelIter++;
606   }
607 }
608
609 void Network::initSetSupports(const QVariantMap &supports) {
610   QMapIterator<QString, QVariant> iter(supports);
611   while(iter.hasNext()) {
612     iter.next();
613     addSupport(iter.key(), iter.value().toString());
614   }
615 }
616
617 void Network::initSetServerList(const QVariantList &serverList) {
618   foreach(QVariant variant, serverList) {
619     _serverList << Server::fromVariant(variant);
620   }
621 }
622
623 IrcUser *Network::updateNickFromMask(const QString &mask) {
624   QString nick(nickFromMask(mask).toLower());
625   IrcUser *ircuser;
626
627   if(_ircUsers.contains(nick)) {
628     ircuser = _ircUsers[nick];
629     ircuser->updateHostmask(mask);
630   } else {
631     ircuser = newIrcUser(mask);
632   }
633   return ircuser;
634 }
635
636 void Network::ircUserNickChanged(QString newnick) {
637   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
638
639   if(oldnick.isNull())
640     return;
641
642   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
643
644   if(myNick().toLower() == oldnick)
645     setMyNick(newnick);
646 }
647
648 void Network::emitConnectionError(const QString &errorMsg) {
649   emit connectionError(errorMsg);
650 }
651
652 // ====================
653 //  Private:
654 // ====================
655 void Network::determinePrefixes() {
656   // seems like we have to construct them first
657   QString prefix = support("PREFIX");
658
659   if(prefix.startsWith("(") && prefix.contains(")")) {
660     _prefixes = prefix.section(")", 1);
661     _prefixModes = prefix.mid(1).section(")", 0, 0);
662   } else {
663     QString defaultPrefixes("~&@%+");
664     QString defaultPrefixModes("qaohv");
665
666     if(prefix.isEmpty()) {
667       _prefixes = defaultPrefixes;
668       _prefixModes = defaultPrefixModes;
669       return;
670     }
671
672     // we just assume that in PREFIX are only prefix chars stored
673     for(int i = 0; i < defaultPrefixes.size(); i++) {
674       if(prefix.contains(defaultPrefixes[i])) {
675         _prefixes += defaultPrefixes[i];
676         _prefixModes += defaultPrefixModes[i];
677       }
678     }
679     // check for success
680     if(!_prefixes.isNull())
681       return;
682
683     // well... our assumption was obviously wrong...
684     // check if it's only prefix modes
685     for(int i = 0; i < defaultPrefixes.size(); i++) {
686       if(prefix.contains(defaultPrefixModes[i])) {
687         _prefixes += defaultPrefixes[i];
688         _prefixModes += defaultPrefixModes[i];
689       }
690     }
691     // now we've done all we've could...
692   }
693 }
694
695 /************************************************************************
696  * NetworkInfo
697  ************************************************************************/
698
699 bool NetworkInfo::operator==(const NetworkInfo &other) const {
700   if(networkId != other.networkId) return false;
701   if(networkName != other.networkName) return false;
702   if(identity != other.identity) return false;
703   if(codecForServer != other.codecForServer) return false;
704   if(codecForEncoding != other.codecForEncoding) return false;
705   if(codecForDecoding != other.codecForDecoding) return false;
706   if(serverList != other.serverList) return false;
707   if(useRandomServer != other.useRandomServer) return false;
708   if(perform != other.perform) return false;
709   if(useAutoIdentify != other.useAutoIdentify) return false;
710   if(autoIdentifyService != other.autoIdentifyService) return false;
711   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
712   if(useAutoReconnect != other.useAutoReconnect) return false;
713   if(autoReconnectInterval != other.autoReconnectInterval) return false;
714   if(autoReconnectRetries != other.autoReconnectRetries) return false;
715   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
716   if(rejoinChannels != other.rejoinChannels) return false;
717   return true;
718 }
719
720 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
721   return !(*this == other);
722 }
723
724 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
725   QVariantMap i;
726   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
727   i["NetworkName"] = info.networkName;
728   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
729   i["CodecForServer"] = info.codecForServer;
730   i["CodecForEncoding"] = info.codecForEncoding;
731   i["CodecForDecoding"] = info.codecForDecoding;
732   i["ServerList"] = info.serverList;
733   i["UseRandomServer"] = info.useRandomServer;
734   i["Perform"] = info.perform;
735   i["UseAutoIdentify"] = info.useAutoIdentify;
736   i["AutoIdentifyService"] = info.autoIdentifyService;
737   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
738   i["UseAutoReconnect"] = info.useAutoReconnect;
739   i["AutoReconnectInterval"] = info.autoReconnectInterval;
740   i["AutoReconnectRetries"] = info.autoReconnectRetries;
741   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
742   i["RejoinChannels"] = info.rejoinChannels;
743   out << i;
744   return out;
745 }
746
747 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
748   QVariantMap i;
749   in >> i;
750   info.networkId = i["NetworkId"].value<NetworkId>();
751   info.networkName = i["NetworkName"].toString();
752   info.identity = i["Identity"].value<IdentityId>();
753   info.codecForServer = i["CodecForServer"].toByteArray();
754   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
755   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
756   info.serverList = i["ServerList"].toList();
757   info.useRandomServer = i["UseRandomServer"].toBool();
758   info.perform = i["Perform"].toStringList();
759   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
760   info.autoIdentifyService = i["AutoIdentifyService"].toString();
761   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
762   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
763   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
764   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
765   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
766   info.rejoinChannels = i["RejoinChannels"].toBool();
767   return in;
768 }
769
770 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
771   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
772       << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
773       << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
774       << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
775       << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
776       << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
777       << " rejoinChannels = " << i.rejoinChannels << ")";
778   return dbg.space();
779 }