Revert "transmission: update from 2.13 to 2.22"
[tomato.git] / release / src / router / transmission / qt / add-data.cc
blob504ca54b908f9fb64310e0781848ccaba7328060
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 11092 2010-08-01 20:36:13Z charles $
13 #include <libtransmission/transmission.h>
14 #include <libtransmission/bencode.h> // tr_base64_encode()
15 #include <libtransmission/utils.h> // tr_base64_encode()
17 #include "add-data.h"
18 #include "utils.h"
20 int
21 AddData :: set( const QString& key )
23 if( Utils::isMagnetLink( key ) )
25 magnet = key;
26 type = MAGNET;
28 else if ( Utils::isURL( key ) )
30 url = key;
31 type = URL;
33 else if( QFile(key).exists( ) )
35 filename = key;
36 type = FILENAME;
38 QFile file( key );
39 file.open( QIODevice::ReadOnly );
40 metainfo = file.readAll( );
41 file.close( );
43 else
45 int len;
46 char * raw = tr_base64_decode( key.toUtf8().constData(), key.toUtf8().size(), &len );
47 if( raw ) {
48 metainfo.append( raw, len );
49 tr_free( raw );
50 type = METAINFO;
52 else type = NONE;
55 return type;
58 QByteArray
59 AddData :: toBase64( ) const
61 QByteArray ret;
63 if( !metainfo.isEmpty( ) )
65 int len = 0;
66 char * b64 = tr_base64_encode( metainfo.constData(), metainfo.size(), &len );
67 ret = QByteArray( b64, len );
68 tr_free( b64 );
71 return ret;
74 QString
75 AddData :: readableName( ) const
77 QString ret;
79 switch( type )
81 case FILENAME: ret = filename; break;
83 case MAGNET: ret = magnet; break;
85 case URL: ret = url.toString(); break;
87 case METAINFO: {
88 tr_info inf;
89 tr_ctor * ctor = tr_ctorNew( NULL );
90 tr_ctorSetMetainfo( ctor, (const uint8_t*)metainfo.constData(), metainfo.size() );
91 if( tr_torrentParse( ctor, &inf ) == TR_PARSE_OK ) {
92 ret = inf.name;
93 tr_metainfoFree( &inf );
95 tr_ctorFree( ctor );
96 break;
100 return ret;