Merge branch 'cmake'
[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   emit myNickSet(nickname);
426 }
427
428 void Network::setIdentity(IdentityId id) {
429   _identity = id;
430   emit identitySet(id);
431 }
432
433 void Network::setServerList(const QVariantList &serverList) {
434   _serverList = serverList;
435   emit serverListSet(serverList);
436 }
437
438 void Network::setUseRandomServer(bool use) {
439   _useRandomServer = use;
440   emit useRandomServerSet(use);
441 }
442
443 void Network::setPerform(const QStringList &perform) {
444   _perform = perform;
445   emit performSet(perform);
446 }
447
448 void Network::setUseAutoIdentify(bool use) {
449   _useAutoIdentify = use;
450   emit useAutoIdentifySet(use);
451 }
452
453 void Network::setAutoIdentifyService(const QString &service) {
454   _autoIdentifyService = service;
455   emit autoIdentifyServiceSet(service);
456 }
457
458 void Network::setAutoIdentifyPassword(const QString &password) {
459   _autoIdentifyPassword = password;
460   emit autoIdentifyPasswordSet(password);
461 }
462
463 void Network::setUseAutoReconnect(bool use) {
464   _useAutoReconnect = use;
465   emit useAutoReconnectSet(use);
466 }
467
468 void Network::setAutoReconnectInterval(quint32 interval) {
469   _autoReconnectInterval = interval;
470   emit autoReconnectIntervalSet(interval);
471 }
472
473 void Network::setAutoReconnectRetries(quint16 retries) {
474   _autoReconnectRetries = retries;
475   emit autoReconnectRetriesSet(retries);
476 }
477
478 void Network::setUnlimitedReconnectRetries(bool unlimited) {
479   _unlimitedReconnectRetries = unlimited;
480   emit unlimitedReconnectRetriesSet(unlimited);
481 }
482
483 void Network::setRejoinChannels(bool rejoin) {
484   _rejoinChannels = rejoin;
485   emit rejoinChannelsSet(rejoin);
486 }
487
488 void Network::addSupport(const QString &param, const QString &value) {
489   if(!_supports.contains(param)) {
490     _supports[param] = value;
491     emit supportAdded(param, value);
492   }
493 }
494
495 void Network::removeSupport(const QString &param) {
496   if(_supports.contains(param)) {
497     _supports.remove(param);
498     emit supportRemoved(param);
499   }
500 }
501
502 QVariantMap Network::initSupports() const {
503   QVariantMap supports;
504   QHashIterator<QString, QString> iter(_supports);
505   while(iter.hasNext()) {
506     iter.next();
507     supports[iter.key()] = iter.value();
508   }
509   return supports;
510 }
511
512 QStringList Network::initIrcUsers() const {
513   QStringList hostmasks;
514   foreach(IrcUser *ircuser, ircUsers()) {
515     hostmasks << ircuser->hostmask();
516   }
517   return hostmasks;
518 }
519
520 QStringList Network::initIrcChannels() const {
521   QStringList channels;
522   QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
523   while(iter != _ircChannels.constEnd()) {
524     channels << iter.value()->name();
525     iter++;
526   }
527   return channels;
528 }
529
530 void Network::initSetSupports(const QVariantMap &supports) {
531   QMapIterator<QString, QVariant> iter(supports);
532   while(iter.hasNext()) {
533     iter.next();
534     addSupport(iter.key(), iter.value().toString());
535   }
536 }
537
538 void Network::initSetIrcUsers(const QStringList &hostmasks) {
539   if(!_ircUsers.empty())
540     return;
541   foreach(QString hostmask, hostmasks) {
542     newIrcUser(hostmask);
543   }
544 }
545
546 void Network::initSetIrcChannels(const QStringList &channels) {
547   if(!_ircChannels.empty())
548     return;
549   foreach(QString channel, channels)
550     newIrcChannel(channel);
551 }
552
553 IrcUser *Network::updateNickFromMask(const QString &mask) {
554   QString nick(nickFromMask(mask).toLower());
555   IrcUser *ircuser;
556   
557   if(_ircUsers.contains(nick)) {
558     ircuser = _ircUsers[nick];
559     ircuser->updateHostmask(mask);
560   } else {
561     ircuser = newIrcUser(mask);
562   }
563   return ircuser;
564 }
565
566 void Network::ircUserNickChanged(QString newnick) {
567   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
568
569   if(oldnick.isNull())
570     return;
571
572   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
573
574   if(myNick().toLower() == oldnick)
575     setMyNick(newnick);
576 }
577
578 void Network::ircUserInitDone() {
579   IrcUser *ircuser = static_cast<IrcUser *>(sender());
580   Q_ASSERT(ircuser);
581   emit ircUserInitDone(ircuser);
582 }
583
584 void Network::ircChannelInitDone() {
585   IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
586   Q_ASSERT(ircchannel);
587   emit ircChannelInitDone(ircchannel);
588 }
589
590 void Network::removeIrcChannel(IrcChannel *channel) {
591   QString chanName = _ircChannels.key(channel);
592   if(chanName.isNull())
593     return;
594   
595   _ircChannels.remove(chanName);
596   disconnect(channel, 0, this, 0);
597   emit ircChannelRemoved(chanName);
598   emit ircChannelRemoved(channel);
599   channel->deleteLater();
600 }
601
602 void Network::removeIrcChannel(const QString &channel) {
603   IrcChannel *chan;
604   if((chan = ircChannel(channel)) != 0)
605     removeIrcChannel(chan);
606 }
607
608 void Network::channelDestroyed() {
609   IrcChannel *channel = static_cast<IrcChannel *>(sender());
610   Q_ASSERT(channel);
611   _ircChannels.remove(_ircChannels.key(channel));
612   emit ircChannelRemoved(channel);
613 }
614
615 void Network::emitConnectionError(const QString &errorMsg) {
616   emit connectionError(errorMsg);
617 }
618
619 // ====================
620 //  Private:
621 // ====================
622 void Network::determinePrefixes() {
623   // seems like we have to construct them first
624   QString PREFIX = support("PREFIX");
625   
626   if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
627     _prefixes = PREFIX.section(")", 1);
628     _prefixModes = PREFIX.mid(1).section(")", 0, 0);
629   } else {
630     QString defaultPrefixes("~&@%+");
631     QString defaultPrefixModes("qaohv");
632
633     // we just assume that in PREFIX are only prefix chars stored
634     for(int i = 0; i < defaultPrefixes.size(); i++) {
635       if(PREFIX.contains(defaultPrefixes[i])) {
636         _prefixes += defaultPrefixes[i];
637         _prefixModes += defaultPrefixModes[i];
638       }
639     }
640     // check for success
641     if(!_prefixes.isNull())
642       return;
643     
644     // well... our assumption was obviously wrong...
645     // check if it's only prefix modes
646     for(int i = 0; i < defaultPrefixes.size(); i++) {
647       if(PREFIX.contains(defaultPrefixModes[i])) {
648         _prefixes += defaultPrefixes[i];
649         _prefixModes += defaultPrefixModes[i];
650       }
651     }
652     // now we've done all we've could...
653   }
654 }
655
656 /************************************************************************
657  * NetworkInfo
658  ************************************************************************/
659
660 bool NetworkInfo::operator==(const NetworkInfo &other) const {
661   if(networkId != other.networkId) return false;
662   if(networkName != other.networkName) return false;
663   if(identity != other.identity) return false;
664   if(codecForServer != other.codecForServer) return false;
665   if(codecForEncoding != other.codecForEncoding) return false;
666   if(codecForDecoding != other.codecForDecoding) return false;
667   if(serverList != other.serverList) return false;
668   if(useRandomServer != other.useRandomServer) return false;
669   if(perform != other.perform) return false;
670   if(useAutoIdentify != other.useAutoIdentify) return false;
671   if(autoIdentifyService != other.autoIdentifyService) return false;
672   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
673   if(useAutoReconnect != other.useAutoReconnect) return false;
674   if(autoReconnectInterval != other.autoReconnectInterval) return false;
675   if(autoReconnectRetries != other.autoReconnectRetries) return false;
676   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
677   if(rejoinChannels != other.rejoinChannels) return false;
678   return true;
679 }
680
681 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
682   return !(*this == other);
683 }
684
685 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
686   QVariantMap i;
687   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
688   i["NetworkName"] = info.networkName;
689   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
690   i["CodecForServer"] = info.codecForServer;
691   i["CodecForEncoding"] = info.codecForEncoding;
692   i["CodecForDecoding"] = info.codecForDecoding;
693   i["ServerList"] = info.serverList;
694   i["UseRandomServer"] = info.useRandomServer;
695   i["Perform"] = info.perform;
696   i["UseAutoIdentify"] = info.useAutoIdentify;
697   i["AutoIdentifyService"] = info.autoIdentifyService;
698   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
699   i["UseAutoReconnect"] = info.useAutoReconnect;
700   i["AutoReconnectInterval"] = info.autoReconnectInterval;
701   i["AutoReconnectRetries"] = info.autoReconnectRetries;
702   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
703   i["RejoinChannels"] = info.rejoinChannels;
704   out << i;
705   return out;
706 }
707
708 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
709   QVariantMap i;
710   in >> i;
711   info.networkId = i["NetworkId"].value<NetworkId>();
712   info.networkName = i["NetworkName"].toString();
713   info.identity = i["Identity"].value<IdentityId>();
714   info.codecForServer = i["CodecForServer"].toByteArray();
715   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
716   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
717   info.serverList = i["ServerList"].toList();
718   info.useRandomServer = i["UseRandomServer"].toBool();
719   info.perform = i["Perform"].toStringList();
720   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
721   info.autoIdentifyService = i["AutoIdentifyService"].toString();
722   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
723   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
724   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
725   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
726   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
727   info.rejoinChannels = i["RejoinChannels"].toBool();
728   return in;
729 }
730
731 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
732   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
733       << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
734       << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
735       << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
736       << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
737       << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
738       << " rejoinChannels = " << i.rejoinChannels << ")";
739   return dbg.space();
740 }