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