-Code formatted using script
[qtrapids] / src / gui / 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 <QDebug>
23
24 #include <QMenuBar>
25 #include <QToolBar>
26 #include <QAction>
27 #include <QFileDialog>
28 #include <QMessageBox>
29 #include <QApplication>
30
31
32 #include "DownloadView.h"
33 #include "SeedView.h"
34 #include "PreferencesDialog.h"
35
36 #include "MainWindow.h"
37
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"));
45
46 // Consturctor
47 MainWindow::MainWindow():
48         QMainWindow(), // Superclass
49         tabWidget_(NULL),
50         dlView_(NULL),
51         seedView_(NULL),
52         preferencesDialog_(NULL),
53         settings_(),
54 //      torrentHandles_(),
55         btSession_()
56 {
57     // MENUBAR
58     QMenuBar *menuBar = new QMenuBar();
59     QMenu *tempMenu = NULL;
60
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"));
66
67     tempMenu = menuBar->addMenu(tr("&Settings"));
68     QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
69
70     tempMenu = menuBar->addMenu(tr("&Help"));
71     QAction *aboutAction = tempMenu->addAction(tr("&About"));
72     QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
73
74     setMenuBar(menuBar);
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()));
82
83     // TABWIDGET (central widget)
84     tabWidget_ = new QTabWidget();
85     tabWidget_->setTabsClosable(true);
86
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()));
96
97
98     // Tab widget as central widget.
99     setCentralWidget(tabWidget_);
100
101     // TOOLBAR
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);
107
108     connect(this, SIGNAL(itemSelected(bool)), removeAction,
109             SLOT(setEnabled(bool)));
110     connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
111             SLOT(handleToolBarAction(QAction*)));
112
113     connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
114             this, SLOT(on_alert(std::auto_ptr<Alert>)));
115
116     LoadPlugins();
117 }
118
119
120 MainWindow::~MainWindow()
121 {
122 }
123
124 // ===================== Implements PluginInterface =========================
125 bool MainWindow::setGui(qtrapids::PluginInterface* from, QWidget* widget)
126 {
127 #ifdef QTRAPIDS_DEBUG
128     qDebug() << "MainWindow::setGui():" << dlView_->currentItem();
129 #endif
130     tabWidget_->addTab(widget, tr("Search"));
131     return true;
132 }
133
134 /// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately
135 void MainWindow::addPluginWidget(qtrapids::PluginInterface* from, QWidget* widget)
136 {
137 #ifdef QTRAPIDS_DEBUG
138     qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
139 #endif
140
141     int index = tabWidget_->addTab(widget, tr("Test"));
142     tabWidget_->setCurrentIndex(index);
143     //layout_->addWidget(widget);
144 }
145
146 void MainWindow::addToolbar(qtrapids::PluginInterface* from, QWidget* widget)
147 {
148 }
149
150 void MainWindow::addToolItem(qtrapids::PluginInterface* from, QWidget* widget)
151 {
152 }
153
154 void MainWindow::addMenu(qtrapids::PluginInterface* from, QWidget* widget)
155 {
156 }
157
158 void MainWindow::addMenuItem(qtrapids::PluginInterface* from, QWidget* widget)
159 {
160 }
161
162 //=========================== PRIVATE ================================
163
164 void MainWindow::LoadPlugins()
165 {
166     /// @todo get plugin directory from settings or go through multiple diectories
167     /// Now we only check the application directory
168     pluginsDir_ = QDir(qApp->applicationDirPath());
169     pluginsDir_.cd("plugins");
170     QStringList nameFilters;
171     nameFilters << "*.so";
172
173     foreach (QString fileName, pluginsDir_.entryList(nameFilters, QDir::Files))
174     {
175         QPluginLoader pluginLoader(pluginsDir_.absoluteFilePath(fileName));
176
177         if (!QLibrary::isLibrary(fileName))
178         {
179             qDebug() << fileName << " not a library";
180         }
181
182         if (pluginLoader.load())
183         {
184             qDebug() << "Plugin loaded: "  << fileName;
185         }
186         else
187         {
188             qDebug() << "Plugin load failed: " << pluginLoader.errorString();
189         }
190
191         QObject *baseInstance = pluginLoader.instance();
192         if (!baseInstance)
193         {
194             qDebug() << "Base instance = NULL.";
195         }
196
197         qtrapids::PluginInterface *plugin = qobject_cast<qtrapids::PluginInterface*>(baseInstance);
198
199         if (!plugin)
200         {
201             qDebug() << "Cast failed.";
202         }
203         else
204         {
205             plugin->initialize(this);
206             pluginFileNames_ += fileName;
207         }
208
209 //              QObject *plugin = pluginLoader.instance();
210 //              if (plugin) {
211 //                              populateMenus(plugin);
212 //                              pluginFileNames += fileName;
213 //              }
214
215     }
216
217     //pluginLoader_.setFileName("../libsearchplugin.so");
218
219
220
221
222 }
223
224 // =========================== SLOTS =================================
225 void MainWindow::on_openAction_clicked()
226 {
227     QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
228     dialog->setFileMode(QFileDialog::ExistingFile);
229     connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
230     dialog->show();
231
232 }
233
234 void MainWindow::on_removeAction_clicked()
235 {
236     qtrapids::QTorrentHandle handle = dlView_->removeSelected();
237     btSession_.removeTorrent(handle);
238 }
239
240 void MainWindow::on_quitAction_clicked()
241 {
242     close();
243 }
244
245 void MainWindow::on_preferencesAction_clicked()
246 {
247     if (!preferencesDialog_)
248     {
249         preferencesDialog_ = new PreferencesDialog(this);
250     }
251     preferencesDialog_->show();
252     preferencesDialog_->raise();
253     preferencesDialog_->activateWindow();
254 }
255
256 void MainWindow::on_aboutAction_clicked()
257 {
258     QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
259 }
260
261
262 void MainWindow::on_aboutQtAction_clicked()
263 {
264     QMessageBox::aboutQt (this, tr("About Qt"));
265 }
266
267
268 void MainWindow::on_downloadItemSelectionChanged()
269 {
270 #ifdef QTRAPIDS_DEBUG
271     qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
272 #endif
273     if (dlView_->currentItem() != NULL)
274     {
275         emit(itemSelected(true));
276     }
277     else
278     {
279         emit(itemSelected(false));
280     }
281 }
282
283 void MainWindow::on_seedItemSelectionChanged()
284 {
285 #ifdef QTRAPIDS_DEBUG
286     qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
287 #endif
288     if (seedView_->currentItem() != NULL)
289     {
290         emit(itemSelected(true));
291     }
292     else
293     {
294         emit(itemSelected(false));
295     }
296 }
297
298 void MainWindow::handleToolBarAction(QAction* action)
299 {
300     if (action->text() == "Open")
301     {
302         on_openAction_clicked();
303     }
304     else if (action->text() == "Remove")
305     {
306         on_removeAction_clicked();
307     }
308 }
309
310 void MainWindow::on_torrentFileSelected(const QString& file)
311 {
312 #ifdef QTRAPIDS_DEBUG
313     qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
314 #endif
315     // Torrent filename empty, do nothing.
316     if (file == "")
317     {
318         return;
319     }
320
321     // Otherwise add torrent
322     // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
323     AddTorrentParams addParams;
324     boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
325         new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
326     addParams.ti = tiTmp;
327     // save_path is the only mandatory parameter, rest are optional.
328     addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
329     //addParams.storage_mode = libtorrent::storage_mode_allocate;
330     qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
331     dlView_->newItem(handle);
332 //      torrentHandles_.push_back(handlePtr);
333 #ifdef QTRAPIDS_DEBUG
334     qDebug() << "Is valid: " << handle.isValid();
335 #endif
336 }
337
338
339 void MainWindow::on_alert(std::auto_ptr<Alert> al)
340 {
341
342
343     if (al.get() != NULL)
344     {
345 //              qDebug()
346 //                              << "MainWindow::on_torrentAlert(): "
347 //                              << QString::fromStdString(al->message());
348
349         TorrentAlert *torrentAlert
350         = dynamic_cast<TorrentAlert*> (al.get());
351
352         if (torrentAlert)
353         {
354             qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
355             dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
356         }
357
358     }
359
360
361
362 }
363
364 /*
365 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
366 {
367         for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
368                 if (torrentHandles_.at(i).get() == handlePtr.get()) {
369                         return false;
370                 } else {
371                         return true;
372                 }
373         }
374 }
375 */