File indexing completed on 2025-02-16 04:37:35

0001 /*
0002     SPDX-FileCopyrightText: 2009 Joris Guisson <joris.guisson@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef BT_MAGNETLINK_H
0008 #define BT_MAGNETLINK_H
0009 
0010 #include <QUrl>
0011 #include <ktorrent_export.h>
0012 #include <util/sha1hash.h>
0013 
0014 namespace bt
0015 {
0016 /**
0017     MagnetLink class
0018     magnet links have the format:
0019     magnet:?xt=urn:btih:info_hash&dn=name&tr=tracker-url[,tracker-url...]
0020     note: a comma-separated list will not work with other clients likely
0021     optional parameters are
0022     to=torrent-file-url (need not be valid)
0023     pt=path-to-download-in-torrent
0024 */
0025 class KTORRENT_EXPORT MagnetLink
0026 {
0027     friend class MagnetDownloader;
0028 
0029 public:
0030     MagnetLink();
0031     MagnetLink(const MagnetLink &mlink);
0032     MagnetLink(const QUrl &mlink);
0033     MagnetLink(const QString &mlink);
0034     ~MagnetLink();
0035 
0036     /// Assignment operator
0037     MagnetLink &operator=(const MagnetLink &mlink);
0038 
0039     /// Equality operator
0040     bool operator==(const MagnetLink &mlink) const;
0041 
0042     /// Is this a valid magnet link
0043     bool isValid() const
0044     {
0045         return !magnet_string.isEmpty();
0046     }
0047 
0048     /// Convert it to a string
0049     QString toString() const
0050     {
0051         return magnet_string;
0052     }
0053 
0054     /// Get the display name (can be empty)
0055     QString displayName() const
0056     {
0057         return name;
0058     }
0059 
0060     /// Get the path of addressed file(s) inside the torrent
0061     QString subPath() const
0062     {
0063         return path;
0064     }
0065 
0066     /// Get the torrent URL (can be empty)
0067     QString torrent() const
0068     {
0069         return torrent_url;
0070     }
0071 
0072     /// Get all possible trackers (can be empty)
0073     QList<QUrl> trackers() const
0074     {
0075         return tracker_urls;
0076     }
0077 
0078     /// Get the info hash
0079     const SHA1Hash &infoHash() const
0080     {
0081         return info_hash;
0082     }
0083 
0084 private:
0085     void parse(const QUrl &mlink);
0086     Uint8 charToHex(const QChar &ch);
0087     QString base32ToHexString(const QString &s);
0088 
0089 private:
0090     QString magnet_string;
0091     SHA1Hash info_hash;
0092     QString torrent_url;
0093     QList<QUrl> tracker_urls;
0094     QString path;
0095     QString name;
0096 };
0097 
0098 }
0099 
0100 #endif // BT_MAGNETLINK_H