1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
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 *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QApplication>
30 #include <QPluginLoader>
32 #include "DownloadView.h"
34 #include "PreferencesDialog.h"
36 #include "MainWindow.h"
38 const QString ABOUT_TEXT
39 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
40 "\nQt and Libtorrent."
41 "\n\nURL: http://qtrapids.garage.maemo.org/"
42 "\n\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
43 "\nDenis Zalevskiy, denis.zalewsky@ixonos.com"
44 "\n\nIxonos Plc, Finland\n"));
46 const QString PLUGINS_DIR = "plugins";
49 MainWindow::MainWindow():
50 QMainWindow(), // Superclass
54 preferencesDialog_(NULL),
61 QMenuBar *menuBar = new QMenuBar();
62 QMenu *tempMenu = NULL;
64 tempMenu = menuBar->addMenu(tr("&File"));
65 QAction *openAction = tempMenu->addAction(tr("&Open"));
66 QAction *removeAction = tempMenu->addAction(tr("&Remove"));
67 removeAction->setEnabled(false);
68 QAction *quitAction = tempMenu->addAction(tr("&Quit"));
70 tempMenu = menuBar->addMenu(tr("&Settings"));
71 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
73 tempMenu = menuBar->addMenu(tr("&Help"));
74 QAction *aboutAction = tempMenu->addAction(tr("&About"));
75 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
78 connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
79 connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
80 connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
81 connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
82 connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
83 connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
84 connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
86 // TABWIDGET (central widget)
87 tabWidget_ = new QTabWidget();
88 tabWidget_->setTabsClosable(true);
90 /// @todo Exception handling
91 dlView_ = new DownloadView(this);
92 seedView_ = new SeedView(this);
93 tabWidget_->addTab(dlView_, tr("Downloads"));
94 tabWidget_->addTab(seedView_, tr("Seeds"));
95 connect(dlView_, SIGNAL(itemSelectionChanged()), this,
96 SLOT(on_downloadItemSelectionChanged()));
97 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
98 SLOT(on_seedItemSelectionChanged()));
101 // Tab widget as central widget.
102 setCentralWidget(tabWidget_);
105 QToolBar *toolBar = new QToolBar();
106 toolBar->addAction(tr("Open"));
107 removeAction = toolBar->addAction(tr("Remove"));
108 removeAction->setEnabled(false);
109 addToolBar(Qt::TopToolBarArea, toolBar);
111 connect(this, SIGNAL(itemSelected(bool)), removeAction,
112 SLOT(setEnabled(bool)));
113 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
114 SLOT(handleToolBarAction(QAction*)));
116 connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
117 this, SLOT(on_alert(std::auto_ptr<Alert>)));
123 MainWindow::~MainWindow()
127 // ===================== Implements PluginInterface =========================
128 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type)
130 #ifdef QTRAPIDS_DEBUG
131 qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
133 tabWidget_->addTab(widget, tr("Search"));
137 /// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately
138 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
140 #ifdef QTRAPIDS_DEBUG
141 qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
144 if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
145 int index = tabWidget_->addTab(widget, tr("Test"));
146 tabWidget_->setCurrentIndex(index);
147 //layout_->addWidget(widget);
150 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
154 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
158 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
162 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
166 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
168 if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
169 QString sourceFile = param.toString();
171 // Get the source files name from the full path:
172 QFileInfo fInfo(sourceFile);
173 QString targetFile = fInfo.fileName();
174 targetFile = settings_.value("download/directory").toString() + targetFile;
176 // Copy temoporary file to Downloads directory...
177 if (!QFile::copy(sourceFile, targetFile)) {
178 qDebug() << "File copying failed";
181 // If copying was successful, remove the original temporary file.
182 QFile::remove(sourceFile);
185 // ...and start the torrent:
186 on_torrentFileSelected(targetFile);
193 //=========================== PRIVATE ================================
195 void MainWindow::LoadPlugins()
197 // Get plugin directories from
198 QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList();
199 QStringList nameFilters("*.so");
201 /// @todo enable "application directory" for plugin search in development/debug mode only. In release version
202 /// search plugins directory under $HOME/.qtrapids or system library paths
203 pluginDirsTmp << qApp->applicationDirPath();
204 pluginDirsTmp.removeDuplicates();
206 foreach (QString dir, pluginDirsTmp) {
207 pluginDirs_.append(QDir(dir));
210 foreach (QDir dir, pluginDirs_) {
212 if (dir.cd(PLUGINS_DIR)) {
214 foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) {
215 QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
217 // If plugin not loaded from another directory, then load
218 if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) {
220 if (pluginLoader.load()) {
221 qDebug() << "Plugin loaded: " << fileName;
223 qWarning() << "Plugin load failed: " << pluginLoader.errorString();
226 QObject *baseInstance = pluginLoader.instance();
228 qDebug() << "Base instance = NULL.";
231 qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
234 qDebug() << "Cast failed.";
236 qtrapids::PluginInterface::Info info;
237 info.directory = dir.path();
238 qDebug() << dir.path();
239 plugin->initialize(this, info);
240 pluginFileNames_ += fileName;
243 qWarning() << "Plugin "
245 << " already loaded from another directory, or not a valid library file";
250 qWarning() << PLUGINS_DIR << "directory not accessible or does not exist in " << dir.path();
255 // =========================== SLOTS =================================
256 void MainWindow::on_openAction_clicked()
258 QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
259 dialog->setFileMode(QFileDialog::ExistingFile);
260 connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
264 void MainWindow::on_removeAction_clicked()
266 qtrapids::QTorrentHandle handle = dlView_->removeSelected();
267 btSession_.removeTorrent(handle);
270 void MainWindow::on_quitAction_clicked()
275 void MainWindow::on_preferencesAction_clicked()
277 if (!preferencesDialog_) {
278 preferencesDialog_ = new PreferencesDialog(this);
280 preferencesDialog_->show();
281 preferencesDialog_->raise();
282 preferencesDialog_->activateWindow();
285 void MainWindow::on_aboutAction_clicked()
287 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
291 void MainWindow::on_aboutQtAction_clicked()
293 QMessageBox::aboutQt (this, tr("About Qt"));
297 void MainWindow::on_downloadItemSelectionChanged()
299 #ifdef QTRAPIDS_DEBUG
300 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
302 if (dlView_->currentItem() != NULL) {
303 emit(itemSelected(true));
305 emit(itemSelected(false));
309 void MainWindow::on_seedItemSelectionChanged()
311 #ifdef QTRAPIDS_DEBUG
312 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
314 if (seedView_->currentItem() != NULL) {
315 emit(itemSelected(true));
317 emit(itemSelected(false));
321 void MainWindow::handleToolBarAction(QAction* action)
323 if (action->text() == "Open") {
324 on_openAction_clicked();
325 } else if (action->text() == "Remove") {
326 on_removeAction_clicked();
330 void MainWindow::on_torrentFileSelected(const QString& file)
332 #ifdef QTRAPIDS_DEBUG
333 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
335 // Torrent filename empty, do nothing.
340 // Otherwise add torrent
341 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
342 AddTorrentParams addParams;
343 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
344 new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
345 addParams.ti = tiTmp;
346 // save_path is the only mandatory parameter, rest are optional.
347 addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
348 //addParams.storage_mode = libtorrent::storage_mode_allocate;
349 qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
350 dlView_->newItem(handle);
351 // torrentHandles_.push_back(handlePtr);
352 #ifdef QTRAPIDS_DEBUG
353 qDebug() << "Is valid: " << handle.isValid();
358 void MainWindow::on_alert(std::auto_ptr<Alert> al)
360 if (al.get() != NULL) {
362 // << "MainWindow::on_torrentAlert(): "
363 // << QString::fromStdString(al->message());
365 TorrentAlert *torrentAlert
366 = dynamic_cast<TorrentAlert*> (al.get());
369 qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
370 dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
377 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
379 for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
380 if (torrentHandles_.at(i).get() == handlePtr.get()) {