Added Q_FUNC_INFO macros to some functions.
[qtrapids] / src / gui / MainWindow.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Ixonos Plc   *
3  *                                                                         *
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.               *
7  *                                                                         *
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.                          *
12  *                                                                         *
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  ***************************************************************************/
18
19
20 #include <QDebug>
21
22 #include <QMenuBar>
23 #include <QToolBar>
24 #include <QAction>
25 #include <QFileDialog>
26 #include <QMessageBox>
27 #include <QApplication>
28 #include <QPluginLoader>
29
30 #include "DownloadView.h"
31 #include "SeedView.h"
32 #include "PreferencesDialog.h"
33
34 #include "MainWindow.h"
35
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"));
43
44 const QString PLUGINS_DIR = "plugins";
45
46 // Consturctor
47 MainWindow::MainWindow():
48                 QMainWindow(), // Superclass
49                 tabWidget_(NULL),
50                 dlView_(NULL),
51                 seedView_(NULL),
52                 searchWidget_(NULL),
53                 preferencesDialog_(NULL),
54                 settings_(),
55                 pluginDirs_(),
56 //      torrentHandles_(),
57                 btSession_()
58 {
59         // MENUBAR
60         QMenuBar *menuBar = new QMenuBar();
61         QMenu *tempMenu = NULL;
62
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"));
68
69         tempMenu = menuBar->addMenu(tr("&Settings"));
70         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
71
72         tempMenu = menuBar->addMenu(tr("&Help"));
73         QAction *aboutAction = tempMenu->addAction(tr("&About"));
74         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
75
76         setMenuBar(menuBar);
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()));
84
85         // TABWIDGET (central widget)
86         tabWidget_ = new QTabWidget();
87         tabWidget_->setTabsClosable(true);
88
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()));
98
99
100         // Tab widget as central widget.
101         setCentralWidget(tabWidget_);
102
103         // TOOLBAR
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);
109
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)));
115         
116         connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
117                 this, SLOT(on_alert(std::auto_ptr<Alert>)));
118
119         LoadPlugins();
120         RestoreSettings();
121 }
122
123
124 MainWindow::~MainWindow()
125 {
126 }
127
128 // ===================== Implements PluginInterface =========================
129 /// @todo add PluginInterface parameter to request plugin name 
130 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin)
131 {
132 #ifdef QTRAPIDS_DEBUG
133         qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
134 #endif
135
136         if (plugin && plugin->identifier() == "SearchPlugin") {
137                 searchWidget_ = widget;
138         } else {
139                 return false;
140         }
141         
142         tabWidget_->addTab(widget, tr("Search"));
143         return true;
144 }
145
146 /// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately
147 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
148 {
149 #ifdef QTRAPIDS_DEBUG
150         qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
151 #endif
152
153         if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
154                 int index = tabWidget_->addTab(widget, tr("Results"));
155                 tabWidget_->setCurrentIndex(index);
156                 //layout_->addWidget(widget);
157         }
158 }
159 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
160 {
161 }
162
163 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
164 {
165 }
166
167 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
168 {
169 }
170
171 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
172 {
173 }
174
175 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
176 {
177         if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
178                 QString sourceFile = param.toString();
179                 
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;
184                 
185                 // Copy temoporary file to Downloads directory...
186                 if (!QFile::copy(sourceFile, targetFile)) {
187                         qDebug() << "File copying failed";
188                         return false;
189                 } else {
190                         // If copying was successful, remove the original temporary file.
191                         QFile::remove(sourceFile);
192                 }
193                 
194                 /// @todo Torrent bencoding validity should be checked before starting(?)
195                 // ...and start the torrent:
196                 on_torrentFileSelected(targetFile);
197         
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());
201         }
202         
203         return true;
204 }
205
206
207 //=========================== PRIVATE ================================
208
209 void MainWindow::LoadPlugins()
210 {
211         // Get plugin directories from 
212         QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList();
213         QStringList nameFilters("*.so");
214         
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();
219         
220         foreach (QString dir, pluginDirsTmp) {
221                 pluginDirs_.append(QDir(dir));
222         }
223         
224         foreach (QDir dir, pluginDirs_) {
225                 
226                 if (dir.cd(PLUGINS_DIR)) {
227                         
228                         foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) {
229                                 QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
230
231                                 // If plugin not loaded from another directory, then load
232                                 if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) {
233
234                                         if (pluginLoader.load()) {
235                                                 qDebug() << "Plugin loaded: "  << fileName;
236                                         } else {
237                                                 qWarning() << "Plugin load failed: " << pluginLoader.errorString();
238                                         }
239
240                                         QObject *baseInstance = pluginLoader.instance();
241                                         if (!baseInstance) {
242                                                 qDebug() << "Base instance = NULL.";
243                                         }
244
245                                         qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
246
247                                         if (!plugin) {
248                                                 qDebug() << "Cast failed.";
249                                         } else {
250                                                 qtrapids::PluginInterface::Info info;
251                                                 info.directory = dir.path();
252                                                 qDebug() << dir.path();
253                                                 plugin->initialize(this, info);
254                                                 pluginFileNames_ += fileName;
255                                         }
256                                 } else {
257                                         qWarning() << "Plugin " 
258                                                 << fileName 
259                                                 << " already loaded from another directory, or not a valid library file";
260                                 }
261                         }
262                         
263                 } else {
264                         qWarning() << PLUGINS_DIR <<  "directory not accessible or does not exist in "  << dir.path();
265                 }
266         }
267 }
268
269
270 void MainWindow::RestoreSettings()
271 {
272         btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt());
273         btSession_.setDownloadRateLimit(settings_.value("network/downloadRate").toInt());
274 }
275
276
277 // Opens torrent information from buffer data and adds torrent to session 
278 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
279 {
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();
294 #endif
295 }
296
297 // =========================== PRIVATE SLOTS =================================
298 void MainWindow::on_openAction_clicked()
299 {
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&)));
303         dialog->show();
304 }
305
306 void MainWindow::on_removeAction_clicked()
307 {
308         qtrapids::QTorrentHandle handle = dlView_->removeSelected();
309         btSession_.removeTorrent(handle);
310 }
311
312
313 void MainWindow::on_quitAction_clicked()
314 {
315         close();
316 }
317
318
319 void MainWindow::on_preferencesAction_clicked()
320 {
321         if (!preferencesDialog_) {
322                 preferencesDialog_ = new PreferencesDialog(this, NULL, &btSession_);
323         }
324
325         preferencesDialog_->show();
326         preferencesDialog_->raise();
327         preferencesDialog_->activateWindow();
328 }
329
330
331 void MainWindow::on_aboutAction_clicked()
332 {
333         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
334 }
335
336
337 void MainWindow::on_aboutQtAction_clicked()
338 {
339         QMessageBox::aboutQt (this, tr("About Qt"));
340 }
341
342
343 void MainWindow::on_tabWidget_tabCloseRequested(int index)
344 {
345         
346         int searchWidgetIndex = tabWidget_->indexOf(searchWidget_);
347         
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);
353                 delete remove;
354                 remove = NULL;
355         }
356 }
357
358
359 void MainWindow::on_downloadItemSelectionChanged()
360 {
361 #ifdef QTRAPIDS_DEBUG
362         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
363 #endif
364         if (dlView_->currentItem() != NULL) {
365                 emit(itemSelected(true));
366         } else {
367                 emit(itemSelected(false));
368         }
369 }
370
371
372 void MainWindow::on_seedItemSelectionChanged()
373 {
374 #ifdef QTRAPIDS_DEBUG
375         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
376 #endif
377         if (seedView_->currentItem() != NULL) {
378                 emit(itemSelected(true));
379         } else {
380                 emit(itemSelected(false));
381         }
382 }
383
384
385 void MainWindow::handleToolBarAction(QAction* action)
386 {
387         if (action->text() == "Open") {
388                 on_openAction_clicked();
389         } else if (action->text() == "Remove") {
390                 on_removeAction_clicked();
391         }
392 }
393
394
395 void MainWindow::on_torrentFileSelected(const QString& file)
396 {
397 #ifdef QTRAPIDS_DEBUG
398         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
399 #endif
400         // Torrent filename empty, do nothing.
401         if (file == "") {
402                 return;
403         }
404
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();
420 #endif
421 }
422
423 void MainWindow::on_alert(std::auto_ptr<Alert> al)
424 {
425         if (al.get() != NULL) {
426 //              qDebug()
427 //                              << "MainWindow::on_torrentAlert(): "
428 //                              << QString::fromStdString(al->message());
429
430                 TorrentAlert *torrentAlert
431                 = dynamic_cast<TorrentAlert*> (al.get());
432
433                 if (torrentAlert) {
434                         qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
435                         dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
436                 }
437
438         }
439 }
440
441 /*
442 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
443 {
444         for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
445     if (torrentHandles_.at(i).get() == handlePtr.get()) {
446                         return false;
447                 } else {
448                         return true;
449                 }
450         }
451 }
452 */