Client-server through DBus, cmake support
[qtrapids] / src / client / 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 <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 "MainWindow.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     MainWindow::MainWindow() :
52         QMainWindow(), // Superclass
53         tabWidget_(NULL),
54         dlView_(NULL),
55         seedView_(NULL),
56         preferencesDialog_(NULL),
57         settings_(QCoreApplication::organizationName()
58                   , QCoreApplication::applicationName()),
59         server_(QtRapidsServer::staticInterfaceName()
60                 , "/qtrapids", QDBusConnection::sessionBus())
61         //      torrentHandles_(),
62     {
63         // MENUBAR 
64         QMenuBar *menuBar = new QMenuBar();
65         QMenu *tempMenu = NULL;
66         
67         tempMenu = menuBar->addMenu(tr("&File"));
68         QAction *openAction = tempMenu->addAction(tr("&Open"));
69         QAction *removeAction = tempMenu->addAction(tr("&Remove"));
70         removeAction->setEnabled(false);
71         QAction *quitAction = tempMenu->addAction(tr("&Quit"));
72         
73         tempMenu = menuBar->addMenu(tr("&Settings"));
74         QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
75         
76         tempMenu = menuBar->addMenu(tr("&Help"));
77         QAction *aboutAction = tempMenu->addAction(tr("&About"));
78         QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
79         
80         setMenuBar(menuBar);
81         connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
82         connect(removeAction, SIGNAL(triggered()), this, SLOT(on_removeAction_clicked()));
83         connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
84         connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
85         connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
86         connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
87         connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
88         
89         // TABWIDGET (central widget)
90         tabWidget_ = new QTabWidget();
91         
92         /// @todo Exception handling
93         dlView_ = new DownloadView(this);
94         seedView_ = new SeedView(this);
95         tabWidget_->addTab(dlView_, tr("Downloads"));
96         tabWidget_->addTab(seedView_, tr("Seeds"));
97         connect(dlView_, SIGNAL(itemSelectionChanged()), this,
98                 SLOT(on_downloadItemSelectionChanged()));
99
100         connect(seedView_, SIGNAL(itemSelectionChanged()), this,
101                 SLOT(on_seedItemSelectionChanged()));
102         
103         // Tab widget as central widget.
104         setCentralWidget(tabWidget_);
105         
106         // TOOLBAR
107         QToolBar *toolBar = new QToolBar();
108         toolBar->addAction(tr("Open"));
109         removeAction = toolBar->addAction(tr("Remove"));
110         removeAction->setEnabled(false);
111         addToolBar(Qt::TopToolBarArea, toolBar);
112         
113         connect(this, SIGNAL(itemSelected(bool)), removeAction,
114                 SLOT(setEnabled(bool)));
115         connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
116                 SLOT(handleToolBarAction(QAction*)));
117
118         QVariant geometry(settings_.value("geometry"));
119         if (!geometry.isNull()) {
120             qDebug() << "restoring geometry";
121             restoreGeometry(geometry.toByteArray());
122         }
123     }
124
125
126     MainWindow::~MainWindow()
127     {
128         settings_.setValue("geometry", saveGeometry());
129     }
130
131     // =========================== SLOTS =================================
132     void MainWindow::on_openAction_clicked()
133     {
134         QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
135         dialog->setFileMode(QFileDialog::ExistingFile);
136         connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
137         dialog->show();
138
139     }
140
141     void MainWindow::on_removeAction_clicked()
142     {
143         QString hash = dlView_->removeSelected();
144         try {
145             server_.removeTorrent(hash);
146         } catch (...) {
147             qDebug() << "Exception removing torrent";
148         }
149     }
150
151     void MainWindow::on_quitAction_clicked()
152     {
153         close();
154     }
155
156     void MainWindow::on_preferencesAction_clicked()
157     {
158         if (!preferencesDialog_) {
159             preferencesDialog_ = new PreferencesDialog(this);
160         }
161         preferencesDialog_->show();
162         preferencesDialog_->raise();
163         preferencesDialog_->activateWindow();
164     }
165
166     void MainWindow::on_aboutAction_clicked()
167     {
168         QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); 
169     }
170                 
171                 
172     void MainWindow::on_aboutQtAction_clicked()
173     {
174         QMessageBox::aboutQt (this, tr("About Qt"));
175     }
176
177
178     void MainWindow::on_downloadItemSelectionChanged()
179     {
180         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
181         if (dlView_->currentItem() != NULL) {
182             emit(itemSelected(true));
183         } else {
184             emit(itemSelected(false));
185         }
186     }
187
188     void MainWindow::on_seedItemSelectionChanged()
189     {
190         qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
191         if (seedView_->currentItem() != NULL) {
192                 emit(itemSelected(true));
193         } else {
194                 emit(itemSelected(false));
195         }
196     }
197                 
198     void MainWindow::handleToolBarAction(QAction* action)
199     {
200         if (action->text() == "Open") {
201             on_openAction_clicked();
202         } else if (action->text() == "Remove") {
203             on_removeAction_clicked();
204         }
205     }
206
207     void MainWindow::on_torrentFileSelected(const QString& file)
208     {
209         qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
210         // Torrent filename empty, do nothing.
211         if (file == "") {
212             return;
213         }
214         
215         // Otherwise add torrent
216         // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
217         // save_path is the only mandatory parameter, rest are optional.
218         //addParams.storage_mode = libtorrent::storage_mode_allocate;
219         try {
220             server_.addTorrent(file, settings_.value("download/directory").toString()
221                                , ParamsMap_t());
222         } catch (...) {
223             qDebug() << "Exception adding torrent";
224         }
225     }
226
227
228     void MainWindow::alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
229     {
230         std::cerr << "got alert" << std::endl;
231         dlView_->updateItem(info, other_info);
232     }
233
234 } // namespace qtrapids