diff --git a/qt/app.cc b/qt/app.cc index 2163fbe54..3ff2d9532 100644 --- a/qt/app.cc +++ b/qt/app.cc @@ -225,7 +225,7 @@ MyApp :: MyApp( int& argc, char ** argv ): } for( QStringList::const_iterator it=filenames.begin(), end=filenames.end(); it!=end; ++it ) - mySession->addTorrent( *it ); + addTorrent( *it ); // register as the dbus handler for Transmission new TrDBusAdaptor( this ); @@ -322,13 +322,19 @@ void MyApp :: addTorrent( const QString& key ) { if( !myPrefs->getBool( Prefs :: OPTIONS_PROMPT ) ) + { mySession->addTorrent( key ); - else if( !QFile( key ).exists( ) ) - myWindow->openURL( key ); - else { + } + else if( Utils::isMagnetLink( key ) || QFile( key ).exists( ) ) + { Options * o = new Options( *mySession, *myPrefs, key, myWindow ); o->show( ); } + else if( Utils::isURL( key ) ) + { + myWindow->openURL( key ); + } + raise( ); } diff --git a/qt/options.cc b/qt/options.cc index 9d23afcdc..b8866132d 100644 --- a/qt/options.cc +++ b/qt/options.cc @@ -38,6 +38,7 @@ #include "prefs.h" #include "session.h" #include "torrent.h" +#include "utils.h" /*** **** @@ -214,7 +215,11 @@ Options :: reload( ) clearVerify( ); tr_ctor * ctor = tr_ctorNew( 0 ); - tr_ctorSetMetainfoFromFile( ctor, myFile.toUtf8().constData() ); + if( Utils::isMagnetLink( myFile ) ) + tr_ctorSetMetainfoFromMagnetLink( ctor, myFile.toUtf8().constData() ); + else + tr_ctorSetMetainfoFromFile( ctor, myFile.toUtf8().constData() ); + const int err = tr_torrentParse( ctor, &myInfo ); myHaveInfo = !err; tr_ctorFree( ctor ); @@ -275,14 +280,18 @@ Options :: onAccepted( ) tr_bencDictAddStr( args, "download-dir", myDestination.absolutePath().toUtf8().constData() ); // "metainfo" - QFile file( myFile ); - file.open( QIODevice::ReadOnly ); - const QByteArray metainfo( file.readAll( ) ); - file.close( ); - int base64Size = 0; - char * base64 = tr_base64_encode( metainfo.constData(), metainfo.size(), &base64Size ); - tr_bencDictAddRaw( args, "metainfo", base64, base64Size ); - tr_free( base64 ); + if( Utils::isMagnetLink( myFile ) || Utils::isURL( myFile ) ) + tr_bencDictAddStr( args, "filename", myFile.toUtf8().constData() ); + else { + QFile file( myFile ); + file.open( QIODevice::ReadOnly ); + const QByteArray metainfo( file.readAll( ) ); + file.close( ); + int base64Size = 0; + char * base64 = tr_base64_encode( metainfo.constData(), metainfo.size(), &base64Size ); + tr_bencDictAddRaw( args, "metainfo", base64, base64Size ); + tr_free( base64 ); + } // paused tr_bencDictAddBool( args, "paused", !myStartCheck->isChecked( ) ); @@ -326,6 +335,7 @@ Options :: onAccepted( ) fileAdded, SLOT(executed(int64_t,const QString&, struct tr_benc*))); } +std::cerr << tr_bencToStr(&top,TR_FMT_JSON,NULL) << std::endl; mySession.exec( &top ); tr_bencFree( &top ); diff --git a/qt/session.cc b/qt/session.cc index 2b10432f1..8bf897fff 100644 --- a/qt/session.cc +++ b/qt/session.cc @@ -35,6 +35,7 @@ #include "session.h" #include "session-dialog.h" #include "torrent.h" +#include "utils.h" // #define DEBUG_HTTP @@ -865,10 +866,7 @@ namespace { bool isLink( const QString& str ) { - return str.startsWith( "magnet:?" ) - || str.startsWith( "http://" ) - || str.startsWith( "https://" ) - || str.startsWith( "ftp://" ); + return Utils::isMagnetLink(str) || Utils::isURL(str); } } diff --git a/qt/utils.h b/qt/utils.h index 0fb8da3a5..d6fada586 100644 --- a/qt/utils.h +++ b/qt/utils.h @@ -37,6 +37,16 @@ class Utils: public QObject // meh static void toStderr( const QString& qstr ); + /// + /// URLs + /// + + static bool isMagnetLink( const QString& s ) { return s.startsWith( "magnet:?" ); } + + static bool isURL( const QString& s ) { return s.startsWith( "http://" ) + || s.startsWith( "https://" ) + || s.startsWith( "ftp://" ); } + }; #endif