2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2012-2023 Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2013-08-24 19:19:45 +00:00
|
|
|
#include <QDir>
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <QFile>
|
2011-03-23 18:22:23 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2022-08-04 13:44:18 +00:00
|
|
|
#include <libtransmission/error.h>
|
2021-12-25 23:48:40 +00:00
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
2021-11-10 00:13:47 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2022-08-04 13:44:18 +00:00
|
|
|
#include <libtransmission/web-utils.h>
|
2021-11-10 00:13:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "AddData.h"
|
|
|
|
#include "Utils.h"
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-12-25 23:48:40 +00:00
|
|
|
QString getNameFromMetainfo(QByteArray const& benc)
|
2020-11-02 01:13:32 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
if (auto metainfo = tr_torrent_metainfo{}; metainfo.parse_benc({ benc.constData(), static_cast<size_t>(benc.size()) }))
|
2020-11-02 01:13:32 +00:00
|
|
|
{
|
2023-04-23 01:25:55 +00:00
|
|
|
return QString::fromStdString(metainfo.name());
|
2020-11-02 01:13:32 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
return {};
|
2020-11-02 01:13:32 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 07:19:28 +00:00
|
|
|
QString getNameFromMagnet(QString const& magnet)
|
|
|
|
{
|
|
|
|
auto tmp = tr_magnet_metainfo{};
|
|
|
|
|
|
|
|
if (!tmp.parseMagnet(magnet.toStdString()))
|
|
|
|
{
|
|
|
|
return magnet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!std::empty(tmp.name()))
|
|
|
|
{
|
|
|
|
return QString::fromStdString(tmp.name());
|
|
|
|
}
|
|
|
|
|
2023-07-04 18:04:03 +00:00
|
|
|
auto const& hashstr = tmp.info_hash_string();
|
|
|
|
return QString::fromUtf8(std::data(hashstr), std::size(hashstr));
|
2022-02-25 07:19:28 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
} // namespace
|
2020-11-02 01:13:32 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int AddData::set(QString const& key)
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
if (auto const key_std = key.toStdString(); tr_urlIsValid(key_std))
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
this->url = key;
|
2017-04-19 12:04:45 +00:00
|
|
|
type = URL;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (QFile(key).exists())
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
this->filename = QDir::fromNativeSeparators(key);
|
2017-04-19 12:04:45 +00:00
|
|
|
type = FILENAME;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2022-02-13 04:16:55 +00:00
|
|
|
auto file = QFile{ key };
|
2017-04-19 12:04:45 +00:00
|
|
|
file.open(QIODevice::ReadOnly);
|
2022-02-13 04:16:55 +00:00
|
|
|
this->metainfo = file.readAll();
|
2017-04-19 12:04:45 +00:00
|
|
|
file.close();
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2022-02-13 04:16:55 +00:00
|
|
|
else if (tr_magnet_metainfo{}.parseMagnet(key_std))
|
|
|
|
{
|
|
|
|
this->magnet = key;
|
|
|
|
this->type = MAGNET;
|
|
|
|
}
|
|
|
|
else if (auto const raw = QByteArray::fromBase64(key.toUtf8()); !raw.isEmpty())
|
2011-01-02 23:42:46 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
this->metainfo.append(raw);
|
|
|
|
this->type = METAINFO;
|
2011-01-02 23:42:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2022-02-13 04:16:55 +00:00
|
|
|
this->type = NONE;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return type;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QByteArray AddData::toBase64() const
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
return metainfo.toBase64();
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString AddData::readableName() const
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (type)
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case FILENAME:
|
2020-11-02 01:13:32 +00:00
|
|
|
return filename;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case MAGNET:
|
2022-02-25 07:19:28 +00:00
|
|
|
return getNameFromMagnet(magnet);
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case URL:
|
2020-11-02 01:13:32 +00:00
|
|
|
return url.toString();
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case METAINFO:
|
2020-11-02 01:13:32 +00:00
|
|
|
return getNameFromMetainfo(metainfo);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
default: // NONE
|
|
|
|
return {};
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QString AddData::readableShortName() const
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (type)
|
2016-04-19 20:41:59 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case FILENAME:
|
2020-08-27 01:42:41 +00:00
|
|
|
return QFileInfo(filename).baseName();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case URL:
|
|
|
|
return url.path().split(QLatin1Char('/')).last();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
return readableName();
|
2016-04-19 20:41:59 +00:00
|
|
|
}
|
|
|
|
}
|