another try to fix those win/linux crashes :)
[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
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     connect(ircuser, SIGNAL(destroyed()), this, SLOT(ircUserDestroyed()));
292     _ircUsers[nick] = ircuser;
293     emit ircUserAdded(hostmask);
294     emit ircUserAdded(ircuser);
295   }
296   return _ircUsers[nick];
297 }
298
299 IrcUser *Network::newIrcUser(const QByteArray &hostmask) {
300   return newIrcUser(decodeServerString(hostmask));
301 }
302
303 void Network::ircUserDestroyed() {
304   IrcUser *ircUser = static_cast<IrcUser *>(sender());
305   if(!ircUser)
306     return;
307
308   QHash<QString, IrcUser *>::iterator ircUserIter = _ircUsers.begin();
309   while(ircUserIter != _ircUsers.end()) {
310     if(ircUser == *ircUserIter) {
311       ircUserIter = _ircUsers.erase(ircUserIter);
312       break;
313     }
314     ircUserIter++;
315   }
316 }
317
318 void Network::removeIrcUser(IrcUser *ircuser) {
319   QString nick = _ircUsers.key(ircuser);
320   if(nick.isNull())
321     return;
322
323   _ircUsers.remove(nick);
324   disconnect(ircuser, 0, this, 0);
325   emit ircUserRemoved(nick);
326   emit ircUserRemoved(ircuser);
327   ircuser->deleteLater();
328 }
329
330 void Network::removeIrcUser(const QString &nick) {
331   IrcUser *ircuser;
332   if((ircuser = ircUser(nick)) != 0)
333     removeIrcUser(ircuser);
334 }
335
336 void Network::removeChansAndUsers() {
337   QList<IrcUser *> users = ircUsers();
338   foreach(IrcUser *user, users) {
339     removeIrcUser(user);
340   }
341   QList<IrcChannel *> channels = ircChannels();
342   foreach(IrcChannel *channel, channels) {
343     removeIrcChannel(channel);
344   }
345 }
346
347 IrcUser *Network::ircUser(QString nickname) const {
348   nickname = nickname.toLower();
349   if(_ircUsers.contains(nickname))
350     return _ircUsers[nickname];
351   else
352     return 0;
353 }
354
355 IrcUser *Network::ircUser(const QByteArray &nickname) const {
356   return ircUser(decodeServerString(nickname));
357 }
358
359 QList<IrcUser *> Network::ircUsers() const {
360   return _ircUsers.values();
361 }
362
363 quint32 Network::ircUserCount() const {
364   return _ircUsers.count();
365 }
366
367 IrcChannel *Network::newIrcChannel(const QString &channelname) {
368   if(!_ircChannels.contains(channelname.toLower())) {
369     IrcChannel *channel = new IrcChannel(channelname, this);
370
371     if(proxy())
372       proxy()->synchronize(channel);
373     else
374       qWarning() << "unable to synchronize new IrcChannel" << channelname << "forgot to call Network::setProxy(SignalProxy *)?";
375
376     connect(channel, SIGNAL(initDone()), this, SLOT(ircChannelInitDone()));
377     connect(channel, SIGNAL(destroyed()), this, SLOT(channelDestroyed()));
378     _ircChannels[channelname.toLower()] = channel;
379     emit ircChannelAdded(channelname);
380     emit ircChannelAdded(channel);
381   }
382   return _ircChannels[channelname.toLower()];
383 }
384
385 IrcChannel *Network::newIrcChannel(const QByteArray &channelname) {
386   return newIrcChannel(decodeServerString(channelname));
387 }
388
389 IrcChannel *Network::ircChannel(QString channelname) const {
390   channelname = channelname.toLower();
391   if(_ircChannels.contains(channelname))
392     return _ircChannels[channelname];
393   else
394     return 0;
395 }
396
397 IrcChannel *Network::ircChannel(const QByteArray &channelname) const {
398   return ircChannel(decodeServerString(channelname));
399 }
400
401
402 QList<IrcChannel *> Network::ircChannels() const {
403   return _ircChannels.values();
404 }
405
406 quint32 Network::ircChannelCount() const {
407   return _ircChannels.count();
408 }
409
410 QByteArray Network::defaultCodecForServer() {
411   if(_defaultCodecForServer) return _defaultCodecForServer->name();
412   return QByteArray();
413 }
414
415 void Network::setDefaultCodecForServer(const QByteArray &name) {
416   _defaultCodecForServer = QTextCodec::codecForName(name);
417 }
418
419 QByteArray Network::defaultCodecForEncoding() {
420   if(_defaultCodecForEncoding) return _defaultCodecForEncoding->name();
421   return QByteArray();
422 }
423
424 void Network::setDefaultCodecForEncoding(const QByteArray &name) {
425   _defaultCodecForEncoding = QTextCodec::codecForName(name);
426 }
427
428 QByteArray Network::defaultCodecForDecoding() {
429   if(_defaultCodecForDecoding) return _defaultCodecForDecoding->name();
430   return QByteArray();
431 }
432
433 void Network::setDefaultCodecForDecoding(const QByteArray &name) {
434   _defaultCodecForDecoding = QTextCodec::codecForName(name);
435 }
436
437 QByteArray Network::codecForServer() const {
438   if(_codecForServer) return _codecForServer->name();
439   return QByteArray();
440 }
441
442 void Network::setCodecForServer(const QByteArray &name) {
443   setCodecForServer(QTextCodec::codecForName(name));
444 }
445
446 void Network::setCodecForServer(QTextCodec *codec) {
447   _codecForServer = codec;
448   emit codecForServerSet(codecForServer());
449 }
450
451 QByteArray Network::codecForEncoding() const {
452   if(_codecForEncoding) return _codecForEncoding->name();
453   return QByteArray();
454 }
455
456 void Network::setCodecForEncoding(const QByteArray &name) {
457   setCodecForEncoding(QTextCodec::codecForName(name));
458 }
459
460 void Network::setCodecForEncoding(QTextCodec *codec) {
461   _codecForEncoding = codec;
462   emit codecForEncodingSet(codecForEncoding());
463 }
464
465 QByteArray Network::codecForDecoding() const {
466   if(_codecForDecoding) return _codecForDecoding->name();
467   else return QByteArray();
468 }
469
470 void Network::setCodecForDecoding(const QByteArray &name) {
471   setCodecForDecoding(QTextCodec::codecForName(name));
472 }
473
474 void Network::setCodecForDecoding(QTextCodec *codec) {
475   _codecForDecoding = codec;
476   emit codecForDecodingSet(codecForDecoding());
477 }
478
479 // FIXME use server encoding if appropriate
480 QString Network::decodeString(const QByteArray &text) const {
481   if(_codecForDecoding) return ::decodeString(text, _codecForDecoding);
482   else return ::decodeString(text, _defaultCodecForDecoding);
483 }
484
485 QByteArray Network::encodeString(const QString &string) const {
486   if(_codecForEncoding) {
487     return _codecForEncoding->fromUnicode(string);
488   }
489   if(_defaultCodecForEncoding) {
490     return _defaultCodecForEncoding->fromUnicode(string);
491   }
492   return string.toAscii();
493 }
494
495 QString Network::decodeServerString(const QByteArray &text) const {
496   if(_codecForServer) return ::decodeString(text, _codecForServer);
497   else return ::decodeString(text, _defaultCodecForServer);
498 }
499
500 QByteArray Network::encodeServerString(const QString &string) const {
501   if(_codecForServer) {
502     return _codecForServer->fromUnicode(string);
503   }
504   if(_defaultCodecForServer) {
505     return _defaultCodecForServer->fromUnicode(string);
506   }
507   return string.toAscii();
508 }
509
510 // ====================
511 //  Public Slots:
512 // ====================
513 void Network::setNetworkName(const QString &networkName) {
514   _networkName = networkName;
515   emit networkNameSet(networkName);
516 }
517
518 void Network::setCurrentServer(const QString &currentServer) {
519   _currentServer = currentServer;
520   emit currentServerSet(currentServer);
521 }
522
523 void Network::setConnected(bool connected) {
524   _connected = connected;
525   if(!connected) {
526     removeChansAndUsers();
527     setCurrentServer(QString());
528   }
529   emit connectedSet(connected);
530 }
531
532 //void Network::setConnectionState(ConnectionState state) {
533 void Network::setConnectionState(int state) {
534   _connectionState = (ConnectionState)state;
535   //qDebug() << "netstate" << networkId() << networkName() << state;
536   emit connectionStateSet(state);
537   emit connectionStateSet(_connectionState);
538 }
539
540 void Network::setMyNick(const QString &nickname) {
541   _myNick = nickname;
542   emit myNickSet(nickname);
543 }
544
545 void Network::setIdentity(IdentityId id) {
546   _identity = id;
547   emit identitySet(id);
548 }
549
550 void Network::setServerList(const QVariantList &serverList) {
551   _serverList = serverList;
552   emit serverListSet(serverList);
553 }
554
555 void Network::setUseRandomServer(bool use) {
556   _useRandomServer = use;
557   emit useRandomServerSet(use);
558 }
559
560 void Network::setPerform(const QStringList &perform) {
561   _perform = perform;
562   emit performSet(perform);
563 }
564
565 void Network::setUseAutoIdentify(bool use) {
566   _useAutoIdentify = use;
567   emit useAutoIdentifySet(use);
568 }
569
570 void Network::setAutoIdentifyService(const QString &service) {
571   _autoIdentifyService = service;
572   emit autoIdentifyServiceSet(service);
573 }
574
575 void Network::setAutoIdentifyPassword(const QString &password) {
576   _autoIdentifyPassword = password;
577   emit autoIdentifyPasswordSet(password);
578 }
579
580 void Network::setUseAutoReconnect(bool use) {
581   _useAutoReconnect = use;
582   emit useAutoReconnectSet(use);
583 }
584
585 void Network::setAutoReconnectInterval(quint32 interval) {
586   _autoReconnectInterval = interval;
587   emit autoReconnectIntervalSet(interval);
588 }
589
590 void Network::setAutoReconnectRetries(quint16 retries) {
591   _autoReconnectRetries = retries;
592   emit autoReconnectRetriesSet(retries);
593 }
594
595 void Network::setUnlimitedReconnectRetries(bool unlimited) {
596   _unlimitedReconnectRetries = unlimited;
597   emit unlimitedReconnectRetriesSet(unlimited);
598 }
599
600 void Network::setRejoinChannels(bool rejoin) {
601   _rejoinChannels = rejoin;
602   emit rejoinChannelsSet(rejoin);
603 }
604
605 void Network::addSupport(const QString &param, const QString &value) {
606   if(!_supports.contains(param)) {
607     _supports[param] = value;
608     emit supportAdded(param, value);
609   }
610 }
611
612 void Network::removeSupport(const QString &param) {
613   if(_supports.contains(param)) {
614     _supports.remove(param);
615     emit supportRemoved(param);
616   }
617 }
618
619 QVariantMap Network::initSupports() const {
620   QVariantMap supports;
621   QHashIterator<QString, QString> iter(_supports);
622   while(iter.hasNext()) {
623     iter.next();
624     supports[iter.key()] = iter.value();
625   }
626   return supports;
627 }
628
629 QVariantList Network::initServerList() const {
630   return serverList();
631 }
632
633 QStringList Network::initIrcUsers() const {
634   QStringList hostmasks;
635   foreach(IrcUser *ircuser, ircUsers()) {
636     hostmasks << ircuser->hostmask();
637   }
638   return hostmasks;
639 }
640
641 QStringList Network::initIrcChannels() const {
642   QStringList channels;
643   QHash<QString, IrcChannel *>::const_iterator iter = _ircChannels.constBegin();
644   while(iter != _ircChannels.constEnd()) {
645     channels << iter.value()->name();
646     iter++;
647   }
648   return channels;
649 }
650
651 void Network::initSetSupports(const QVariantMap &supports) {
652   QMapIterator<QString, QVariant> iter(supports);
653   while(iter.hasNext()) {
654     iter.next();
655     addSupport(iter.key(), iter.value().toString());
656   }
657 }
658
659 void Network::initSetServerList(const QVariantList & serverList) {
660   setServerList(serverList);
661 }
662
663 void Network::initSetIrcUsers(const QStringList &hostmasks) {
664   if(!_ircUsers.empty())
665     return;
666   foreach(QString hostmask, hostmasks) {
667     newIrcUser(hostmask);
668   }
669 }
670
671 void Network::initSetIrcChannels(const QStringList &channels) {
672   if(!_ircChannels.empty())
673     return;
674   foreach(QString channel, channels)
675     newIrcChannel(channel);
676 }
677
678 IrcUser *Network::updateNickFromMask(const QString &mask) {
679   QString nick(nickFromMask(mask).toLower());
680   IrcUser *ircuser;
681   
682   if(_ircUsers.contains(nick)) {
683     ircuser = _ircUsers[nick];
684     ircuser->updateHostmask(mask);
685   } else {
686     ircuser = newIrcUser(mask);
687   }
688   return ircuser;
689 }
690
691 void Network::ircUserNickChanged(QString newnick) {
692   QString oldnick = _ircUsers.key(qobject_cast<IrcUser*>(sender()));
693
694   if(oldnick.isNull())
695     return;
696
697   if(newnick.toLower() != oldnick) _ircUsers[newnick.toLower()] = _ircUsers.take(oldnick);
698
699   if(myNick().toLower() == oldnick)
700     setMyNick(newnick);
701 }
702
703 void Network::ircUserInitDone() {
704   IrcUser *ircuser = static_cast<IrcUser *>(sender());
705   Q_ASSERT(ircuser);
706   emit ircUserInitDone(ircuser);
707 }
708
709 void Network::ircChannelInitDone() {
710   IrcChannel *ircchannel = static_cast<IrcChannel *>(sender());
711   Q_ASSERT(ircchannel);
712   emit ircChannelInitDone(ircchannel);
713 }
714
715 void Network::removeIrcChannel(IrcChannel *channel) {
716   QString chanName = _ircChannels.key(channel);
717   if(chanName.isNull())
718     return;
719   
720   _ircChannels.remove(chanName);
721   disconnect(channel, 0, this, 0);
722   emit ircChannelRemoved(chanName);
723   emit ircChannelRemoved(channel);
724   channel->deleteLater();
725 }
726
727 void Network::removeIrcChannel(const QString &channel) {
728   IrcChannel *chan;
729   if((chan = ircChannel(channel)) != 0)
730     removeIrcChannel(chan);
731 }
732
733 void Network::channelDestroyed() {
734   IrcChannel *channel = static_cast<IrcChannel *>(sender());
735   Q_ASSERT(channel);
736   _ircChannels.remove(_ircChannels.key(channel));
737   emit ircChannelRemoved(channel);
738 }
739
740 void Network::requestConnect() const {
741   if(!proxy()) return;
742   if(proxy()->proxyMode() == SignalProxy::Client) emit connectRequested(); // on the client this triggers calling this slot on the core
743   else {
744     if(connectionState() != Disconnected) {
745       qWarning() << "Requesting connect while already being connected!";
746       return;
747     }
748     emit connectRequested(networkId());  // and this is for CoreSession :)
749   }
750 }
751
752 void Network::requestDisconnect() const {
753   if(!proxy()) return;
754   if(proxy()->proxyMode() == SignalProxy::Client) emit disconnectRequested(); // on the client this triggers calling this slot on the core
755   else {
756     if(connectionState() == Disconnected) {
757       qWarning() << "Requesting disconnect while not being connected!";
758       return;
759     }
760     emit disconnectRequested(networkId());  // and this is for CoreSession :)
761   }
762 }
763
764 void Network::emitConnectionError(const QString &errorMsg) {
765   emit connectionError(errorMsg);
766 }
767
768 // ====================
769 //  Private:
770 // ====================
771 void Network::determinePrefixes() {
772   // seems like we have to construct them first
773   QString PREFIX = support("PREFIX");
774   
775   if(PREFIX.startsWith("(") && PREFIX.contains(")")) {
776     _prefixes = PREFIX.section(")", 1);
777     _prefixModes = PREFIX.mid(1).section(")", 0, 0);
778   } else {
779     QString defaultPrefixes("~&@%+");
780     QString defaultPrefixModes("qaohv");
781
782     // we just assume that in PREFIX are only prefix chars stored
783     for(int i = 0; i < defaultPrefixes.size(); i++) {
784       if(PREFIX.contains(defaultPrefixes[i])) {
785         _prefixes += defaultPrefixes[i];
786         _prefixModes += defaultPrefixModes[i];
787       }
788     }
789     // check for success
790     if(!_prefixes.isNull())
791       return;
792     
793     // well... our assumption was obviously wrong...
794     // check if it's only prefix modes
795     for(int i = 0; i < defaultPrefixes.size(); i++) {
796       if(PREFIX.contains(defaultPrefixModes[i])) {
797         _prefixes += defaultPrefixes[i];
798         _prefixModes += defaultPrefixModes[i];
799       }
800     }
801     // now we've done all we've could...
802   }
803 }
804
805 /************************************************************************
806  * NetworkInfo
807  ************************************************************************/
808
809 bool NetworkInfo::operator==(const NetworkInfo &other) const {
810   if(networkId != other.networkId) return false;
811   if(networkName != other.networkName) return false;
812   if(identity != other.identity) return false;
813   if(codecForServer != other.codecForServer) return false;
814   if(codecForEncoding != other.codecForEncoding) return false;
815   if(codecForDecoding != other.codecForDecoding) return false;
816   if(serverList != other.serverList) return false;
817   if(useRandomServer != other.useRandomServer) return false;
818   if(perform != other.perform) return false;
819   if(useAutoIdentify != other.useAutoIdentify) return false;
820   if(autoIdentifyService != other.autoIdentifyService) return false;
821   if(autoIdentifyPassword != other.autoIdentifyPassword) return false;
822   if(useAutoReconnect != other.useAutoReconnect) return false;
823   if(autoReconnectInterval != other.autoReconnectInterval) return false;
824   if(autoReconnectRetries != other.autoReconnectRetries) return false;
825   if(unlimitedReconnectRetries != other.unlimitedReconnectRetries) return false;
826   if(rejoinChannels != other.rejoinChannels) return false;
827   return true;
828 }
829
830 bool NetworkInfo::operator!=(const NetworkInfo &other) const {
831   return !(*this == other);
832 }
833
834 QDataStream &operator<<(QDataStream &out, const NetworkInfo &info) {
835   QVariantMap i;
836   i["NetworkId"] = QVariant::fromValue<NetworkId>(info.networkId);
837   i["NetworkName"] = info.networkName;
838   i["Identity"] = QVariant::fromValue<IdentityId>(info.identity);
839   i["CodecForServer"] = info.codecForServer;
840   i["CodecForEncoding"] = info.codecForEncoding;
841   i["CodecForDecoding"] = info.codecForDecoding;
842   i["ServerList"] = info.serverList;
843   i["UseRandomServer"] = info.useRandomServer;
844   i["Perform"] = info.perform;
845   i["UseAutoIdentify"] = info.useAutoIdentify;
846   i["AutoIdentifyService"] = info.autoIdentifyService;
847   i["AutoIdentifyPassword"] = info.autoIdentifyPassword;
848   i["UseAutoReconnect"] = info.useAutoReconnect;
849   i["AutoReconnectInterval"] = info.autoReconnectInterval;
850   i["AutoReconnectRetries"] = info.autoReconnectRetries;
851   i["UnlimitedReconnectRetries"] = info.unlimitedReconnectRetries;
852   i["RejoinChannels"] = info.rejoinChannels;
853   out << i;
854   return out;
855 }
856
857 QDataStream &operator>>(QDataStream &in, NetworkInfo &info) {
858   QVariantMap i;
859   in >> i;
860   info.networkId = i["NetworkId"].value<NetworkId>();
861   info.networkName = i["NetworkName"].toString();
862   info.identity = i["Identity"].value<IdentityId>();
863   info.codecForServer = i["CodecForServer"].toByteArray();
864   info.codecForEncoding = i["CodecForEncoding"].toByteArray();
865   info.codecForDecoding = i["CodecForDecoding"].toByteArray();
866   info.serverList = i["ServerList"].toList();
867   info.useRandomServer = i["UseRandomServer"].toBool();
868   info.perform = i["Perform"].toStringList();
869   info.useAutoIdentify = i["UseAutoIdentify"].toBool();
870   info.autoIdentifyService = i["AutoIdentifyService"].toString();
871   info.autoIdentifyPassword = i["AutoIdentifyPassword"].toString();
872   info.useAutoReconnect = i["UseAutoReconnect"].toBool();
873   info.autoReconnectInterval = i["AutoReconnectInterval"].toUInt();
874   info.autoReconnectRetries = i["AutoReconnectRetries"].toInt();
875   info.unlimitedReconnectRetries = i["UnlimitedReconnectRetries"].toBool();
876   info.rejoinChannels = i["RejoinChannels"].toBool();
877   return in;
878 }
879
880 QDebug operator<<(QDebug dbg, const NetworkInfo &i) {
881   dbg.nospace() << "(id = " << i.networkId << " name = " << i.networkName << " identity = " << i.identity
882       << " codecForServer = " << i.codecForServer << " codecForEncoding = " << i.codecForEncoding << " codecForDecoding = " << i.codecForDecoding
883       << " serverList = " << i.serverList << " useRandomServer = " << i.useRandomServer << " perform = " << i.perform
884       << " useAutoIdentify = " << i.useAutoIdentify << " autoIdentifyService = " << i.autoIdentifyService << " autoIdentifyPassword = " << i.autoIdentifyPassword
885       << " useAutoReconnect = " << i.useAutoReconnect << " autoReconnectInterval = " << i.autoReconnectInterval
886       << " autoReconnectRetries = " << i.autoReconnectRetries << " unlimitedReconnectRetries = " << i.unlimitedReconnectRetries
887       << " rejoinChannels = " << i.rejoinChannels << ")";
888   return dbg.space();
889 }
890
891
892
893