<method name="AddMetainfo">
<arg type="b" name="handled" direction="out"/>
<arg type="s" name="metainfo" direction="in"/>
- <arg type="s" name="filename" direction="in"/>
</method>
<method name="PresentWindow">
<arg type="b" name="handled" direction="out"/>
gboolean
tr_core_add_metainfo( TrCore * core,
const char * payload,
- const char * filename,
gboolean * setme_handled,
GError ** gerr UNUSED )
{
tr_core_add_from_url( core, payload );
*setme_handled = TRUE;
}
- else
+ else /* base64-encoded metainfo */
{
+ int file_length;
tr_ctor * ctor;
- gboolean has_metainfo = FALSE;
- const gboolean do_prompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
+ char * file_contents;
+ gboolean do_prompt = pref_flag_get( PREF_KEY_OPTIONS_PROMPT );
- /* create the constructor */
ctor = tr_ctorNew( session );
tr_core_apply_defaults( ctor );
- if( !has_metainfo && g_file_test( filename, G_FILE_TEST_IS_REGULAR ) )
- {
- /* set the metainfo from a local file */
- has_metainfo = !tr_ctorSetMetainfoFromFile( ctor, filename );
- }
-
- if( !has_metainfo )
- {
- /* base64-encoded metainfo */
- int file_length;
- char * file_contents = tr_base64_decode( payload, -1, &file_length );
- has_metainfo = !tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length );
- tr_free( file_contents );
- }
-
- if( has_metainfo )
- {
- add_ctor( core, ctor, do_prompt, TRUE );
- tr_core_torrents_added( core );
- }
+ file_contents = tr_base64_decode( payload, -1, &file_length );
+ tr_ctorSetMetainfo( ctor, (const uint8_t*)file_contents, file_length );
+ add_ctor( core, ctor, do_prompt, TRUE );
- *setme_handled = has_metainfo;
+ tr_free( file_contents );
+ tr_core_torrents_added( core );
+ *setme_handled = TRUE;
}
return TRUE;
/** @brief Add a torrent. */
gboolean tr_core_add_metainfo( TrCore * core,
- const char * payload,
- const char * optional_filename,
+ const char * base64_metainfo,
gboolean * setme_success,
GError ** err );
if( proxy )
dbus_g_proxy_call( proxy, "AddMetainfo", &err,
G_TYPE_STRING, payload,
- G_TYPE_STRING, filename,
G_TYPE_INVALID,
G_TYPE_BOOLEAN, &handled,
G_TYPE_INVALID );
--- /dev/null
+/*
+ * This file Copyright (C) 2010 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 <libtransmission/transmission.h>
+#include <libtransmission/bencode.h> // tr_base64_encode()
+#include <libtransmission/utils.h> // tr_base64_encode()
+
+#include "add-data.h"
+#include "utils.h"
+
+int
+AddData :: set( const QString& key )
+{
+ if( Utils::isMagnetLink( key ) )
+ {
+ magnet = key;
+ type = MAGNET;
+ }
+ else if ( Utils::isURL( key ) )
+ {
+ url = key;
+ type = URL;
+ }
+ else if( QFile(key).exists( ) )
+ {
+ filename = key;
+ type = FILENAME;
+
+ QFile file( key );
+ file.open( QIODevice::ReadOnly );
+ metainfo = file.readAll( );
+ file.close( );
+ }
+ else
+ {
+ 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;
+ }
+
+ return type;
+}
+
+QByteArray
+AddData :: toBase64( ) const
+{
+ QByteArray ret;
+
+ if( !metainfo.isEmpty( ) )
+ {
+ int len = 0;
+ char * b64 = tr_base64_encode( metainfo.constData(), metainfo.size(), &len );
+ ret = QByteArray( b64, len );
+ tr_free( b64 );
+ }
+
+ return ret;
+}
+
+QString
+AddData :: readableName( ) const
+{
+ QString ret;
+
+ switch( type )
+ {
+ case FILENAME: ret = filename; break;
+
+ case MAGNET: ret = magnet; break;
+
+ case URL: ret = url.toString(); break;
+
+ case METAINFO: {
+ tr_info inf;
+ tr_ctor * ctor = tr_ctorNew( NULL );
+ tr_ctorSetMetainfo( ctor, (const uint8_t*)metainfo.constData(), metainfo.size() );
+ if( tr_torrentParse( ctor, &inf ) == TR_PARSE_OK ) {
+ ret = inf.name;
+ tr_metainfoFree( &inf );
+ }
+ tr_ctorFree( ctor );
+ break;
+ }
+ }
+
+ return ret;
+}
--- /dev/null
+/*
+ * This file Copyright (C) 2010 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$
+ */
+
+#ifndef ADD_DATA_H
+#define ADD_DATA_H
+
+#include <QByteArray>
+#include <QFile>
+#include <QObject>
+#include <QString>
+#include <QUrl>
+
+class AddData
+{
+ public:
+
+ enum { NONE, MAGNET, URL, FILENAME, METAINFO };
+ int type;
+
+ QByteArray metainfo;
+ QString filename;
+ QString magnet;
+ QUrl url;
+
+ public:
+
+ int set( const QString& );
+ AddData( const QString& str ) { set(str); }
+ AddData( ): type(NONE) { }
+
+ QByteArray toBase64( ) const;
+
+ QString readableName( ) const;
+};
+
+#endif
#include <libtransmission/utils.h>
#include <libtransmission/version.h>
+#include "add-data.h"
#include "app.h"
#include "dbus-adaptor.h"
#include "formatter.h"
// register as the dbus handler for Transmission
new TrDBusAdaptor( this );
QDBusConnection bus = QDBusConnection::sessionBus();
- if (!bus.registerService("com.transmissionbt.Transmission"))
- if(bus.lastError().isValid())
- std::cerr << qPrintable(bus.lastError().message()) << std::endl;
- if( !bus.registerObject( "/com/transmissionbt/Transmission", this ))
- if(bus.lastError().isValid())
- std::cerr << qPrintable(bus.lastError().message()) << std::endl;
+ if( !bus.registerService( DBUS_SERVICE ) )
+ std::cerr << "couldn't register " << DBUS_SERVICE << std::endl;
+ if( !bus.registerObject( DBUS_OBJECT_PATH, this ) )
+ std::cerr << "couldn't register " << DBUS_OBJECT_PATH << std::endl;
}
void
void
MyApp :: addTorrent( const QString& key )
+{
+ const AddData addme( key );
+
+ if( addme.type != addme.NONE )
+ addTorrent( addme );
+}
+
+void
+MyApp :: addTorrent( const AddData& addme )
{
if( !myPrefs->getBool( Prefs :: OPTIONS_PROMPT ) )
{
- mySession->addTorrent( key );
+ mySession->addTorrent( addme );
}
- else if( Utils::isMagnetLink( key ) || QFile( key ).exists( ) )
+ else if( addme.type == addme.URL )
{
- Options * o = new Options( *mySession, *myPrefs, key, myWindow );
- o->show( );
+ myWindow->openURL( addme.url.toString( ) );
}
- else if( Utils::isURL( key ) )
+ else
{
- myWindow->openURL( key );
+ Options * o = new Options( *mySession, *myPrefs, addme, myWindow );
+ o->show( );
}
raise( );
QDBusConnection bus = QDBusConnection::sessionBus();
for( int i=0, n=addme.size(); i<n; ++i )
{
- const QString key = addme[i];
-
QDBusMessage request = QDBusMessage::createMethodCall( DBUS_SERVICE,
DBUS_OBJECT_PATH,
DBUS_INTERFACE,
"AddMetainfo" );
QList<QVariant> arguments;
- arguments.push_back( QVariant( key ) );
+ arguments.push_back( AddData(addme[i]).toBase64().constData() );
request.setArguments( arguments );
QDBusMessage response = bus.call( request );
+ //std::cerr << qPrintable(response.errorName()) << std::endl;
+ //std::cerr << qPrintable(response.errorMessage()) << std::endl;
arguments = response.arguments( );
delegated |= (arguments.size()==1) && arguments[0].toBool();
}
#include <QApplication>
#include <QSet>
#include <QTimer>
+
#include "favicon.h"
+class AddData;
class Prefs;
class Session;
class TorrentModel;
public slots:
void addTorrent( const QString& );
+ void addTorrent( const AddData& );
private:
void maybeUpdateBlocklist( );
* $Id:$
*/
-#include <QFile>
+#include "add-data.h"
#include "app.h"
#include "dbus-adaptor.h"
}
bool
-TrDBusAdaptor :: AddMetainfo( const QString& payload, const QString& filename )
+TrDBusAdaptor :: AddMetainfo( const QString& key )
{
- myApp->addTorrent( QFile(filename).exists() ? filename : payload );
+ AddData addme( key );
+
+ if( addme.type != addme.NONE )
+ myApp->addTorrent( addme );
+
return true;
}
public slots:
bool PresentWindow();
- bool AddMetainfo( const QString& payload, const QString& filename );
+ bool AddMetainfo( const QString& );
};
#endif
{
case QDialogButtonBox::Open:
std::cerr << "calling mySession.addTorrent( " << qPrintable(myTarget) << ", " << qPrintable(QFileInfo(myBuilder->top).dir().path()) << ')' << std::endl;
- mySession.addTorrent( myTarget, QFileInfo(myBuilder->top).dir().path() );
+ mySession.addNewlyCreatedTorrent( myTarget, QFileInfo(myBuilder->top).dir().path() );
break;
case QDialogButtonBox::Abort:
myBuilder->abortFlag = true;
#include <cstdio>
#include <iostream>
+#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QFileInfo>
#include <QGridLayout>
#include <QLabel>
+#include <QMessageBox>
#include <QPushButton>
#include <QResizeEvent>
#include <QSet>
#include <libtransmission/bencode.h>
#include <libtransmission/utils.h> /* mime64 */
+#include "add-data.h"
#include "file-tree.h"
#include "hig.h"
#include "options.h"
****
***/
-Options :: Options( Session& session, const Prefs& prefs, const QString& filename, QWidget * parent ):
+void
+FileAdded :: executed( int64_t tag, const QString& result, struct tr_benc * arguments )
+{
+ Q_UNUSED( arguments );
+
+ if( tag != myTag )
+ return;
+
+ if( result == "success" )
+ if( !myDelFile.isEmpty( ) )
+ QFile( myDelFile ).remove( );
+
+ if( result != "success" ) {
+ QString text = result;
+ for( int i=0, n=text.size(); i<n; ++i )
+ if( !i || text[i-1].isSpace() )
+ text[i] = text[i].toUpper();
+ QMessageBox::warning( QApplication::activeWindow(),
+ tr( "Error Adding Torrent" ),
+ QString("<p><b>%1</b></p><p>%2</p>").arg(text).arg(myName) );
+ }
+
+ deleteLater();
+}
+
+/***
+****
+***/
+
+Options :: Options( Session& session, const Prefs& prefs, const AddData& addme, QWidget * parent ):
QDialog( parent, Qt::Dialog ),
mySession( session ),
- myFile( filename ),
+ myAdd( addme ),
myHaveInfo( false ),
myDestinationButton( 0 ),
myVerifyButton( 0 ),
void
Options :: refreshFileButton( int width )
{
- refreshButton( myFileButton, QFileInfo(myFile).baseName(), width );
+ QString text;
+
+ switch( myAdd.type )
+ {
+ case AddData::FILENAME: text = QFileInfo(myAdd.filename).baseName(); break;
+ case AddData::URL: text = myAdd.url.toString(); break;
+ case AddData::MAGNET: text = myAdd.magnet; break;
+ default: break;
+ }
+
+ refreshButton( myFileButton, text, width );
}
void
clearVerify( );
tr_ctor * ctor = tr_ctorNew( 0 );
- if( Utils::isMagnetLink( myFile ) )
- tr_ctorSetMetainfoFromMagnetLink( ctor, myFile.toUtf8().constData() );
- else
- tr_ctorSetMetainfoFromFile( ctor, myFile.toUtf8().constData() );
+
+ switch( myAdd.type ) {
+ case AddData::MAGNET: tr_ctorSetMetainfoFromMagnetLink( ctor, myAdd.magnet.toUtf8().constData() ); break;
+ case AddData::FILENAME: tr_ctorSetMetainfoFromFile( ctor, myAdd.filename.toUtf8().constData() ); break;
+ case AddData::METAINFO: tr_ctorSetMetainfo( ctor, (const uint8_t*)myAdd.metainfo.constData(), myAdd.metainfo.size() ); break;
+ default: break;
+ }
const int err = tr_torrentParse( ctor, &myInfo );
myHaveInfo = !err;
tr_bencDictAddStr( args, "download-dir", myDestination.absolutePath().toUtf8().constData() );
// "metainfo"
- 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 );
+ switch( myAdd.type )
+ {
+ case AddData::MAGNET:
+ tr_bencDictAddStr( args, "filename", myAdd.magnet.toUtf8().constData() );
+ break;
+
+ case AddData::URL:
+ tr_bencDictAddStr( args, "filename", myAdd.url.toString().toUtf8().constData() );
+ break;
+
+ case AddData::FILENAME:
+ case AddData::METAINFO: {
+ const QByteArray b64 = myAdd.toBase64( );
+ tr_bencDictAddRaw( args, "metainfo", b64.constData(), b64.size() );
+ break;
+ }
+
+ default:
+ std::cerr << "unhandled AddData.type: " << myAdd.type << std::endl;
}
// paused
}
// maybe delete the source .torrent
- if( myTrashCheck->isChecked( ) ) {
- FileAdded * fileAdded = new FileAdded( tag, myFile );
- connect( &mySession, SIGNAL(executed(int64_t,const QString&, struct tr_benc*)),
- fileAdded, SLOT(executed(int64_t,const QString&, struct tr_benc*)));
- }
+ FileAdded * fileAdded = new FileAdded( tag, myAdd.readableName() );
+ if( myTrashCheck->isChecked( ) && ( myAdd.type==AddData::FILENAME ) )
+ fileAdded->setFileToDelete( myAdd.filename );
+ connect( &mySession, SIGNAL(executed(int64_t,const QString&, struct tr_benc*)),
+ fileAdded, SLOT(executed(int64_t,const QString&, struct tr_benc*)));
//std::cerr << tr_bencToStr(&top,TR_FMT_JSON,NULL) << std::endl;
mySession.exec( &top );
void
Options :: onFilenameClicked( )
{
- QFileDialog * d = new QFileDialog( this,
- tr( "Add Torrent" ),
- QFileInfo(myFile).absolutePath(),
- tr( "Torrent Files (*.torrent);;All Files (*.*)" ) );
- d->setFileMode( QFileDialog::ExistingFile );
- connect( d, SIGNAL(filesSelected(const QStringList&)), this, SLOT(onFilesSelected(const QStringList&)) );
- d->show( );
+ if( myAdd.type == AddData::FILENAME )
+ {
+ QFileDialog * d = new QFileDialog( this,
+ tr( "Add Torrent" ),
+ QFileInfo(myAdd.filename).absolutePath(),
+ tr( "Torrent Files (*.torrent);;All Files (*.*)" ) );
+ d->setFileMode( QFileDialog::ExistingFile );
+ connect( d, SIGNAL(filesSelected(const QStringList&)), this, SLOT(onFilesSelected(const QStringList&)) );
+ d->show( );
+ }
}
void
{
if( files.size() == 1 )
{
- myFile = files.at( 0 );
+ myAdd.set( files.at(0) );
refreshFileButton( );
reload( );
}
#ifndef OPTIONS_DIALOG_H
#define OPTIONS_DIALOG_H
+#include <iostream>
+
#include <QDialog>
#include <QEvent>
#include <QString>
#include <QVector>
#include <QMap>
#include <QPushButton>
+#include <QString>
#include <QStringList>
#include <QCryptographicHash>
#include <QFile>
#include <QTimer>
-#include "file-tree.h"
+#include "add-data.h" // AddData
+#include "file-tree.h" // FileList
class FileTreeView;
class Prefs;
{
Q_OBJECT
const int64_t myTag;
+ QString myName;
QString myDelFile;
public:
- FileAdded( int tag, const QString file ): myTag(tag), myDelFile(file) { }
+ FileAdded( int tag, const QString& name ): myTag(tag), myName(name) { }
~FileAdded( ) { }
+ void setFileToDelete( const QString& file ) { myDelFile = file; }
public slots:
- void executed( int64_t tag, const QString& result, struct tr_benc * arguments ) {
- Q_UNUSED( arguments );
- if( tag == myTag ) {
- if( result == "success" )
- QFile( myDelFile ).remove( );
- deleteLater();
- }
- }
+ void executed( int64_t tag, const QString& result, struct tr_benc * arguments );
};
class Options: public QDialog
Q_OBJECT
public:
- Options( Session& session, const Prefs& prefs, const QString& filename, QWidget * parent = 0 );
+ Options( Session& session, const Prefs& prefs, const AddData& addme, QWidget * parent = 0 );
~Options( );
private:
private:
Session& mySession;
- QString myFile;
+ AddData myAdd;
QDir myDestination;
bool myHaveInfo;
tr_info myInfo;
FORMS += mainwin.ui
RESOURCES += application.qrc
-SOURCES += about.cc app.cc dbus-adaptor.cc details.cc favicon.cc file-tree.cc \
- filterbar.cc filters.cc formatter.cc hig.cc license.cc mainwin.cc \
- make-dialog.cc options.cc prefs.cc prefs-dialog.cc qticonloader.cc \
- relocate.cc session.cc session-dialog.cc squeezelabel.cc \
- stats-dialog.cc torrent.cc torrent-delegate.cc \
- torrent-delegate-min.cc torrent-filter.cc torrent-model.cc \
- tracker-delegate.cc tracker-model.cc tracker-model-filter.cc \
- triconpushbutton.cc utils.cc watchdir.cc
+SOURCES += about.cc \
+ add-data.cc \
+ app.cc \
+ dbus-adaptor.cc \
+ details.cc \
+ favicon.cc \
+ file-tree.cc \
+ filterbar.cc \
+ filters.cc \
+ formatter.cc \
+ hig.cc \
+ license.cc \
+ mainwin.cc \
+ make-dialog.cc \
+ options.cc \
+ prefs.cc \
+ prefs-dialog.cc \
+ qticonloader.cc \
+ relocate.cc \
+ session.cc \
+ session-dialog.cc \
+ squeezelabel.cc \
+ stats-dialog.cc \
+ torrent.cc torrent-delegate.cc \
+ torrent-delegate-min.cc \
+ torrent-filter.cc \
+ torrent-model.cc \
+ tracker-delegate.cc \
+ tracker-model.cc \
+ tracker-model-filter.cc \
+ triconpushbutton.cc \
+ utils.cc \
+ watchdir.cc
HEADERS += $$replace(SOURCES, .cc, .h)
HEADERS += speed.h types.h
#include <libtransmission/utils.h> /* tr_free */
#include <libtransmission/version.h> /* LONG_VERSION */
+#include "add-data.h"
#include "prefs.h"
#include "qticonloader.h"
#include "session.h"
}
void
-Session :: addTorrent( QString filename )
+Session :: addTorrent( const AddData& addMe )
{
- addTorrent( filename, myPrefs.getString( Prefs::DOWNLOAD_DIR ) );
-}
+ const QByteArray b64 = addMe.toBase64();
-namespace
-{
- bool isLink( const QString& str )
- {
- return Utils::isMagnetLink(str) || Utils::isURL(str);
+ tr_benc top, *args;
+ tr_bencInitDict( &top, 2 );
+ tr_bencDictAddStr( &top, "method", "torrent-add" );
+ args = tr_bencDictAddDict( &top, "arguments", 2 );
+ tr_bencDictAddBool( args, "paused", !myPrefs.getBool( Prefs::START ) );
+ switch( addMe.type ) {
+ case AddData::MAGNET: tr_bencDictAddStr( args, "filename", addMe.magnet.toUtf8().constData() ); break;
+ case AddData::URL: tr_bencDictAddStr( args, "filename", addMe.url.toString().toUtf8().constData() ); break;
+ case AddData::FILENAME: /* fall-through */
+ case AddData::METAINFO: tr_bencDictAddRaw( args, "metainfo", b64.constData(), b64.size() ); break;
+ default: std::cerr << "Unhandled AddData type: " << addMe.type << std::endl;
}
+ exec( &top );
+ tr_bencFree( &top );
}
void
-Session :: addTorrent( QString key, QString localPath )
+Session :: addNewlyCreatedTorrent( const QString& filename, const QString& localPath )
{
+ const QByteArray b64 = AddData(filename).toBase64();
+
tr_benc top, *args;
tr_bencInitDict( &top, 2 );
tr_bencDictAddStr( &top, "method", "torrent-add" );
args = tr_bencDictAddDict( &top, "arguments", 3 );
tr_bencDictAddStr( args, "download-dir", qPrintable(localPath) );
tr_bencDictAddBool( args, "paused", !myPrefs.getBool( Prefs::START ) );
-
- // figure out what to do with "key"....
- bool keyHandled = false;
- if( !keyHandled && isLink( key )) {
- tr_bencDictAddStr( args, "filename", key.toUtf8().constData() );
- keyHandled = true; // it's a URL or magnet link...
- }
- if( !keyHandled ) {
- QFile file( key );
- file.open( QIODevice::ReadOnly );
- const QByteArray raw( file.readAll( ) );
- file.close( );
- if( !raw.isEmpty( ) ) {
- int b64len = 0;
- char * b64 = tr_base64_encode( raw.constData(), raw.size(), &b64len );
- tr_bencDictAddRaw( args, "metainfo", b64, b64len );
- tr_free( b64 );
- keyHandled = true; // it's a local file...
- }
- }
- if( !keyHandled ) {
- const QByteArray tmp = key.toUtf8();
- tr_bencDictAddRaw( args, "metainfo", tmp.constData(), tmp.length() );
- keyHandled = true; // treat it as base64
- }
-
+ tr_bencDictAddRaw( args, "metainfo", b64.constData(), b64.size() );
exec( &top );
tr_bencFree( &top );
}
class QStringList;
+class AddData;
+
#include <libtransmission/transmission.h>
extern "C"
void refreshActiveTorrents( );
void refreshAllTorrents( );
void initTorrents( const QSet<int>& ids = QSet<int>() );
- void addTorrent( QString filename );
- void addTorrent( QString filename, QString localPath );
+ void addNewlyCreatedTorrent( const QString& filename, const QString& localPath );
+ void addTorrent( const AddData& addme );
void removeTorrents( const QSet<int>& torrentIds, bool deleteFiles=false );
void verifyTorrents( const QSet<int>& torrentIds );
void reannounceTorrents( const QSet<int>& torrentIds );