-Added QTorrentHandle and started torrent adding implementation
[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
33 #include "MainWindow.h"
34
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         btSession_()                                    
51 {
52         // MENUBAR 
53         QMenuBar *menuBar = new QMenuBar();
54         QMenu *tempMenu = NULL;
55         
56         tempMenu = menuBar->addMenu(tr("&File"));
57         QAction *openAction = tempMenu->addAction(tr("&Open"));
58         QAction *quitAction = tempMenu->addAction(tr("&Quit"));
59         
60         tempMenu = menuBar->addMenu(tr("&Help"));
61         QAction *aboutAction = tempMenu->addAction(tr("&About"));
62         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
63         
64                 setMenuBar(menuBar);
65         connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
66         connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
67         connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
68         connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
69         
70         
71         // TABWIDGET (central widget)
72         tabWidget_ = new QTabWidget();
73         
74         /// @todo Exception handling
75         dlView_ = new DownloadView(this);
76         seedView_ = new SeedView(this);
77         
78         tabWidget_->addTab(dlView_, tr("Downloads"));
79         tabWidget_->addTab(seedView_, tr("Seeds"));
80         
81         // Tab widget as central widget.
82         setCentralWidget(tabWidget_);
83         
84         
85         // TOOLBAR
86         QToolBar *toolBar = new QToolBar();
87         toolBar->addAction(tr("Open"));
88         
89         addToolBar(Qt::TopToolBarArea, toolBar);
90         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
91         
92 }
93
94
95 MainWindow::~MainWindow()
96 {
97 }
98
99 // =========================== SLOTS =================================
100 void MainWindow::on_openAction_clicked()
101 {
102         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
103         dialog->setFileMode(QFileDialog::ExistingFile);
104         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
105         dialog->show();
106
107 }
108
109
110 void MainWindow::on_quitAction_clicked()
111 {
112         close();
113 }
114
115
116 void MainWindow::on_aboutAction_clicked()
117 {
118         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); 
119 }
120                 
121                 
122 void MainWindow::on_aboutQtAction_clicked()
123 {
124         QMessageBox::aboutQt (this, tr("About Qt"));
125 }
126
127
128 void MainWindow::handleToolBarAction(QAction* action)
129 {
130         if (action->text() == "Open") {
131                 on_openAction_clicked();
132         } else {
133         }
134 }
135
136 void MainWindow::on_torrentFileSelected(const QString& file)
137 {
138         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
139         // Torrent filename empty, do nothing.
140         if (file == "") {
141                 return;
142         }
143         
144         // Otherwise add torrent
145         /*      
146         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
147         AddTorrentParams addParams;
148         addParams.ti = torrent_info(boost::filesystem::path const& filename);
149         addParams.save_path = "/home/vaatala/Downloads"; // The only mandatory parameter, rest are optional.
150         //addParams.storage_mode = libtorrent::storage_mode_allocate;
151         btSession_.addTorrent();
152         */
153 }