1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2015  Vladimir Golovnev <glassez@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL".  If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #pragma once
30 
31 #include <libtorrent/torrent_info.hpp>
32 
33 #include <QCoreApplication>
34 #include <QtContainerFwd>
35 
36 #include "base/indexrange.h"
37 #include "abstractfilestorage.h"
38 #include "torrentcontentlayout.h"
39 
40 class QByteArray;
41 class QDateTime;
42 class QString;
43 class QUrl;
44 
45 namespace BitTorrent
46 {
47     class InfoHash;
48     struct TrackerEntry;
49 
50     class TorrentInfo final : public AbstractFileStorage
51     {
52         Q_DECLARE_TR_FUNCTIONS(TorrentInfo)
53 
54     public:
55         explicit TorrentInfo(std::shared_ptr<const lt::torrent_info> nativeInfo = {});
56         TorrentInfo(const TorrentInfo &other);
57 
58         static TorrentInfo load(const QByteArray &data, QString *error = nullptr) noexcept;
59         static TorrentInfo loadFromFile(const QString &path, QString *error = nullptr) noexcept;
60         void saveToFile(const QString &path) const;
61 
62         TorrentInfo &operator=(const TorrentInfo &other);
63 
64         bool isValid() const;
65         InfoHash infoHash() const;
66         QString name() const;
67         QDateTime creationDate() const;
68         QString creator() const;
69         QString comment() const;
70         bool isPrivate() const;
71         qlonglong totalSize() const;
72         int filesCount() const override;
73         int pieceLength() const;
74         int pieceLength(int index) const;
75         int piecesCount() const;
76         QString filePath(int index) const override;
77         QStringList filePaths() const;
78         QString fileName(int index) const override;
79         QString origFilePath(int index) const;
80         qlonglong fileSize(int index) const override;
81         qlonglong fileOffset(int index) const;
82         QVector<TrackerEntry> trackers() const;
83         QVector<QUrl> urlSeeds() const;
84         QByteArray metadata() const;
85         QStringList filesForPiece(int pieceIndex) const;
86         QVector<int> fileIndicesForPiece(int pieceIndex) const;
87         QVector<QByteArray> pieceHashes() const;
88 
89         using PieceRange = IndexRange<int>;
90         // returns pair of the first and the last pieces into which
91         // the given file extends (maybe partially).
92         PieceRange filePieces(const QString &file) const;
93         PieceRange filePieces(int fileIndex) const;
94 
95         void renameFile(int index, const QString &newPath) override;
96 
97         QString rootFolder() const;
98         bool hasRootFolder() const;
99         void setContentLayout(TorrentContentLayout layout);
100 
101         std::shared_ptr<lt::torrent_info> nativeInfo() const;
102 
103     private:
104         // returns file index or -1 if fileName is not found
105         int fileIndex(const QString &fileName) const;
106         void stripRootFolder();
107         void addRootFolder();
108         TorrentContentLayout defaultContentLayout() const;
109 
110         std::shared_ptr<lt::torrent_info> m_nativeInfo;
111     };
112 }
113 
114 Q_DECLARE_METATYPE(BitTorrent::TorrentInfo)
115