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