2010-08-01 18:55:04 +00:00
|
|
|
/*
|
2016-04-19 20:41:59 +00:00
|
|
|
* This file Copyright (C) 2012-2016 Mnemosyne LLC
|
2010-08-01 18:55:04 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
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
|
|
|
|
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>
|
2021-12-25 23:48:40 +00:00
|
|
|
#include <libtransmission/error.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
|
|
|
{
|
2021-12-25 23:48:40 +00:00
|
|
|
auto metainfo = tr_torrent_metainfo{};
|
2021-12-27 00:32:36 +00:00
|
|
|
if (!metainfo.parseBenc({ benc.constData(), size_t(benc.size()) }))
|
2020-11-02 01:13:32 +00:00
|
|
|
{
|
2021-12-27 00:32:36 +00:00
|
|
|
return {};
|
2020-11-02 01:13:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-27 00:32:36 +00:00
|
|
|
return QString::fromStdString(metainfo.name());
|
2020-11-02 01:13:32 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (Utils::isMagnetLink(key))
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
magnet = key;
|
|
|
|
type = MAGNET;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (Utils::isUriWithSupportedScheme(key))
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
url = key;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
filename = QDir::fromNativeSeparators(key);
|
|
|
|
type = FILENAME;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QFile file(key);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
metainfo = file.readAll();
|
|
|
|
file.close();
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else if (Utils::isHexHashcode(key))
|
2011-01-02 23:42:46 +00:00
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
magnet = QStringLiteral("magnet:?xt=urn:btih:") + key;
|
2017-04-19 12:04:45 +00:00
|
|
|
type = MAGNET;
|
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-01-08 12:46:25 +00:00
|
|
|
auto raw = QByteArray::fromBase64(key.toUtf8());
|
|
|
|
if (!raw.isEmpty())
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2022-01-08 12:46:25 +00:00
|
|
|
metainfo.append(raw);
|
2017-04-19 12:04:45 +00:00
|
|
|
type = METAINFO;
|
2013-09-14 22:45:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
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:
|
2020-11-02 01:13:32 +00:00
|
|
|
return 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
|
|
|
}
|
|
|
|
}
|