- Added daemon start functionality
[qtrapids] / src / client / MainWindow.cpp
1 /***************************************************************************
2  *   Copyright (C) 2009 by Lassi Väätämöinen   *
3  *   lassi.vaatamoinen@ixonos.com   *
4  *                                                                         *
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.                                   *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21
22 #include <qtrapids/dbus.hpp>
23
24 #include <QDebug>
25 #include <QtGui/QMenuBar>
26 #include <QtGui/QToolBar>
27 #include <QAction>
28 #include <QFileDialog>
29 #include <QMessageBox>
30 //#include <QTreeWidgetItem>
31 #include <QApplication>
32 #include <QPluginLoader>
33
34 #include "DownloadView.h"
35 #include "SeedView.h"
36 #include "PreferencesDialog.h"
37 #include "ColumnSelectorDialog.h"
38
39 #include "MainWindow.h"
40
41 namespace qtrapids
42 {
43
44 const QString ABOUT_TEXT
45 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
46                           "\nQt and Libtorrent."
47                           "\n\nURL: http://qtrapids.garage.maemo.org/"
48                           "\n\nAuthors:\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
49                           "\nDenis Zalevskiy, denis.zalewsky@gmail.com"
50                           "\n\nIxonos Plc, Finland\n"));
51
52 const QString PLUGINS_DIR = "plugins";
53
54 // Consturctor
55 MainWindow::MainWindow() :
56                 QMainWindow(), // Superclass
57                 tabWidget_(NULL),
58                 dlView_(NULL),
59                 seedView_(NULL),
60                 searchWidget_(NULL),
61                 startDaemonAction_(NULL),
62                 stopDaemonAction_(NULL),
63                 preferencesDialog_(NULL),
64                 settings_(QCoreApplication::organizationName()
65                           , QCoreApplication::applicationName()),
66                 pluginDirs_(),
67                 server_(QtRapidsServer::staticInterfaceName()
68                         , "/qtrapids", QDBusConnection::sessionBus())
69                 //      torrentHandles_(),
70 {
71         
72         setWindowTitle("QtRapids");
73         
74         // MENUBAR
75         QMenuBar *menuBar = new QMenuBar();
76         QMenu *tempMenu = NULL;
77
78         tempMenu = menuBar->addMenu(tr("&File"));
79         QAction *openAction = tempMenu->addAction(tr("&Open"));
80         QAction *removeAction = tempMenu->addAction(tr("&Remove"));
81         removeAction->setEnabled(false);
82         
83         startDaemonAction_ = tempMenu->addAction(tr("S&tart daemon"));
84         stopDaemonAction_ = tempMenu->addAction(tr("Sto&p daemon"));
85         startDaemonAction_->setEnabled(false);
86         
87         QAction *quitAction = tempMenu->addAction(tr("&Quit"));
88
89         tempMenu = menuBar->addMenu(tr("&View"));
90         QAction *columnsAction = tempMenu->addAction(tr("&Columns"));
91         
92         tempMenu = menuBar->addMenu(tr("&Settings"));
93         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
94
95         tempMenu = menuBar->addMenu(tr("&Help"));
96         QAction *aboutAction = tempMenu->addAction(tr("&About"));
97         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
98
99         setMenuBar(menuBar);
100         connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
101         connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
102         connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
103         connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
104         connect(startDaemonAction_, SIGNAL(triggered()), this, SLOT(on_startDaemonAction_clicked()));
105         connect(stopDaemonAction_, SIGNAL(triggered()), this, SLOT(on_stopDaemonAction_clicked()));
106         connect(columnsAction, SIGNAL(triggered()), this, SLOT(on_columnsAction_clicked()));
107         connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
108         connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
109         connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
110
111         // TABWIDGET (central widget)
112         tabWidget_ = new QTabWidget();
113         tabWidget_->setTabsClosable(true);
114
115         /// @todo Exception handling
116         dlView_ = new DownloadView(this);
117         seedView_ = new SeedView(this);
118         tabWidget_->addTab(dlView_, tr("Downloads"));
119         tabWidget_->addTab(seedView_, tr("Seeds"));
120         connect(dlView_, SIGNAL(itemSelectionChanged()), this,
121                 SLOT(on_downloadItemSelectionChanged()));
122
123         connect(seedView_, SIGNAL(itemSelectionChanged()), this,
124                 SLOT(on_seedItemSelectionChanged()));
125
126         // Tab widget as central widget.
127         setCentralWidget(tabWidget_);
128
129         // TOOLBAR
130         QToolBar *toolBar = new QToolBar();
131         toolBar->addAction(tr("Open"));
132         removeAction = toolBar->addAction(tr("Remove"));
133         removeAction->setEnabled(false);
134         addToolBar(Qt::TopToolBarArea, toolBar);
135
136         connect(this, SIGNAL(itemSelected(bool)), removeAction,
137                 SLOT(setEnabled(bool)));
138         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
139                 SLOT(handleToolBarAction(QAction*)));
140         connect (tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_tabCloseRequested(int)));
141
142         connect(&server_, SIGNAL(alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)), 
143                                         this, SLOT(on_alert(qtrapids::TorrentState, qtrapids::ParamsMap_t)));
144         
145         connect(&server_, SIGNAL(sessionTerminated()), this, SLOT(on_serverTerminated()));
146         
147 //      connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
148 //              this, SLOT(on_alert(std::auto_ptr<Alert>)));
149
150
151         LoadPlugins();
152
153 }
154
155
156 MainWindow::~MainWindow()
157 {
158         settings_.setValue("geometry", saveGeometry());
159 }
160
161 // ===================== Implements PluginInterface =========================
162 /// @todo add PluginInterface parameter to request plugin name 
163 bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin)
164 {
165 #ifdef QTRAPIDS_DEBUG
166         qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
167 #endif
168
169         if (plugin && plugin->identifier() == "SearchPlugin") {
170                 searchWidget_ = widget;
171         } else {
172                 return false;
173         }
174         
175         tabWidget_->addTab(widget, tr("Search"));
176         return true;
177 }
178
179 /// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately
180 void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
181 {
182 #ifdef QTRAPIDS_DEBUG
183         qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
184 #endif
185
186         if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
187                 int index = tabWidget_->addTab(widget, tr("Results"));
188                 tabWidget_->setCurrentIndex(index);
189                 //layout_->addWidget(widget);
190         }
191 }
192 void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
193 {
194 }
195
196 void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
197 {
198 }
199
200 void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
201 {
202 }
203
204 void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
205 {
206 }
207
208 bool MainWindow::eventRequest(QVariant param, PluginRequest req)
209 {
210         if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
211                 QString sourceFile = param.toString();
212                 
213                 // Get the source files name from the full path:
214                 QFileInfo fInfo(sourceFile);
215                 QString targetFile = fInfo.fileName();
216                 targetFile = settings_.value("download/directory").toString() + "/" + targetFile;
217                 
218                 // Copy temoporary file to Downloads directory...
219                 if (!QFile::copy(sourceFile, targetFile)) {
220                         qDebug() << "File copying failed";
221                         return false;
222                 } else {
223                         // If copying was successful, remove the original temporary file.
224                         QFile::remove(sourceFile);
225                 }
226                 
227                 /// @todo Torrent bencoding validity should be checked before starting(?)
228                 // ...and start the torrent:
229                 on_torrentFileSelected(targetFile);
230         
231         } else if (req == qtrapids::PluginHostInterface::READ_BUFFER) {
232                 // Create torrent information from char* buffer and start.
233                 StartTorrentFromBufferData(param.toByteArray().constData(), param.toByteArray().size());
234         }
235         
236         return true;
237 }
238
239
240 //=========================== PRIVATE ================================
241
242 void MainWindow::LoadPlugins()
243 {
244         // Get plugin directories from 
245         QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList();
246         QStringList nameFilters("*.so");
247         
248         /// @todo enable "application directory" for plugin search in development/debug mode only. In release version
249         /// search plugins directory under $HOME/.qtrapids or system library paths
250         pluginDirsTmp << qApp->applicationDirPath();
251         pluginDirsTmp.removeDuplicates();
252         
253         foreach (QString dir, pluginDirsTmp) {
254                 pluginDirs_.append(QDir(dir));
255         }
256         
257         foreach (QDir dir, pluginDirs_) {
258                 
259                 if (dir.cd(PLUGINS_DIR)) {
260                         
261                         foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) {
262                                 QPluginLoader pluginLoader(dir.absoluteFilePath(fileName));
263
264                                 // If plugin not loaded from another directory, then load
265                                 if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) {
266
267                                         if (pluginLoader.load()) {
268                                                 qDebug() << "Plugin loaded: "  << fileName;
269                                         } else {
270                                                 qWarning() << "Plugin load failed: " << pluginLoader.errorString();
271                                         }
272
273                                         QObject *baseInstance = pluginLoader.instance();
274                                         if (!baseInstance) {
275                                                 qDebug() << "Base instance = NULL.";
276                                         }
277
278                                         qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
279
280                                         if (!plugin) {
281                                                 qDebug() << "Cast failed.";
282                                         } else {
283                                                 qtrapids::PluginInterface::Info info;
284                                                 info.directory = dir.path();
285                                                 qDebug() << dir.path();
286                                                 plugin->initialize(this, info);
287                                                 pluginFileNames_ += fileName;
288                                         }
289                                 } else {
290                                         qWarning() << "Plugin " 
291                                                 << fileName 
292                                                 << " already loaded from another directory, or not a valid library file";
293                                 }
294                         }
295                         
296                 } else {
297                         qWarning() << PLUGINS_DIR <<  "directory not accessible or does not exist in "  << dir.path();
298                 }
299         }
300 }
301
302
303 void MainWindow::RestoreSettings()
304 {
305         
306         // Restore previous main window geometry:
307         QVariant geometry(settings_.value("geometry"));
308         if (!geometry.isNull()) {
309                 qDebug() << "restoring geometry";
310                 restoreGeometry(geometry.toByteArray());
311         }
312         
313         // Restore DownloadView columns:
314         dlView_->restoreView();
315         
316         // Restore torrent session settings to server:
317         qtrapids::ParamsMap_t options;
318         options["net/downloadRate"] = settings_.value("net/downloadRate").toString();
319         options["net/uploadRate"] = settings_.value("net/uploadRate").toString();
320         server_.setOptions(options);
321 }
322
323
324 // Opens torrent information from buffer data and adds torrent to session 
325 void MainWindow::StartTorrentFromBufferData(char const* data, int size)
326 {
327
328 }
329
330 // =========================== PRIVATE SLOTS =================================
331 void MainWindow::on_openAction_clicked()
332 {
333         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
334         dialog->setFileMode(QFileDialog::ExistingFile);
335         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
336         dialog->show();
337
338 }
339
340 void MainWindow::on_removeAction_clicked()
341 {
342         QString hash = dlView_->prepareRemoveSelected();
343         try {
344                 server_.removeTorrent(hash);
345         } catch (...) {
346                 qDebug() << "Exception while removing torrent";
347         }
348 }
349
350
351 void MainWindow::on_quitAction_clicked()
352 {
353         close();
354 }
355
356
357 void MainWindow::on_startDaemonAction_clicked()
358 {
359         server_.getState();
360         /// @todo create signal that signals server startup and 
361         /// enable controls in the handler slot
362         stopDaemonAction_->setEnabled(true);
363         startDaemonAction_->setEnabled(false);
364 }
365
366
367 void MainWindow::on_stopDaemonAction_clicked()
368 {
369         server_.terminateSession();
370 }
371
372
373 void MainWindow::on_serverTerminated()
374 {
375         stopDaemonAction_->setEnabled(false);
376         startDaemonAction_->setEnabled(true);
377 }
378
379 void MainWindow::on_columnsAction_clicked()
380 {       
381         ColumnSelectorDialog *dialog = new ColumnSelectorDialog(dlView_);
382         dialog->show();
383         dialog->exec();
384 //      dialog->raise();
385 //      dialog->activateWindow();
386         qDebug() << "dialog exit";
387
388         if (dialog->result() == QDialog::Accepted) {
389         qDebug() << "saved";
390                 dlView_->saveView();
391         }
392 }
393
394
395 void MainWindow::on_preferencesAction_clicked()
396 {
397         if (!preferencesDialog_) {
398                 preferencesDialog_ = new PreferencesDialog(this, 0, &server_);
399         }
400
401         preferencesDialog_->show();
402         preferencesDialog_->raise();
403         preferencesDialog_->activateWindow();
404 }
405
406
407 void MainWindow::on_aboutAction_clicked()
408 {
409         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
410 }
411
412
413 void MainWindow::on_aboutQtAction_clicked()
414 {
415         QMessageBox::aboutQt (this, tr("About Qt"));
416 }
417
418
419 void MainWindow::on_tabWidget_tabCloseRequested(int index)
420 {
421         
422         int searchWidgetIndex = tabWidget_->indexOf(searchWidget_);
423         
424         // Allow closing other tabs than the first two
425         // TODO The first two may well be closable, just add "show tabs" action for these in the menu
426         if (index != 0 && index != 1 && index != searchWidgetIndex) {
427                 QWidget *remove = tabWidget_->widget(index);
428                 tabWidget_->removeTab(index);
429                 delete remove;
430                 remove = NULL;
431         }
432 }
433
434
435 void MainWindow::on_downloadItemSelectionChanged()
436 {
437 #ifdef QTRAPIDS_DEBUG
438         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
439 #endif
440         if (dlView_->currentItem() != NULL) {
441                 emit(itemSelected(true));
442         } else {
443                 emit(itemSelected(false));
444         }
445 }
446
447
448 void MainWindow::on_seedItemSelectionChanged()
449 {
450 #ifdef QTRAPIDS_DEBUG
451         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
452 #endif
453         if (seedView_->currentItem() != NULL) {
454                 emit(itemSelected(true));
455         } else {
456                 emit(itemSelected(false));
457         }
458 }
459
460
461 void MainWindow::handleToolBarAction(QAction* action)
462 {
463         if (action->text() == "Open") {
464                 on_openAction_clicked();
465         } else if (action->text() == "Remove") {
466                 on_removeAction_clicked();
467         }
468 }
469
470
471 void MainWindow::on_torrentFileSelected(const QString& file)
472 {
473 #ifdef QTRAPIDS_DEBUG
474         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
475 #endif
476         // Torrent filename empty, do nothing.
477         if (file == "") {
478                 return;
479         }
480
481         // Otherwise add torrent
482         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
483         // save_path is the only mandatory parameter, rest are optional.
484         //addParams.storage_mode = libtorrent::storage_mode_allocate;
485         try {
486                 server_.addTorrent(file, settings_.value("download/directory").toString()
487                                    , ParamsMap_t());
488         } catch (...) {
489                 qDebug() << "Exception adding torrent";
490         }
491 }
492
493
494 void MainWindow::on_alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
495 {
496 #ifdef QTRAPIDS_DEBUG
497         qDebug() << "got alert";
498 #endif
499         dlView_->updateItem(info, other_info);
500 }
501
502 } // namespace qtrapids