Code formatting/indentation unified in trunk
[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     {
121         qDebug() << "restoring geometry";
122         restoreGeometry(geometry.toByteArray());
123     }
124 }
125
126
127 MainWindow::~MainWindow()
128 {
129     settings_.setValue("geometry", saveGeometry());
130 }
131
132 // =========================== SLOTS =================================
133 void MainWindow::on_openAction_clicked()
134 {
135     QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
136     dialog->setFileMode(QFileDialog::ExistingFile);
137     connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
138     dialog->show();
139
140 }
141
142 void MainWindow::on_removeAction_clicked()
143 {
144     QString hash = dlView_->prepareRemoveSelected();
145     try
146     {
147         server_.removeTorrent(hash);
148     }
149     catch (...)
150     {
151         qDebug() << "Exception removing torrent";
152     }
153 }
154
155 void MainWindow::on_quitAction_clicked()
156 {
157     close();
158 }
159
160 void MainWindow::on_preferencesAction_clicked()
161 {
162     if (!preferencesDialog_)
163     {
164         preferencesDialog_ = new PreferencesDialog(this);
165     }
166     preferencesDialog_->show();
167     preferencesDialog_->raise();
168     preferencesDialog_->activateWindow();
169 }
170
171 void MainWindow::on_aboutAction_clicked()
172 {
173     QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
174 }
175
176
177 void MainWindow::on_aboutQtAction_clicked()
178 {
179     QMessageBox::aboutQt (this, tr("About Qt"));
180 }
181
182
183 void MainWindow::on_downloadItemSelectionChanged()
184 {
185     qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
186     if (dlView_->currentItem() != NULL)
187     {
188         emit(itemSelected(true));
189     }
190     else
191     {
192         emit(itemSelected(false));
193     }
194 }
195
196 void MainWindow::on_seedItemSelectionChanged()
197 {
198     qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
199     if (seedView_->currentItem() != NULL)
200     {
201         emit(itemSelected(true));
202     }
203     else
204     {
205         emit(itemSelected(false));
206     }
207 }
208
209 void MainWindow::handleToolBarAction(QAction* action)
210 {
211     if (action->text() == "Open")
212     {
213         on_openAction_clicked();
214     }
215     else if (action->text() == "Remove")
216     {
217         on_removeAction_clicked();
218     }
219 }
220
221 void MainWindow::on_torrentFileSelected(const QString& file)
222 {
223     qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
224     // Torrent filename empty, do nothing.
225     if (file == "")
226     {
227         return;
228     }
229
230     // Otherwise add torrent
231     // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
232     // save_path is the only mandatory parameter, rest are optional.
233     //addParams.storage_mode = libtorrent::storage_mode_allocate;
234     try
235     {
236         server_.addTorrent(file, settings_.value("download/directory").toString()
237                            , ParamsMap_t());
238     }
239     catch (...)
240     {
241         qDebug() << "Exception adding torrent";
242     }
243 }
244
245
246 void MainWindow::alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
247 {
248     std::cerr << "got alert" << std::endl;
249     dlView_->updateItem(info, other_info);
250 }
251
252 } // namespace qtrapids