transmission/qt/add-data.cc

121 lines
2.4 KiB
C++
Raw Normal View History

/*
* This file Copyright (C) 2010-2014 Mnemosyne LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
* $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;
}