1 /***************************************************************************
2 * Copyright (C) 2010 by Ixonos Plc *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; version 2 of the License. *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU General Public License for more details. *
13 * You should have received a copy of the GNU General Public License *
14 * along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
17 ***************************************************************************/
20 #include <qtrapids/dbus.hpp>
23 #include <QtGui/QMenuBar>
24 #include <QtGui/QToolBar>
26 #include <QFileDialog>
27 #include <QMessageBox>
28 //#include <QTreeWidgetItem>
29 #include <QApplication>
30 #include <QPluginLoader>
32 #include "DownloadView.h"
34 #include "PreferencesDialog.h"
35 #include "ColumnSelectorDialog.h"
37 #include "MainWindow.h"
42 const QString ABOUT_TEXT
43 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
44 "\nQt and Libtorrent."
45 "\n\nURL: http://qtrapids.garage.maemo.org/"
46 "\n\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
47 "\nDenis Zalevskiy, denis.zalewsky@gmail.com"
48 "\n\nIxonos Plc, Finland\n"));
50 const QString PLUGINS_DIR = "plugins";
53 MainWindow::MainWindow() :
54 QMainWindow(), // Superclass
59 startDaemonAction_(NULL),
60 stopDaemonAction_(NULL),
61 preferencesDialog_(NULL),
62 settings_(QCoreApplication::organizationName()
63 , QCoreApplication::applicationName()),
65 server_(QtRapidsServer::staticInterfaceName()
66 , "/qtrapids", QDBusConnection::sessionBus())
70 setWindowTitle("QtRapids");
73 QMenuBar *menuBar = new QMenuBar();
74 QMenu *tempMenu = NULL;
76 tempMenu = menuBar->addMenu(tr("&File"));
77 QAction *openAction = tempMenu->addAction(tr("&Open"));
78 QAction *removeAction = tempMenu->addAction(tr("&Remove"));
79 removeAction->setEnabled(false);
81 startDaemonAction_ = tempMenu->addAction(tr("S&tart daemon"));
82 stopDaemonAction_ = tempMenu->addAction(tr("Sto&p daemon"));
83 startDaemonAction_->setEnabled(false);
85 QAction *quitAction = tempMenu->addAction(tr("&Quit"));
87 tempMenu = menuBar->addMenu(tr("&View"));
88 QAction *columnsAction = tempMenu->addAction(tr("&Columns"));
90 tempMenu = menuBar->addMenu(tr("&Settings"));
91 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
93 tempMenu = menuBar->addMenu(tr("&Help"));
94 QAction *aboutAction = tempMenu->addAction(tr("&About"));
95 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
98 connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
99 connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
100 connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
101 connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
102 connect(startDaemonAction_, SIGNAL(triggered()), this, SLOT(on_startDaemonAction_clicked()));
103 connect(stopDaemonAction_, SIGNAL(triggered()), this, SLOT(on_stopDaemonAction_clicked()));
104 connect(columnsAction, SIGNAL(triggered()), this, SLOT(on_columnsAction_clicked()));
105 connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
106 connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
107 connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
109 // TABWIDGET (central widget)
110 tabWidget_ = new QTabWidget();
111 tabWidget_->setTabsClosable(true);
113 /// @todo Exception handling
114 dlView_ = new DownloadView(this);
115 seedView_ = new SeedView(this);
116 tabWidget_->addTab(dlView_, tr("Downloads"));
117 tabWidget_->addTab(seedView_, tr("Seeds"));
118 connect(dlView_, SIGNAL(itemSelectionChanged()), this,
119 SLOT(on_downloadItemSelectionChanged()));
121 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
122 SLOT(on_seedItemSelectionChanged()));
124 // Tab widget as central widget.
125 setCentralWidget(tabWidget_);
128 QToolBar *toolBar = new QToolBar();
129 toolBar->addAction(tr("Open"));
130 removeAction = toolBar->addAction(tr("Remove"));
131 removeAction->setEnabled(false);
132 addToolBar(Qt::BottomToolBarArea, toolBar);
134 connect(this, SIGNAL(itemSelected(bool)), removeAction,
135 SLOT(setEnabled(bool)));
136 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
137 SLOT(handleToolBarAction(QAction*)));
138 connect (tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_tabCloseRequested(int)));
140 connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)),
141 this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
143 connect(&server_, SIGNAL(sessionTerminated()), this, SLOT(on_serverTerminated()));
145 // connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
146 // this, SLOT(on_alert(std::auto_ptr<Alert>)));
154 MainWindow::~MainWindow()
156 settings_.setValue("geometry", saveGeometry());
159 // ===================== Implements PluginInterface =========================
160 /// @todo add PluginInterface parameter to request plugin name
161 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin)
163 #ifdef QTRAPIDS_DEBUG
164 qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
167 if (plugin && plugin->identifier() == "SearchPlugin") {
168 searchWidget_ = widget;
173 tabWidget_->addTab(widget, tr("Search"));
177 /// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately
178 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
180 #ifdef QTRAPIDS_DEBUG
181 qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
184 if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
185 int index = tabWidget_->addTab(widget, tr("Results"));
186 tabWidget_->setCurrentIndex(index);
187 //layout_->addWidget(widget);
190 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
194 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
198 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
202 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
206 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
208 if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
209 QString sourceFile = param.toString();
211 // Get the source files name from the full path:
212 QFileInfo fInfo(sourceFile);
213 QString targetFile = fInfo.fileName();
214 targetFile = settings_.value("download/directory").toString() + "/" + targetFile;
216 // Copy temoporary file to Downloads directory...
217 if (!QFile::copy(sourceFile, targetFile)) {
218 qDebug() << "File copying failed";
221 // If copying was successful, remove the original temporary file.
222 QFile::remove(sourceFile);
225 /// @todo Torrent bencoding validity should be checked before starting(?)
226 // ...and start the torrent:
227 on_torrentFileSelected(targetFile);
229 } else if (req == qtrapids::PluginHostInterface::READ_BUFFER) {
230 // Create torrent information from char* buffer and start.
231 StartTorrentFromBufferData(param.toByteArray().constData(), param.toByteArray().size());
238 //=========================== PRIVATE ================================
240 void MainWindow::LoadPlugins()
242 // Get plugin directories from
243 QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList();
244 QStringList nameFilters("*.so");
246 /// @todo enable "application directory" for plugin search in development/debug mode only. In release version
247 /// search plugins directory under $HOME/.qtrapids or system library paths
248 pluginDirsTmp << qApp->applicationDirPath();
249 pluginDirsTmp.removeDuplicates();
251 foreach (QString dir, pluginDirsTmp) {
252 pluginDirs_.append(QDir(dir));
255 foreach (QDir dir, pluginDirs_) {
257 if (dir.cd(PLUGINS_DIR)) {
259 foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) {
260 QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
262 // If plugin not loaded from another directory, then load
263 if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) {
265 if (pluginLoader.load()) {
266 qDebug() << "Plugin loaded: " << fileName;
268 qWarning() << "Plugin load failed: " << pluginLoader.errorString();
271 QObject *baseInstance = pluginLoader.instance();
273 qDebug() << "Base instance = NULL.";
276 qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
279 qDebug() << "Cast failed.";
281 qtrapids::PluginInterface::Info info;
282 info.directory = dir.path();
283 qDebug() << dir.path();
284 plugin->initialize(this, info);
285 pluginFileNames_ += fileName;
288 qWarning() << "Plugin "
290 << " already loaded from another directory, or not a valid library file";
295 qWarning() << PLUGINS_DIR << "directory not accessible or does not exist in " << dir.path();
301 void MainWindow::RestoreSettings()
304 // Restore previous main window geometry:
305 QVariant geometry(settings_.value("geometry"));
306 if (!geometry.isNull()) {
307 qDebug() << "restoring geometry";
308 restoreGeometry(geometry.toByteArray());
311 // Restore DownloadView columns:
312 dlView_->restoreView();
314 // Restore torrent session settings to server:
315 qtrapids::ParamsMap_t options;
316 options["net/downloadRate"] = settings_.value("net/downloadRate").toString();
317 options["net/uploadRate"] = settings_.value("net/uploadRate").toString();
318 server_.setOptions(options);
322 // Opens torrent information from buffer data and adds torrent to session
323 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
328 // =========================== PRIVATE SLOTS =================================
329 void MainWindow::on_openAction_clicked()
331 QString filename = QFileDialog::getOpenFileName( this, tr("Open torrent file"), QString(), tr("Torrent files (*.torrent)") );
332 on_torrentFileSelected(filename);
334 QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
335 dialog->setFileMode(QFileDialog::ExistingFile);
336 connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
341 void MainWindow::on_removeAction_clicked()
343 QString hash = dlView_->prepareRemoveSelected();
345 server_.removeTorrent(hash);
347 qDebug() << "Exception while removing torrent";
352 void MainWindow::on_quitAction_clicked()
358 void MainWindow::on_startDaemonAction_clicked()
361 /// @todo create signal that signals server startup and
362 /// enable controls in the handler slot
363 stopDaemonAction_->setEnabled(true);
364 startDaemonAction_->setEnabled(false);
368 void MainWindow::on_stopDaemonAction_clicked()
370 server_.terminateSession();
374 void MainWindow::on_serverTerminated()
376 stopDaemonAction_->setEnabled(false);
377 startDaemonAction_->setEnabled(true);
380 void MainWindow::on_columnsAction_clicked()
382 ColumnSelectorDialog *dialog = new ColumnSelectorDialog(dlView_);
386 // dialog->activateWindow();
387 qDebug() << "dialog exit";
389 if (dialog->result() == QDialog::Accepted) {
396 void MainWindow::on_preferencesAction_clicked()
398 if (!preferencesDialog_) {
399 preferencesDialog_ = new PreferencesDialog(this, 0, &server_);
402 preferencesDialog_->show();
403 preferencesDialog_->raise();
404 preferencesDialog_->activateWindow();
408 void MainWindow::on_aboutAction_clicked()
410 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
414 void MainWindow::on_aboutQtAction_clicked()
416 QMessageBox::aboutQt (this, tr("About Qt"));
420 void MainWindow::on_tabWidget_tabCloseRequested(int index)
423 int searchWidgetIndex = tabWidget_->indexOf(searchWidget_);
425 // Allow closing other tabs than the first two
426 // TODO The first two may well be closable, just add "show tabs" action for these in the menu
427 if (index != 0 && index != 1 && index != searchWidgetIndex) {
428 QWidget *remove = tabWidget_->widget(index);
429 tabWidget_->removeTab(index);
436 void MainWindow::on_downloadItemSelectionChanged()
438 #ifdef QTRAPIDS_DEBUG
439 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
441 if (dlView_->currentItem() != NULL) {
442 emit(itemSelected(true));
444 emit(itemSelected(false));
449 void MainWindow::on_seedItemSelectionChanged()
451 #ifdef QTRAPIDS_DEBUG
452 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
454 if (seedView_->currentItem() != NULL) {
455 emit(itemSelected(true));
457 emit(itemSelected(false));
462 void MainWindow::handleToolBarAction(QAction* action)
464 if (action->text() == "Open") {
465 on_openAction_clicked();
466 } else if (action->text() == "Remove") {
467 on_removeAction_clicked();
472 void MainWindow::on_torrentFileSelected(QString file)
474 #ifdef QTRAPIDS_DEBUG
475 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
477 // Torrent filename empty, do nothing.
482 // Otherwise add torrent
483 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
484 // save_path is the only mandatory parameter, rest are optional.
485 //addParams.storage_mode = libtorrent::storage_mode_allocate;
487 server_.addTorrent(file, settings_.value("download/directory").toString()
490 qDebug() << "Exception adding torrent";
495 void MainWindow::on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
497 #ifdef QTRAPIDS_DEBUG
498 qDebug() << "got alert";
500 dlView_->updateItem(info, other_info);
503 } // namespace qtrapids