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
SET(SRC
./main.cpp
+ ./proxy.cpp
./MainPageHandler.cpp
./models/QDeclarativeDownloadListModel.cpp
)
#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;
}
-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;
}
//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
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
{
{
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
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <QtDeclarative>
-#include <QDeclarativeContext>
+// #include <QDeclarativeContext>
#include <QTest>
#include <QDebug>
#include <QtDeclarative/qdeclarative.h>
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"));
QTest::qSleep(2000);
}
*/
-
+/*
qtrapids::TorrentState editItem;
for (unsigned i = 0; i < 10; ++i)
{
editItem.total_done = 10000+i;
downloadModel.updateItem(editItem, qtrapids::ParamsMap_t());
}
+ */
return app.exec();
}
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);
}
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 :
text: url
}
anchors.fill: parent
- onClicked: { console.log("selected torrent: " + url); }
+ onClicked: { console.log("selected torrent: " + url); mainPageHandler.on_torrentFileSelected(url); }
}
}
}