author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "addtorrentdialog.h" |
|
44 |
#include "mainwindow.h" |
|
45 |
#include "ratecontroller.h" |
|
46 |
#include "torrentclient.h" |
|
47 |
||
48 |
// TorrentView extends QTreeWidget to allow drag and drop. |
|
49 |
class TorrentView : public QTreeWidget |
|
50 |
{ |
|
51 |
Q_OBJECT |
|
52 |
public: |
|
53 |
TorrentView(QWidget *parent = 0); |
|
54 |
||
55 |
signals: |
|
56 |
void fileDropped(const QString &fileName); |
|
57 |
||
58 |
protected: |
|
59 |
void dragMoveEvent(QDragMoveEvent *event); |
|
60 |
void dropEvent(QDropEvent *event); |
|
61 |
}; |
|
62 |
||
63 |
// TorrentViewDelegate is used to draw the progress bars. |
|
64 |
class TorrentViewDelegate : public QItemDelegate |
|
65 |
{ |
|
66 |
Q_OBJECT |
|
67 |
public: |
|
68 |
inline TorrentViewDelegate(MainWindow *mainWindow) : QItemDelegate(mainWindow) {} |
|
69 |
||
70 |
inline void paint(QPainter *painter, const QStyleOptionViewItem &option, |
|
71 |
const QModelIndex &index ) const |
|
72 |
{ |
|
73 |
if (index.column() != 2) { |
|
74 |
QItemDelegate::paint(painter, option, index); |
|
75 |
return; |
|
76 |
} |
|
77 |
||
78 |
// Set up a QStyleOptionProgressBar to precisely mimic the |
|
79 |
// environment of a progress bar. |
|
80 |
QStyleOptionProgressBar progressBarOption; |
|
81 |
progressBarOption.state = QStyle::State_Enabled; |
|
82 |
progressBarOption.direction = QApplication::layoutDirection(); |
|
83 |
progressBarOption.rect = option.rect; |
|
84 |
progressBarOption.fontMetrics = QApplication::fontMetrics(); |
|
85 |
progressBarOption.minimum = 0; |
|
86 |
progressBarOption.maximum = 100; |
|
87 |
progressBarOption.textAlignment = Qt::AlignCenter; |
|
88 |
progressBarOption.textVisible = true; |
|
89 |
||
90 |
// Set the progress and text values of the style option. |
|
91 |
int progress = qobject_cast<MainWindow *>(parent())->clientForRow(index.row())->progress(); |
|
92 |
progressBarOption.progress = progress < 0 ? 0 : progress; |
|
93 |
progressBarOption.text = QString().sprintf("%d%%", progressBarOption.progress); |
|
94 |
||
95 |
// Draw the progress bar onto the view. |
|
96 |
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); |
|
97 |
} |
|
98 |
}; |
|
99 |
||
100 |
MainWindow::MainWindow(QWidget *parent) |
|
101 |
: QMainWindow(parent), quitDialog(0), saveChanges(false) |
|
102 |
{ |
|
103 |
// Initialize some static strings |
|
104 |
QStringList headers; |
|
105 |
headers << tr("Torrent") << tr("Peers/Seeds") << tr("Progress") |
|
106 |
<< tr("Down rate") << tr("Up rate") << tr("Status"); |
|
107 |
||
108 |
// Main torrent list |
|
109 |
torrentView = new TorrentView(this); |
|
110 |
torrentView->setItemDelegate(new TorrentViewDelegate(this)); |
|
111 |
torrentView->setHeaderLabels(headers); |
|
112 |
torrentView->setSelectionBehavior(QAbstractItemView::SelectRows); |
|
113 |
torrentView->setAlternatingRowColors(true); |
|
114 |
torrentView->setRootIsDecorated(false); |
|
115 |
setCentralWidget(torrentView); |
|
116 |
||
117 |
// Set header resize modes and initial section sizes |
|
118 |
QFontMetrics fm = fontMetrics(); |
|
119 |
QHeaderView *header = torrentView->header(); |
|
120 |
header->resizeSection(0, fm.width("typical-name-for-a-torrent.torrent")); |
|
121 |
header->resizeSection(1, fm.width(headers.at(1) + " ")); |
|
122 |
header->resizeSection(2, fm.width(headers.at(2) + " ")); |
|
123 |
header->resizeSection(3, qMax(fm.width(headers.at(3) + " "), fm.width(" 1234.0 KB/s "))); |
|
124 |
header->resizeSection(4, qMax(fm.width(headers.at(4) + " "), fm.width(" 1234.0 KB/s "))); |
|
125 |
header->resizeSection(5, qMax(fm.width(headers.at(5) + " "), fm.width(tr("Downloading") + " "))); |
|
126 |
||
127 |
// Create common actions |
|
128 |
QAction *newTorrentAction = new QAction(QIcon(":/icons/bottom.png"), tr("Add &new torrent"), this); |
|
129 |
pauseTorrentAction = new QAction(QIcon(":/icons/player_pause.png"), tr("&Pause torrent"), this); |
|
130 |
removeTorrentAction = new QAction(QIcon(":/icons/player_stop.png"), tr("&Remove torrent"), this); |
|
131 |
||
132 |
// File menu |
|
133 |
QMenu *fileMenu = menuBar()->addMenu(tr("&File")); |
|
134 |
fileMenu->addAction(newTorrentAction); |
|
135 |
fileMenu->addAction(pauseTorrentAction); |
|
136 |
fileMenu->addAction(removeTorrentAction); |
|
137 |
fileMenu->addSeparator(); |
|
138 |
fileMenu->addAction(QIcon(":/icons/exit.png"), tr("E&xit"), this, SLOT(close())); |
|
139 |
||
140 |
// Help menu |
|
141 |
QMenu *helpMenu = menuBar()->addMenu(tr("&Help")); |
|
142 |
helpMenu->addAction(tr("&About"), this, SLOT(about())); |
|
143 |
helpMenu->addAction(tr("About &Qt"), qApp, SLOT(aboutQt())); |
|
144 |
||
145 |
// Top toolbar |
|
146 |
QToolBar *topBar = new QToolBar(tr("Tools")); |
|
147 |
addToolBar(Qt::TopToolBarArea, topBar); |
|
148 |
topBar->setMovable(false); |
|
149 |
topBar->addAction(newTorrentAction); |
|
150 |
topBar->addAction(removeTorrentAction); |
|
151 |
topBar->addAction(pauseTorrentAction); |
|
152 |
topBar->addSeparator(); |
|
153 |
downActionTool = topBar->addAction(QIcon(tr(":/icons/1downarrow.png")), tr("Move down")); |
|
154 |
upActionTool = topBar->addAction(QIcon(tr(":/icons/1uparrow.png")), tr("Move up")); |
|
155 |
||
156 |
// Bottom toolbar |
|
157 |
QToolBar *bottomBar = new QToolBar(tr("Rate control")); |
|
158 |
addToolBar(Qt::BottomToolBarArea, bottomBar); |
|
159 |
bottomBar->setMovable(false); |
|
160 |
downloadLimitSlider = new QSlider(Qt::Horizontal); |
|
161 |
downloadLimitSlider->setRange(0, 1000); |
|
162 |
bottomBar->addWidget(new QLabel(tr("Max download:"))); |
|
163 |
bottomBar->addWidget(downloadLimitSlider); |
|
164 |
bottomBar->addWidget((downloadLimitLabel = new QLabel(tr("0 KB/s")))); |
|
165 |
downloadLimitLabel->setFixedSize(QSize(fm.width(tr("99999 KB/s")), fm.lineSpacing())); |
|
166 |
bottomBar->addSeparator(); |
|
167 |
uploadLimitSlider = new QSlider(Qt::Horizontal); |
|
168 |
uploadLimitSlider->setRange(0, 1000); |
|
169 |
bottomBar->addWidget(new QLabel(tr("Max upload:"))); |
|
170 |
bottomBar->addWidget(uploadLimitSlider); |
|
171 |
bottomBar->addWidget((uploadLimitLabel = new QLabel(tr("0 KB/s")))); |
|
172 |
uploadLimitLabel->setFixedSize(QSize(fm.width(tr("99999 KB/s")), fm.lineSpacing())); |
|
173 |
||
174 |
// Set up connections |
|
175 |
connect(torrentView, SIGNAL(itemSelectionChanged()), |
|
176 |
this, SLOT(setActionsEnabled())); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
connect(torrentView, SIGNAL(fileDropped(QString)), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
this, SLOT(acceptFileDrop(QString))); |
0 | 179 |
connect(uploadLimitSlider, SIGNAL(valueChanged(int)), |
180 |
this, SLOT(setUploadLimit(int))); |
|
181 |
connect(downloadLimitSlider, SIGNAL(valueChanged(int)), |
|
182 |
this, SLOT(setDownloadLimit(int))); |
|
183 |
connect(newTorrentAction, SIGNAL(triggered()), |
|
184 |
this, SLOT(addTorrent())); |
|
185 |
connect(pauseTorrentAction, SIGNAL(triggered()), |
|
186 |
this, SLOT(pauseTorrent())); |
|
187 |
connect(removeTorrentAction, SIGNAL(triggered()), |
|
188 |
this, SLOT(removeTorrent())); |
|
189 |
connect(upActionTool, SIGNAL(triggered(bool)), |
|
190 |
this, SLOT(moveTorrentUp())); |
|
191 |
connect(downActionTool, SIGNAL(triggered(bool)), |
|
192 |
this, SLOT(moveTorrentDown())); |
|
193 |
||
194 |
// Load settings and start |
|
195 |
setWindowTitle(tr("Torrent Client")); |
|
196 |
setActionsEnabled(); |
|
197 |
QMetaObject::invokeMethod(this, "loadSettings", Qt::QueuedConnection); |
|
198 |
} |
|
199 |
||
200 |
QSize MainWindow::sizeHint() const |
|
201 |
{ |
|
202 |
const QHeaderView *header = torrentView->header(); |
|
203 |
||
204 |
// Add up the sizes of all header sections. The last section is |
|
205 |
// stretched, so its size is relative to the size of the width; |
|
206 |
// instead of counting it, we count the size of its largest value. |
|
207 |
int width = fontMetrics().width(tr("Downloading") + " "); |
|
208 |
for (int i = 0; i < header->count() - 1; ++i) |
|
209 |
width += header->sectionSize(i); |
|
210 |
||
211 |
return QSize(width, QMainWindow::sizeHint().height()) |
|
212 |
.expandedTo(QApplication::globalStrut()); |
|
213 |
} |
|
214 |
||
215 |
const TorrentClient *MainWindow::clientForRow(int row) const |
|
216 |
{ |
|
217 |
// Return the client at the given row. |
|
218 |
return jobs.at(row).client; |
|
219 |
} |
|
220 |
||
221 |
int MainWindow::rowOfClient(TorrentClient *client) const |
|
222 |
{ |
|
223 |
// Return the row that displays this client's status, or -1 if the |
|
224 |
// client is not known. |
|
225 |
int row = 0; |
|
226 |
foreach (Job job, jobs) { |
|
227 |
if (job.client == client) |
|
228 |
return row; |
|
229 |
++row; |
|
230 |
} |
|
231 |
return -1; |
|
232 |
} |
|
233 |
||
234 |
void MainWindow::loadSettings() |
|
235 |
{ |
|
236 |
// Load base settings (last working directory, upload/download limits). |
|
237 |
QSettings settings("Trolltech", "Torrent"); |
|
238 |
lastDirectory = settings.value("LastDirectory").toString(); |
|
239 |
if (lastDirectory.isEmpty()) |
|
240 |
lastDirectory = QDir::currentPath(); |
|
241 |
int up = settings.value("UploadLimit").toInt(); |
|
242 |
int down = settings.value("DownloadLimit").toInt(); |
|
243 |
uploadLimitSlider->setValue(up ? up : 170); |
|
244 |
downloadLimitSlider->setValue(down ? down : 550); |
|
245 |
||
246 |
// Resume all previous downloads. |
|
247 |
int size = settings.beginReadArray("Torrents"); |
|
248 |
for (int i = 0; i < size; ++i) { |
|
249 |
settings.setArrayIndex(i); |
|
250 |
QByteArray resumeState = settings.value("resumeState").toByteArray(); |
|
251 |
QString fileName = settings.value("sourceFileName").toString(); |
|
252 |
QString dest = settings.value("destinationFolder").toString(); |
|
253 |
||
254 |
if (addTorrent(fileName, dest, resumeState)) { |
|
255 |
TorrentClient *client = jobs.last().client; |
|
256 |
client->setDownloadedBytes(settings.value("downloadedBytes").toLongLong()); |
|
257 |
client->setUploadedBytes(settings.value("uploadedBytes").toLongLong()); |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
||
262 |
bool MainWindow::addTorrent() |
|
263 |
{ |
|
264 |
// Show the file dialog, let the user select what torrent to start downloading. |
|
265 |
QString fileName = QFileDialog::getOpenFileName(this, tr("Choose a torrent file"), |
|
266 |
lastDirectory, |
|
267 |
tr("Torrents (*.torrent);;" |
|
268 |
" All files (*.*)")); |
|
269 |
if (fileName.isEmpty()) |
|
270 |
return false; |
|
271 |
lastDirectory = QFileInfo(fileName).absolutePath(); |
|
272 |
||
273 |
// Show the "Add Torrent" dialog. |
|
274 |
AddTorrentDialog *addTorrentDialog = new AddTorrentDialog(this); |
|
275 |
addTorrentDialog->setTorrent(fileName); |
|
276 |
addTorrentDialog->deleteLater(); |
|
277 |
if (!addTorrentDialog->exec()) |
|
278 |
return false; |
|
279 |
||
280 |
// Add the torrent to our list of downloads |
|
281 |
addTorrent(fileName, addTorrentDialog->destinationFolder()); |
|
282 |
if (!saveChanges) { |
|
283 |
saveChanges = true; |
|
284 |
QTimer::singleShot(1000, this, SLOT(saveSettings())); |
|
285 |
} |
|
286 |
return true; |
|
287 |
} |
|
288 |
||
289 |
void MainWindow::removeTorrent() |
|
290 |
{ |
|
291 |
// Find the row of the current item, and find the torrent client |
|
292 |
// for that row. |
|
293 |
int row = torrentView->indexOfTopLevelItem(torrentView->currentItem()); |
|
294 |
TorrentClient *client = jobs.at(row).client; |
|
295 |
||
296 |
// Stop the client. |
|
297 |
client->disconnect(); |
|
298 |
connect(client, SIGNAL(stopped()), this, SLOT(torrentStopped())); |
|
299 |
client->stop(); |
|
300 |
||
301 |
// Remove the row from the view. |
|
302 |
delete torrentView->takeTopLevelItem(row); |
|
303 |
jobs.removeAt(row); |
|
304 |
setActionsEnabled(); |
|
305 |
||
306 |
saveChanges = true; |
|
307 |
saveSettings(); |
|
308 |
} |
|
309 |
||
310 |
void MainWindow::torrentStopped() |
|
311 |
{ |
|
312 |
// Schedule the client for deletion. |
|
313 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
314 |
client->deleteLater(); |
|
315 |
||
316 |
// If the quit dialog is shown, update its progress. |
|
317 |
if (quitDialog) { |
|
318 |
if (++jobsStopped == jobsToStop) |
|
319 |
quitDialog->close(); |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
void MainWindow::torrentError(TorrentClient::Error) |
|
324 |
{ |
|
325 |
// Delete the client. |
|
326 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
327 |
int row = rowOfClient(client); |
|
328 |
QString fileName = jobs.at(row).torrentFileName; |
|
329 |
jobs.removeAt(row); |
|
330 |
||
331 |
// Display the warning. |
|
332 |
QMessageBox::warning(this, tr("Error"), |
|
333 |
tr("An error occurred while downloading %0: %1") |
|
334 |
.arg(fileName) |
|
335 |
.arg(client->errorString())); |
|
336 |
||
337 |
delete torrentView->takeTopLevelItem(row); |
|
338 |
client->deleteLater(); |
|
339 |
} |
|
340 |
||
341 |
bool MainWindow::addTorrent(const QString &fileName, const QString &destinationFolder, |
|
342 |
const QByteArray &resumeState) |
|
343 |
{ |
|
344 |
// Check if the torrent is already being downloaded. |
|
345 |
foreach (Job job, jobs) { |
|
346 |
if (job.torrentFileName == fileName && job.destinationDirectory == destinationFolder) { |
|
347 |
QMessageBox::warning(this, tr("Already downloading"), |
|
348 |
tr("The torrent file %1 is " |
|
349 |
"already being downloaded.").arg(fileName)); |
|
350 |
return false; |
|
351 |
} |
|
352 |
} |
|
353 |
||
354 |
// Create a new torrent client and attempt to parse the torrent data. |
|
355 |
TorrentClient *client = new TorrentClient(this); |
|
356 |
if (!client->setTorrent(fileName)) { |
|
357 |
QMessageBox::warning(this, tr("Error"), |
|
358 |
tr("The torrent file %1 cannot not be opened/resumed.").arg(fileName)); |
|
359 |
delete client; |
|
360 |
return false; |
|
361 |
} |
|
362 |
client->setDestinationFolder(destinationFolder); |
|
363 |
client->setDumpedState(resumeState); |
|
364 |
||
365 |
// Setup the client connections. |
|
366 |
connect(client, SIGNAL(stateChanged(TorrentClient::State)), this, SLOT(updateState(TorrentClient::State))); |
|
367 |
connect(client, SIGNAL(peerInfoUpdated()), this, SLOT(updatePeerInfo())); |
|
368 |
connect(client, SIGNAL(progressUpdated(int)), this, SLOT(updateProgress(int))); |
|
369 |
connect(client, SIGNAL(downloadRateUpdated(int)), this, SLOT(updateDownloadRate(int))); |
|
370 |
connect(client, SIGNAL(uploadRateUpdated(int)), this, SLOT(updateUploadRate(int))); |
|
371 |
connect(client, SIGNAL(stopped()), this, SLOT(torrentStopped())); |
|
372 |
connect(client, SIGNAL(error(TorrentClient::Error)), this, SLOT(torrentError(TorrentClient::Error))); |
|
373 |
||
374 |
// Add the client to the list of downloading jobs. |
|
375 |
Job job; |
|
376 |
job.client = client; |
|
377 |
job.torrentFileName = fileName; |
|
378 |
job.destinationDirectory = destinationFolder; |
|
379 |
jobs << job; |
|
380 |
||
381 |
// Create and add a row in the torrent view for this download. |
|
382 |
QTreeWidgetItem *item = new QTreeWidgetItem(torrentView); |
|
383 |
||
384 |
QString baseFileName = QFileInfo(fileName).fileName(); |
|
385 |
if (baseFileName.toLower().endsWith(".torrent")) |
|
386 |
baseFileName.remove(baseFileName.size() - 8); |
|
387 |
||
388 |
item->setText(0, baseFileName); |
|
389 |
item->setToolTip(0, tr("Torrent: %1<br>Destination: %2") |
|
390 |
.arg(baseFileName).arg(destinationFolder)); |
|
391 |
item->setText(1, tr("0/0")); |
|
392 |
item->setText(2, "0"); |
|
393 |
item->setText(3, "0.0 KB/s"); |
|
394 |
item->setText(4, "0.0 KB/s"); |
|
395 |
item->setText(5, tr("Idle")); |
|
396 |
item->setFlags(item->flags() & ~Qt::ItemIsEditable); |
|
397 |
item->setTextAlignment(1, Qt::AlignHCenter); |
|
398 |
||
399 |
if (!saveChanges) { |
|
400 |
saveChanges = true; |
|
401 |
QTimer::singleShot(5000, this, SLOT(saveSettings())); |
|
402 |
} |
|
403 |
client->start(); |
|
404 |
return true; |
|
405 |
} |
|
406 |
||
407 |
void MainWindow::saveSettings() |
|
408 |
{ |
|
409 |
if (!saveChanges) |
|
410 |
return; |
|
411 |
saveChanges = false; |
|
412 |
||
413 |
// Prepare and reset the settings |
|
414 |
QSettings settings("Trolltech", "Torrent"); |
|
415 |
settings.clear(); |
|
416 |
||
417 |
settings.setValue("LastDirectory", lastDirectory); |
|
418 |
settings.setValue("UploadLimit", uploadLimitSlider->value()); |
|
419 |
settings.setValue("DownloadLimit", downloadLimitSlider->value()); |
|
420 |
||
421 |
// Store data on all known torrents |
|
422 |
settings.beginWriteArray("Torrents"); |
|
423 |
for (int i = 0; i < jobs.size(); ++i) { |
|
424 |
settings.setArrayIndex(i); |
|
425 |
settings.setValue("sourceFileName", jobs.at(i).torrentFileName); |
|
426 |
settings.setValue("destinationFolder", jobs.at(i).destinationDirectory); |
|
427 |
settings.setValue("uploadedBytes", jobs.at(i).client->uploadedBytes()); |
|
428 |
settings.setValue("downloadedBytes", jobs.at(i).client->downloadedBytes()); |
|
429 |
settings.setValue("resumeState", jobs.at(i).client->dumpedState()); |
|
430 |
} |
|
431 |
settings.endArray(); |
|
432 |
settings.sync(); |
|
433 |
} |
|
434 |
||
435 |
void MainWindow::updateState(TorrentClient::State) |
|
436 |
{ |
|
437 |
// Update the state string whenever the client's state changes. |
|
438 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
439 |
int row = rowOfClient(client); |
|
440 |
QTreeWidgetItem *item = torrentView->topLevelItem(row); |
|
441 |
if (item) { |
|
442 |
item->setToolTip(0, tr("Torrent: %1<br>Destination: %2<br>State: %3") |
|
443 |
.arg(jobs.at(row).torrentFileName) |
|
444 |
.arg(jobs.at(row).destinationDirectory) |
|
445 |
.arg(client->stateString())); |
|
446 |
||
447 |
item->setText(5, client->stateString()); |
|
448 |
} |
|
449 |
setActionsEnabled(); |
|
450 |
} |
|
451 |
||
452 |
void MainWindow::updatePeerInfo() |
|
453 |
{ |
|
454 |
// Update the number of connected, visited, seed and leecher peers. |
|
455 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
456 |
int row = rowOfClient(client); |
|
457 |
||
458 |
QTreeWidgetItem *item = torrentView->topLevelItem(row); |
|
459 |
item->setText(1, tr("%1/%2").arg(client->connectedPeerCount()) |
|
460 |
.arg(client->seedCount())); |
|
461 |
} |
|
462 |
||
463 |
void MainWindow::updateProgress(int percent) |
|
464 |
{ |
|
465 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
466 |
int row = rowOfClient(client); |
|
467 |
||
468 |
// Update the progressbar. |
|
469 |
QTreeWidgetItem *item = torrentView->topLevelItem(row); |
|
470 |
if (item) |
|
471 |
item->setText(2, QString::number(percent)); |
|
472 |
} |
|
473 |
||
474 |
void MainWindow::setActionsEnabled() |
|
475 |
{ |
|
476 |
// Find the view item and client for the current row, and update |
|
477 |
// the states of the actions. |
|
478 |
QTreeWidgetItem *item = 0; |
|
479 |
if (!torrentView->selectedItems().isEmpty()) |
|
480 |
item = torrentView->selectedItems().first(); |
|
481 |
TorrentClient *client = item ? jobs.at(torrentView->indexOfTopLevelItem(item)).client : 0; |
|
482 |
bool pauseEnabled = client && ((client->state() == TorrentClient::Paused) |
|
483 |
|| (client->state() > TorrentClient::Preparing)); |
|
484 |
||
485 |
removeTorrentAction->setEnabled(item != 0); |
|
486 |
pauseTorrentAction->setEnabled(item != 0 && pauseEnabled); |
|
487 |
||
488 |
if (client && client->state() == TorrentClient::Paused) { |
|
489 |
pauseTorrentAction->setIcon(QIcon(":/icons/player_play.png")); |
|
490 |
pauseTorrentAction->setText(tr("Resume torrent")); |
|
491 |
} else { |
|
492 |
pauseTorrentAction->setIcon(QIcon(":/icons/player_pause.png")); |
|
493 |
pauseTorrentAction->setText(tr("Pause torrent")); |
|
494 |
} |
|
495 |
||
496 |
int row = torrentView->indexOfTopLevelItem(item); |
|
497 |
upActionTool->setEnabled(item && row != 0); |
|
498 |
downActionTool->setEnabled(item && row != jobs.size() - 1); |
|
499 |
} |
|
500 |
||
501 |
void MainWindow::updateDownloadRate(int bytesPerSecond) |
|
502 |
{ |
|
503 |
// Update the download rate. |
|
504 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
505 |
int row = rowOfClient(client); |
|
506 |
QString num; |
|
507 |
num.sprintf("%.1f KB/s", bytesPerSecond / 1024.0); |
|
508 |
torrentView->topLevelItem(row)->setText(3, num); |
|
509 |
||
510 |
if (!saveChanges) { |
|
511 |
saveChanges = true; |
|
512 |
QTimer::singleShot(5000, this, SLOT(saveSettings())); |
|
513 |
} |
|
514 |
} |
|
515 |
||
516 |
void MainWindow::updateUploadRate(int bytesPerSecond) |
|
517 |
{ |
|
518 |
// Update the upload rate. |
|
519 |
TorrentClient *client = qobject_cast<TorrentClient *>(sender()); |
|
520 |
int row = rowOfClient(client); |
|
521 |
QString num; |
|
522 |
num.sprintf("%.1f KB/s", bytesPerSecond / 1024.0); |
|
523 |
torrentView->topLevelItem(row)->setText(4, num); |
|
524 |
||
525 |
if (!saveChanges) { |
|
526 |
saveChanges = true; |
|
527 |
QTimer::singleShot(5000, this, SLOT(saveSettings())); |
|
528 |
} |
|
529 |
} |
|
530 |
||
531 |
void MainWindow::pauseTorrent() |
|
532 |
{ |
|
533 |
// Pause or unpause the current torrent. |
|
534 |
int row = torrentView->indexOfTopLevelItem(torrentView->currentItem()); |
|
535 |
TorrentClient *client = jobs.at(row).client; |
|
536 |
client->setPaused(client->state() != TorrentClient::Paused); |
|
537 |
setActionsEnabled(); |
|
538 |
} |
|
539 |
||
540 |
void MainWindow::moveTorrentUp() |
|
541 |
{ |
|
542 |
QTreeWidgetItem *item = torrentView->currentItem(); |
|
543 |
int row = torrentView->indexOfTopLevelItem(item); |
|
544 |
if (row == 0) |
|
545 |
return; |
|
546 |
||
547 |
Job tmp = jobs.at(row - 1); |
|
548 |
jobs[row - 1] = jobs[row]; |
|
549 |
jobs[row] = tmp; |
|
550 |
||
551 |
QTreeWidgetItem *itemAbove = torrentView->takeTopLevelItem(row - 1); |
|
552 |
torrentView->insertTopLevelItem(row, itemAbove); |
|
553 |
setActionsEnabled(); |
|
554 |
} |
|
555 |
||
556 |
void MainWindow::moveTorrentDown() |
|
557 |
{ |
|
558 |
QTreeWidgetItem *item = torrentView->currentItem(); |
|
559 |
int row = torrentView->indexOfTopLevelItem(item); |
|
560 |
if (row == jobs.size() - 1) |
|
561 |
return; |
|
562 |
||
563 |
Job tmp = jobs.at(row + 1); |
|
564 |
jobs[row + 1] = jobs[row]; |
|
565 |
jobs[row] = tmp; |
|
566 |
||
567 |
QTreeWidgetItem *itemAbove = torrentView->takeTopLevelItem(row + 1); |
|
568 |
torrentView->insertTopLevelItem(row, itemAbove); |
|
569 |
setActionsEnabled(); |
|
570 |
} |
|
571 |
||
572 |
static int rateFromValue(int value) |
|
573 |
{ |
|
574 |
int rate = 0; |
|
575 |
if (value >= 0 && value < 250) { |
|
576 |
rate = 1 + int(value * 0.124); |
|
577 |
} else if (value < 500) { |
|
578 |
rate = 32 + int((value - 250) * 0.384); |
|
579 |
} else if (value < 750) { |
|
580 |
rate = 128 + int((value - 500) * 1.536); |
|
581 |
} else { |
|
582 |
rate = 512 + int((value - 750) * 6.1445); |
|
583 |
} |
|
584 |
return rate; |
|
585 |
} |
|
586 |
||
587 |
void MainWindow::setUploadLimit(int value) |
|
588 |
{ |
|
589 |
int rate = rateFromValue(value); |
|
590 |
uploadLimitLabel->setText(tr("%1 KB/s").arg(QString().sprintf("%4d", rate))); |
|
591 |
RateController::instance()->setUploadLimit(rate * 1024); |
|
592 |
} |
|
593 |
||
594 |
void MainWindow::setDownloadLimit(int value) |
|
595 |
{ |
|
596 |
int rate = rateFromValue(value); |
|
597 |
downloadLimitLabel->setText(tr("%1 KB/s").arg(QString().sprintf("%4d", rate))); |
|
598 |
RateController::instance()->setDownloadLimit(rate * 1024); |
|
599 |
} |
|
600 |
||
601 |
void MainWindow::about() |
|
602 |
{ |
|
603 |
QLabel *icon = new QLabel; |
|
604 |
icon->setPixmap(QPixmap(":/icons/peertopeer.png")); |
|
605 |
||
606 |
QLabel *text = new QLabel; |
|
607 |
text->setWordWrap(true); |
|
608 |
text->setText("<p>The <b>Torrent Client</b> example demonstrates how to" |
|
609 |
" write a complete peer-to-peer file sharing" |
|
610 |
" application using Qt's network and thread classes.</p>" |
|
611 |
"<p>This feature complete client implementation of" |
|
612 |
" the BitTorrent protocol can efficiently" |
|
613 |
" maintain several hundred network connections" |
|
614 |
" simultaneously.</p>"); |
|
615 |
||
616 |
QPushButton *quitButton = new QPushButton("OK"); |
|
617 |
||
618 |
QHBoxLayout *topLayout = new QHBoxLayout; |
|
619 |
topLayout->setMargin(10); |
|
620 |
topLayout->setSpacing(10); |
|
621 |
topLayout->addWidget(icon); |
|
622 |
topLayout->addWidget(text); |
|
623 |
||
624 |
QHBoxLayout *bottomLayout = new QHBoxLayout; |
|
625 |
bottomLayout->addStretch(); |
|
626 |
bottomLayout->addWidget(quitButton); |
|
627 |
bottomLayout->addStretch(); |
|
628 |
||
629 |
QVBoxLayout *mainLayout = new QVBoxLayout; |
|
630 |
mainLayout->addLayout(topLayout); |
|
631 |
mainLayout->addLayout(bottomLayout); |
|
632 |
||
633 |
QDialog about(this); |
|
634 |
about.setModal(true); |
|
635 |
about.setWindowTitle(tr("About Torrent Client")); |
|
636 |
about.setLayout(mainLayout); |
|
637 |
||
638 |
connect(quitButton, SIGNAL(clicked()), &about, SLOT(close())); |
|
639 |
||
640 |
about.exec(); |
|
641 |
} |
|
642 |
||
643 |
void MainWindow::acceptFileDrop(const QString &fileName) |
|
644 |
{ |
|
645 |
// Create and show the "Add Torrent" dialog. |
|
646 |
AddTorrentDialog *addTorrentDialog = new AddTorrentDialog; |
|
647 |
lastDirectory = QFileInfo(fileName).absolutePath(); |
|
648 |
addTorrentDialog->setTorrent(fileName); |
|
649 |
addTorrentDialog->deleteLater(); |
|
650 |
if (!addTorrentDialog->exec()) |
|
651 |
return; |
|
652 |
||
653 |
// Add the torrent to our list of downloads. |
|
654 |
addTorrent(fileName, addTorrentDialog->destinationFolder()); |
|
655 |
saveSettings(); |
|
656 |
} |
|
657 |
||
658 |
void MainWindow::closeEvent(QCloseEvent *) |
|
659 |
{ |
|
660 |
if (jobs.isEmpty()) |
|
661 |
return; |
|
662 |
||
663 |
// Save upload / download numbers. |
|
664 |
saveSettings(); |
|
665 |
saveChanges = false; |
|
666 |
||
667 |
quitDialog = new QProgressDialog(tr("Disconnecting from trackers"), tr("Abort"), 0, jobsToStop, this); |
|
668 |
||
669 |
// Stop all clients, remove the rows from the view and wait for |
|
670 |
// them to signal that they have stopped. |
|
671 |
jobsToStop = 0; |
|
672 |
jobsStopped = 0; |
|
673 |
foreach (Job job, jobs) { |
|
674 |
++jobsToStop; |
|
675 |
TorrentClient *client = job.client; |
|
676 |
client->disconnect(); |
|
677 |
connect(client, SIGNAL(stopped()), this, SLOT(torrentStopped())); |
|
678 |
client->stop(); |
|
679 |
delete torrentView->takeTopLevelItem(0); |
|
680 |
} |
|
681 |
||
682 |
if (jobsToStop > jobsStopped) |
|
683 |
quitDialog->exec(); |
|
684 |
quitDialog->deleteLater(); |
|
685 |
quitDialog = 0; |
|
686 |
} |
|
687 |
||
688 |
TorrentView::TorrentView(QWidget *parent) |
|
689 |
: QTreeWidget(parent) |
|
690 |
{ |
|
691 |
setAcceptDrops(true); |
|
692 |
} |
|
693 |
||
694 |
void TorrentView::dragMoveEvent(QDragMoveEvent *event) |
|
695 |
{ |
|
696 |
// Accept file actions with a '.torrent' extension. |
|
697 |
QUrl url(event->mimeData()->text()); |
|
698 |
if (url.isValid() && url.scheme().toLower() == "file" |
|
699 |
&& url.path().toLower().endsWith(".torrent")) |
|
700 |
event->acceptProposedAction(); |
|
701 |
} |
|
702 |
||
703 |
void TorrentView::dropEvent(QDropEvent *event) |
|
704 |
{ |
|
705 |
// Accept drops if the file has a '.torrent' extension and it |
|
706 |
// exists. |
|
707 |
QString fileName = QUrl(event->mimeData()->text()).path(); |
|
708 |
if (QFile::exists(fileName) && fileName.toLower().endsWith(".torrent")) |
|
709 |
emit fileDropped(fileName); |
|
710 |
} |
|
711 |
||
712 |
#include "mainwindow.moc" |