WebUI: migrate to fetch API
[qBittorrent.git] / src / base / bittorrent / infohash.h
blobd870e0fd257df23cf0625c79f3d82410821fefa0
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015, 2021 Vladimir Golovnev <glassez@yandex.ru>
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.
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.
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.
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.
29 #pragma once
31 #ifdef QBT_USES_LIBTORRENT2
32 #include <libtorrent/info_hash.hpp>
33 #endif
35 #include <QMetaType>
37 #include "base/digest32.h"
39 using SHA1Hash = Digest32<160>;
40 using SHA256Hash = Digest32<256>;
42 Q_DECLARE_METATYPE(SHA1Hash)
43 Q_DECLARE_METATYPE(SHA256Hash)
45 namespace BitTorrent
47 class InfoHash;
49 class TorrentID : public Digest32<160>
51 public:
52 using BaseType = Digest32<160>;
53 using BaseType::BaseType;
55 static TorrentID fromString(const QString &hashString);
56 static TorrentID fromInfoHash(const InfoHash &infoHash);
57 static TorrentID fromSHA1Hash(const SHA1Hash &hash);
58 static TorrentID fromSHA256Hash(const SHA256Hash &hash);
61 class InfoHash
63 public:
64 #ifdef QBT_USES_LIBTORRENT2
65 using WrappedType = lt::info_hash_t;
66 #else
67 using WrappedType = lt::sha1_hash;
68 #endif
70 InfoHash() = default;
71 InfoHash(const WrappedType &nativeHash);
72 #ifdef QBT_USES_LIBTORRENT2
73 InfoHash(const SHA1Hash &v1, const SHA256Hash &v2);
74 #endif
76 bool isValid() const;
77 bool isHybrid() const;
78 SHA1Hash v1() const;
79 SHA256Hash v2() const;
80 TorrentID toTorrentID() const;
82 operator WrappedType() const;
84 private:
85 bool m_valid = false;
86 WrappedType m_nativeHash;
89 std::size_t qHash(const TorrentID &key, std::size_t seed = 0);
90 std::size_t qHash(const InfoHash &key, std::size_t seed = 0);
92 bool operator==(const InfoHash &left, const InfoHash &right);
95 Q_DECLARE_METATYPE(BitTorrent::TorrentID)
96 // We can declare it as Q_MOVABLE_TYPE to improve performance
97 // since base type uses QSharedDataPointer as the only member
98 Q_DECLARE_TYPEINFO(BitTorrent::TorrentID, Q_MOVABLE_TYPE);