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 ***************************************************************************/
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QApplication>
28 #include <QPluginLoader>
30 #include "DownloadView.h"
32 #include "PreferencesDialog.h"
34 #include "MainWindow.h"
36 const QString ABOUT_TEXT
37 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
38 "\nQt and Libtorrent."
39 "\n\nURL: http://qtrapids.garage.maemo.org/"
40 "\n\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
41 "\nDenis Zalevskiy, denis.zalewsky@ixonos.com"
42 "\n\nIxonos Plc, Finland\n"));
44 const QString PLUGINS_DIR = "plugins";
47 MainWindow::MainWindow():
48 QMainWindow(), // Superclass
53 preferencesDialog_(NULL),
60 QMenuBar *menuBar = new QMenuBar();
61 QMenu *tempMenu = NULL;
63 tempMenu = menuBar->addMenu(tr("&File"));
64 QAction *openAction = tempMenu->addAction(tr("&Open"));
65 QAction *removeAction = tempMenu->addAction(tr("&Remove"));
66 removeAction->setEnabled(false);
67 QAction *quitAction = tempMenu->addAction(tr("&Quit"));
69 tempMenu = menuBar->addMenu(tr("&Settings"));
70 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
72 tempMenu = menuBar->addMenu(tr("&Help"));
73 QAction *aboutAction = tempMenu->addAction(tr("&About"));
74 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
77 connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
78 connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
79 connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
80 connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
81 connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
82 connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
83 connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
85 // TABWIDGET (central widget)
86 tabWidget_ = new QTabWidget();
87 tabWidget_->setTabsClosable(true);
89 /// @todo Exception handling
90 dlView_ = new DownloadView(this);
91 seedView_ = new SeedView(this);
92 tabWidget_->addTab(dlView_, tr("Downloads"));
93 tabWidget_->addTab(seedView_, tr("Seeds"));
94 connect(dlView_, SIGNAL(itemSelectionChanged()), this,
95 SLOT(on_downloadItemSelectionChanged()));
96 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
97 SLOT(on_seedItemSelectionChanged()));
100 // Tab widget as central widget.
101 setCentralWidget(tabWidget_);
104 QToolBar *toolBar = new QToolBar();
105 toolBar->addAction(tr("Open"));
106 removeAction = toolBar->addAction(tr("Remove"));
107 removeAction->setEnabled(false);
108 addToolBar(Qt::TopToolBarArea, toolBar);
110 connect(this, SIGNAL(itemSelected(bool)), removeAction,
111 SLOT(setEnabled(bool)));
112 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
113 SLOT(handleToolBarAction(QAction*)));
114 connect (tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_tabCloseRequested(int)));
116 connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
117 this, SLOT(on_alert(std::auto_ptr<Alert>)));
124 MainWindow::~MainWindow()
128 // ===================== Implements PluginInterface =========================
129 /// @todo add PluginInterface parameter to request plugin name
130 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin)
132 #ifdef QTRAPIDS_DEBUG
133 qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
136 if (plugin && plugin->identifier() == "SearchPlugin") {
137 searchWidget_ = widget;
142 tabWidget_->addTab(widget, tr("Search"));
146 /// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately
147 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
149 #ifdef QTRAPIDS_DEBUG
150 qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
153 if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
154 int index = tabWidget_->addTab(widget, tr("Results"));
155 tabWidget_->setCurrentIndex(index);
156 //layout_->addWidget(widget);
159 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
163 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
167 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
171 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
175 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
177 if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
178 QString sourceFile = param.toString();
180 // Get the source files name from the full path:
181 QFileInfo fInfo(sourceFile);
182 QString targetFile = fInfo.fileName();
183 targetFile = settings_.value("download/directory").toString() + "/" + targetFile;
185 // Copy temoporary file to Downloads directory...
186 if (!QFile::copy(sourceFile, targetFile)) {
187 qDebug() << "File copying failed";
190 // If copying was successful, remove the original temporary file.
191 QFile::remove(sourceFile);
194 /// @todo Torrent bencoding validity should be checked before starting(?)
195 // ...and start the torrent:
196 on_torrentFileSelected(targetFile);
198 } else if (req == qtrapids::PluginHostInterface::READ_BUFFER) {
199 // Create torrent information from char* buffer and start.
200 StartTorrentFromBufferData(param.toByteArray().constData(), param.toByteArray().size());
207 //=========================== PRIVATE ================================
209 void MainWindow::LoadPlugins()
211 // Get plugin directories from
212 QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList();
213 QStringList nameFilters("*.so");
215 /// @todo enable "application directory" for plugin search in development/debug mode only. In release version
216 /// search plugins directory under $HOME/.qtrapids or system library paths
217 pluginDirsTmp << qApp->applicationDirPath();
218 pluginDirsTmp.removeDuplicates();
220 foreach (QString dir, pluginDirsTmp) {
221 pluginDirs_.append(QDir(dir));
224 foreach (QDir dir, pluginDirs_) {
226 if (dir.cd(PLUGINS_DIR)) {
228 foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) {
229 QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
231 // If plugin not loaded from another directory, then load
232 if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) {
234 if (pluginLoader.load()) {
235 qDebug() << "Plugin loaded: " << fileName;
237 qWarning() << "Plugin load failed: " << pluginLoader.errorString();
240 QObject *baseInstance = pluginLoader.instance();
242 qDebug() << "Base instance = NULL.";
245 qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
248 qDebug() << "Cast failed.";
250 qtrapids::PluginInterface::Info info;
251 info.directory = dir.path();
252 qDebug() << dir.path();
253 plugin->initialize(this, info);
254 pluginFileNames_ += fileName;
257 qWarning() << "Plugin "
259 << " already loaded from another directory, or not a valid library file";
264 qWarning() << PLUGINS_DIR << "directory not accessible or does not exist in " << dir.path();
270 void MainWindow::RestoreSettings()
272 btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt());
273 btSession_.setDownloadRateLimit(settings_.value("network/downloadRate").toInt());
277 // Opens torrent information from buffer data and adds torrent to session
278 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
280 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
281 /// @todo Should typedef libtorrent::torrent_info to something
282 AddTorrentParams addParams;
283 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
284 new libtorrent::torrent_info(data, size);
285 addParams.ti = tiTmp;
286 // save_path is the only mandatory parameter, rest are optional.
287 addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
288 //addParams.storage_mode = libtorrent::storage_mode_allocate;
289 qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
290 dlView_->newItem(handle);
291 // torrentHandles_.push_back(handlePtr);
292 #ifdef QTRAPIDS_DEBUG
293 qDebug() << "Is valid: " << handle.isValid();
297 // =========================== PRIVATE SLOTS =================================
298 void MainWindow::on_openAction_clicked()
300 QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
301 dialog->setFileMode(QFileDialog::ExistingFile);
302 connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
306 void MainWindow::on_removeAction_clicked()
308 qtrapids::QTorrentHandle handle = dlView_->removeSelected();
309 btSession_.removeTorrent(handle);
313 void MainWindow::on_quitAction_clicked()
319 void MainWindow::on_preferencesAction_clicked()
321 if (!preferencesDialog_) {
322 preferencesDialog_ = new PreferencesDialog(this, NULL, &btSession_);
325 preferencesDialog_->show();
326 preferencesDialog_->raise();
327 preferencesDialog_->activateWindow();
331 void MainWindow::on_aboutAction_clicked()
333 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
337 void MainWindow::on_aboutQtAction_clicked()
339 QMessageBox::aboutQt (this, tr("About Qt"));
343 void MainWindow::on_tabWidget_tabCloseRequested(int index)
346 int searchWidgetIndex = tabWidget_->indexOf(searchWidget_);
348 // Allow closing other tabs than the first two
349 // TODO The first two may well be closable, just add "show tabs" action for these in the menu
350 if (index != 0 && index != 1 && index != searchWidgetIndex) {
351 QWidget *remove = tabWidget_->widget(index);
352 tabWidget_->removeTab(index);
359 void MainWindow::on_downloadItemSelectionChanged()
361 #ifdef QTRAPIDS_DEBUG
362 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
364 if (dlView_->currentItem() != NULL) {
365 emit(itemSelected(true));
367 emit(itemSelected(false));
372 void MainWindow::on_seedItemSelectionChanged()
374 #ifdef QTRAPIDS_DEBUG
375 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
377 if (seedView_->currentItem() != NULL) {
378 emit(itemSelected(true));
380 emit(itemSelected(false));
385 void MainWindow::handleToolBarAction(QAction* action)
387 if (action->text() == "Open") {
388 on_openAction_clicked();
389 } else if (action->text() == "Remove") {
390 on_removeAction_clicked();
395 void MainWindow::on_torrentFileSelected(const QString& file)
397 #ifdef QTRAPIDS_DEBUG
398 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
400 // Torrent filename empty, do nothing.
405 // Otherwise add torrent
406 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
407 /// @todo Should typedef libtorrent::torrent_info to something
408 AddTorrentParams addParams;
409 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
410 new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
411 addParams.ti = tiTmp;
412 // save_path is the only mandatory parameter, rest are optional.
413 addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
414 //addParams.storage_mode = libtorrent::storage_mode_allocate;
415 qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
416 dlView_->newItem(handle);
417 // torrentHandles_.push_back(handlePtr);
418 #ifdef QTRAPIDS_DEBUG
419 qDebug() << "Is valid: " << handle.isValid();
423 void MainWindow::on_alert(std::auto_ptr<Alert> al)
425 if (al.get() != NULL) {
427 // << "MainWindow::on_torrentAlert(): "
428 // << QString::fromStdString(al->message());
430 TorrentAlert *torrentAlert
431 = dynamic_cast<TorrentAlert*> (al.get());
434 qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
435 dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
442 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
444 for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
445 if (torrentHandles_.at(i).get() == handlePtr.get()) {