- Namespaced QBittorrentSession and QTorrentHandle to avoid possible future conflicts
[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
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 Zalievsky, denis.zalewsky@ixonos.com"
42                 "\n\nIxonos Plc, Finland\n"));
43
44
45 // Consturctor
46 MainWindow::MainWindow():
47         QMainWindow(), // Superclass
48         tabWidget_(NULL),
49         dlView_(NULL),
50         seedView_(NULL),
51         preferencesDialog_(NULL),
52         settings_(),
53 //      torrentHandles_(),
54         btSession_()                                    
55 {
56         // MENUBAR 
57         QMenuBar *menuBar = new QMenuBar();
58         QMenu *tempMenu = NULL;
59         
60         tempMenu = menuBar->addMenu(tr("&File"));
61         QAction *openAction = tempMenu->addAction(tr("&Open"));
62         QAction *removeAction = tempMenu->addAction(tr("&Remove"));
63         removeAction->setEnabled(false);
64         QAction *quitAction = tempMenu->addAction(tr("&Quit"));
65         
66         tempMenu = menuBar->addMenu(tr("&Settings"));
67         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
68         
69         tempMenu = menuBar->addMenu(tr("&Help"));
70         QAction *aboutAction = tempMenu->addAction(tr("&About"));
71         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
72         
73         setMenuBar(menuBar);
74         connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
75         connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
76         connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
77         connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
78         connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
79         connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
80         connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
81         
82         // TABWIDGET (central widget)
83         tabWidget_ = new QTabWidget();
84         
85         /// @todo Exception handling
86         dlView_ = new DownloadView(this);
87         seedView_ = new SeedView(this);
88         tabWidget_->addTab(dlView_, tr("Downloads"));
89         tabWidget_->addTab(seedView_, tr("Seeds"));
90         connect(dlView_, SIGNAL(itemSelectionChanged()), this,
91                                         SLOT(on_downloadItemSelectionChanged()));
92         connect(seedView_, SIGNAL(itemSelectionChanged()), this,
93                                         SLOT(on_seedItemSelectionChanged()));
94
95         
96         // Tab widget as central widget.
97         setCentralWidget(tabWidget_);
98         
99         // TOOLBAR
100         QToolBar *toolBar = new QToolBar();
101         toolBar->addAction(tr("Open"));
102         removeAction = toolBar->addAction(tr("Remove"));
103         removeAction->setEnabled(false);
104         addToolBar(Qt::TopToolBarArea, toolBar);
105         
106         connect(this, SIGNAL(itemSelected(bool)), removeAction,
107                                         SLOT(setEnabled(bool)));
108         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
109                                         SLOT(handleToolBarAction(QAction*)));
110         
111         connect(&btSession_, SIGNAL(alert(std::auto_ptr<Alert>)),
112                                          this, SLOT(on_alert(std::auto_ptr<Alert>)));
113 }
114
115
116 MainWindow::~MainWindow()
117 {
118 }
119
120 // =========================== SLOTS =================================
121 void MainWindow::on_openAction_clicked()
122 {
123         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
124         dialog->setFileMode(QFileDialog::ExistingFile);
125         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
126         dialog->show();
127
128 }
129
130 void MainWindow::on_removeAction_clicked()
131 {
132         qtrapids::QTorrentHandle handle = dlView_->removeSelected();
133         btSession_.removeTorrent(handle);
134 }
135
136 void MainWindow::on_quitAction_clicked()
137 {
138         close();
139 }
140
141 void MainWindow::on_preferencesAction_clicked()
142 {
143         if (!preferencesDialog_) {
144                 preferencesDialog_ = new PreferencesDialog(this);
145         }
146         preferencesDialog_->show();
147         preferencesDialog_->raise();
148         preferencesDialog_->activateWindow();
149 }
150
151 void MainWindow::on_aboutAction_clicked()
152 {
153         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); 
154 }
155                 
156                 
157 void MainWindow::on_aboutQtAction_clicked()
158 {
159         QMessageBox::aboutQt (this, tr("About Qt"));
160 }
161
162
163 void MainWindow::on_downloadItemSelectionChanged()
164 {
165 #ifdef QTRAPIDS_DEBUG
166         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
167 #endif
168         if (dlView_->currentItem() != NULL) {
169                 emit(itemSelected(true));
170         } else {
171                 emit(itemSelected(false));
172         }
173 }
174
175 void MainWindow::on_seedItemSelectionChanged()
176 {
177 #ifdef QTRAPIDS_DEBUG
178         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
179 #endif
180         if (seedView_->currentItem() != NULL) {
181                 emit(itemSelected(true));
182         } else {
183                 emit(itemSelected(false));
184         }
185 }
186                 
187 void MainWindow::handleToolBarAction(QAction* action)
188 {
189         if (action->text() == "Open") {
190                 on_openAction_clicked();
191         } else if (action->text() == "Remove") {
192                 on_removeAction_clicked();
193         }
194 }
195
196 void MainWindow::on_torrentFileSelected(const QString& file)
197 {
198 #ifdef QTRAPIDS_DEBUG
199         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
200 #endif
201         // Torrent filename empty, do nothing.
202         if (file == "") {
203                 return;
204         }
205         
206         // Otherwise add torrent
207         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
208         AddTorrentParams addParams;
209         boost::intrusive_ptr<libtorrent::torrent_info> tiTmp = 
210                         new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
211         addParams.ti = tiTmp;
212         // save_path is the only mandatory parameter, rest are optional.
213         addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString()); 
214         //addParams.storage_mode = libtorrent::storage_mode_allocate;
215         qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams);
216         dlView_->newItem(handle);
217 //      torrentHandles_.push_back(handlePtr);
218 #ifdef QTRAPIDS_DEBUG
219         qDebug() << "Is valid: " << handle.isValid();
220 #endif
221 }
222
223
224 void MainWindow::on_alert(std::auto_ptr<Alert> al)
225 {
226
227         
228         if (al.get() != NULL) {
229 //              qDebug() 
230 //                              << "MainWindow::on_torrentAlert(): " 
231 //                              << QString::fromStdString(al->message());
232
233                 TorrentAlert *torrentAlert 
234                                 = dynamic_cast<TorrentAlert*> (al.get());
235
236                 if (torrentAlert) {
237                         qtrapids::QTorrentHandle torrentHandle = qtrapids::QTorrentHandle(torrentAlert->handle);
238                         dlView_->updateItem(qtrapids::QTorrentHandle(torrentAlert->handle));
239                 }
240         
241         }
242         
243         
244         
245 }
246
247 /*
248 bool MainWindow::IsNewTorrent(std::auto_ptr<qtrapids::QTorrentHandle> handlePtr)
249 {
250         for (unsigned i = 0; i < torrentHandles_.size(); ++i) {
251                 if (torrentHandles_.at(i).get() == handlePtr.get()) {
252                         return false;
253                 } else {
254                         return true;
255                 }
256         }
257 }
258 */