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