1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "addtorrentdialog.h"
52#include "metainfo.h"
53
54#include <QFile>
55#include <QFileDialog>
56#include <QLineEdit>
57#include <QMetaObject>
58
59static QString stringNumber(qint64 number)
60{
61 if (number > (1024 * 1024 * 1024))
62 return QString::asprintf(format: "%.2fGB", number / (1024.0 * 1024.0 * 1024.0));
63 else if (number > (1024 * 1024))
64 return QString::asprintf(format: "%.2fMB", number / (1024.0 * 1024.0));
65 else if (number > (1024))
66 return QString::asprintf(format: "%.2fKB", number / (1024.0));
67 else
68 return QString::asprintf(format: "%d bytes", int(number));
69}
70
71AddTorrentDialog::AddTorrentDialog(QWidget *parent)
72 : QDialog(parent, Qt::Sheet)
73{
74 ui.setupUi(this);
75
76 connect(sender: ui.browseTorrents, signal: &QPushButton::clicked,
77 receiver: this, slot: &AddTorrentDialog::selectTorrent);
78 connect(sender: ui.browseDestination, signal: &QPushButton::clicked,
79 receiver: this, slot: &AddTorrentDialog::selectDestination);
80 connect(sender: ui.torrentFile, signal: &QLineEdit::textChanged,
81 receiver: this, slot: &AddTorrentDialog::setTorrent);
82
83 ui.destinationFolder->setText(destinationDirectory = QDir::current().path());
84 ui.torrentFile->setFocus();
85}
86
87void AddTorrentDialog::selectTorrent()
88{
89 QString fileName = QFileDialog::getOpenFileName(parent: this, caption: tr(s: "Choose a torrent file"),
90 dir: lastDirectory,
91 filter: tr(s: "Torrents (*.torrent);; All files (*.*)"));
92 if (fileName.isEmpty())
93 return;
94 lastDirectory = QFileInfo(fileName).absolutePath();
95 setTorrent(fileName);
96}
97
98void AddTorrentDialog::selectDestination()
99{
100 QString dir = QFileDialog::getExistingDirectory(parent: this, caption: tr(s: "Choose a destination directory"),
101 dir: lastDestinationDirectory);
102 if (dir.isEmpty())
103 return;
104 lastDestinationDirectory = destinationDirectory = dir;
105 ui.destinationFolder->setText(destinationDirectory);
106 enableOkButton();
107}
108
109void AddTorrentDialog::enableOkButton()
110{
111 ui.okButton->setEnabled(!ui.destinationFolder->text().isEmpty()
112 && !ui.torrentFile->text().isEmpty());
113}
114
115void AddTorrentDialog::setTorrent(const QString &torrentFile)
116{
117 if (torrentFile.isEmpty()) {
118 enableOkButton();
119 return;
120 }
121
122 ui.torrentFile->setText(torrentFile);
123 if (!torrentFile.isEmpty())
124 lastDirectory = QFileInfo(torrentFile).absolutePath();
125
126 if (lastDestinationDirectory.isEmpty())
127 lastDestinationDirectory = lastDirectory;
128
129 MetaInfo metaInfo;
130 QFile torrent(torrentFile);
131 if (!torrent.open(flags: QFile::ReadOnly) || !metaInfo.parse(data: torrent.readAll())) {
132 enableOkButton();
133 return;
134 }
135
136 ui.torrentFile->setText(torrentFile);
137 ui.announceUrl->setText(metaInfo.announceUrl());
138 if (metaInfo.comment().isEmpty())
139 ui.commentLabel->setText("<unknown>");
140 else
141 ui.commentLabel->setText(metaInfo.comment());
142 if (metaInfo.createdBy().isEmpty())
143 ui.creatorLabel->setText("<unknown>");
144 else
145 ui.creatorLabel->setText(metaInfo.createdBy());
146 ui.sizeLabel->setText(stringNumber(number: metaInfo.totalSize()));
147 if (metaInfo.fileForm() == MetaInfo::SingleFileForm) {
148 ui.torrentContents->setHtml(metaInfo.singleFile().name);
149 } else {
150 QString html;
151 const QList<MetaInfoMultiFile> multiFiles = metaInfo.multiFiles();
152 for (const MetaInfoMultiFile &file : multiFiles) {
153 QString name = metaInfo.name();
154 if (!name.isEmpty()) {
155 html += name;
156 if (!name.endsWith(c: '/'))
157 html += '/';
158 }
159 html += file.path + "<br>";
160 }
161 ui.torrentContents->setHtml(html);
162 }
163
164 QFileInfo info(torrentFile);
165 ui.destinationFolder->setText(info.absolutePath());
166
167 enableOkButton();
168}
169
170QString AddTorrentDialog::torrentFileName() const
171{
172 return ui.torrentFile->text();
173}
174
175QString AddTorrentDialog::destinationFolder() const
176{
177 return ui.destinationFolder->text();
178}
179

source code of qtbase/examples/network/torrent/addtorrentdialog.cpp