updating files
[qtrapids] / src / client / MainWindowPage.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 <qtrapids/dbus.hpp>
23
24 #include <QDebug>
25 #include <QtGui/QMenuBar>
26 #include <QtGui/QToolBar>
27 #include <QAction>
28 #include <QtGui/QFileDialog>
29 #include <QtGui/QMessageBox>
30 #include <QtGui/QTabWidget>
31
32 #include "DownloadView.h"
33 #include "SeedView.h"
34 #include "PreferencesDialog.h"
35
36 #include "MainWindowPage.h"
37
38 namespace qtrapids
39 {
40
41 const QString ABOUT_TEXT
42 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
43                           "\nQt and Libtorrent."
44                           "\n\nURL: http://qtrapids.garage.maemo.org/"
45                           "\n\nAuthor(s):\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
46                           "\nDenis Zalevskiy, denis.zalewsky@ixonos.com"
47                           "\n\nIxonos Plc, Finland\n"));
48
49
50 // Consturctor
51 MainWindowPage::MainWindowPage() :
52                 DuiApplicationPage()
53                 //QMainWindowPage(), // Superclass
54                 tabWidget_(NULL),
55                 dlView_(NULL),
56                 seedView_(NULL),
57                 preferencesDialog_(NULL),
58                 settings_(QCoreApplication::organizationName()
59                           , QCoreApplication::applicationName()),
60                 server_(QtRapidsServer::staticInterfaceName()
61                         , "/qtrapids", QDBusConnection::sessionBus())
62                 //      torrentHandles_(),
63 {}
64
65
66 void MainWindowPage::createContent()
67 {
68         // MENUBAR
69 #if 0
70         QMenuBar *menuBar = new QMenuBar();
71         QMenu *tempMenu = NULL;
72
73         tempMenu = menuBar->addMenu(tr("&File"));
74         QAction *openAction = tempMenu->addAction(tr("&Open"));
75         QAction *removeAction = tempMenu->addAction(tr("&Remove"));
76         removeAction->setEnabled(false);
77         QAction *quitAction = tempMenu->addAction(tr("&Quit"));
78
79         tempMenu = menuBar->addMenu(tr("&Settings"));
80         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
81
82         tempMenu = menuBar->addMenu(tr("&Help"));
83         QAction *aboutAction = tempMenu->addAction(tr("&About"));
84         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
85
86         setMenuBar(menuBar);
87 #endif
88         
89         DuiAction* action;
90         DuiWidget* panel = centralWidget();
91         DuiLayout* layout = new DuiLayout( panel );
92         panel->setLayout( layout );
93         DuiLinearLayoutPolicy* policy = new DuiLinearLayoutPolicy( layout, Qt::Vertical );
94         policy->setContentMargins( 12, 12, 12, 12 );
95
96         action = new DuiAction( NULL, "Open", panel );
97         action->setLocation( DuiAction::ViewMenu );
98         this->addAction( action );
99         connect(action, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
100
101         action = new DuiAction( NULL, "Remove", panel );
102         action->setLocation( DuiAction::ViewMenu );
103         this->addAction( action );
104         connect(action, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
105
106         //connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
107         
108         action = new DuiAction( NULL, "Quit", panel );
109         action->setLocation( DuiAction::ViewMenu );
110         this->addAction( action );
111         connect(action, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
112
113         action = new DuiAction( NULL, "Preferences", panel );
114         action->setLocation( DuiAction::ViewMenu );
115         this->addAction( action );
116         connect(action, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
117         
118         action = new DuiAction( NULL, "About", panel );
119         action->setLocation( DuiAction::ViewMenu );
120         this->addAction( action );
121         connect(action, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
122
123         action = new DuiAction( NULL, "About Qt", panel );
124         connect(action, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
125
126 #if 0
127         // TABWIDGET (central widget)
128         tabWidget_ = new QTabWidget();
129 #endif
130
131         /// @todo Exception handling
132         dlView_ = new DownloadView(this);
133         seedView_ = new SeedView(this);
134         tabWidget_->addTab(dlView_, tr("Downloads"));
135         tabWidget_->addTab(seedView_, tr("Seeds"));
136         connect(dlView_, SIGNAL(itemSelectionChanged()), this,
137                 SLOT(on_downloadItemSelectionChanged()));
138
139         connect(seedView_, SIGNAL(itemSelectionChanged()), this,
140                 SLOT(on_seedItemSelectionChanged()));
141
142 #if 0
143         // Tab widget as central widget.
144         setCentralWidget(tabWidget_);
145
146         // TOOLBAR
147         QToolBar *toolBar = new QToolBar();
148         toolBar->addAction(tr("Open"));
149         removeAction = toolBar->addAction(tr("Remove"));
150         removeAction->setEnabled(false);
151         addToolBar(Qt::TopToolBarArea, toolBar);
152
153         connect(this, SIGNAL(itemSelected(bool)), removeAction,
154                 SLOT(setEnabled(bool)));
155         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
156                 SLOT(handleToolBarAction(QAction*)));
157
158         QVariant geometry(settings_.value("geometry"));
159         if (!geometry.isNull()) {
160                 qDebug() << "restoring geometry";
161                 restoreGeometry(geometry.toByteArray());
162         }
163 #endif
164 }
165
166
167 MainWindowPage::~MainWindowPage()
168 {
169         settings_.setValue("geometry", saveGeometry());
170 }
171
172 // =========================== SLOTS =================================
173 void MainWindowPage::on_openAction_clicked()
174 {
175         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
176         dialog->setFileMode(QFileDialog::ExistingFile);
177         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
178         dialog->show();
179
180 }
181
182 void MainWindowPage::on_removeAction_clicked()
183 {
184         QString hash = dlView_->prepareRemoveSelected();
185         try {
186                 server_.removeTorrent(hash);
187         } catch (...) {
188                 qDebug() << "Exception removing torrent";
189         }
190 }
191
192 void MainWindowPage::on_quitAction_clicked()
193 {
194         close();
195 }
196
197 void MainWindowPage::on_preferencesAction_clicked()
198 {
199         if (!preferencesDialog_) {
200                 preferencesDialog_ = new PreferencesDialog(this);
201         }
202         preferencesDialog_->show();
203         preferencesDialog_->raise();
204         preferencesDialog_->activateWindow();
205 }
206
207 void MainWindowPage::on_aboutAction_clicked()
208 {
209         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
210 }
211
212
213 void MainWindowPage::on_aboutQtAction_clicked()
214 {
215         QMessageBox::aboutQt (this, tr("About Qt"));
216 }
217
218
219 void MainWindowPage::on_downloadItemSelectionChanged()
220 {
221         qDebug() << "MainWindowPage::on_seedItemSelectionChanged():" << dlView_->currentItem();
222         if (dlView_->currentItem() != NULL) {
223                 emit(itemSelected(true));
224         } else {
225                 emit(itemSelected(false));
226         }
227 }
228
229 void MainWindowPage::on_seedItemSelectionChanged()
230 {
231         qDebug() << "MainWindowPage::on_seedItemSelectionChanged():" << seedView_->currentItem();
232         if (seedView_->currentItem() != NULL) {
233                 emit(itemSelected(true));
234         } else {
235                 emit(itemSelected(false));
236         }
237 }
238
239 void MainWindowPage::handleToolBarAction(QAction* action)
240 {
241         if (action->text() == "Open") {
242                 on_openAction_clicked();
243         } else if (action->text() == "Remove") {
244                 on_removeAction_clicked();
245         }
246 }
247
248 void MainWindowPage::on_torrentFileSelected(const QString& file)
249 {
250         qDebug() << " MainWindowPage::on_torrentFileSelected(): " << file;
251         // Torrent filename empty, do nothing.
252         if (file == "") {
253                 return;
254         }
255
256         // Otherwise add torrent
257         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
258         // save_path is the only mandatory parameter, rest are optional.
259         //addParams.storage_mode = libtorrent::storage_mode_allocate;
260         try {
261                 server_.addTorrent(file, settings_.value("download/directory").toString()
262                                    , ParamsMap_t());
263         } catch (...) {
264                 qDebug() << "Exception adding torrent";
265         }
266 }
267
268
269 void MainWindowPage::alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
270 {
271         std::cerr << "got alert" << std::endl;
272         dlView_->updateItem(info, other_info);
273 }
274
275 } // namespace qtrapids