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 Public License v2 or v3 licenses,
* or any future license endorsed by Mnemosyne LLC.
*
* $Id$
*/
#include <QFile>
#include <QDir>
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h> // tr_base64_encode()
#include "add-data.h"
#include "utils.h"
int
2013-09-14 22:45:04 +00:00
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
{
2013-09-14 22:45:04 +00:00
int len;
char * raw = tr_base64_decode (key.toUtf8().constData(), key.toUtf8().size(), &len);
if (raw)
{
metainfo.append (raw, len);
tr_free (raw);
type = METAINFO;
}
else
{
type = NONE;
}
}
2013-09-14 22:45:04 +00:00
return type;
}
QByteArray
2013-09-14 22:45:04 +00:00
AddData :: toBase64 () const
{
2013-09-14 22:45:04 +00:00
QByteArray ret;
2013-09-14 22:45:04 +00:00
if (!metainfo.isEmpty ())
{
2013-09-14 22:45:04 +00:00
int len = 0;
char * b64 = tr_base64_encode (metainfo.constData(), metainfo.size(), &len);
ret = QByteArray (b64, len);
tr_free (b64);
}
2013-09-14 22:45:04 +00:00
return ret;
}
QString
2013-09-14 22:45:04 +00:00
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, (const quint8*)metainfo.constData(), metainfo.size());
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;
}