+void Core::socketError(QAbstractSocket::SocketError err)
+{
+ QAbstractSocket *socket = qobject_cast<QAbstractSocket *>(sender());
+ if (socket && err != QAbstractSocket::RemoteHostClosedError)
+ qWarning() << "Core::socketError()" << socket << err << socket->errorString();
+}
+
+
+// migration / backend selection
+bool Core::selectBackend(const QString &backend)
+{
+ // reregister all storage backends
+ registerStorageBackends();
+ if (!_storageBackends.contains(backend)) {
+ qWarning() << qPrintable(QString("Core::selectBackend(): unsupported backend: %1").arg(backend));
+ qWarning() << " supported backends are:" << qPrintable(QStringList(_storageBackends.keys()).join(", "));
+ return false;
+ }
+
+ Storage *storage = _storageBackends[backend];
+ QVariantMap settings = promptForSettings(storage);
+
+ Storage::State storageState = storage->init(settings);
+ switch (storageState) {
+ case Storage::IsReady:
+ saveBackendSettings(backend, settings);
+ qWarning() << "Switched backend to:" << qPrintable(backend);
+ qWarning() << "Backend already initialized. Skipping Migration";
+ return true;
+ case Storage::NotAvailable:
+ qCritical() << "Backend is not available:" << qPrintable(backend);
+ return false;
+ case Storage::NeedsSetup:
+ if (!storage->setup(settings)) {
+ qWarning() << qPrintable(QString("Core::selectBackend(): unable to setup backend: %1").arg(backend));
+ return false;
+ }
+
+ if (storage->init(settings) != Storage::IsReady) {
+ qWarning() << qPrintable(QString("Core::migrateBackend(): unable to initialize backend: %1").arg(backend));
+ return false;
+ }
+
+ saveBackendSettings(backend, settings);
+ qWarning() << "Switched backend to:" << qPrintable(backend);
+ break;
+ }
+
+ // let's see if we have a current storage object we can migrate from
+ AbstractSqlMigrationReader *reader = getMigrationReader(_storage);
+ AbstractSqlMigrationWriter *writer = getMigrationWriter(storage);
+ if (reader && writer) {
+ qDebug() << qPrintable(QString("Migrating Storage backend %1 to %2...").arg(_storage->displayName(), storage->displayName()));
+ delete _storage;
+ _storage = 0;
+ delete storage;
+ storage = 0;
+ if (reader->migrateTo(writer)) {
+ qDebug() << "Migration finished!";
+ saveBackendSettings(backend, settings);
+ return true;
+ }
+ return false;
+ qWarning() << qPrintable(QString("Core::migrateDb(): unable to migrate storage backend! (No migration writer for %1)").arg(backend));
+ }
+
+ // inform the user why we cannot merge
+ if (!_storage) {
+ qWarning() << "No currently active backend. Skipping migration.";
+ }
+ else if (!reader) {
+ qWarning() << "Currently active backend does not support migration:" << qPrintable(_storage->displayName());
+ }
+ if (writer) {
+ qWarning() << "New backend does not support migration:" << qPrintable(backend);
+ }
+
+ // so we were unable to merge, but let's create a user \o/
+ _storage = storage;
+ createUser();
+ return true;
+}
+
+
+void Core::createUser()
+{
+ QTextStream out(stdout);
+ QTextStream in(stdin);
+ out << "Add a new user:" << endl;
+ out << "Username: ";
+ out.flush();
+ QString username = in.readLine().trimmed();
+
+ disableStdInEcho();
+ out << "Password: ";
+ out.flush();
+ QString password = in.readLine().trimmed();
+ out << endl;
+ out << "Repeat Password: ";
+ out.flush();
+ QString password2 = in.readLine().trimmed();
+ out << endl;
+ enableStdInEcho();
+
+ if (password != password2) {
+ qWarning() << "Passwords don't match!";
+ return;
+ }
+ if (password.isEmpty()) {
+ qWarning() << "Password is empty!";
+ return;
+ }
+
+ if (_configured && _storage->addUser(username, password).isValid()) {
+ out << "Added user " << username << " successfully!" << endl;
+ }
+ else {
+ qWarning() << "Unable to add user:" << qPrintable(username);
+ }
+}
+
+
+void Core::changeUserPass(const QString &username)
+{
+ QTextStream out(stdout);
+ QTextStream in(stdin);
+ UserId userId = _storage->getUserId(username);
+ if (!userId.isValid()) {
+ out << "User " << username << " does not exist." << endl;
+ return;
+ }
+
+ out << "Change password for user: " << username << endl;
+
+ disableStdInEcho();
+ out << "New Password: ";
+ out.flush();
+ QString password = in.readLine().trimmed();
+ out << endl;
+ out << "Repeat Password: ";
+ out.flush();
+ QString password2 = in.readLine().trimmed();
+ out << endl;
+ enableStdInEcho();
+
+ if (password != password2) {
+ qWarning() << "Passwords don't match!";
+ return;
+ }
+ if (password.isEmpty()) {
+ qWarning() << "Password is empty!";
+ return;
+ }
+
+ if (_configured && _storage->updateUser(userId, password)) {
+ out << "Password changed successfully!" << endl;
+ }
+ else {
+ qWarning() << "Failed to change password!";
+ }
+}
+
+
+AbstractSqlMigrationReader *Core::getMigrationReader(Storage *storage)
+{
+ if (!storage)
+ return 0;
+
+ AbstractSqlStorage *sqlStorage = qobject_cast<AbstractSqlStorage *>(storage);
+ if (!sqlStorage) {
+ qDebug() << "Core::migrateDb(): only SQL based backends can be migrated!";
+ return 0;
+ }
+
+ return sqlStorage->createMigrationReader();
+}
+
+
+AbstractSqlMigrationWriter *Core::getMigrationWriter(Storage *storage)
+{
+ if (!storage)
+ return 0;
+
+ AbstractSqlStorage *sqlStorage = qobject_cast<AbstractSqlStorage *>(storage);
+ if (!sqlStorage) {
+ qDebug() << "Core::migrateDb(): only SQL based backends can be migrated!";
+ return 0;
+ }
+
+ return sqlStorage->createMigrationWriter();