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