KanoopTorrentQt 0.1.0
Qt6 wrapper library for libtorrent-rasterbar
Loading...
Searching...
No Matches
magnetlink.h
1#ifndef MAGNETLINK_H
2#define MAGNETLINK_H
3
4#include <Kanoop/torrent/kanooptorrent.h>
5#include <QByteArray>
6#include <QString>
7#include <QStringList>
8
9/**
10 * @brief Parses and constructs BitTorrent magnet URIs.
11 *
12 * MagnetLink handles both hex (40-char) and Base32-encoded info hashes,
13 * extracts tracker URLs and display names, and can reconstruct a
14 * well-formed magnet URI string.
15 *
16 * @code
17 * MagnetLink link("magnet:?xt=urn:btih:...");
18 * if (link.isValid()) {
19 * qDebug() << link.displayName() << link.infoHashHex();
20 * }
21 * @endcode
22 */
23class LIBKANOOPTORRENT_EXPORT MagnetLink
24{
25public:
26 /** @brief Construct an invalid (empty) magnet link. */
28
29 /**
30 * @brief Parse a magnet URI string.
31 *
32 * Supports the @c xt (exact topic / info hash), @c dn (display name),
33 * @c tr (tracker), and @c xl (exact length) parameters.
34 * @param uri A @c "magnet:?..." URI string.
35 */
36 explicit MagnetLink(const QString& uri);
37
38 /** @brief Whether the URI was parsed successfully and contains a valid info hash. */
39 bool isValid() const { return _valid; }
40
41 /** @brief Raw 20-byte SHA-1 info hash. */
42 QByteArray infoHash() const { return _infoHash; }
43 /** @brief Info hash as a lowercase hex string (40 characters). */
44 QString infoHashHex() const { return QString::fromLatin1(_infoHash.toHex()); }
45
46 /** @brief Human-readable name extracted from the @c dn parameter. */
47 QString displayName() const { return _displayName; }
48 /** @brief Set or override the display name. */
49 void setDisplayName(const QString& value) { _displayName = value; }
50
51 /** @brief Tracker URLs extracted from @c tr parameters. */
52 QStringList trackers() const { return _trackers; }
53 /** @brief Append a tracker URL. */
54 void addTracker(const QString& url) { _trackers.append(url); }
55
56 /** @brief Exact content length from the @c xl parameter, or 0 if not present. */
57 qint64 exactLength() const { return _exactLength; }
58
59 /**
60 * @brief Reconstruct a magnet URI string from the current state.
61 * @return A @c "magnet:?..." URI string.
62 */
63 QString toUri() const;
64
65 /**
66 * @brief Create a MagnetLink from a hex-encoded info hash.
67 *
68 * Automatically appends a set of well-known public trackers.
69 * @param hexHash 40-character hex-encoded SHA-1 hash.
70 * @return A valid MagnetLink, or an invalid one if @p hexHash is malformed.
71 */
72 static MagnetLink fromInfoHash(const QString& hexHash);
73
74private:
75 static QByteArray decodeBase32(const QByteArray& encoded);
76
77 bool _valid = false;
78 QByteArray _infoHash;
79 QString _displayName;
80 QStringList _trackers;
81 qint64 _exactLength = 0;
82};
83
84#endif // MAGNETLINK_H