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 $
15 #include <libtransmission/transmission.h>
16 #include <libtransmission/utils.h> // tr_base64_encode()
22 AddData :: set( const QString
& key
)
24 if( Utils::isMagnetLink( key
) )
29 else if ( Utils::isUriWithSupportedScheme( key
) )
34 else if( QFile(key
).exists( ) )
40 file
.open( QIODevice::ReadOnly
);
41 metainfo
= file
.readAll( );
44 else if( Utils::isHexHashcode( key
) )
46 magnet
= QString::fromUtf8("magnet:?xt=urn:btih:") + key
;
52 char * raw
= tr_base64_decode( key
.toUtf8().constData(), key
.toUtf8().size(), &len
);
54 metainfo
.append( raw
, len
);
65 AddData :: toBase64( ) const
69 if( !metainfo
.isEmpty( ) )
72 char * b64
= tr_base64_encode( metainfo
.constData(), metainfo
.size(), &len
);
73 ret
= QByteArray( b64
, len
);
81 AddData :: readableName( ) const
87 case FILENAME
: ret
= filename
; break;
89 case MAGNET
: ret
= magnet
; break;
91 case URL
: ret
= url
.toString(); break;
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
);