fixing BR #100 and #187
[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       ircUserIter = _ircUsers.erase(ircUserIter);
212       break;
213     }
214     ircUserIter++;
215   }
216 }
217
218 void Network::removeIrcUser(IrcUser *ircuser) {
219   QString nick = _ircUsers.key(ircuser);
220   if(nick.isNull())
221     return;
222
223   _ircUsers.remove(nick);
224   disconnect(ircuser, 0, this, 0);
225   emit ircUserRemoved(nick);
226   emit ircUserRemoved(ircuser);
227   ircuser->deleteLater();
228 }
229
230 void Network::removeIrcUser(const QString &nick) {
231   IrcUser *ircuser;
232   if((ircuser = ircUser(nick)) != 0)
233     removeIrcUser(ircuser);
234 }
235
236 void Network::removeChansAndUsers() {
237   QList<IrcUser *> users = ircUsers();
238   foreach(IrcUser *user, users) {
239     removeIrcUser(user);
240   }
241   QList<IrcChannel *> channels = ircChannels();
242   foreach(IrcChannel *channel, channels) {
243     removeIrcChannel(channel);
244   }
245 }
246
247 IrcUser *Network::ircUser(QString nickname) const {
248   nickname = nickname.toLower();
249   if(_ircUsers.contains(nickname))
250     return _ircUsers[nickname];
251   else
252     return 0;
253 }
254
255 IrcChannel *Network::newIrcChannel(const QString &channelname) {
256   if(!_ircChannels.contains(channelname.toLower())) {
257     IrcChannel *channel = new IrcChannel(channelname, this);
258
259     if(proxy())
260       proxy()->synchronize(channel);
261     else
262       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
263
264     connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
265     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
266     _ircChannels[channelname.toLower()] = channel;
267     emit ircChannelAdded(channelname);
268     emit ircChannelAdded(channel);
269   }
270   return _ircChannels[channelname.toLower()];
271 }
272
273 IrcChannel *Network::ircChannel(QString channelname) const {
274   channelname = channelname.toLower();
275   if(_ircChannels.contains(channelname))
276     return _ircChannels[channelname];
277   else
278     return 0;
279 }
280
281 QByteArray Network::defaultCodecForServer() {
282   if(_defaultCodecForServer)
283     return _defaultCodecForServer->name();
284   return QByteArray();
285 }
286
287 void Network::setDefaultCodecForServer(const QByteArray &name) {
288   _defaultCodecForServer = QTextCodec::codecForName(name);
289 }
290
291 QByteArray Network::defaultCodecForEncoding() {
292   if(_defaultCodecForEncoding)
293     return _defaultCodecForEncoding->name();
294   return QByteArray();
295 }
296
297 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
298   _defaultCodecForEncoding = QTextCodec::codecForName(name);
299 }
300
301 QByteArray Network::defaultCodecForDecoding() {
302   if(_defaultCodecForDecoding)
303     return _defaultCodecForDecoding->name();
304   return QByteArray();
305 }
306
307 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
308   _defaultCodecForDecoding = QTextCodec::codecForName(name);
309 }
310
311 QByteArray Network::codecForServer() const {
312   if(_codecForServer)
313     return _codecForServer->name();
314   return QByteArray();
315 }
316
317 void Network::setCodecForServer(const QByteArray &name) {
318   setCodecForServer(QTextCodec::codecForName(name));
319 }
320
321 void Network::setCodecForServer(QTextCodec *codec) {
322   _codecForServer = codec;
323   emit codecForServerSet(codecForServer());
324 }
325
326 QByteArray Network::codecForEncoding() const {
327   if(_codecForEncoding)
328     return _codecForEncoding->name();
329   return QByteArray();
330 }
331
332 void Network::setCodecForEncoding(const QByteArray &name) {
333   setCodecForEncoding(QTextCodec::codecForName(name));
334 }
335
336 void Network::setCodecForEncoding(QTextCodec *codec) {
337   _codecForEncoding = codec;
338   emit codecForEncodingSet(codecForEncoding());
339 }
340
341 QByteArray Network::codecForDecoding() const {
342   if(_codecForDecoding)
343     return _codecForDecoding->name();
344   else return QByteArray();
345 }
346
347 void Network::setCodecForDecoding(const QByteArray &name) {
348   setCodecForDecoding(QTextCodec::codecForName(name));
349 }
350
351 void Network::setCodecForDecoding(QTextCodec *codec) {
352   _codecForDecoding = codec;
353   emit codecForDecodingSet(codecForDecoding());
354 }
355
356 // FIXME use server encoding if appropriate
357 QString Network::decodeString(const QByteArray &text) const {
358   if(_codecForDecoding)
359     return ::decodeString(text, _codecForDecoding);
360   else return ::decodeString(text, _defaultCodecForDecoding);
361 }
362
363 QByteArray Network::encodeString(const QString &string) const {
364   if(_codecForEncoding) {
365     return _codecForEncoding->fromUnicode(string);
366   }
367   if(_defaultCodecForEncoding) {
368     return _defaultCodecForEncoding->fromUnicode(string);
369   }
370   return string.toAscii();
371 }
372
373 QString Network::decodeServerString(const QByteArray &text) const {
374   if(_codecForServer)
375     return ::decodeString(text, _codecForServer);
376   else
377     return ::decodeString(text, _defaultCodecForServer);
378 }
379
380 QByteArray Network::encodeServerString(const QString &string) const {
381   if(_codecForServer) {
382     return _codecForServer->fromUnicode(string);
383   }
384   if(_defaultCodecForServer) {
385     return _defaultCodecForServer->fromUnicode(string);
386   }
387   return string.toAscii();
388 }
389
390 // ====================
391 //  Public Slots:
392 // ====================
393 void Network::setNetworkName(const QString &networkName) {
394   _networkName = networkName;
395   emit networkNameSet(networkName);
396 }
397
398 void Network::setCurrentServer(const QString &currentServer) {
399   _currentServer = currentServer;
400   emit currentServerSet(currentServer);
401 }
402
403 void Network::setConnected(bool connected) {
404   if(_connected == connected)
405     return;
406   
407   _connected = connected;
408   if(!connected) {
409     removeChansAndUsers();
410     setCurrentServer(QString());
411   }
412   emit connectedSet(connected);
413 }
414
415 //void Network::setConnectionState(ConnectionState state) {
416 void Network::setConnectionState(int state) {
417   _connectionState = (ConnectionState)state;
418   //qDebug() << "netstate" << networkId() << networkName() << state;
419   emit connectionStateSet(state);
420   emit connectionStateSet(_connectionState);
421 }
422
423 void Network::setMyNick(const QString &nickname) {
424   _myNick = nickname;
425   if(!_myNick.isEmpty() && !ircUser(myNick())) {
426     newIrcUser(myNick());
427   }
428   emit myNickSet(nickname);
429 }
430
431 void Network::setIdentity(IdentityId id) {
432   _identity = id;
433   emit identitySet(id);
434 }
435
436 void Network::setServerList(const QVariantList &serverList) {
437   _serverList = serverList;
438   emit serverListSet(serverList);
439 }
440
441 void Network::setUseRandomServer(bool use) {
442   _useRandomServer = use;
443   emit useRandomServerSet(use);
444 }
445
446 void Network::setPerform(const QStringList &perform) {
447   _perform = perform;
448   emit performSet(perform);
449 }
450
451 void Network::setUseAutoIdentify(bool use) {
452   _useAutoIdentify = use;
453   emit useAutoIdentifySet(use);
454 }
455
456 void Network::setAutoIdentifyService(const QString &service) {
457   _autoIdentifyService = service;
458   emit autoIdentifyServiceSet(service);
459 }
460
461 void Network::setAutoIdentifyPassword(const QString &password) {
462   _autoIdentifyPassword = password;
463   emit autoIdentifyPasswordSet(password);
464 }
465
466 void Network::setUseAutoReconnect(bool use) {
467   _useAutoReconnect = use;
468   emit useAutoReconnectSet(use);
469 }
470
471 void Network::setAutoReconnectInterval(quint32 interval) {
472   _autoReconnectInterval = interval;
473   emit autoReconnectIntervalSet(interval);
474 }
475
476 void Network::setAutoReconnectRetries(quint16 retries) {
477   _autoReconnectRetries = retries;
478   emit autoReconnectRetriesSet(retries);
479 }
480
481 void Network::setUnlimitedReconnectRetries(bool unlimited) {
482   _unlimitedReconnectRetries = unlimited;
483   emit unlimitedReconnectRetriesSet(unlimited);
484 }
485
486 void Network::setRejoinChannels(bool rejoin) {
487   _rejoinChannels = rejoin;
488   emit rejoinChannelsSet(rejoin);
489 }
490
491 void Network::addSupport(const QString &param, const QString &value) {
492   if(!_supports.contains(param)) {
493     _supports[param] = value;
494     emit supportAdded(param, value);
495   }
496 }
497
498 void Network::removeSupport(const QString &param) {
499   if(_supports.contains(param)) {
500     _supports.remove(param);
501     emit supportRemoved(param);
502   }
503 }
504
505 QVariantMap Network::initSupports() const {
506   QVariantMap supports;
507   QHashIterator<QString, QString> iter(_supports);
508   while(iter.hasNext()) {
509     iter.next();
510     supports[iter.key()] = iter.value();
511   }
512   return supports;
513 }
514
515 QStringList Network::initIrcUsers() const {
516   QStringList hostmasks;
517   foreach(IrcUser *ircuser, ircUsers()) {
518     hostmasks << ircuser->hostmask();
519   }
520   return hostmasks;
521 }
522
523 QStringList Network::initIrcChannels() const {
524   QStringList channels;
525   QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
526   while(iter != _ircChannels.constEnd()) {
527     channels << iter.value()->name();
528     iter++;
529   }
530   return channels;
531 }
532
533 void Network::initSetSupports(const QVariantMap &supports) {
534   QMapIterator<QString, QVariant> iter(supports);
535   while(iter.hasNext()) {
536     iter.next();
537     addSupport(iter.key(), iter.value().toString());
538   }
539 }
540
541 void Network::initSetIrcUsers(const QStringList &hostmasks) {
542   if(!_ircUsers.empty())
543     return;
544   foreach(QString hostmask, hostmasks) {
545     newIrcUser(hostmask);
546   }
547 }
548
549 void Network::initSetIrcChannels(const QStringList &channels) {
550   if(!_ircChannels.empty())
551     return;
552   foreach(QString channel, channels)
553     newIrcChannel(channel);
554 }
555
556 IrcUser *Network::updateNickFromMask(const QString &mask) {
557   QString nick(nickFromMask(mask).toLower());
558   IrcUser *ircuser;
559   
560   if(_ircUsers.contains(nick)) {
561     ircuser = _ircUsers[nick];
562     ircuser->updateHostmask(mask);
563   } else {
564     ircuser = newIrcUser(mask);
565   }
566   return ircuser;
567 }
568
569 void Network::ircUserNickChanged(QString newnick) {
570   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
571
572   if(oldnick.isNull())
573     return;
574
575   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
576
577   if(myNick().toLower() == oldnick)
578     setMyNick(newnick);
579 }
580
581 void Network::ircUserInitDone() {
582   IrcUser *ircuser = static_cast<IrcUser *>(sender());
583   Q_ASSERT(ircuser);
584   emit ircUserInitDone(ircuser);
585 }
586
587 void Network::ircChannelInitDone() {
588   IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
589   Q_ASSERT(ircchannel);
590   emit ircChannelInitDone(ircchannel);
591 }
592
593 void Network::removeIrcChannel(IrcChannel *channel) {
594   QString chanName = _ircChannels.key(channel);
595   if(chanName.isNull())
596     return;
597   
598   _ircChannels.remove(chanName);
599   disconnect(channel, 0, this, 0);
600   emit ircChannelRemoved(chanName);
601   emit ircChannelRemoved(channel);
602   channel->deleteLater();
603 }
604
605 void Network::removeIrcChannel(const QString &channel) {
606   IrcChannel *chan;
607   if((chan = ircChannel(channel)) != 0)
608     removeIrcChannel(chan);
609 }
610
611 void Network::channelDestroyed() {
612   IrcChannel *channel = static_cast<IrcChannel *>(sender());
613   Q_ASSERT(channel);
614   _ircChannels.remove(_ircChannels.key(channel));
615   emit ircChannelRemoved(channel);
616 }
617
618 void Network::emitConnectionError(const QString &errorMsg) {
619   emit connectionError(errorMsg);
620 }
621
622 // ====================
623 //  Private:
624 // ====================
625 void Network::determinePrefixes() {
626   // seems like we have to construct them first
627   QString PREFIX = support("PREFIX");
628   
629   if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
630     _prefixes = PREFIX.section(")", 1);
631     _prefixModes = PREFIX.mid(1).section(")", 0, 0);
632   } else {
633     QString defaultPrefixes("~&@%+");
634     QString defaultPrefixModes("qaohv");
635
636     // we just assume that in PREFIX are only prefix chars stored
637     for(int i = 0; i < defaultPrefixes.size(); i++) {
638       if(PREFIX.contains(defaultPrefixes[i])) {
639         _prefixes += defaultPrefixes[i];
640         _prefixModes += defaultPrefixModes[i];
641       }
642     }
643     // check for success
644     if(!_prefixes.isNull())
645       return;
646     
647     // well... our assumption was obviously wrong...
648     // check if it's only prefix modes
649     for(int i = 0; i < defaultPrefixes.size(); i++) {
650       if(PREFIX.contains(defaultPrefixModes[i])) {
651         _prefixes += defaultPrefixes[i];
652         _prefixModes += defaultPrefixModes[i];
653       }
654     }
655     // now we've done all we've could...
656   }
657 }
658
659 /************************************************************************
660  * NetworkInfo
661  ************************************************************************/
662
663 bool NetworkInfo::operator==(const NetworkInfo &other) const {
664   if(networkId != other.networkId) return false;
665   if(networkName != other.networkName) return false;
666   if(identity != other.identity) return false;
667   if(codecForServer != other.codecForServer) return false;
668   if(codecForEncoding != other.codecForEncoding) return false;
669   if(codecForDecoding != other.codecForDecoding) return false;
670   if(serverList != other.serverList) return false;
671   if(useRandomServer != other.useRandomServer) return false;
672   if(perform != other.perform) return false;
673   if(useAutoIdentify != other.useAutoIdentify) return false;
674   if(autoIdentifyService != other.autoIdentifyService) return false;
675   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
676   if(useAutoReconnect != other.useAutoReconnect) return false;
677   if(autoReconnectInterval != other.autoReconnectInterval) return false;
678   if(autoReconnectRetries != other.autoReconnectRetries) return false;
679   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
680   if(rejoinChannels != other.rejoinChannels) return false;
681   return true;
682 }
683
684 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
685   return !(*this == other);
686 }
687
688 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
689   QVariantMap i;
690   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
691   i["NetworkName"] = info.networkName;
692   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
693   i["CodecForServer"] = info.codecForServer;
694   i["CodecForEncoding"] = info.codecForEncoding;
695   i["CodecForDecoding"] = info.codecForDecoding;
696   i["ServerList"] = info.serverList;
697   i["UseRandomServer"] = info.useRandomServer;
698   i["Perform"] = info.perform;
699   i["UseAutoIdentify"] = info.useAutoIdentify;
700   i["AutoIdentifyService"] = info.autoIdentifyService;
701   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
702   i["UseAutoReconnect"] = info.useAutoReconnect;
703   i["AutoReconnectInterval"] = info.autoReconnectInterval;
704   i["AutoReconnectRetries"] = info.autoReconnectRetries;
705   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
706   i["RejoinChannels"] = info.rejoinChannels;
707   out << i;
708   return out;
709 }
710
711 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
712   QVariantMap i;
713   in >> i;
714   info.networkId = i["NetworkId"].value<NetworkId>();
715   info.networkName = i["NetworkName"].toString();
716   info.identity = i["Identity"].value<IdentityId>();
717   info.codecForServer = i["CodecForServer"].toByteArray();
718   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
719   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
720   info.serverList = i["ServerList"].toList();
721   info.useRandomServer = i["UseRandomServer"].toBool();
722   info.perform = i["Perform"].toStringList();
723   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
724   info.autoIdentifyService = i["AutoIdentifyService"].toString();
725   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
726   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
727   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
728   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
729   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
730   info.rejoinChannels = i["RejoinChannels"].toBool();
731   return in;
732 }
733
734 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
735   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
736       << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
737       << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
738       << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
739       << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
740       << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
741       << " rejoinChannels = " << i.rejoinChannels << ")";
742   return dbg.space();
743 }