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