+ _processMessages = false;
+ _messageQueue.clear();
+}
+
+
+Protocol::SessionState CoreSession::sessionState() const
+{
+ QVariantList bufferInfos;
+ QVariantList networkIds;
+ QVariantList identities;
+
+ foreach(const BufferInfo &id, buffers())
+ bufferInfos << QVariant::fromValue(id);
+ foreach(const NetworkId &id, _networks.keys())
+ networkIds << QVariant::fromValue(id);
+ foreach(const Identity *i, _identities.values())
+ identities << QVariant::fromValue(*i);
+
+ return Protocol::SessionState(identities, bufferInfos, networkIds);
+}
+
+
+void CoreSession::initScriptEngine()
+{
+ signalProxy()->attachSlot(SIGNAL(scriptRequest(QString)), this, SLOT(scriptRequest(QString)));
+ signalProxy()->attachSignal(this, SIGNAL(scriptResult(QString)));
+
+ // FIXME
+ //QScriptValue storage_ = scriptEngine->newQObject(storage);
+ //scriptEngine->globalObject().setProperty("storage", storage_);
+}
+
+
+void CoreSession::scriptRequest(QString script)
+{
+ emit scriptResult(scriptEngine->evaluate(script).toString());
+}
+
+
+/*** Identity Handling ***/
+void CoreSession::createIdentity(const Identity &identity, const QVariantMap &additional)
+{
+#ifndef HAVE_SSL
+ Q_UNUSED(additional)
+#endif
+
+ CoreIdentity coreIdentity(identity);
+#ifdef HAVE_SSL
+ if (additional.contains("KeyPem"))
+ coreIdentity.setSslKey(additional["KeyPem"].toByteArray());
+ if (additional.contains("CertPem"))
+ coreIdentity.setSslCert(additional["CertPem"].toByteArray());
+#endif
+ qDebug() << Q_FUNC_INFO;
+ IdentityId id = Core::createIdentity(user(), coreIdentity);
+ if (!id.isValid())
+ return;
+ else
+ createIdentity(coreIdentity);
+}
+
+
+void CoreSession::createIdentity(const CoreIdentity &identity)
+{
+ CoreIdentity *coreIdentity = new CoreIdentity(identity, this);
+ _identities[identity.id()] = coreIdentity;
+ // CoreIdentity has its own synchronize method since its "private" sslManager needs to be synced as well
+ coreIdentity->synchronize(signalProxy());
+ connect(coreIdentity, SIGNAL(updated()), this, SLOT(updateIdentityBySender()));
+ emit identityCreated(*coreIdentity);
+}
+
+
+void CoreSession::updateIdentityBySender()
+{
+ CoreIdentity *identity = qobject_cast<CoreIdentity *>(sender());
+ if (!identity)
+ return;
+ Core::updateIdentity(user(), *identity);
+}
+
+
+void CoreSession::removeIdentity(IdentityId id)
+{
+ CoreIdentity *identity = _identities.take(id);
+ if (identity) {
+ emit identityRemoved(id);
+ Core::removeIdentity(user(), id);
+ identity->deleteLater();
+ }
+}
+
+
+/*** Network Handling ***/
+
+void CoreSession::createNetwork(const NetworkInfo &info_, const QStringList &persistentChans)
+{
+ NetworkInfo info = info_;
+ int id;
+
+ if (!info.networkId.isValid())
+ Core::createNetwork(user(), info);
+
+ if (!info.networkId.isValid()) {
+ qWarning() << qPrintable(tr("CoreSession::createNetwork(): Got invalid networkId from Core when trying to create network %1!").arg(info.networkName));
+ return;
+ }
+
+ id = info.networkId.toInt();
+ if (!_networks.contains(id)) {
+ // create persistent chans
+ QRegExp rx("\\s*(\\S+)(?:\\s*(\\S+))?\\s*");
+ foreach(QString channel, persistentChans) {
+ if (!rx.exactMatch(channel)) {
+ qWarning() << QString("Invalid persistent channel declaration: %1").arg(channel);
+ continue;
+ }
+ Core::bufferInfo(user(), info.networkId, BufferInfo::ChannelBuffer, rx.cap(1), true);
+ Core::setChannelPersistent(user(), info.networkId, rx.cap(1), true);
+ if (!rx.cap(2).isEmpty())
+ Core::setPersistentChannelKey(user(), info.networkId, rx.cap(1), rx.cap(2));
+ }
+
+ CoreNetwork *net = new CoreNetwork(id, this);
+ connect(net, SIGNAL(displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &, const QString &, const QString &, Message::Flags)),
+ SLOT(recvMessageFromServer(NetworkId, Message::Type, BufferInfo::Type, const QString &, const QString &, const QString &, Message::Flags)));
+ connect(net, SIGNAL(displayStatusMsg(QString)), SLOT(recvStatusMsgFromServer(QString)));
+ connect(net, SIGNAL(disconnected(NetworkId)), SIGNAL(networkDisconnected(NetworkId)));
+
+ net->setNetworkInfo(info);
+ net->setProxy(signalProxy());
+ _networks[id] = net;
+ signalProxy()->synchronize(net);
+ emit networkCreated(id);
+ }
+ else {
+ qWarning() << qPrintable(tr("CoreSession::createNetwork(): Trying to create a network that already exists, updating instead!"));
+ _networks[info.networkId]->requestSetNetworkInfo(info);
+ }
+}
+
+
+void CoreSession::removeNetwork(NetworkId id)
+{
+ // Make sure the network is disconnected!
+ CoreNetwork *net = network(id);
+ if (!net)
+ return;
+
+ if (net->connectionState() != Network::Disconnected) {
+ // make sure we no longer receive data from the tcp buffer
+ disconnect(net, SIGNAL(displayMsg(NetworkId, Message::Type, BufferInfo::Type, const QString &, const QString &, const QString &, Message::Flags)), this, 0);
+ disconnect(net, SIGNAL(displayStatusMsg(QString)), this, 0);
+ connect(net, SIGNAL(disconnected(NetworkId)), this, SLOT(destroyNetwork(NetworkId)));
+ net->disconnectFromIrc();
+ }
+ else {
+ destroyNetwork(id);
+ }
+}
+
+
+void CoreSession::destroyNetwork(NetworkId id)
+{
+ QList<BufferId> removedBuffers = Core::requestBufferIdsForNetwork(user(), id);
+ Network *net = _networks.take(id);
+ if (net && Core::removeNetwork(user(), id)) {
+ // make sure that all unprocessed RawMessages from this network are removed
+ QList<RawMessage>::iterator messageIter = _messageQueue.begin();
+ while (messageIter != _messageQueue.end()) {
+ if (messageIter->networkId == id) {
+ messageIter = _messageQueue.erase(messageIter);
+ }
+ else {
+ messageIter++;
+ }
+ }
+ // remove buffers from syncer
+ foreach(BufferId bufferId, removedBuffers) {
+ _bufferSyncer->removeBuffer(bufferId);
+ }
+ emit networkRemoved(id);
+ net->deleteLater();
+ }
+}
+
+
+void CoreSession::renameBuffer(const NetworkId &networkId, const QString &newName, const QString &oldName)
+{
+ BufferInfo bufferInfo = Core::bufferInfo(user(), networkId, BufferInfo::QueryBuffer, oldName, false);
+ if (bufferInfo.isValid()) {
+ _bufferSyncer->renameBuffer(bufferInfo.bufferId(), newName);
+ }
+}
+
+
+void CoreSession::clientsConnected()
+{
+ QHash<NetworkId, CoreNetwork *>::iterator netIter = _networks.begin();
+ Identity *identity = 0;
+ CoreNetwork *net = 0;
+ IrcUser *me = 0;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ netIter++;
+
+ if (!net->isConnected())
+ continue;
+ identity = net->identityPtr();
+ if (!identity)
+ continue;
+ me = net->me();
+ if (!me)
+ continue;
+
+ if (identity->detachAwayEnabled() && me->isAway()) {
+ net->userInputHandler()->handleAway(BufferInfo(), QString());
+ }
+ }
+}
+
+
+void CoreSession::clientsDisconnected()
+{
+ QHash<NetworkId, CoreNetwork *>::iterator netIter = _networks.begin();
+ Identity *identity = 0;
+ CoreNetwork *net = 0;
+ IrcUser *me = 0;
+ QString awayReason;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ netIter++;
+
+ if (!net->isConnected())
+ continue;
+
+ identity = net->identityPtr();
+ if (!identity)
+ continue;
+ me = net->me();
+ if (!me)
+ continue;
+
+ if (identity->detachAwayEnabled() && !me->isAway()) {
+ if (!identity->detachAwayReason().isEmpty())
+ awayReason = identity->detachAwayReason();
+ net->setAutoAwayActive(true);
+ net->userInputHandler()->handleAway(BufferInfo(), awayReason);
+ }
+ }
+}
+
+
+void CoreSession::globalAway(const QString &msg)
+{
+ QHash<NetworkId, CoreNetwork *>::iterator netIter = _networks.begin();
+ CoreNetwork *net = 0;
+ while (netIter != _networks.end()) {
+ net = *netIter;
+ netIter++;
+
+ if (!net->isConnected())
+ continue;
+
+ net->userInputHandler()->issueAway(msg, false /* no force away */);
+ }
+}
+
+void CoreSession::changePassword(PeerPtr peer, const QString &userName, const QString &oldPassword, const QString &newPassword)
+{
+ bool success = false;
+ UserId uid = Core::validateUser(userName, oldPassword);
+ if (uid.isValid() && uid == user())
+ success = Core::changeUserPassword(uid, newPassword);
+
+ emit passwordChanged(peer, success);