Very rough initial implementation of torrent adding working.
[qtrapids] / src / qml-client / MainPageHandler.cpp
1 /*
2     <one line to give the program's name and a brief idea of what it does.>
3     Copyright (C) 2012  Ixonos Plc
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <QtCore/QString>
21 #include <QtCore/QDebug>
22 #include <QDeclarativeContext>
23 #include <QDeclarativeEngine>
24 //#include <QFileDialog>
25 #include <meegotouch/mwindow.h>
26
27 #include "MainPageHandler.h"
28 #include "qtrapids/dbus.hpp"
29
30
31 namespace qtrapids
32 {
33
34 MainPageHandler::MainPageHandler(QDeclarativeEngine *engine, QObject *parent) :
35     QObject(parent),
36     engine_(engine),
37     settings_(QCoreApplication::organizationName(),
38               QCoreApplication::applicationName()),
39     server_(QtRapidsServer::staticInterfaceName(),
40             "/qtrapids", QDBusConnection::sessionBus())
41 {
42     QDBusConnection dbus = QDBusConnection::sessionBus();
43     dbus.registerObject("/qtrapids_gui", this);
44     dbus.registerService("com.ixonos.qtrapids_gui");
45
46     connectToServer();
47     restoreSettings();
48     // Add to QML context. This can then be used in QML context.
49     QDeclarativeContext *context = engine->rootContext();
50     context->setContextProperty("downloadModel", &downloadModel_);
51     context->setContextProperty("mainPageHandler", this);
52     
53     connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
54             this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
55     // TODO: Check if this is a problem, as connection is done also in connectToServer()
56 //     connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
57 //             this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
58     connect(&server_, SIGNAL(sessionTerminated()), this, SLOT(on_serverTerminated()));
59 }
60
61 MainPageHandler::~MainPageHandler()
62 {
63 }
64
65 void MainPageHandler::connectToServer() {
66         qDBusRegisterMetaType<qtrapids::TorrentState>();
67         qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
68
69         connect(&server_,
70                 SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
71                 this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
72         // Request server state. 
73         // NOTE: This call starts qtrapids-server automatically with the GUI, 
74         // if the .service file is in /usr/share/dbus-1/services/
75         server_.getState();
76 }
77
78 void MainPageHandler::restoreSettings()
79 {
80
81         // TODO: Rewrite restore to QML age..
82         // Restore DownloadView columns:
83         //dlView_->restoreView();
84
85         // Restore torrent session settings to server:
86         qtrapids::ParamsMap_t options;
87         options["net/downloadRate"] = settings_.value("net/downloadRate").toString();
88         options["net/uploadRate"] = settings_.value("net/uploadRate").toString();
89         server_.setOptions(options);
90 }
91
92 void MainPageHandler::fileSelectorOpen()
93 {
94     qDebug() << Q_FUNC_INFO;
95     QWidget* widget = qobject_cast<QWidget*>(MApplication::activeWindow());
96     QString filename = QFileDialog::getOpenFileName(widget, tr("Open torrent file"), QString(), tr("Torrent files (*.torrent)") );
97     on_torrentFileSelected(filename);
98 }
99
100
101 void MainPageHandler::on_torrentFileSelected(const QString& fileUrl)
102 {
103     qDebug() << Q_FUNC_INFO << " file selected: " << fileUrl;
104
105     QUrl filePathUrl(fileUrl);
106
107     if (filePathUrl.scheme() != "file" && filePathUrl.scheme() != "") {
108         qDebug() << Q_FUNC_INFO << ": currently file:// scheme only supported";
109     }
110
111     // Torrent filename empty, do nothing.
112     if (filePathUrl.toLocalFile().isEmpty()) {
113         qDebug() << Q_FUNC_INFO << ": empty torrent file path. No action.";
114         return;
115     }
116
117     // Otherwise add torrent
118     // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
119     // save_path is the only mandatory parameter, rest are optional.
120     //addParams.storage_mode = libtorrent::storage_mode_allocate;
121     try {
122         // TODO: Enable this once server_ is in place
123         server_.addTorrent(filePathUrl.toString(QUrl::RemoveScheme),
124                            settings_.value("download/directory").toString(),
125                            ParamsMap_t());
126     } catch (...) {
127         qDebug() << Q_FUNC_INFO << "Exception adding torrent";
128     }
129 }
130
131 void MainPageHandler::on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
132 {
133         qDebug() << "got alert";
134         downloadModel_.updateItem(info, other_info);
135 }
136
137 } //namespace qtrapids