Very rough initial implementation of torrent adding working.
authorlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Sun, 19 Feb 2012 19:14:11 +0000 (19:14 +0000)
committerlvaatamoinen <lassi.vaatamoinen@ixonos.com>
Sun, 19 Feb 2012 19:14:11 +0000 (19:14 +0000)
TODO: Status indication do UI, torrent removal, state restoring.

git-svn-id: file:///svnroot/qtrapids/trunk@89 42ac0dd5-4c8c-4c71-bb3e-ecdfe252ffda

src/qml-client/CMakeLists.txt
src/qml-client/MainPageHandler.cpp
src/qml-client/MainPageHandler.h
src/qml-client/main.cpp
src/qml-client/models/QDeclarativeDownloadListModel.cpp
src/qml-client/models/QDeclarativeDownloadListModel.h
src/qml-client/qml/TorrentPickerPage.qml

index a9d4dbc..68ce22b 100644 (file)
@@ -24,7 +24,9 @@ INCLUDE_DIRECTORIES(
 
 SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/proxy.cpp ${CMAKE_CURRENT_SOURCE_DIR}/proxy.h PROPERTIES GENERATED 1)
 
+# TODO: Use proxy from dedicated directory, so we don't generate them in all client dirs.
 SET(MOC_HEADERS
+    ./proxy.h
     ./MainPageHandler.h
     ./models/QDeclarativeDownloadListModel.h
     ./models/QDeclarativeDownloadListModel_p.h
@@ -32,6 +34,7 @@ SET(MOC_HEADERS
 
 SET(SRC
     ./main.cpp
+    ./proxy.cpp
     ./MainPageHandler.cpp
     ./models/QDeclarativeDownloadListModel.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
index ada83d1..d0321bb 100644 (file)
     with this program; if not, write to the Free Software Foundation, Inc.,
     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
-
-
 #ifndef MAINPAGEHANDLER_H
 #define MAINPAGEHANDLER_H
 
 // Used in this class and main.cpp, so include here
 #include <meegotouch/mapplication.h> 
 #include <QtCore/QObject>
+#include <QtCore/QSettings>
+
+#include "proxy.h"
+#include "models/QDeclarativeDownloadListModel.h"
+
+class QDeclarativeEngine;
 
 namespace qtrapids
 {
@@ -32,16 +36,26 @@ class MainPageHandler : public QObject
 {
     Q_OBJECT
 public:
-    MainPageHandler(QObject *parent = 0);
+    MainPageHandler(QDeclarativeEngine *engine, QObject *parent = 0);
     virtual ~MainPageHandler();
 
     Q_INVOKABLE void fileSelectorOpen();
-
+    Q_INVOKABLE void on_torrentFileSelected(const QString& fileUrl);
 private slots:
-    void on_torrentFileSelected(const QString& file);
+    //void on_torrentFileSelected(const QString& file);
+    void on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info);
+private:
+    void connectToServer();
+    void restoreSettings();
 
 private:
     Q_DISABLE_COPY(MainPageHandler);
+    
+    QDeclarativeDownloadListModel downloadModel_;
+
+    QDeclarativeEngine *engine_;
+    QSettings settings_;
+    QtRapidsServer server_;
 };
 
 } // namespace qtrapids
index 674eb3e..3829f4c 100644 (file)
@@ -16,7 +16,7 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 #include <QtDeclarative>
-#include <QDeclarativeContext>
+// #include <QDeclarativeContext>
 #include <QTest>
 #include <QDebug>
 #include <QtDeclarative/qdeclarative.h>
@@ -48,12 +48,12 @@ int main(int argc, char *argv[])
     QDeclarativeView window;
     // Setting the root context properties. Create an QObject-derived
     // object:
-    qtrapids::QDeclarativeDownloadListModel downloadModel;
-    qtrapids::MainPageHandler mainPageHandler;
+    //qtrapids::QDeclarativeDownloadListModel downloadModel;
+    qtrapids::MainPageHandler mainPageHandler(window.engine());
     // ... and add to QML context. This can then be used in QML context.
-    QDeclarativeContext *context = window.rootContext();
-    context->setContextProperty("downloadModel", &downloadModel);
-    context->setContextProperty("mainPageHandler", &mainPageHandler);
+    //QDeclarativeContext *context = window.rootContext();
+    //context->setContextProperty("downloadModel", &downloadModel);
+    //context->setContextProperty("mainPageHandler", &mainPageHandler);
 
     // Initial QML file/resource:
     window.setSource(QUrl("qrc:/main.qml"));
@@ -100,7 +100,7 @@ int main(int argc, char *argv[])
         QTest::qSleep(2000);
     }
     */
-
+/*
     qtrapids::TorrentState editItem;
     for (unsigned i = 0; i < 10; ++i)
     {
@@ -118,6 +118,7 @@ int main(int argc, char *argv[])
         editItem.total_done = 10000+i;
         downloadModel.updateItem(editItem, qtrapids::ParamsMap_t());
     }
+    */
 
     return app.exec();
 }
