- Added PreferencesDialog with settings saving and reading.
[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
37 const QString ABOUT_TEXT 
38         = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
39                 "\nQt and Libtorrent."
40                 "\n\nURL: http://qtrapids.garage.maemo.org/"
41                 "\n\nAuthor(s):\nLassi Väätämöinen, lassi.vaatamoinen@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         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 *quitAction = tempMenu->addAction(tr("&Quit"));
62         
63         tempMenu = menuBar->addMenu(tr("&Settings"));
64         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
65         
66         tempMenu = menuBar->addMenu(tr("&Help"));
67         QAction *aboutAction = tempMenu->addAction(tr("&About"));
68         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
69         
70                 setMenuBar(menuBar);
71         connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
72         connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
73         connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
74         connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
75         connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
76         
77         // TABWIDGET (central widget)
78         tabWidget_ = new QTabWidget();
79         
80         /// @todo Exception handling
81         dlView_ = new DownloadView(this);
82         seedView_ = new SeedView(this);
83         
84         tabWidget_->addTab(dlView_, tr("Downloads"));
85         tabWidget_->addTab(seedView_, tr("Seeds"));
86         
87         // Tab widget as central widget.
88         setCentralWidget(tabWidget_);
89         
90         
91         // TOOLBAR
92         QToolBar *toolBar = new QToolBar();
93         toolBar->addAction(tr("Open"));
94         
95         addToolBar(Qt::TopToolBarArea, toolBar);
96         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
97         
98 }
99
100
101 MainWindow::~MainWindow()
102 {
103 }
104
105 // =========================== SLOTS =================================
106 void MainWindow::on_openAction_clicked()
107 {
108         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
109         dialog->setFileMode(QFileDialog::ExistingFile);
110         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
111         dialog->show();
112
113 }
114
115
116 void MainWindow::on_quitAction_clicked()
117 {
118         close();
119 }
120
121 void MainWindow::on_preferencesAction_clicked()
122 {
123         if (!preferencesDialog_) {
124                 preferencesDialog_ = new PreferencesDialog(this);
125         }
126         preferencesDialog_->show();
127         preferencesDialog_->raise();
128         preferencesDialog_->activateWindow();
129 }
130
131 void MainWindow::on_aboutAction_clicked()
132 {
133         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); 
134 }
135                 
136                 
137 void MainWindow::on_aboutQtAction_clicked()
138 {
139         QMessageBox::aboutQt (this, tr("About Qt"));
140 }
141
142
143 void MainWindow::handleToolBarAction(QAction* action)
144 {
145         if (action->text() == "Open") {
146                 on_openAction_clicked();
147         } else {
148         }
149 }
150
151 void MainWindow::on_torrentFileSelected(const QString& file)
152 {
153         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
154         // Torrent filename empty, do nothing.
155         if (file == "") {
156                 return;
157         }
158         
159         // Otherwise add torrent
160         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
161         AddTorrentParams addParams;
162         boost::intrusive_ptr<libtorrent::torrent_info> tiTmp = 
163                         new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
164         addParams.ti = tiTmp;
165         addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString()); // The only mandatory parameter, rest are optional.
166         //addParams.storage_mode = libtorrent::storage_mode_allocate;
167         btSession_.addTorrent(addParams);
168 }