- // init BufferSyncer
- QHash<BufferId, QDateTime> lastSeenHash = Core::bufferLastSeenDates(user());
- foreach(BufferId id, lastSeenHash.keys()) _bufferSyncer->requestSetLastSeen(id, lastSeenHash[id]);
- connect(_bufferSyncer, SIGNAL(lastSeenSet(BufferId, const QDateTime &)), this, SLOT(storeBufferLastSeen(BufferId, const QDateTime &)));
- connect(_bufferSyncer, SIGNAL(removeBufferRequested(BufferId)), this, SLOT(removeBufferRequested(BufferId)));
- connect(this, SIGNAL(bufferRemoved(BufferId)), _bufferSyncer, SLOT(removeBuffer(BufferId)));
- connect(this, SIGNAL(bufferRenamed(BufferId, QString)), _bufferSyncer, SLOT(renameBuffer(BufferId, QString)));
- p->synchronize(_bufferSyncer);
-
- // Restore session state
- if(restoreState) restoreSessionState();
-
- emit initialized();
-}
-
-CoreSession::~CoreSession() {
- saveSessionState();
- foreach(NetworkConnection *conn, _connections.values()) {
- delete conn;
- }
- foreach(Network *net, _networks.values()) {
- delete net;
- }
-}
-
-UserId CoreSession::user() const {
- return _user;
-}
-
-Network *CoreSession::network(NetworkId id) const {
- if(_networks.contains(id)) return _networks[id];
- return 0;
-}
-
-NetworkConnection *CoreSession::networkConnection(NetworkId id) const {
- if(_connections.contains(id)) return _connections[id];
- return 0;
-}
-
-Identity *CoreSession::identity(IdentityId id) const {
- if(_identities.contains(id)) return _identities[id];
- return 0;
-}
-
-void CoreSession::loadSettings() {
- CoreUserSettings s(user());
-
- foreach(IdentityId id, s.identityIds()) {
- Identity *i = new Identity(s.identity(id), this);
- if(!i->isValid()) {
- qWarning() << QString("Invalid identity! Removing...");
- s.removeIdentity(id);
- delete i;
- continue;
- }
- if(_identities.contains(i->id())) {
- qWarning() << "Duplicate identity, ignoring!";
- delete i;
- continue;
- }
- _identities[i->id()] = i;
- signalProxy()->synchronize(i);
- }
- if(!_identities.count()) {
- Identity i(1);
- i.setToDefaults();
- i.setIdentityName(tr("Default Identity"));
- createIdentity(i);
- }
-
-
- // migration to pure DB storage
- QList<NetworkId> netIds = s.networkIds();
- if(!netIds.isEmpty()) {
- qDebug() << "Migrating Networksettings to DB Storage for User:" << user();
- foreach(NetworkId id, netIds) {
- NetworkInfo info = s.networkInfo(id);
-
- // default new options
- info.useRandomServer = false;
- info.useAutoReconnect = true;
- info.autoReconnectInterval = 60;
- info.autoReconnectRetries = 20;
- info.unlimitedReconnectRetries = false;
- info.useAutoIdentify = false;
- info.autoIdentifyService = "NickServ";
- info.rejoinChannels = true;
-
- Core::updateNetwork(user(), info);
- s.removeNetworkInfo(id);
- }
- }
-
- foreach(NetworkInfo info, Core::networks(user())) {
- createNetwork(info);
- }
-}
-
-void CoreSession::saveSessionState() const {
- QVariantMap res;
- QVariantList conn;
- foreach(NetworkConnection *nc, _connections.values()) {
- QHash<QString, QString> persistentChans = nc->network()->persistentChannels();
- QStringList list;
- foreach(QString chan, persistentChans.keys()) list << QString("%1/%2").arg(chan).arg(persistentChans.value(chan));
- QVariantMap m;
- m["NetworkId"] = QVariant::fromValue<NetworkId>(nc->networkId());
- m["PersistentChannels"] = list;
- conn << m;
- }
- res["CoreBuild"] = Global::quasselBuild;
- res["ConnectedNetworks"] = conn;
- CoreUserSettings s(user());
- s.setSessionState(res);
-}
-
-void CoreSession::restoreSessionState() {
- CoreUserSettings s(user());
- uint build = s.sessionState().toMap()["CoreBuild"].toUInt();
- if(build < 362) {
- qWarning() << qPrintable(tr("Session state does not exist or is too old!"));
- return;
- }
- QVariantList conn = s.sessionState().toMap()["ConnectedNetworks"].toList();
- foreach(QVariant v, conn) {
- NetworkId id = v.toMap()["NetworkId"].value<NetworkId>();
- // TODO remove migration code some time
- QStringList list = v.toMap()["PersistentChannels"].toStringList();
- if(!list.count()) {
- // migrate older state
- QStringList old = v.toMap()["State"].toStringList();
- foreach(QString chan, old) list << QString("%1/").arg(chan);
- }
- foreach(QString chan, list) {
- QStringList l = chan.split("/");
- network(id)->addPersistentChannel(l[0], l[1]);
- }
- //qDebug() << "User" << user() << "connecting to" << network(id)->networkName();
- connectToNetwork(id);
- }
-}
-
-void CoreSession::updateBufferInfo(UserId uid, const BufferInfo &bufinfo) {
- if(uid == user()) emit bufferInfoUpdated(bufinfo);
-}
-
-// FIXME remove
-/*
-void CoreSession::connectToNetwork(QString netname, const QVariant &previousState) {
- Network *net = 0;
- foreach(Network *n, _networks.values()) {
- if(n->networkName() == netname) {
- net = n; break;
- }
- }
- if(!net) {
- qWarning() << "Connect to unknown network requested, ignoring!";
- return;
- }
- connectToNetwork(net->networkId(), previousState);
-}
-*/
-
-void CoreSession::connectToNetwork(NetworkId id) {
- Network *net = network(id);
- if(!net) {
- qWarning() << "Connect to unknown network requested! net:" << id << "user:" << user();
- return;
- }
-
- NetworkConnection *conn = networkConnection(id);
- if(!conn) {
- conn = new NetworkConnection(net, this);
- _connections[id] = conn;
- attachNetworkConnection(conn);
- }
- conn->connectToIrc();
-}
-
-void CoreSession::attachNetworkConnection(NetworkConnection *conn) {
- connect(conn, SIGNAL(connected(NetworkId)), this, SLOT(networkConnected(NetworkId)));
- connect(conn, SIGNAL(quitRequested(NetworkId)), this, SLOT(networkDisconnected(NetworkId)));
-
- // I guess we don't need these anymore, client-side can just connect the network's signals directly
- //signalProxy()->attachSignal(conn, SIGNAL(connected(NetworkId)), SIGNAL(networkConnected(NetworkId)));
- //signalProxy()->attachSignal(conn, SIGNAL(disconnected(NetworkId)), SIGNAL(networkDisconnected(NetworkId)));