Transmission: update to 2.82
[tomato.git] / release / src / router / transmission / qt / add-data.cc
blob960c8d7e20a3389a4525e4636393d9e94164f938
1 /*
2 * This file Copyright (C) Mnemosyne LLC
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2
6 * as published by the Free Software Foundation.
8 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10 * $Id: add-data.cc 14150 2013-07-27 21:58:14Z jordan $
13 #include <QFile>
15 #include <libtransmission/transmission.h>
16 #include <libtransmission/utils.h> // tr_base64_encode()
18 #include "add-data.h"
19 #include "utils.h"
21 int
22 AddData :: set( const QString& key )
24 if( Utils::isMagnetLink( key ) )
26 magnet = key;
27 type = MAGNET;
29 else if ( Utils::isUriWithSupportedScheme( key ) )
31 url = key;
32 type = URL;
34 else if( QFile(key).exists( ) )
36 filename = key;
37 type = FILENAME;
39 QFile file( key );
40 file.open( QIODevice::ReadOnly );
41 metainfo = file.readAll( );
42 file.close( );
44 else if( Utils::isHexHashcode( key ) )
46 magnet = QString::fromUtf8("magnet:?xt=urn:btih:") + key;
47 type = MAGNET;
49 else
51 int len;
52 char * raw = tr_base64_decode( key.toUtf8().constData(), key.toUtf8().size(), &len );
53 if( raw ) {
54 metainfo.append( raw, len );
55 tr_free( raw );
56 type = METAINFO;
58 else type = NONE;
61 return type;
64 QByteArray
65 AddData :: toBase64( ) const
67 QByteArray ret;
69 if( !metainfo.isEmpty( ) )
71 int len = 0;
72 char * b64 = tr_base64_encode( metainfo.constData(), metainfo.size(), &len );
73 ret = QByteArray( b64, len );
74 tr_free( b64 );
77 return ret;
80 QString
81 AddData :: readableName( ) const
83 QString ret;
85 switch( type )
87 case FILENAME: ret = filename; break;
89 case MAGNET: ret = magnet; break;
91 case URL: ret = url.toString(); break;
93 case METAINFO: {
94 tr_info inf;
95 tr_ctor * ctor = tr_ctorNew( NULL );
96 tr_ctorSetMetainfo( ctor, (const uint8_t*)metainfo.constData(), metainfo.size() );
97 if( tr_torrentParse( ctor, &inf ) == TR_PARSE_OK ) {
98 ret = QString::fromUtf8( inf.name ); // metainfo is required to be UTF-8
99 tr_metainfoFree( &inf );
101 tr_ctorFree( ctor );
102 break;
106 return ret;