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