Finally! The new identities plus a nice shiny settingspage for editing them are done!
[quassel.git] / src / core / core.cpp
index d03ca6b..7da0aee 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
+ *   Copyright (C) 2005-08 by the Quassel Project                          *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -27,6 +27,8 @@
 #include <QMetaObject>
 #include <QMetaMethod>
 
+#include <QCoreApplication>
+
 Core *Core::instanceptr = 0;
 
 Core *Core::instance() {
@@ -41,56 +43,110 @@ void Core::destroy() {
   instanceptr = 0;
 }
 
-Core::Core() {
-  storage = NULL;
+Core::Core()
+  : storage(0)
+{
 }
 
 void Core::init() {
-  CoreSettings s;
-  configured = false;
+  // TODO: Remove this again at some point
+  // Check if old core settings need to be migrated in order to make the switch to the
+  // new location less painful.
+  CoreSettings cs;
+  QVariant foo = cs.databaseSettings();
+  
+  if(!foo.isValid()) {
+    // ok, no settings stored yet. check for old ones.
+#ifdef Q_WS_MAC
+    QSettings os("quassel-irc.org", "Quassel IRC", this);
+#else
+    QSettings os("Quassel IRC Development Team", "Quassel IRC");
+#endif
+    QVariant bar = os.value("Core/DatabaseSettings");
+    if(bar.isValid()) {
+      // old settings available -- migrate!
+      qWarning() << "\n\nOld settings detected. Will migrate core settings to the new location...\nNOTE: GUI style settings won't be migrated!\n";
+#ifdef Q_WS_MAC
+      QSettings ncs("quassel-irc.org", "Quassel Core");
+#else
+      QSettings ncs("Quassel Project", "Quassel Core");
+#endif
+      ncs.setValue("Core/CoreState", os.value("Core/CoreState"));
+      ncs.setValue("Core/DatabaseSettings", os.value("Core/DatabaseSettings"));
+      os.beginGroup("SessionData");
+      foreach(QString group, os.childGroups()) {
+        ncs.setValue(QString("CoreUser/%1/SessionData/Identities").arg(group), os.value(QString("%1/Identities").arg(group)));
+        ncs.setValue(QString("CoreUser/%1/SessionData/Networks").arg(group), os.value(QString("%1/Networks").arg(group)));
+      }
+      os.endGroup();
+#ifdef Q_WS_MAC
+      QSettings ngs("quassel-irc.org", "Quassel Client");
+#else
+      QSettings ngs("Quassel Project", "Quassel Client");
+#endif
+      os.beginGroup("Accounts");
+      foreach(QString key, os.childKeys()) {
+        ngs.setValue(QString("Accounts/%1").arg(key), os.value(key));
+      }
+      foreach(QString group, os.childGroups()) {
+        ngs.setValue(QString("Accounts/%1/AccountData").arg(group), os.value(QString("%1/AccountData").arg(group)));
+      }
+      os.endGroup();
+      os.beginGroup("Geometry");
+      foreach(QString key, os.childKeys()) {
+        ngs.setValue(QString("UI/%1").arg(key), os.value(key));
+      }
+      os.endGroup();
 
-  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.");
+      ncs.sync();
+      ngs.sync();
+      qWarning() << "Migration successfully finished. You may now delete $HOME/.config/Quassel IRC Development Team/ (on Linux).\n\n";
+    }
   }
-  
-  if (!configured) {
+  // END
+
+  configured = false;
+
+  if(!(configured = initStorage(cs.databaseSettings().toMap()))) {
     qWarning("Core is currently not configured!");
   }
-    
+  
   connect(&server, SIGNAL(newConnection()), this, SLOT(incomingConnection()));
-  startListening(s.port());
+  startListening(cs.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) {
+bool Core::initStorage(QVariantMap dbSettings, bool setup) {
+  QString engine = dbSettings["Type"].toString().toLower();
+
+  if(storage) {
     qDebug() << "Deleting old storage object.";
-    delete storage;
-    storage = NULL;
+    storage->deleteLater();
+    storage = 0;
   }
-  
-  storage = new SqliteStorage();
-  if (setup && !storage->setup(dbSettings)) {
-    return false;
+
+  // FIXME register new storageProviders here
+  if(engine == "sqlite" && SqliteStorage::isAvailable()) {
+    storage = new SqliteStorage(this);
+  } else {
+    qWarning() << "Selected StorageBackend is not available:" << dbSettings["Type"].toString();
+    return configured = false;
   }
-  
-  return storage->init(dbSettings);
+
+  if(setup && !storage->setup(dbSettings)) {
+    return configured = false;
+  }
+
+  return configured = storage->init(dbSettings);
+}
+
+bool Core::initStorage(QVariantMap dbSettings) {
+  return initStorage(dbSettings, false);
 }
 
 Core::~Core() {
   qDeleteAll(sessions);
-  if (storage) {
-    delete storage;
-    storage = NULL;
-  }
 }
 
 void Core::restoreState() {
@@ -106,6 +162,7 @@ void Core::restoreState() {
         sess->restoreState(m["State"]);
       }
     }
+    qDebug() << "...done.";
   }
 }
 
@@ -249,13 +306,8 @@ void Core::processCoreSetup(QTcpSocket *socket, QVariantMap &msg) {
     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) {
+
+    if(!initStorage(msg, true)) {
       // notify client to start wizard again
       qWarning("Core is currently not configured!");
       QVariantMap reply;
@@ -299,7 +351,7 @@ QStringList Core::availableStorageProviders() {
     storageProviders.append(SqliteStorage::displayName());
   }
   // TODO: temporary
-  storageProviders.append("MySQL");
+  // storageProviders.append("MySQL");
   
   return storageProviders;
 }