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