1 /***************************************************************************
2 * Copyright (C) 2009 by Lassi Väätämöinen *
3 * lassi.vaatamoinen@ixonos.com *
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. *
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. *
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 ***************************************************************************/
22 #include <qtrapids/dbus.hpp>
25 #include <QtGui/QMenuBar>
26 #include <QtGui/QToolBar>
28 #include <QtGui/QFileDialog>
29 #include <QtGui/QMessageBox>
30 #include <QtGui/QTabWidget>
32 #include "DownloadView.h"
34 #include "PreferencesDialog.h"
36 #include "MainWindowPage.h"
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"));
51 MainWindowPage::MainWindowPage() :
53 //QMainWindowPage(), // Superclass
57 preferencesDialog_(NULL),
58 settings_(QCoreApplication::organizationName()
59 , QCoreApplication::applicationName()),
60 server_(QtRapidsServer::staticInterfaceName()
61 , "/qtrapids", QDBusConnection::sessionBus())
66 void MainWindowPage::createContent()
70 QMenuBar *menuBar = new QMenuBar();
71 QMenu *tempMenu = NULL;
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"));
79 tempMenu = menuBar->addMenu(tr("&Settings"));
80 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
82 tempMenu = menuBar->addMenu(tr("&Help"));
83 QAction *aboutAction = tempMenu->addAction(tr("&About"));
84 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
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 );
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()));
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()));
106 //connect(this, SIGNAL(itemSelected(bool)), removeAction, SLOT(setEnabled(bool)));
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()));
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()));
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()));
123 action = new DuiAction( NULL, "About Qt", panel );
124 connect(action, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
127 // TABWIDGET (central widget)
128 tabWidget_ = new QTabWidget();
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()));
139 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
140 SLOT(on_seedItemSelectionChanged()));
143 // Tab widget as central widget.
144 setCentralWidget(tabWidget_);
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);
153 connect(this, SIGNAL(itemSelected(bool)), removeAction,
154 SLOT(setEnabled(bool)));
155 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
156 SLOT(handleToolBarAction(QAction*)));
158 QVariant geometry(settings_.value("geometry"));
159 if (!geometry.isNull()) {
160 qDebug() << "restoring geometry";
161 restoreGeometry(geometry.toByteArray());
167 MainWindowPage::~MainWindowPage()
169 settings_.setValue("geometry", saveGeometry());
172 // =========================== SLOTS =================================
173 void MainWindowPage::on_openAction_clicked()
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&)));
182 void MainWindowPage::on_removeAction_clicked()
184 QString hash = dlView_->prepareRemoveSelected();
186 server_.removeTorrent(hash);
188 qDebug() << "Exception removing torrent";
192 void MainWindowPage::on_quitAction_clicked()
197 void MainWindowPage::on_preferencesAction_clicked()
199 if (!preferencesDialog_) {
200 preferencesDialog_ = new PreferencesDialog(this);
202 preferencesDialog_->show();
203 preferencesDialog_->raise();
204 preferencesDialog_->activateWindow();
207 void MainWindowPage::on_aboutAction_clicked()
209 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
213 void MainWindowPage::on_aboutQtAction_clicked()
215 QMessageBox::aboutQt (this, tr("About Qt"));
219 void MainWindowPage::on_downloadItemSelectionChanged()
221 qDebug() << "MainWindowPage::on_seedItemSelectionChanged():" << dlView_->currentItem();
222 if (dlView_->currentItem() != NULL) {
223 emit(itemSelected(true));
225 emit(itemSelected(false));
229 void MainWindowPage::on_seedItemSelectionChanged()
231 qDebug() << "MainWindowPage::on_seedItemSelectionChanged():" << seedView_->currentItem();
232 if (seedView_->currentItem() != NULL) {
233 emit(itemSelected(true));
235 emit(itemSelected(false));
239 void MainWindowPage::handleToolBarAction(QAction* action)
241 if (action->text() == "Open") {
242 on_openAction_clicked();
243 } else if (action->text() == "Remove") {
244 on_removeAction_clicked();
248 void MainWindowPage::on_torrentFileSelected(const QString& file)
250 qDebug() << " MainWindowPage::on_torrentFileSelected(): " << file;
251 // Torrent filename empty, do nothing.
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;
261 server_.addTorrent(file, settings_.value("download/directory").toString()
264 qDebug() << "Exception adding torrent";
269 void MainWindowPage::alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
271 std::cerr << "got alert" << std::endl;
272 dlView_->updateItem(info, other_info);
275 } // namespace qtrapids