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 "MainWindow.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 MainWindow::MainWindow() :
52 QMainWindow(), // Superclass
56 preferencesDialog_(NULL),
57 settings_(QCoreApplication::organizationName()
58 , QCoreApplication::applicationName()),
59 server_(QtRapidsServer::staticInterfaceName()
60 , "/qtrapids", QDBusConnection::sessionBus())
64 QMenuBar *menuBar = new QMenuBar();
65 QMenu *tempMenu = NULL;
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"));
73 tempMenu = menuBar->addMenu(tr("&Settings"));
74 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
76 tempMenu = menuBar->addMenu(tr("&Help"));
77 QAction *aboutAction = tempMenu->addAction(tr("&About"));
78 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
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()));
89 // TABWIDGET (central widget)
90 tabWidget_ = new QTabWidget();
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()));
100 connect(seedView_, SIGNAL(itemSelectionChanged()), this,
101 SLOT(on_seedItemSelectionChanged()));
103 // Tab widget as central widget.
104 setCentralWidget(tabWidget_);
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);
113 connect(this, SIGNAL(itemSelected(bool)), removeAction,
114 SLOT(setEnabled(bool)));
115 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this,
116 SLOT(handleToolBarAction(QAction*)));
118 QVariant geometry(settings_.value("geometry"));
119 if (!geometry.isNull())
121 qDebug() << "restoring geometry";
122 restoreGeometry(geometry.toByteArray());
127 MainWindow::~MainWindow()
129 settings_.setValue("geometry", saveGeometry());
132 // =========================== SLOTS =================================
133 void MainWindow::on_openAction_clicked()
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&)));
142 void MainWindow::on_removeAction_clicked()
144 QString hash = dlView_->prepareRemoveSelected();
147 server_.removeTorrent(hash);
151 qDebug() << "Exception removing torrent";
155 void MainWindow::on_quitAction_clicked()
160 void MainWindow::on_preferencesAction_clicked()
162 if (!preferencesDialog_)
164 preferencesDialog_ = new PreferencesDialog(this);
166 preferencesDialog_->show();
167 preferencesDialog_->raise();
168 preferencesDialog_->activateWindow();
171 void MainWindow::on_aboutAction_clicked()
173 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
177 void MainWindow::on_aboutQtAction_clicked()
179 QMessageBox::aboutQt (this, tr("About Qt"));
183 void MainWindow::on_downloadItemSelectionChanged()
185 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << dlView_->currentItem();
186 if (dlView_->currentItem() != NULL)
188 emit(itemSelected(true));
192 emit(itemSelected(false));
196 void MainWindow::on_seedItemSelectionChanged()
198 qDebug() << "MainWindow::on_seedItemSelectionChanged():" << seedView_->currentItem();
199 if (seedView_->currentItem() != NULL)
201 emit(itemSelected(true));
205 emit(itemSelected(false));
209 void MainWindow::handleToolBarAction(QAction* action)
211 if (action->text() == "Open")
213 on_openAction_clicked();
215 else if (action->text() == "Remove")
217 on_removeAction_clicked();
221 void MainWindow::on_torrentFileSelected(const QString& file)
223 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
224 // Torrent filename empty, do nothing.
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;
236 server_.addTorrent(file, settings_.value("download/directory").toString()
241 qDebug() << "Exception adding torrent";
246 void MainWindow::alert(qtrapids::TorrentState info, qtrapids::ParamsMap_t other_info)
248 std::cerr << "got alert" << std::endl;
249 dlView_->updateItem(info, other_info);
252 } // namespace qtrapids