transmission/qt/add-data.cc

118 lines
2.3 KiB
C++
Raw Normal View History

/*
* This file Copyright (C) 2012-2014 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
* $Id$
*/
#include <QFile>
#include <QDir>
#include <libtransmission/transmission.h>
#include <libtransmission/crypto-utils.h> // tr_base64_encode()
#include "add-data.h"
#include "utils.h"
int
AddData::set (const QString& key)
{
2013-09-14 22:45:04 +00:00
if (Utils::isMagnetLink (key))
{
2013-09-14 22:45:04 +00:00
magnet = key;
type = MAGNET;
}
2013-09-14 22:45:04 +00:00
else if (Utils::isUriWithSupportedScheme (key))
{
2013-09-14 22:45:04 +00:00
url = key;
type = URL;
}
2013-09-14 22:45:04 +00:00
else if (QFile(key).exists ())
{
2013-09-14 22:45:04 +00:00
filename = QDir::fromNativeSeparators (key);
type = FILENAME;
2013-09-14 22:45:04 +00:00
QFile file (key);
file.open (QIODevice::ReadOnly);
metainfo = file.readAll ();
file.close ();
}
2013-09-14 22:45:04 +00:00
else if (Utils::isHexHashcode (key))
{
2013-09-14 22:45:04 +00:00
magnet = QString::fromUtf8("magnet:?xt=urn:btih:") + key;
type = MAGNET;
}
2013-09-14 22:45:04 +00:00
else
{
size_t len;
void * raw = tr_base64_decode (key.toUtf8().constData(), key.toUtf8().size(), &len);
2013-09-14 22:45:04 +00:00
if (raw)
{
metainfo.append (static_cast<const char*> (raw), (int) len);
2013-09-14 22:45:04 +00:00
tr_free (raw);
type = METAINFO;
}
else
{
type = NONE;
}
}
2013-09-14 22:45:04 +00:00
return type;
}
QByteArray
AddData::toBase64 () const
{
2013-09-14 22:45:04 +00:00
QByteArray ret;
2013-09-14 22:45:04 +00:00
if (!metainfo.isEmpty ())
{
size_t len;
void * b64 = tr_base64_encode (metainfo.constData(), metainfo.size(), &len);
ret = QByteArray (static_cast<const char*> (b64), (int) len);
2013-09-14 22:45:04 +00:00
tr_free (b64);
}
2013-09-14 22:45:04 +00:00
return ret;
}
QString
AddData::readableName () const
{
2013-09-14 22:45:04 +00:00
QString ret;
2013-09-14 22:45:04 +00:00
switch (type)
{
2013-09-14 22:45:04 +00:00
case FILENAME:
ret = filename;
break;
2013-09-14 22:45:04 +00:00
case MAGNET:
ret = magnet;
break;
2013-09-14 22:45:04 +00:00
case URL:
ret = url.toString();
break;
2013-09-14 22:45:04 +00:00
case METAINFO:
{
tr_info inf;
tr_ctor * ctor = tr_ctorNew (NULL);
tr_ctorSetMetainfo (ctor, reinterpret_cast<const quint8*> (metainfo.constData()), metainfo.size());
2013-09-14 22:45:04 +00:00
if (tr_torrentParse (ctor, &inf) == TR_PARSE_OK )
{
ret = QString::fromUtf8 (inf.name); // metainfo is required to be UTF-8
tr_metainfoFree (&inf);
}
2013-09-14 22:45:04 +00:00
tr_ctorFree (ctor);
break;
}
}
2013-09-14 22:45:04 +00:00
return ret;
}