Very rough initial implementation of torrent adding working.
[qtrapids] / src / qml-client / MainPageHandler.cpp
index 26a7760..0d3e94f 100644 (file)
 
 #include <QtCore/QString>
 #include <QtCore/QDebug>
-#include <QFileDialog>
+#include <QDeclarativeContext>
+#include <QDeclarativeEngine>
+//#include <QFileDialog>
 #include <meegotouch/mwindow.h>
 
 #include "MainPageHandler.h"
+#include "qtrapids/dbus.hpp"
 
 
 namespace qtrapids
 {
 
-MainPageHandler::MainPageHandler(QObject *parent) :
-    QObject(parent)
+MainPageHandler::MainPageHandler(QDeclarativeEngine *engine, QObject *parent) :
+    QObject(parent),
+    engine_(engine),
+    settings_(QCoreApplication::organizationName(),
+              QCoreApplication::applicationName()),
+    server_(QtRapidsServer::staticInterfaceName(),
+            "/qtrapids", QDBusConnection::sessionBus())
 {
+    QDBusConnection dbus = QDBusConnection::sessionBus();
+    dbus.registerObject("/qtrapids_gui", this);
+    dbus.registerService("com.ixonos.qtrapids_gui");
+
+    connectToServer();
+    restoreSettings();
+    // Add to QML context. This can then be used in QML context.
+    QDeclarativeContext *context = engine->rootContext();
+    context->setContextProperty("downloadModel", &downloadModel_);
+    context->setContextProperty("mainPageHandler", this);
+    
+    connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
+            this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
+    // TODO: Check if this is a problem, as connection is done also in connectToServer()
+//     connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
+//             this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
+    connect(&server_, SIGNAL(sessionTerminated()), this, SLOT(on_serverTerminated()));
 }
 
 MainPageHandler::~MainPageHandler()
 {
 }
 
+void MainPageHandler::connectToServer() {
+        qDBusRegisterMetaType<qtrapids::TorrentState>();
+        qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
+
+        connect(&server_,
+                SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
+                this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
+        // Request server state. 
+        // NOTE: This call starts qtrapids-server automatically with the GUI, 
+        // if the .service file is in /usr/share/dbus-1/services/
+        server_.getState();
+}
+
+void MainPageHandler::restoreSettings()
+{
+
+        // TODO: Rewrite restore to QML age..
+        // Restore DownloadView columns:
+        //dlView_->restoreView();
+
+        // Restore torrent session settings to server:
+        qtrapids::ParamsMap_t options;
+        options["net/downloadRate"] = settings_.value("net/downloadRate").toString();
+        options["net/uploadRate"] = settings_.value("net/uploadRate").toString();
+        server_.setOptions(options);
+}
+
 void MainPageHandler::fileSelectorOpen()
 {
     qDebug() << Q_FUNC_INFO;
@@ -46,12 +98,19 @@ void MainPageHandler::fileSelectorOpen()
 }
 
 
-void MainPageHandler::on_torrentFileSelected(const QString& file)
+void MainPageHandler::on_torrentFileSelected(const QString& fileUrl)
 {
-    qDebug() << Q_FUNC_INFO << " file selected: " << file;
+    qDebug() << Q_FUNC_INFO << " file selected: " << fileUrl;
+
+    QUrl filePathUrl(fileUrl);
+
+    if (filePathUrl.scheme() != "file" && filePathUrl.scheme() != "") {
+        qDebug() << Q_FUNC_INFO << ": currently file:// scheme only supported";
+    }
 
     // Torrent filename empty, do nothing.
-    if (file == "") {
+    if (filePathUrl.toLocalFile().isEmpty()) {
+        qDebug() << Q_FUNC_INFO << ": empty torrent file path. No action.";
         return;
     }
 
@@ -61,12 +120,18 @@ void MainPageHandler::on_torrentFileSelected(const QString& file)
     //addParams.storage_mode = libtorrent::storage_mode_allocate;
     try {
         // TODO: Enable this once server_ is in place
-//         server_.addTorrent(file,
-//                            settings_.value("download/directory").toString(),
-//                            ParamsMap_t());
+        server_.addTorrent(filePathUrl.toString(QUrl::RemoveScheme),
+                           settings_.value("download/directory").toString(),
+                           ParamsMap_t());
     } catch (...) {
         qDebug() << Q_FUNC_INFO << "Exception adding torrent";
     }
 }
 
+void MainPageHandler::on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
+{
+        qDebug() << "got alert";
+        downloadModel_.updateItem(info, other_info);
+}
+
 } //namespace qtrapids