+/*** Initialization/destruction ***/
+
+bool Client::instanceExists()
+{
+ return instanceptr;
+}
+
+
+Client *Client::instance()
+{
+ if (!instanceptr)
+ instanceptr = new Client();
+ return instanceptr;
+}
+
+
+void Client::destroy()
+{
+ if (instanceptr) {
+ delete instanceptr->mainUi();
+ instanceptr->deleteLater();
+ instanceptr = 0;
+ }
+}
+
+
+void Client::init(AbstractUi *ui)
+{
+ instance()->_mainUi = ui;
+ instance()->init();
+}
+
+
+Client::Client(QObject *parent)
+ : QObject(parent),
+ _signalProxy(new SignalProxy(SignalProxy::Client, this)),
+ _mainUi(0),
+ _networkModel(0),
+ _bufferModel(0),
+ _bufferSyncer(0),
+ _aliasManager(0),
+ _backlogManager(new ClientBacklogManager(this)),
+ _bufferViewManager(0),
+ _bufferViewOverlay(new BufferViewOverlay(this)),
+ _dccConfig(0),
+ _ircListHelper(new ClientIrcListHelper(this)),
+ _inputHandler(0),
+ _networkConfig(0),
+ _ignoreListManager(0),
+ _transferManager(0),
+ _transferModel(new TransferModel(this)),
+ _messageModel(0),
+ _messageProcessor(0),
+ _coreAccountModel(new CoreAccountModel(this)),
+ _coreConnection(new CoreConnection(this)),
+ _connected(false),
+ _debugLog(&_debugLogBuffer)
+{
+ _signalProxy->synchronize(_ircListHelper);
+}
+
+
+Client::~Client()
+{
+ disconnectFromCore();
+}
+
+
+void Client::init()
+{
+ _networkModel = new NetworkModel(this);
+
+ connect(this, SIGNAL(networkRemoved(NetworkId)),
+ _networkModel, SLOT(networkRemoved(NetworkId)));
+
+ _bufferModel = new BufferModel(_networkModel);
+ _messageModel = mainUi()->createMessageModel(this);
+ _messageProcessor = mainUi()->createMessageProcessor(this);
+ _inputHandler = new ClientUserInputHandler(this);
+
+ SignalProxy *p = signalProxy();
+
+ p->attachSlot(SIGNAL(displayMsg(const Message &)), this, SLOT(recvMessage(const Message &)));
+ p->attachSlot(SIGNAL(displayStatusMsg(QString, QString)), this, SLOT(recvStatusMsg(QString, QString)));
+
+ p->attachSlot(SIGNAL(bufferInfoUpdated(BufferInfo)), _networkModel, SLOT(bufferUpdated(BufferInfo)));
+ p->attachSignal(inputHandler(), SIGNAL(sendInput(BufferInfo, QString)));
+ p->attachSignal(this, SIGNAL(requestNetworkStates()));
+
+ p->attachSignal(this, SIGNAL(requestCreateIdentity(const Identity &, const QVariantMap &)), SIGNAL(createIdentity(const Identity &, const QVariantMap &)));
+ p->attachSignal(this, SIGNAL(requestRemoveIdentity(IdentityId)), SIGNAL(removeIdentity(IdentityId)));
+ p->attachSlot(SIGNAL(identityCreated(const Identity &)), this, SLOT(coreIdentityCreated(const Identity &)));
+ p->attachSlot(SIGNAL(identityRemoved(IdentityId)), this, SLOT(coreIdentityRemoved(IdentityId)));
+
+ p->attachSignal(this, SIGNAL(requestCreateNetwork(const NetworkInfo &, const QStringList &)), SIGNAL(createNetwork(const NetworkInfo &, const QStringList &)));
+ p->attachSignal(this, SIGNAL(requestRemoveNetwork(NetworkId)), SIGNAL(removeNetwork(NetworkId)));
+ p->attachSlot(SIGNAL(networkCreated(NetworkId)), this, SLOT(coreNetworkCreated(NetworkId)));
+ p->attachSlot(SIGNAL(networkRemoved(NetworkId)), this, SLOT(coreNetworkRemoved(NetworkId)));
+
+ p->attachSignal(this, SIGNAL(requestPasswordChange(PeerPtr,QString,QString,QString)), SIGNAL(changePassword(PeerPtr,QString,QString,QString)));
+ p->attachSlot(SIGNAL(passwordChanged(PeerPtr,bool)), this, SLOT(corePasswordChanged(PeerPtr,bool)));
+
+ //connect(mainUi(), SIGNAL(connectToCore(const QVariantMap &)), this, SLOT(connectToCore(const QVariantMap &)));
+ connect(mainUi(), SIGNAL(disconnectFromCore()), this, SLOT(disconnectFromCore()));
+ connect(this, SIGNAL(connected()), mainUi(), SLOT(connectedToCore()));
+ connect(this, SIGNAL(disconnected()), mainUi(), SLOT(disconnectedFromCore()));
+
+ // attach backlog manager
+ p->synchronize(backlogManager());
+ connect(backlogManager(), SIGNAL(messagesReceived(BufferId, int)), _messageModel, SLOT(messagesReceived(BufferId, int)));
+
+ coreAccountModel()->load();
+
+ connect(coreConnection(), SIGNAL(stateChanged(CoreConnection::ConnectionState)), SLOT(connectionStateChanged(CoreConnection::ConnectionState)));
+ coreConnection()->init();
+}
+
+
+/*** public static methods ***/
+
+AbstractUi *Client::mainUi()
+{
+ return instance()->_mainUi;
+}
+
+
+void Client::setCoreFeatures(Quassel::Features features)
+{
+ _coreFeatures = features;
+}
+
+
+bool Client::isConnected()
+{
+ return instance()->_connected;
+}
+
+
+bool Client::internalCore()
+{
+ return currentCoreAccount().isInternal();
+}
+
+
+/*** Network handling ***/
+
+QList<NetworkId> Client::networkIds()
+{
+ return instance()->_networks.keys();
+}
+
+
+const Network *Client::network(NetworkId networkid)
+{
+ if (instance()->_networks.contains(networkid)) return instance()->_networks[networkid];
+ else return 0;
+}
+
+
+void Client::createNetwork(const NetworkInfo &info, const QStringList &persistentChannels)
+{
+ emit instance()->requestCreateNetwork(info, persistentChannels);
+}
+
+
+void Client::removeNetwork(NetworkId id)
+{
+ emit instance()->requestRemoveNetwork(id);
+}
+
+
+void Client::updateNetwork(const NetworkInfo &info)
+{
+ Network *netptr = instance()->_networks.value(info.networkId, 0);
+ if (!netptr) {
+ qWarning() << "Update for unknown network requested:" << info;
+ return;
+ }
+ netptr->requestSetNetworkInfo(info);
+}
+
+
+void Client::addNetwork(Network *net)
+{
+ net->setProxy(signalProxy());
+ signalProxy()->synchronize(net);
+ networkModel()->attachNetwork(net);
+ connect(net, SIGNAL(destroyed()), instance(), SLOT(networkDestroyed()));
+ instance()->_networks[net->networkId()] = net;
+ emit instance()->networkCreated(net->networkId());
+}
+
+
+void Client::coreNetworkCreated(NetworkId id)
+{
+ if (_networks.contains(id)) {
+ qWarning() << "Creation of already existing network requested!";
+ return;
+ }
+ Network *net = new Network(id, this);
+ addNetwork(net);
+}
+
+
+void Client::coreNetworkRemoved(NetworkId id)
+{
+ if (!_networks.contains(id))
+ return;
+ Network *net = _networks.take(id);
+ emit networkRemoved(net->networkId());
+ net->deleteLater();
+}
+
+
+/*** Identity handling ***/
+
+QList<IdentityId> Client::identityIds()
+{
+ return instance()->_identities.keys();