| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "addtorrentdialog.h"
|
|---|
| 43 | #include "metainfo.h"
|
|---|
| 44 |
|
|---|
| 45 | #include <QFile>
|
|---|
| 46 | #include <QFileDialog>
|
|---|
| 47 | #include <QLineEdit>
|
|---|
| 48 | #include <QMetaObject>
|
|---|
| 49 |
|
|---|
| 50 | static QString stringNumber(qint64 number)
|
|---|
| 51 | {
|
|---|
| 52 | QString tmp;
|
|---|
| 53 | if (number > (1024 * 1024 * 1024))
|
|---|
| 54 | tmp.sprintf("%.2fGB", number / (1024.0 * 1024.0 * 1024.0));
|
|---|
| 55 | else if (number > (1024 * 1024))
|
|---|
| 56 | tmp.sprintf("%.2fMB", number / (1024.0 * 1024.0));
|
|---|
| 57 | else if (number > (1024))
|
|---|
| 58 | tmp.sprintf("%.2fKB", number / (1024.0));
|
|---|
| 59 | else
|
|---|
| 60 | tmp.sprintf("%d bytes", int(number));
|
|---|
| 61 | return tmp;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | AddTorrentDialog::AddTorrentDialog(QWidget *parent)
|
|---|
| 65 | : QDialog(parent, Qt::Sheet)
|
|---|
| 66 | {
|
|---|
| 67 | ui.setupUi(this);
|
|---|
| 68 |
|
|---|
| 69 | connect(ui.browseTorrents, SIGNAL(clicked()),
|
|---|
| 70 | this, SLOT(selectTorrent()));
|
|---|
| 71 | connect(ui.browseDestination, SIGNAL(clicked()),
|
|---|
| 72 | this, SLOT(selectDestination()));
|
|---|
| 73 | connect(ui.torrentFile, SIGNAL(textChanged(const QString &)),
|
|---|
| 74 | this, SLOT(setTorrent(const QString &)));
|
|---|
| 75 |
|
|---|
| 76 | ui.destinationFolder->setText(destinationDirectory = QDir::current().path());
|
|---|
| 77 | ui.torrentFile->setFocus();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | void AddTorrentDialog::selectTorrent()
|
|---|
| 81 | {
|
|---|
| 82 | QString fileName = QFileDialog::getOpenFileName(this, tr("Choose a torrent file"),
|
|---|
| 83 | lastDirectory,
|
|---|
| 84 | tr("Torrents (*.torrent);; All files (*.*)"));
|
|---|
| 85 | if (fileName.isEmpty())
|
|---|
| 86 | return;
|
|---|
| 87 | lastDirectory = QFileInfo(fileName).absolutePath();
|
|---|
| 88 | setTorrent(fileName);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void AddTorrentDialog::selectDestination()
|
|---|
| 92 | {
|
|---|
| 93 | QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a destination directory"),
|
|---|
| 94 | lastDestinationDirectory);
|
|---|
| 95 | if (dir.isEmpty())
|
|---|
| 96 | return;
|
|---|
| 97 | lastDestinationDirectory = destinationDirectory = dir;
|
|---|
| 98 | ui.destinationFolder->setText(destinationDirectory);
|
|---|
| 99 | enableOkButton();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | void AddTorrentDialog::enableOkButton()
|
|---|
| 103 | {
|
|---|
| 104 | ui.okButton->setEnabled(!ui.destinationFolder->text().isEmpty()
|
|---|
| 105 | && !ui.torrentFile->text().isEmpty());
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | void AddTorrentDialog::setTorrent(const QString &torrentFile)
|
|---|
| 109 | {
|
|---|
| 110 | if (torrentFile.isEmpty()) {
|
|---|
| 111 | enableOkButton();
|
|---|
| 112 | return;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | ui.torrentFile->setText(torrentFile);
|
|---|
| 116 | if (!torrentFile.isEmpty())
|
|---|
| 117 | lastDirectory = QFileInfo(torrentFile).absolutePath();
|
|---|
| 118 |
|
|---|
| 119 | if (lastDestinationDirectory.isEmpty())
|
|---|
| 120 | lastDestinationDirectory = lastDirectory;
|
|---|
| 121 |
|
|---|
| 122 | MetaInfo metaInfo;
|
|---|
| 123 | QFile torrent(torrentFile);
|
|---|
| 124 | if (!torrent.open(QFile::ReadOnly) || !metaInfo.parse(torrent.readAll())) {
|
|---|
| 125 | enableOkButton();
|
|---|
| 126 | return;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | ui.torrentFile->setText(torrentFile);
|
|---|
| 130 | ui.announceUrl->setText(metaInfo.announceUrl());
|
|---|
| 131 | if (metaInfo.comment().isEmpty())
|
|---|
| 132 | ui.commentLabel->setText("<unknown>");
|
|---|
| 133 | else
|
|---|
| 134 | ui.commentLabel->setText(metaInfo.comment());
|
|---|
| 135 | if (metaInfo.createdBy().isEmpty())
|
|---|
| 136 | ui.creatorLabel->setText("<unknown>");
|
|---|
| 137 | else
|
|---|
| 138 | ui.creatorLabel->setText(metaInfo.createdBy());
|
|---|
| 139 | ui.sizeLabel->setText(stringNumber(metaInfo.totalSize()));
|
|---|
| 140 | if (metaInfo.fileForm() == MetaInfo::SingleFileForm) {
|
|---|
| 141 | ui.torrentContents->setHtml(metaInfo.singleFile().name);
|
|---|
| 142 | } else {
|
|---|
| 143 | QString html;
|
|---|
| 144 | foreach (MetaInfoMultiFile file, metaInfo.multiFiles()) {
|
|---|
| 145 | QString name = metaInfo.name();
|
|---|
| 146 | if (!name.isEmpty()) {
|
|---|
| 147 | html += name;
|
|---|
| 148 | if (!name.endsWith('/'))
|
|---|
| 149 | html += '/';
|
|---|
| 150 | }
|
|---|
| 151 | html += file.path + "<br>";
|
|---|
| 152 | }
|
|---|
| 153 | ui.torrentContents->setHtml(html);
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | QFileInfo info(torrentFile);
|
|---|
| 157 | ui.destinationFolder->setText(info.absolutePath());
|
|---|
| 158 |
|
|---|
| 159 | enableOkButton();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | QString AddTorrentDialog::torrentFileName() const
|
|---|
| 163 | {
|
|---|
| 164 | return ui.torrentFile->text();
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | QString AddTorrentDialog::destinationFolder() const
|
|---|
| 168 | {
|
|---|
| 169 | return ui.destinationFolder->text();
|
|---|
| 170 | }
|
|---|