index e990705..44d4a0d 100644 (file)
@@ -244,34 +244,39 @@ void QDeclarativeDownloadListModel::addItem_(TorrentState const& info,
 void QDeclarativeDownloadListModel::updateItem_(TorrentState const& info, ParamsMap_t)
 {
      qDebug() << Q_FUNC_INFO << " enter";
-    /*
-    item->setData(2, Qt::DisplayRole,
-                  QVariant(GetStatusString((TorrentStatus::Id)info.state)));
-    item->setData(3, Qt::DisplayRole,
-                  QVariant(formatProgress(info.progress)));
-    item->setData(4, Qt::DisplayRole,
-                  QVariant(formatSize(info.down_rate)));
-    item->setData(5, Qt::DisplayRole,
-                  QVariant(formatSize(info.up_rate)));
-    item->setData(6, Qt::DisplayRole,
-                  QString::number(info.seeds) + "/" + QString::number(info.leeches));
-    item->setData(7, Qt::DisplayRole, QString::number(info.ratio));
-
-    // Calculate ETA
-    if (info.down_rate > 0) {
-        qulonglong eta = (info.total_size - info.total_done) / info.down_rate;
-        item->setData(8, Qt::DisplayRole, formatElapsedTime(eta));
-    }       else {
-        item->setData(8, Qt::DisplayRole, "N/A");
+
+    // Check if we even have the item in model. At this point we should...
+    if (!d->items_.contains(info.hash)) {
+        qWarning() << Q_FUNC_INFO << " torrent with hash \'" 
+            << info.hash << "\' not in model";
+        return;
     }
-*/
-    // TODO: Update items data & Emit data changed.
 
-/*
-    // Set color for status text
-    QBrush brushTmp(GetStatusColor((TorrentStatus::Id)info.state));
-    item->setForeground(2, brushTmp);
-    */
+    int row = 0;
+    ItemIndex_t::iterator iterBegin =  d->itemIndexes_.begin();
+    ItemIndex_t::iterator iterEnd =  d->itemIndexes_.end();
+    while (iterBegin != iterEnd) {
+        if (iterBegin.value() == info.hash) {
+            break;
+        }
+        ++row;
+        ++iterBegin;
+    }
+
+    // We iterated past the end of map -> index did not exist.
+    if (iterBegin == iterEnd) {
+        qWarning() << Q_FUNC_INFO << " torrent with hash \'" 
+            << info.hash << "\' not in item index table";
+        return;
+    }
+
+    // Get the model index for item from base class:
+    QModelIndex modelIndex = index(row);
+    // For QHash, insert() replaces existing item with same key.
+    d->items_.insert(info.hash, info);
+
+    // Notify view about the change.
+    emit dataChanged(modelIndex, modelIndex);
 }
 
 
index ee9cadd..88a28b6 100644 (file)
 
 QT_MODULE(Declarative);
 
+
+class QDeclarativeContext;
+
 namespace qtrapids
 {
 
 typedef QHash<QString, TorrentState> DownloadItems_t;
 typedef QMap<int, QString> ItemIndex_t;
 
-class QDeclarativeContext;
 class QDeclarativeDownloadListModelPrivate;
 
 class QDeclarativeDownloadListModel :
index 162dea6..beed7d4 100644 (file)
@@ -51,7 +51,7 @@ Page {
                     text: url
                 }
                 anchors.fill: parent
-                onClicked: { console.log("selected torrent: " + url); }
+                onClicked: { console.log("selected torrent: " + url); mainPageHandler.on_torrentFileSelected(url); }
             }
         }
     }