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"));
47 MainWindow::MainWindow():
48 QMainWindow(), // Superclass
52 preferencesDialog_(NULL),
58 QMenuBar *menuBar = new QMenuBar();
59 QMenu *tempMenu = NULL;
61 tempMenu = menuBar->addMenu(tr("&File"));
62 QAction *openAction = tempMenu->addAction(tr("&Open"));
63 QAction *removeAction = tempMenu->addAction(tr("&Remove"));
64 removeAction->setEnabled(false);
65 QAction *quitAction = tempMenu->addAction(tr("&Quit"));
67 tempMenu = menuBar->addMenu(tr("&Settings"));
68 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
70 tempMenu = menuBar->addMenu(tr("&Help"));
71 QAction *aboutAction = tempMenu->addAction(tr("&About"));
72 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
75 connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
76 connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
77 connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
78 connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
79 connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
80 connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
81 connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
83 // TABWIDGET (central widget)
84 tabWidget_ = new QTabWidget();
85 tabWidget_->setTabsClosable(true);
87 /// @todo Exception handling
88 dlView_ = new DownloadView(this);
89 seedView_ = new SeedView(this);
90 tabWidget_->addTab(dlView_, tr("Downloads"));
91 tabWidget_->addTab(seedView_, tr("Seeds"));
92 connect(dlView_, SIGNAL(itemSelectionChanged()), this,
93 SLOT(on_downloadItemSelectionChanged()));
94 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
95 SLOT(on_seedItemSelectionChanged()));
98 // Tab widget as central widget.
99 setCentralWidget(tabWidget_);
102 QToolBar *toolBar = new QToolBar();
103 toolBar->addAction(tr("Open"));
104 removeAction = toolBar->addAction(tr("Remove"));
105 removeAction->setEnabled(false);
106 addToolBar(Qt::TopToolBarArea, toolBar);
108 connect(this, SIGNAL(itemSelected(bool)), removeAction,
109 SLOT(setEnabled(bool)));
110 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
111 SLOT(handleToolBarAction(QAction*)));
113 connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
114 this, SLOT(on_alert(std::auto_ptr<Alert>)));
120 MainWindow::~MainWindow()
124 // ===================== Implements PluginInterface =========================
125 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type)
127 #ifdef QTRAPIDS_DEBUG
128 qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
130 tabWidget_->addTab(widget, tr("Search"));
134 /// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately
135 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
137 #ifdef QTRAPIDS_DEBUG
138 qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
141 if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
142 int index = tabWidget_->addTab(widget, tr("Test"));
143 tabWidget_->setCurrentIndex(index);
144 //layout_->addWidget(widget);
147 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
151 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
155 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
159 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
163 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
165 if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
166 QString sourceFile = param.toString();
168 // Get the source files name from the full path:
169 QFileInfo fInfo(sourceFile);
170 QString targetFile = fInfo.fileName();
171 targetFile = settings_.value("download/directory").toString() + targetFile;
173 // Copy temoporary file to Downloads directory...
174 if (!QFile::copy(sourceFile, targetFile)) {
175 qDebug() << "File copying failed";
178 // If copying was successful, remove the original temporary file.
179 QFile::remove(sourceFile);
182 // ...and start the torrent:
183 on_torrentFileSelected(targetFile);
190 //=========================== PRIVATE ================================
192 void MainWindow::LoadPlugins()
194 /// @todo get plugin directory from settings or go through multiple directories
195 /// Now we only check the application directory
196 pluginsDir_ = QDir(qApp->applicationDirPath());
197 pluginsDir_.cd("plugins");
198 QStringList nameFilters;
199 nameFilters << "*.so";
201 foreach (QString fileName, pluginsDir_.entryList(nameFilters, QDir::Files)) {
202 QPluginLoader pluginLoader(pluginsDir_.absoluteFilePath(fileName));
204 if (!QLibrary::isLibrary(fileName)) {
205 qDebug() << fileName << " not a library";
208 if (pluginLoader.load()) {
209 qDebug() << "Plugin loaded: " << fileName;
211 qDebug() << "Plugin load failed: " << pluginLoader.errorString();
214 QObject *baseInstance = pluginLoader.instance();
216 qDebug() << "Base instance = NULL.";
219 qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
222 qDebug() << "Cast failed.";
224 plugin->initialize(this);
225 pluginFileNames_ += fileName;
230 // =========================== SLOTS =================================
231 void MainWindow::on_openAction_clicked()
233 QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
234 dialog->setFileMode(QFileDialog::ExistingFile);
235 connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
239 void MainWindow::on_removeAction_clicked()
241 qtrapids::QTorrentHandle handle = dlView_->removeSelected();
242 btSession_.removeTorrent(handle);
245 void MainWindow::on_quitAction_clicked()
250 void MainWindow::on_preferencesAction_clicked()
252 if (!preferencesDialog_) {
253 preferencesDialog_ = new PreferencesDialog(this);
255 preferencesDialog_->show();
256 preferencesDialog_->raise();
257 preferencesDialog_->activateWindow();
260 void MainWindow::on_aboutAction_clicked()
262 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
266 void MainWindow::on_aboutQtAction_clicked()
268 QMessageBox::aboutQt (this, tr("About Qt"));
272 void MainWindow::on_downloadItemSelectionChanged()
274 #ifdef QTRAPIDS_DEBUG
275 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
277 if (dlView_->currentItem() != NULL) {
278 emit(itemSelected(true));
280 emit(itemSelected(false));
284 void MainWindow::on_seedItemSelectionChanged()
286 #ifdef QTRAPIDS_DEBUG
287 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
289 if (seedView_->currentItem() != NULL) {
290 emit(itemSelected(true));
292 emit(itemSelected(false));
296 void MainWindow::handleToolBarAction(QAction* action)
298 if (action->text() == "Open") {
299 on_openAction_clicked();
300 } else if (action->text() == "Remove") {
301 on_removeAction_clicked();
305 void MainWindow::on_torrentFileSelected(const QString& file)
307 #ifdef QTRAPIDS_DEBUG
308 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
310 // Torrent filename empty, do nothing.
315 // Otherwise add torrent
316 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
317 AddTorrentParams addParams;
318 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
319 new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
320 addParams.ti = tiTmp;
321 // save_path is the only mandatory parameter, rest are optional.
322 addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
323 //addParams.storage_mode = libtorrent::storage_mode_allocate;
324 qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
325 dlView_->newItem(handle);
326 // torrentHandles_.push_back(handlePtr);
327 #ifdef QTRAPIDS_DEBUG
328 qDebug() << "Is valid: " << handle.isValid();
333 void MainWindow::on_alert(std::auto_ptr<Alert> al)
335 if (al.get() != NULL) {
337 // << "MainWindow::on_torrentAlert(): "
338 // << QString::fromStdString(al->message());
340 TorrentAlert *torrentAlert
341 = dynamic_cast<TorrentAlert*> (al.get());
344 qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
345 dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
352 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
354 for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
355 if (torrentHandles_.at(i).get() == handlePtr.get()) {