+#include <stdio.h>
+#include <stdlib.h>
+
+QPointer<Client> Client::instanceptr = 0;
+Quassel::Features Client::_coreFeatures = 0;
+
+/*** 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)),
+ _ircListHelper(new ClientIrcListHelper(this)),
+ _inputHandler(0),
+ _networkConfig(0),
+ _ignoreListManager(0),
+ _transferManager(0),
+ _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)));
+
+ //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();