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 ***************************************************************************/
27 #include <QFileDialog>
28 #include <QMessageBox>
30 #include "DownloadView.h"
32 #include "PreferencesDialog.h"
34 #include "MainWindow.h"
36 const QString ABOUT_TEXT
37 = QString(QObject::trUtf8("QtRapids, a simple BitTorrent client based on"
38 "\nQt and Libtorrent."
39 "\n\nURL: http://qtrapids.garage.maemo.org/"
40 "\n\nAuthor(s):\nLassi Väätämöinen, lassi.vaatamoinen@ixonos.com"
41 "\n\nIxonos Plc, Finland\n"));
45 MainWindow::MainWindow():
46 QMainWindow(), // Superclass
50 preferencesDialog_(NULL),
55 QMenuBar *menuBar = new QMenuBar();
56 QMenu *tempMenu = NULL;
58 tempMenu = menuBar->addMenu(tr("&File"));
59 QAction *openAction = tempMenu->addAction(tr("&Open"));
60 QAction *quitAction = tempMenu->addAction(tr("&Quit"));
62 tempMenu = menuBar->addMenu(tr("&Settings"));
63 QAction *preferencesAction = tempMenu->addAction(tr("&Preferences"));
65 tempMenu = menuBar->addMenu(tr("&Help"));
66 QAction *aboutAction = tempMenu->addAction(tr("&About"));
67 QAction *aboutQtAction = tempMenu->addAction(tr("About &Qt"));
70 connect(openAction, SIGNAL(triggered()), this, SLOT(on_openAction_clicked()));
71 connect(quitAction, SIGNAL(triggered()), this, SLOT(on_quitAction_clicked()));
72 connect(preferencesAction, SIGNAL(triggered()), this, SLOT(on_preferencesAction_clicked()));
73 connect(aboutAction, SIGNAL(triggered()), this, SLOT(on_aboutAction_clicked()));
74 connect(aboutQtAction, SIGNAL(triggered()), this, SLOT(on_aboutQtAction_clicked()));
76 // TABWIDGET (central widget)
77 tabWidget_ = new QTabWidget();
79 /// @todo Exception handling
80 dlView_ = new DownloadView(this);
81 seedView_ = new SeedView(this);
83 tabWidget_->addTab(dlView_, tr("Downloads"));
84 tabWidget_->addTab(seedView_, tr("Seeds"));
86 // Tab widget as central widget.
87 setCentralWidget(tabWidget_);
90 QToolBar *toolBar = new QToolBar();
91 toolBar->addAction(tr("Open"));
93 addToolBar(Qt::TopToolBarArea, toolBar);
94 connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*)));
97 connect(&btSession_, SIGNAL(alert(std::auto_ptr<TorrentAlert>)),
98 this, SLOT(on_torrentAlert(std::auto_ptr<TorrentAlert>)));
102 MainWindow::~MainWindow()
106 // =========================== SLOTS =================================
107 void MainWindow::on_openAction_clicked()
109 QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)"));
110 dialog->setFileMode(QFileDialog::ExistingFile);
111 connect(dialog, SIGNAL(fileSelected(const QString&)), this, SLOT(on_torrentFileSelected(const QString&)));
117 void MainWindow::on_quitAction_clicked()
122 void MainWindow::on_preferencesAction_clicked()
124 if (!preferencesDialog_) {
125 preferencesDialog_ = new PreferencesDialog(this);
127 preferencesDialog_->show();
128 preferencesDialog_->raise();
129 preferencesDialog_->activateWindow();
132 void MainWindow::on_aboutAction_clicked()
134 QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT);
138 void MainWindow::on_aboutQtAction_clicked()
140 QMessageBox::aboutQt (this, tr("About Qt"));
144 void MainWindow::handleToolBarAction(QAction* action)
146 if (action->text() == "Open") {
147 on_openAction_clicked();
152 void MainWindow::on_torrentFileSelected(const QString& file)
154 qDebug() << " MainWindow::on_torrentFileSelected(): " << file;
155 // Torrent filename empty, do nothing.
160 // Otherwise add torrent
161 // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent
162 AddTorrentParams addParams;
163 boost::intrusive_ptr<libtorrent::torrent_info> tiTmp =
164 new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
165 addParams.ti = tiTmp;
166 // save_path is the only mandatory parameter, rest are optional.
167 addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString());
168 //addParams.storage_mode = libtorrent::storage_mode_allocate;
169 std::auto_ptr<QTorrentHandle> handlePtr = btSession_.addTorrent(addParams);
170 dlView_->newItem(handlePtr.get());
171 qDebug() << "Is valid: " << handlePtr->isValid();
174 void MainWindow::on_torrentAlert(std::auto_ptr<TorrentAlert> al)
176 if (al.get() != NULL)
177 qDebug() << "MainWindow::on_torrentAlert(): " << QString::fromStdString(al->message());