-// ALL messages coming pass through these functions before going to the GUI.
-// So this is the perfect place for storing the backlog and log stuff.
-void CoreSession::recvMessageFromServer(Message::Type type, QString target, QString text, QString sender, quint8 flags) {
- NetworkConnection *s = qobject_cast<NetworkConnection*>(this->sender());
- Q_ASSERT(s);
- BufferInfo buf;
- if((flags & Message::PrivMsg) && !(flags & Message::Self)) {
- buf = Core::bufferInfo(user(), s->networkName(), nickFromMask(sender));
- } else {
- buf = Core::bufferInfo(user(), s->networkName(), target);
- }
- Message msg(buf, type, text, sender, flags);
- msg.setMsgId(Core::storeMessage(msg));
- Q_ASSERT(msg.msgId());
- emit displayMsg(msg);
-}
-
-void CoreSession::recvStatusMsgFromServer(QString msg) {
- NetworkConnection *s = qobject_cast<NetworkConnection*>(sender());
- Q_ASSERT(s);
- emit displayStatusMsg(s->networkName(), msg);
-}
-
-QList<BufferInfo> CoreSession::buffers() const {
- return Core::requestBuffers(user());
-}
-
-
-QVariant CoreSession::sessionState() {
- QVariantMap v;
-
- QVariantList bufs;
- foreach(BufferInfo id, buffers()) bufs << QVariant::fromValue(id);
- v["BufferInfos"] = bufs;
- QVariantList networkids;
- foreach(NetworkId id, _networks.keys()) networkids << QVariant::fromValue(id);
- v["NetworkIds"] = networkids;
-
- quint32 ircusercount = 0;
- quint32 ircchannelcount = 0;
- foreach(Network *net, _networks.values()) {
- ircusercount += net->ircUserCount();
- ircchannelcount += net->ircChannelCount();
- }
- v["IrcUserCount"] = ircusercount;
- v["IrcChannelCount"] = ircchannelcount;
- qDebug() << "nets:" << _networks.count() << " chans:" << ircchannelcount << " users:" << ircusercount;
-
- QList<QVariant> idlist;
- foreach(Identity *i, _identities.values()) idlist << QVariant::fromValue<Identity>(*i);
- v["Identities"] = idlist;
-
- v["SessionData"] = sessionData;
-
- //v["Payload"] = QByteArray(100000000, 'a'); // for testing purposes
- return v;
-}
-
-void CoreSession::sendBacklog(BufferInfo id, QVariant v1, QVariant v2) {
- QList<QVariant> log;
- QList<Message> msglist;
- if(v1.type() == QVariant::DateTime) {
-
-
- } else {
- msglist = Core::requestMsgs(id, v1.toInt(), v2.toInt());
- }
-
- // Send messages out in smaller packages - we don't want to make the signal data too large!
- for(int i = 0; i < msglist.count(); i++) {
- log.append(QVariant::fromValue(msglist[i]));
- if(log.count() >= 5) {
- emit backlogData(id, log, i >= msglist.count() - 1);
- log.clear();
+
+/*** 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);