2 <one line to give the program's name and a brief idea of what it does.>
3 Copyright (C) 2012 Ixonos Plc
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.
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.
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.
20 #include <QtCore/QString>
21 #include <QtCore/QDebug>
22 #include <QDeclarativeContext>
23 #include <QDeclarativeEngine>
24 //#include <QFileDialog>
25 #include <meegotouch/mwindow.h>
27 #include "MainPageHandler.h"
28 #include "qtrapids/dbus.hpp"
34 MainPageHandler::MainPageHandler(QDeclarativeEngine *engine, QObject *parent) :
37 settings_(QCoreApplication::organizationName(),
38 QCoreApplication::applicationName()),
39 server_(QtRapidsServer::staticInterfaceName(),
40 "/qtrapids", QDBusConnection::sessionBus())
42 QDBusConnection dbus = QDBusConnection::sessionBus();
43 dbus.registerObject("/qtrapids_gui", this);
44 dbus.registerService("com.ixonos.qtrapids_gui");
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);
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()));
61 MainPageHandler::~MainPageHandler()
65 void MainPageHandler::connectToServer() {
66 qDBusRegisterMetaType<qtrapids::TorrentState>();
67 qDBusRegisterMetaType<qtrapids::ParamsMap_t>();
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/
78 void MainPageHandler::restoreSettings()
81 // TODO: Rewrite restore to QML age..
82 // Restore DownloadView columns:
83 //dlView_->restoreView();
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);
92 void MainPageHandler::fileSelectorOpen()
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);
101 void MainPageHandler::on_torrentFileSelected(const QString& fileUrl)
103 qDebug() << Q_FUNC_INFO << " file selected: " << fileUrl;
105 QUrl filePathUrl(fileUrl);
107 if (filePathUrl.scheme() != "file" && filePathUrl.scheme() != "") {
108 qDebug() << Q_FUNC_INFO << ": currently file:// scheme only supported";
111 // Torrent filename empty, do nothing.
112 if (filePathUrl.toLocalFile().isEmpty()) {
113 qDebug() << Q_FUNC_INFO << ": empty torrent file path. No action.";
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;
122 // TODO: Enable this once server_ is in place
123 server_.addTorrent(filePathUrl.toString(QUrl::RemoveScheme),
124 settings_.value("download/directory").toString(),
127 qDebug() << Q_FUNC_INFO << "Exception adding torrent";
131 void MainPageHandler::on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
133 qDebug() << "got alert";
134 downloadModel_.updateItem(info, other_info);
137 } //namespace qtrapids