KanoopTorrentQt 0.1.0
Qt6 wrapper library for libtorrent-rasterbar
Loading...
Searching...
No Matches
torrentcreator.h
1#ifndef TORRENTCREATOR_H
2#define TORRENTCREATOR_H
3
4#include <Kanoop/torrent/kanooptorrent.h>
5#include <Kanoop/utility/loggingbaseclass.h>
6#include <QObject>
7#include <QStringList>
8
9/**
10 * @brief Creates @c .torrent metainfo files from local content.
11 *
12 * TorrentCreator wraps libtorrent's @c create_torrent API and produces
13 * bencoded @c .torrent files that can be distributed to trackers or
14 * loaded by TorrentClient::addTorrent().
15 *
16 * The creation runs synchronously in create() — for large file sets
17 * consider calling from a worker thread or using QFuture.
18 *
19 * @code
20 * TorrentCreator creator;
21 * creator.setTrackers({"udp://tracker.example.com:6969/announce"});
22 * creator.setComment("My album");
23 * creator.setPrivate(true);
24 *
25 * if (creator.create("/path/to/album", "/tmp/album.torrent")) {
26 * qDebug() << "Created" << creator.totalSize() << "bytes,"
27 * << creator.fileCount() << "files,"
28 * << creator.pieceCount() << "pieces";
29 * }
30 * @endcode
31 */
32class LIBKANOOPTORRENT_EXPORT TorrentCreator : public QObject,
33 public LoggingBaseClass
34{
35 Q_OBJECT
36public:
37 /**
38 * @brief Construct a TorrentCreator.
39 * @param parent Optional QObject parent.
40 */
41 explicit TorrentCreator(QObject* parent = nullptr);
42
43 // ── Configuration ───────────────────────────────────────────────────
44
45 /**
46 * @brief Set the piece size in bytes.
47 *
48 * Must be a power of two (e.g. 16384, 32768, 65536, ...).
49 * If not set or set to 0, libtorrent picks an optimal size based
50 * on total content size.
51 * @param bytes Piece size in bytes, or 0 for automatic.
52 */
53 void setPieceSize(int bytes);
54 /** @brief Current piece size setting. 0 means automatic. */
55 int pieceSize() const { return _pieceSize; }
56
57 /**
58 * @brief Set tracker announce URLs.
59 *
60 * Each string is a single announce URL. Multiple trackers are added
61 * as separate tiers (one tier per URL). For grouped tiers, use
62 * addTrackerTier().
63 * @param urls List of tracker announce URLs.
64 */
65 void setTrackers(const QStringList& urls);
66 /** @brief Current tracker list. */
67 QStringList trackers() const { return _trackers; }
68
69 /**
70 * @brief Add a tracker tier (group of announce URLs).
71 *
72 * All URLs in the list share the same tier, and the client will
73 * pick randomly among them.
74 * @param urls List of announce URLs for this tier.
75 */
76 void addTrackerTier(const QStringList& urls);
77
78 /**
79 * @brief Set web seed URLs (BEP 19 / GetRight-style HTTP seeding).
80 * @param urls List of HTTP/HTTPS base URLs.
81 */
82 void setWebSeeds(const QStringList& urls);
83 /** @brief Current web seed list. */
84 QStringList webSeeds() const { return _webSeeds; }
85
86 /**
87 * @brief Set the comment embedded in the torrent metainfo.
88 * @param value Free-text comment string.
89 */
90 void setComment(const QString& value);
91 /** @brief Current comment. */
92 QString comment() const { return _comment; }
93
94 /**
95 * @brief Set the creator string embedded in the torrent metainfo.
96 *
97 * Defaults to @c "KanoopTorrentQt" if not set.
98 * @param value Creator identification string.
99 */
100 void setCreator(const QString& value);
101 /** @brief Current creator string. */
102 QString creator() const { return _creator; }
103
104 /**
105 * @brief Mark the torrent as private (BEP 27).
106 *
107 * Private torrents disable DHT and PEX; peers can only be
108 * obtained from the tracker.
109 * @param enabled @c true for a private torrent.
110 */
111 void setPrivate(bool enabled);
112 /** @brief Whether the torrent will be marked private. */
113 bool isPrivate() const { return _private; }
114
115 // ── Creation ────────────────────────────────────────────────────────
116
117 /**
118 * @brief Create a @c .torrent file from local content.
119 *
120 * @p sourcePath can be a single file or a directory. When a directory
121 * is given, all files in it (recursively) are included.
122 *
123 * @param sourcePath Path to the file or directory to torrent.
124 * @param outputPath Where to write the @c .torrent file.
125 * @return @c true on success, @c false on failure (check errorString()).
126 */
127 bool create(const QString& sourcePath, const QString& outputPath);
128
129 // ── Result info (valid after successful create()) ───────────────────
130
131 /** @brief Total content size in bytes. Valid after a successful create(). */
132 qint64 totalSize() const { return _totalSize; }
133 /** @brief Number of files in the torrent. Valid after a successful create(). */
134 int fileCount() const { return _fileCount; }
135 /** @brief Number of pieces in the torrent. Valid after a successful create(). */
136 int pieceCount() const { return _pieceCount; }
137 /** @brief Last error message, or empty if no error. */
138 QString errorString() const { return _errorString; }
139
140signals:
141 /**
142 * @brief Emitted during piece hashing to report progress.
143 * @param piecesHashed Number of pieces hashed so far.
144 * @param totalPieces Total number of pieces.
145 */
146 void progressUpdated(int piecesHashed, int totalPieces);
147
148private:
149 int _pieceSize = 0;
150 QStringList _trackers;
151 QList<QStringList> _trackerTiers;
152 QStringList _webSeeds;
153 QString _comment;
154 QString _creator = "KanoopTorrentQt";
155 bool _private = false;
156
157 // Result state
158 qint64 _totalSize = 0;
159 int _fileCount = 0;
160 int _pieceCount = 0;
161 QString _errorString;
162};
163
164#endif // TORRENTCREATOR_H
Creates .torrent metainfo files from local content.
int pieceCount() const
Number of pieces in the torrent.
QString comment() const
Current comment.
int pieceSize() const
Current piece size setting.
void setPrivate(bool enabled)
Mark the torrent as private (BEP 27).
void progressUpdated(int piecesHashed, int totalPieces)
Emitted during piece hashing to report progress.
bool create(const QString &sourcePath, const QString &outputPath)
Create a .torrent file from local content.
void setComment(const QString &value)
Set the comment embedded in the torrent metainfo.
bool isPrivate() const
Whether the torrent will be marked private.
int fileCount() const
Number of files in the torrent.
void setCreator(const QString &value)
Set the creator string embedded in the torrent metainfo.
QString creator() const
Current creator string.
void setTrackers(const QStringList &urls)
Set tracker announce URLs.
TorrentCreator(QObject *parent=nullptr)
Construct a TorrentCreator.
void setWebSeeds(const QStringList &urls)
Set web seed URLs (BEP 19 / GetRight-style HTTP seeding).
void setPieceSize(int bytes)
Set the piece size in bytes.
QStringList trackers() const
Current tracker list.
QStringList webSeeds() const
Current web seed list.
void addTrackerTier(const QStringList &urls)
Add a tracker tier (group of announce URLs).
QString errorString() const
Last error message, or empty if no error.
qint64 totalSize() const
Total content size in bytes.