X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcore%2Fcore.cpp;h=b3abba622d7507465f4b60b174919533c7bb33f4;hb=36b2b9680ff608f41c681b8c23865abb06a2464c;hp=dfaa1b723b263bd6ef3a47896406442e1c6c5cb6;hpb=06a03c2c69ee934aaeec83512bae2fffee83a340;p=quassel.git diff --git a/src/core/core.cpp b/src/core/core.cpp index dfaa1b72..b3abba62 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -1,11 +1,11 @@ /*************************************************************************** - * Copyright (C) 2005-07 by The Quassel IRC Development Team * + * Copyright (C) 2005-07 by the Quassel IRC Team * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * + * (at your option) version 3. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * @@ -20,12 +20,16 @@ #include "core.h" #include "coresession.h" +#include "coresettings.h" #include "sqlitestorage.h" #include "util.h" +#include +#include + Core *Core::instanceptr = 0; -Core * Core::instance() { +Core *Core::instance() { if(instanceptr) return instanceptr; instanceptr = new Core(); instanceptr->init(); @@ -33,32 +37,59 @@ Core * Core::instance() { } void Core::destroy() { - //instanceptr->deleteLater(); delete instanceptr; instanceptr = 0; } Core::Core() { - + storage = NULL; } void Core::init() { - if(!SqliteStorage::isAvailable()) { - qFatal("Sqlite is currently required! Please make sure your Qt library has sqlite support enabled."); + CoreSettings s; + configured = false; + + QVariantMap dbSettings = s.databaseSettings().toMap(); + QString hname = dbSettings["Type"].toString().toLower(); + hname[0] = hname[0].toUpper(); + hname = "initStorage" + hname; + if (!QMetaObject::invokeMethod(this, hname.toAscii(), Q_RETURN_ARG(bool, configured), Q_ARG(QVariantMap, dbSettings), Q_ARG(bool, false))) { + qWarning("No database backend configured."); } - //SqliteStorage::init(); - storage = new SqliteStorage(); + + if (!configured) { + qWarning("Core is currently not configured!"); + } + connect(&server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); - startListening(); // FIXME make configurable + startListening(s.port()); guiUser = 0; } +bool Core::initStorageSqlite(QVariantMap dbSettings, bool setup) { + if (!SqliteStorage::isAvailable()) { + qFatal("Sqlite is currently required! Please make sure your Qt library has sqlite support enabled."); + } + if (storage) { + qDebug() << "Deleting old storage object."; + delete storage; + storage = NULL; + } + + storage = new SqliteStorage(); + if (setup && !storage->setup(dbSettings)) { + return false; + } + + return storage->init(dbSettings); +} + Core::~Core() { - //foreach(QTcpSocket *sock, validClients.keys()) { - // delete sock; - //} qDeleteAll(sessions); - delete storage; + if (storage) { + delete storage; + storage = NULL; + } } CoreSession *Core::session(UserId uid) { @@ -78,11 +109,9 @@ CoreSession *Core::createSession(UserId uid) { Q_ASSERT(!core->sessions.contains(uid)); CoreSession *sess = new CoreSession(uid, core->storage); core->sessions[uid] = sess; - //connect(sess, SIGNAL(proxySignal(CoreSignal, QVariant, QVariant, QVariant)), core, SLOT(recvProxySignal(CoreSignal, QVariant, QVariant, QVariant))); return sess; } - bool Core::startListening(uint port) { if(!server.listen(QHostAddress::Any, port)) { qWarning(QString(QString("Could not open GUI client port %1: %2").arg(port).arg(server.errorString())).toAscii()); @@ -99,11 +128,18 @@ void Core::stopListening() { void Core::incomingConnection() { // TODO implement SSL - QTcpSocket *socket = server.nextPendingConnection(); - connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); - connect(socket, SIGNAL(readyRead()), this, SLOT(clientHasData())); - blockSizes.insert(socket, (quint32)0); - qDebug() << "Client connected from " << socket->peerAddress().toString(); + while (server.hasPendingConnections()) { + QTcpSocket *socket = server.nextPendingConnection(); + connect(socket, SIGNAL(disconnected()), this, SLOT(clientDisconnected())); + connect(socket, SIGNAL(readyRead()), this, SLOT(clientHasData())); + blockSizes.insert(socket, (quint32)0); + qDebug() << "Client connected from " << socket->peerAddress().toString(); + + if (!configured) { + server.close(); + qDebug() << "Closing server for basic setup."; + } + } } void Core::clientHasData() { @@ -112,35 +148,42 @@ void Core::clientHasData() { quint32 bsize = blockSizes.value(socket); QVariant item; if(readDataFromDevice(socket, bsize, item)) { - /* this is probably obsolete now */ - if(validClients.contains(socket)) { - Q_ASSERT(false); - //QList sigdata = item.toList(); - //sessions[validClients[socket]]->processSignal((ClientSignal)sigdata[0].toInt(), sigdata[1], sigdata[2], sigdata[3]); - } else { - // we need to auth the client - try { - processClientInit(socket, item); - } catch(Storage::AuthError) { - qWarning() << "Authentification error!"; // FIXME - socket->close(); - return; - } catch(Exception e) { - qWarning() << "Client init error:" << e.msg(); - socket->close(); - return; + // we need to auth the client + try { + QVariantMap msg = item.toMap(); + if (msg["GuiProtocol"].toUInt() != GUI_PROTOCOL) { + throw Exception("GUI client version mismatch"); } + if (configured) { + processClientInit(socket, msg); + } else { + processCoreSetup(socket, msg); + } + } catch(Storage::AuthError) { + qWarning() << "Authentification error!"; // FIXME: send auth error to client + socket->close(); + return; + } catch(Exception e) { + qWarning() << "Client init error:" << e.msg(); + socket->close(); + return; } - blockSizes[socket] = bsize = 0; // FIXME blockSizes aufräum0rn! } - blockSizes[socket] = bsize; + blockSizes[socket] = bsize = 0; // FIXME blockSizes aufr�um0rn! } +// FIXME: no longer called, since connection handling is now in SignalProxy +// No, it is called as long as core is not configured. (kaffeedoktor) void Core::clientDisconnected() { QTcpSocket *socket = dynamic_cast(sender()); blockSizes.remove(socket); - validClients.remove(socket); qDebug() << "Client disconnected."; + + // make server listen again if still not configured + if (!configured) { + startListening(); + } + // TODO remove unneeded sessions - if necessary/possible... } @@ -157,46 +200,77 @@ void Core::disconnectLocalClient() { instance()->guiUser = 0; } -void Core::processClientInit(QTcpSocket *socket, const QVariant &v) { - QVariantMap msg = v.toMap(); - if(msg["GuiProtocol"].toUInt() != GUI_PROTOCOL) { - //qWarning() << "Client version mismatch."; - throw Exception("GUI client version mismatch"); - } - // Auth - UserId uid = storage->validateUser(msg["User"].toString(), msg["Password"].toString()); // throws exception if this failed - QVariant reply = initSession(uid); - validClients[socket] = uid; // still needed? FIXME - //QList sigdata; - //sigdata.append(CS_CORE_STATE); sigdata.append(reply); sigdata.append(QVariant()); sigdata.append(QVariant()); +void Core::processClientInit(QTcpSocket *socket, const QVariantMap &msg) { + // Auth + QVariantMap reply; + UserId uid = storage->validateUser(msg["User"].toString(), msg["Password"].toString()); // throws exception if this failed + reply["StartWizard"] = false; + reply["Reply"] = initSession(uid); disconnect(socket, 0, this, 0); sessions[uid]->addClient(socket); + qDebug() << "Client initialized successfully."; writeDataToDevice(socket, reply); } +void Core::processCoreSetup(QTcpSocket *socket, QVariantMap &msg) { + if(msg["HasSettings"].toBool()) { + QVariantMap auth; + auth["User"] = msg["User"]; + auth["Password"] = msg["Password"]; + msg.remove("User"); + msg.remove("Password"); + qDebug() << "Initializing storage provider" << msg["Type"].toString(); + QString hname = msg["Type"].toString().toLower(); + hname[0] = hname[0].toUpper(); + hname = "initStorage" + hname; + if (!QMetaObject::invokeMethod(this, hname.toAscii(), Q_RETURN_ARG(bool, configured), Q_ARG(QVariantMap, msg), Q_ARG(bool, true))) { + qWarning("No database backend configured."); + } + if (!configured) { + // notify client to start wizard again + qWarning("Core is currently not configured!"); + QVariantMap reply; + reply["StartWizard"] = true; + reply["StorageProviders"] = availableStorageProviders(); + writeDataToDevice(socket, reply); + } else { + // write coresettings + CoreSettings s; + s.setDatabaseSettings(msg); + // write admin user to database & make the core listen again to connections + storage->addUser(auth["User"].toString(), auth["Password"].toString()); + startListening(); + // continue the normal procedure + processClientInit(socket, auth); + } + } else { + // notify client to start wizard + QVariantMap reply; + reply["StartWizard"] = true; + reply["StorageProviders"] = availableStorageProviders(); + writeDataToDevice(socket, reply); + } +} + QVariant Core::initSession(UserId uid) { // Find or create session for validated user CoreSession *sess; - if(sessions.contains(uid)) sess = sessions[uid]; - else { + if(sessions.contains(uid)) + sess = sessions[uid]; + else sess = createSession(uid); - //validClients[socket] = uid; - } QVariantMap reply; reply["SessionState"] = sess->sessionState(); return reply; } -/* -void Core::recvProxySignal(CoreSignal sig, QVariant arg1, QVariant arg2, QVariant arg3) { - CoreSession *sess = qobject_cast(sender()); - Q_ASSERT(sess); - UserId uid = sess->userId(); - QList sigdata; - sigdata.append(sig); sigdata.append(arg1); sigdata.append(arg2); sigdata.append(arg3); - //qDebug() << "Sending signal: " << sigdata; - foreach(QTcpSocket *socket, validClients.keys()) { - if(validClients[socket] == uid) writeDataToDevice(socket, QVariant(sigdata)); +QStringList Core::availableStorageProviders() { + QStringList storageProviders; + if (SqliteStorage::isAvailable()) { + storageProviders.append(SqliteStorage::displayName()); } + // TODO: temporary + storageProviders.append("MySQL"); + + return storageProviders; } -*/