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
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-03-23 18:22:23 +00:00
|
|
|
#include <QFile>
|
2013-08-24 19:19:45 +00:00
|
|
|
#include <QDir>
|
2011-03-23 18:22:23 +00:00
|
|
|
|
2010-08-01 18:55:04 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2014-12-04 19:58:34 +00:00
|
|
|
#include <libtransmission/crypto-utils.h> // tr_base64_encode()
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "AddData.h"
|
|
|
|
#include "Utils.h"
|
2010-08-01 18:55:04 +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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
magnet = QString::fromUtf8("magnet:?xt=urn:btih:") + key;
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
void* raw = tr_base64_decode(key.toUtf8().constData(), key.toUtf8().size(), &len);
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (raw != nullptr)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
metainfo.append(static_cast<char const*>(raw), int(len));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(raw);
|
|
|
|
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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QByteArray ret;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!metainfo.isEmpty())
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
size_t len;
|
|
|
|
void* b64 = tr_base64_encode(metainfo.constData(), metainfo.size(), &len);
|
2017-04-20 16:02:19 +00:00
|
|
|
ret = QByteArray(static_cast<char const*>(b64), int(len));
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_free(b64);
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
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
|
|
|
QString ret;
|
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:
|
2013-09-14 22:45:04 +00:00
|
|
|
ret = filename;
|
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case MAGNET:
|
2013-09-14 22:45:04 +00:00
|
|
|
ret = magnet;
|
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case URL:
|
2013-09-14 22:45:04 +00:00
|
|
|
ret = url.toString();
|
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case METAINFO:
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_info inf;
|
2017-04-30 09:29:58 +00:00
|
|
|
tr_ctor* ctor = tr_ctorNew(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_ctorSetMetainfo(ctor, reinterpret_cast<quint8 const*>(metainfo.constData()), metainfo.size());
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (tr_torrentParse(ctor, &inf) == TR_PARSE_OK)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = QString::fromUtf8(inf.name); // metainfo is required to be UTF-8
|
|
|
|
tr_metainfoFree(&inf);
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
break;
|
2010-08-01 18:55:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
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:
|
|
|
|
return QFileInfo(filename).fileName();
|
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
|
|
|
}
|
|
|
|
}
|