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