[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [ktorrent] kio-magnet: Automatically redirect in new browsable URL-schema which should make files ac
From:       Christian Weilbach <christian_weilbach () web ! de>
Date:       2012-03-08 23:05:17
Message-ID: 20120308230517.90D5FA60E1 () git ! kde ! org
[Download RAW message or body]

Git commit 0b3b0e648b29aa99c951e814f33ee30718e96121 by Christian Weilbach.
Committed on 09/03/2012 at 00:02.
Pushed by weilbach into branch 'master'.

Automatically redirect in new browsable URL-schema which should make files accessible \
in most applications by default. Hack debug level to 7019 "kioslave" for now to get \
debug output. Only allow one slave at a time to ensure we don't cause multithreading \
issues with KTorrent's DBus interface for now.

M  +1    -1    kio-magnet/INSTALL
M  +45   -50   kio-magnet/dbushandler.cpp
M  +4    -4    kio-magnet/dbusthread.cpp
M  +53   -33   kio-magnet/kio_magnet.cpp
M  +1    -1    kio-magnet/magnet.protocol

http://commits.kde.org/ktorrent/0b3b0e648b29aa99c951e814f33ee30718e96121

diff --git a/kio-magnet/INSTALL b/kio-magnet/INSTALL
index f95aded..8299d38 100644
--- a/kio-magnet/INSTALL
+++ b/kio-magnet/INSTALL
@@ -8,7 +8,7 @@ will rely on it as well.
 
 BUILD INSTRUCTIONS
 
-# in the sourc directory
+# in the source directory
 mkdir build
 cd build
 cmake -DCMAKE_INSTALL_PREFIX=`kde4-config --prefix` ..
diff --git a/kio-magnet/dbushandler.cpp b/kio-magnet/dbushandler.cpp
index 1aa610c..5b5627b 100644
--- a/kio-magnet/dbushandler.cpp
+++ b/kio-magnet/dbushandler.cpp
@@ -44,6 +44,8 @@
 const int timeout = 5000;
 const int repeat = 6;
 
+/** Represents the Interface to KTorrent.
+ */
 DBusHandler::DBusHandler(MagnetProtocol* slave) :
 		m_coreInt(0)
 		, m_torrentInt(0)
@@ -54,7 +56,7 @@ DBusHandler::DBusHandler(MagnetProtocol* slave) :
 		, m_passedTime(0)
 		, m_init(false)
 {
-	kDebug() << "Thread: " << thread();
+	kDebug(7019) << "Thread: " << thread();
 	m_thread = new DBusThread(this);
 	m_thread->start();
 	moveToThread(m_thread);
@@ -62,7 +64,7 @@ DBusHandler::DBusHandler(MagnetProtocol* slave) :
 
 DBusHandler::~DBusHandler()
 {
-	kDebug();
+	kDebug(7019);
 	m_thread->exit();
 
 	if (!m_tor.isEmpty())
@@ -78,25 +80,21 @@ DBusHandler::~DBusHandler()
 		m_torrentInt->removeStream(m_file);
 
 	delete m_process;
-
 	delete m_streamInt;
-
 	delete m_torrentInt;
-
 	delete m_coreInt;
-
 	delete m_thread;
 }
 
 void DBusHandler::init()
 {
-	kDebug();
+	kDebug(7019);
 
 	if (!MagnetSettings::enabled())
 	{
 		m_slave->error(KIO::ERR_ABORTED,
-		               i18n("The Web share for magnet-links is disabled. \
-        You can set it up at settings:/network-and-connectivity/sharing"));
+		               i18n("The Web share for magnet-links is disabled. " 
+	"You can set it up at settings:/network-and-connectivity/sharing"));
 	}
 	else
 	{
@@ -106,13 +104,13 @@ void DBusHandler::init()
 
 void DBusHandler::initDBus()
 {
-	kDebug() << "Thread: " << thread();
+	kDebug(7019) << "Thread: " << thread();
 	m_coreInt = new org::ktorrent::core("org.ktorrent.ktorrent", "/core",
 	                                    QDBusConnection::sessionBus());
 
 	if (!m_coreInt->isValid())
 	{
-		kDebug() << "Could not initialize org.ktorrent.ktorrent /core.\
+		kDebug(7019) << "Could not initialize org.ktorrent.ktorrent /core.\
                      KTorrent seems to be not running.";
 		m_process = new KProcess();
 
@@ -137,7 +135,7 @@ void DBusHandler::initDBus()
 
 void DBusHandler::connectToDBus()
 {
-	kDebug();
+	kDebug(7019);
 
 	// TODO check if this test is necessary with the dbus wrapper interface
 
@@ -172,7 +170,7 @@ void DBusHandler::connectToDBus()
 
 void DBusHandler::setupDBus()
 {
-	kDebug();
+	kDebug(7019);
 	QDBusConnection bus = QDBusConnection::sessionBus();
 	QDBusReply<QStringList>groupList = m_coreInt->groups();
 
@@ -211,17 +209,19 @@ void DBusHandler::setupDBus()
 	}
 
 
-	connect(m_coreInt, SIGNAL(torrentAdded(const QString&))
-
-	        , this, SLOT(slotTorrentAdded(const QString&)));
-	connect(m_coreInt, SIGNAL(finished(const QString&))
-	        , this, SLOT(slotFinished(const QString&)));
-	connect(m_coreInt, SIGNAL(torrentRemoved(const QString&))
-	        , this, SLOT(slotTorrentRemoved(const QString&)));
-	connect(m_coreInt
-	        , SIGNAL(torrentStoppedByError(const QString&, const QString&))
-	        , this
-	        , SLOT(slotTorrentStoppedByError(const QString&, const QString&)));
+	if( !connect(m_coreInt, SIGNAL(torrentAdded(const QString&))
+			, this, SLOT(slotTorrentAdded(const QString&)))
+		|| !connect(m_coreInt, SIGNAL(finished(const QString&))
+			, this, SLOT(slotFinished(const QString&)))
+		|| !connect(m_coreInt, SIGNAL(torrentRemoved(const QString&))
+			, this, SLOT(slotTorrentRemoved(const QString&)))
+		|| !connect(m_coreInt
+			, SIGNAL(torrentStoppedByError(const QString&, const QString&))
+			, this
+			, SLOT(slotTorrentStoppedByError(const QString&, const QString&)))
+	) {
+		kDebug(7019) << "Cannot connect DBus signals." << endl;
+	}
 
 	startTimer(1000);
 
@@ -233,7 +233,7 @@ void DBusHandler::timerEvent(QTimerEvent* event)
 {
 	Q_UNUSED(event);
 
-	kDebug();
+// 	kDebug(7019);
 
 	if (!m_torrentInt || m_file == -1)
 		return;
@@ -255,7 +255,7 @@ bool DBusHandler::seek(qint64 pos)
 
 qlonglong DBusHandler::fileSize(qint32 idx)
 {
-	kDebug();
+	kDebug(7019);
 	Q_ASSERT(m_torrentInt->isValid());
 
 	return m_torrentInt->fileSize(idx);
@@ -263,7 +263,7 @@ qlonglong DBusHandler::fileSize(qint32 idx)
 
 bool DBusHandler::createFileStream(int file)
 {
-	kDebug();
+	kDebug(7019);
 	Q_ASSERT(m_torrentInt->isValid());
 
 	int speed = m_url.queryItem("sp").toInt();
@@ -290,7 +290,7 @@ bool DBusHandler::createFileStream(int file)
 
 bool DBusHandler::load(const KUrl& u)
 {
-	kDebug() << u.url();
+	kDebug(7019) << u.url();
 
 	if (!m_init)
 	{
@@ -330,7 +330,8 @@ bool DBusHandler::load(const KUrl& u)
 	if (xt.isEmpty() || !xt.contains("urn:btih:"))
 	{
 		m_slave->error(KIO::ERR_ABORTED
-		               , i18n("The link for %1 does not contain the required btih \
hash-parameter.") +		               , i18n("The link for %1 does not contain the \
required btih " +				"hash-parameter.")
 		               .arg(u.url())
 		              );
 		return true;
@@ -371,7 +372,7 @@ bool DBusHandler::load(const KUrl& u)
 
 	if (name.isValid())
 	{
-		kDebug() << "Torrent " + name.value()
+		kDebug(7019) << "Torrent " + name.value()
 		+ "(" + m_tor + ") already loaded in KTorrent.";
 		selectFiles(false);
 		// HACK speed things up by forcing a restart
@@ -398,7 +399,7 @@ bool DBusHandler::load(const KUrl& u)
 
 void DBusHandler::loadFiles()
 {
-	kDebug();
+	kDebug(7019);
 	Q_ASSERT(m_torrentInt->isValid());
 
 	if (m_files.isEmpty())
@@ -416,7 +417,7 @@ void DBusHandler::loadFiles()
 
 void DBusHandler::selectFiles(bool init)
 {
-	kDebug();
+	kDebug(7019);
 	Q_ASSERT(m_torrentInt->isValid());
 	loadFiles();
 
@@ -429,10 +430,10 @@ void DBusHandler::selectFiles(bool init)
 
 	m_file = m_files.indexOf(m_path);
 
-	kDebug() << "file: " << m_path << "id: " << m_file;
+	kDebug(7019) << "file: " << m_path << "id: " << m_file;
 
 	qint32 n =  m_torrentInt->numFiles();
-	kDebug() << "Number of files: " << n;
+	kDebug(7019) << "Number of files: " << n;
 	m_slave->setNumFiles(n);
 
 	QString pf = m_url.queryItem("pf");
@@ -486,7 +487,7 @@ void DBusHandler::selectFiles(bool init)
 
 void DBusHandler::torrentLoaded()
 {
-	kDebug();
+	kDebug(7019);
 	QString filename, path;
 	qlonglong size;
 	bool downloaded = false;
@@ -521,7 +522,7 @@ void DBusHandler::torrentLoaded()
 
 void DBusHandler::cleanup()
 {
-	kDebug();
+	kDebug(7019);
 	/*    if (!m_torrentInt)
 	        return;
 
@@ -543,14 +544,14 @@ void DBusHandler::cleanup()
 	            delete torrentInt;
 	            torrentInt = 0;
 	            m_coreInt->remove(torrent,true);
-	            kDebug() << "removed " << torrent << " torrent.";
+	            kDebug(7019) << "removed " << torrent << " torrent.";
 	            continue;
 	        }
 	        totalSize+=torrentInt->bytesDownloaded(); // TODO might get wrong
 	results if network connection is buggy?
 	        if (torrentInt->bytesLeft()==0) {
 	            shareRatioList[torrent]=torrentInt->shareRatio();
-	            kDebug() << torrent << "-> " << torrentInt->shareRatio();
+	            kDebug(7019) << torrent << "-> " << torrentInt->shareRatio();
 	        }
 	        delete torrentInt;
 	    }
@@ -584,13 +585,13 @@ void DBusHandler::cleanup()
 
 void DBusHandler::slotFinished(const QString& tor)
 {
-	kDebug() << tor;
+	kDebug(7019) << tor;
 	m_slave->downloaded(true);
 }
 
 void DBusHandler::slotTorrentAdded(const QString& tor)
 {
-	kDebug() << tor;
+	kDebug(7019) << tor;
 
 	if (tor != m_tor)
 		return;
@@ -601,33 +602,27 @@ void DBusHandler::slotTorrentAdded(const QString& tor)
 	selectFiles(true);
 
 	QStringList mt = MagnetSettings::managedTorrents();
-
 	QStringList rt = MagnetSettings::runningTorrents();
-
 	mt.append(m_tor);
-
 	rt.append(m_tor);
 
 	MagnetSettings::setManagedTorrents(mt);
-
 	MagnetSettings::setRunningTorrents(rt);
-
 	MagnetSettings::self()->writeConfig();
 
 	cleanup();
-
 	m_coreInt->start(m_tor);
-
 	torrentLoaded();
 }
 
 void DBusHandler::slotTorrentRemoved(const QString& tor)
 {
-	kDebug() << tor;
+	kDebug(7019) << tor;
 
 	if (m_tor == tor)
 	{
-		m_slave->error(KIO::ERR_DOES_NOT_EXIST, i18n("Torrent has been removed from \
KTorrent.")); +		m_slave->error(KIO::ERR_DOES_NOT_EXIST, i18n("Torrent has been \
removed from" +			" KTorrent."));
 		QStringList mt = MagnetSettings::managedTorrents();
 		mt.removeAll(m_tor);
 		MagnetSettings::setManagedTorrents(mt);
@@ -636,7 +631,7 @@ void DBusHandler::slotTorrentRemoved(const QString& tor)
 
 void DBusHandler::slotTorrentStoppedByError(const QString& tor, const QString& \
error)  {
-	kDebug() << tor << error;
+	kDebug(7019) << tor << error;
 
 	if (m_tor == tor)
 	{
diff --git a/kio-magnet/dbusthread.cpp b/kio-magnet/dbusthread.cpp
index 0f4fcdf..a5f541f 100644
--- a/kio-magnet/dbusthread.cpp
+++ b/kio-magnet/dbusthread.cpp
@@ -24,16 +24,16 @@
 
 DBusThread::DBusThread(DBusHandler* i)
 {
-	kDebug();
+	kDebug(7019);
 	m_handler = i;
 }
 
 
 void DBusThread::run()
 {
-	kDebug();
+	kDebug(7019);
 	m_handler->init();
-	kDebug() << " starting event loop.";
+	kDebug(7019) << " starting event loop.";
 	exec();
-	kDebug() << " event loop finished.";
+	kDebug(7019) << " event loop finished.";
 }
diff --git a/kio-magnet/kio_magnet.cpp b/kio-magnet/kio_magnet.cpp
index f8680c6..4b7fd12 100644
--- a/kio-magnet/kio_magnet.cpp
+++ b/kio-magnet/kio_magnet.cpp
@@ -45,7 +45,7 @@ extern "C"
 
 int kdemain(int argc, char **argv)
 {
-	kDebug() << "Starting" << getpid();
+	kDebug(7019) << "Starting" << getpid();
 	KComponentData componentData("kio_magnet");
 	QCoreApplication app(argc, argv);
 
@@ -59,7 +59,7 @@ int kdemain(int argc, char **argv)
 
 	slave.dispatchLoop();
 
-	kDebug() << "Done";
+	kDebug(7019) << "Done";
 	return (0);
 }
 
@@ -70,22 +70,39 @@ MagnetProtocol::MagnetProtocol(const QByteArray &pool, const \
QByteArray &app)  , m_position(0)
 		, m_numFiles(0)
 {
-	kDebug();
+	kDebug(7019);
 	m_dbusHandler = new DBusHandler(this);
 }
 
 MagnetProtocol::~MagnetProtocol()
 {
-	kDebug();
+	kDebug(7019);
 	delete m_dbusHandler;
 }
 
 void MagnetProtocol::load(const KUrl& url)
 {
+	kDebug(7019);
 	m_loadMutex.lock();
-	kDebug();
+	// redirect from URI schema to URL
+	if( !url.hasHost() ) {
+	    KUrl redUrl(url);
+	    // rewrite to host part
+	    QString hash = redUrl.queryItem("xt");
+	    hash.remove("urn:btih:");
+	    redUrl.setHost(hash + ".btih.dht");
+	    redUrl.removeQueryItem("xt");
+
+	    // set path
+	    QString path = redUrl.queryItem("pt");
+	    redUrl.setPath(path);
+	    kDebug(7019) << "redirect to " + redUrl.url();
+	    redirection(redUrl);
+	    m_url=redUrl;
+	} else {
+	    m_url=url;
+	}
 
-	m_url = url;
 	m_downloaded = false;
 	m_size = -1;
 	m_position = 0;
@@ -111,7 +128,7 @@ void MagnetProtocol::load(const KUrl& url)
  */
 bool MagnetProtocol::isDir(const KUrl& url)
 {
-	kDebug();
+	kDebug(7019);
 	QString pt = url.queryItem("pt");
 	// TODO is the tailing "/" ok for a standard?
 
@@ -132,7 +149,7 @@ bool MagnetProtocol::isDir(const KUrl& url)
 
 void MagnetProtocol::stat(const KUrl& url)
 {
-	kDebug() << url.url();
+	kDebug(7019) << url.url();
 	load(url);
 
 	UDSEntry entry;
@@ -158,7 +175,7 @@ void MagnetProtocol::stat(const KUrl& url)
 
 void MagnetProtocol::listDir(const KUrl& u)
 {
-	kDebug() << u.url();
+	kDebug(7019) << u.url();
 	load(u);
 
 	totalSize(m_numFiles);
@@ -228,7 +245,7 @@ void MagnetProtocol::listDir(const KUrl& u)
 		}
 
 		if (!url.hasQueryItem("sp") &&
-		        file.contains(QRegExp("\\.(avi|mp2|mp3|mp4|m4v|webm|ogv|ogg|mpeg|mpg|wmv)$")))
 +		    file.contains(QRegExp("\\.(avi|mp2|mp3|mp4|m4v|webm|ogv|ogg|mpeg|mpg|wmv)$")))
  {
 			//HACK enable streaming for seemless playback on known containers for now
 			url.addQueryItem("sp", "200");
@@ -237,7 +254,8 @@ void MagnetProtocol::listDir(const KUrl& u)
 		entry.insert(KIO::UDSEntry::UDS_NAME, file);
 
 		// TODO KUrl doesn't like to have URNs without a "/" after the protocol as it \
                seems
-		entry.insert(KIO::UDSEntry::UDS_TARGET_URL, url.url().replace("magnet:/?", \
"magnet:?")); +		entry.insert(KIO::UDSEntry::UDS_TARGET_URL
+			, url.url().replace("magnet:/?", "magnet:?"));
 		// readable by everybody
 		entry.insert(KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IRGRP | S_IROTH);
 		eList << entry;
@@ -250,7 +268,7 @@ void MagnetProtocol::listDir(const KUrl& u)
 
 void MagnetProtocol::mimetype(const KUrl& url)
 {
-	kDebug();
+	kDebug(7019);
 	load(url);
 
 	KMimeType::Ptr mt = KMimeType::findByUrl(m_filename, 0, false /* local URL */);
@@ -260,7 +278,7 @@ void MagnetProtocol::mimetype(const KUrl& url)
 
 void MagnetProtocol::get(const KUrl& url)
 {
-	kDebug() << url.url() << "path: " << m_path;
+	kDebug(7019) << url.url() << "path: " << m_path;
 	load(url);
 
 	totalSize(m_size);
@@ -273,8 +291,8 @@ void MagnetProtocol::get(const KUrl& url)
 		if (!file.open(QIODevice::ReadOnly))
 		{
 			error(KIO::ERR_ABORTED,
-			      i18n("File exists in KTorrent, but cannot open it on disk at path \"%1\"."
-			           " Have you removed the file manually?").arg(m_path));
+			      i18n("File exists in KTorrent, but cannot open it on disk at path"
+				   "\"%1\". Have you removed thefile manually?").arg(m_path));
 			return;
 		}
 	}
@@ -297,8 +315,9 @@ void MagnetProtocol::get(const KUrl& url)
 		{
 			if (emitMimetype)
 			{
-				KMimeType::Ptr mt = KMimeType::findByUrl(m_path, 0, true /* local URL */);
-				kDebug() << "mimetype: " << mt->name();
+				KMimeType::Ptr mt = KMimeType::findByUrl(m_path, 0
+					, true /* local URL */);
+				kDebug(7019) << "mimetype: " << mt->name();
 				emit mimeType(mt->name());
 				emitMimetype = false;
 			}
@@ -316,7 +335,7 @@ void MagnetProtocol::get(const KUrl& url)
 
 			data(d);
 
-			kDebug() << "processed size: " << ps;
+			kDebug(7019) << "processed size: " << ps;
 
 			processedSize(ps);
 		}
@@ -326,7 +345,7 @@ void MagnetProtocol::get(const KUrl& url)
 		}
 	}
 
-	kDebug() << "reading ended.";
+	kDebug(7019) << "reading ended.";
 
 	file.close();
 	finished();
@@ -334,7 +353,7 @@ void MagnetProtocol::get(const KUrl& url)
 
 void MagnetProtocol::added(const QString& fn)
 {
-	kDebug();
+	kDebug(7019);
 
 	m_filename = fn;
 	m_loadWaiter.wakeOne();
@@ -342,17 +361,18 @@ void MagnetProtocol::added(const QString& fn)
 
 void MagnetProtocol::downloaded(bool dl)
 {
-	kDebug();
+// 	kDebug(7019);
 	m_downloaded = dl;
 }
 
 void MagnetProtocol::open(const KUrl& url, QIODevice::OpenMode mode)
 {
-	kDebug() << url.url() << "path: " << m_path;
+	kDebug(7019) << url.url() << "path: " << m_path;
 
 	if (mode != QIODevice::ReadOnly)
 	{
-		error(KIO::ERR_CANNOT_OPEN_FOR_WRITING, i18n("You cannot write to magnet \
resources.")); +		error(KIO::ERR_CANNOT_OPEN_FOR_WRITING
+		      , i18n("You cannot write to magnet resources."));
 		return;
 	}
 
@@ -372,8 +392,8 @@ void MagnetProtocol::open(const KUrl& url, QIODevice::OpenMode \
mode)  if (!file.open(QIODevice::ReadOnly))
 		{
 			error(KIO::ERR_ABORTED,
-			      i18n("File exists in KTorrent, but cannot open it on disk at path \"%1\"."
-			           " Have you removed the file manually?").arg(m_path));
+			      i18n("File exists in KTorrent, but cannot open it on disk at path"
+				   "\"%1\". Have you removed the file manually?").arg(m_path));
 			return;
 		}
 	}
@@ -393,7 +413,7 @@ void MagnetProtocol::open(const KUrl& url, QIODevice::OpenMode \
mode)  
 void MagnetProtocol::read(filesize_t size)
 {
-	kDebug() << size;
+	kDebug(7019) << size;
 
 	QFile file(m_path);
 
@@ -406,8 +426,8 @@ void MagnetProtocol::read(filesize_t size)
 		else
 		{
 			error(KIO::ERR_CANNOT_OPEN_FOR_READING,
-			      i18n("File exists in KTorrent, but cannot open it on disk at path \"%1\"."
-			           " Have you removed the file manually?").arg(m_path));
+			      i18n("File exists in KTorrent, but cannot open it on disk at path "
+				   "\"%1\". Have you removed the file manually?").arg(m_path));
 		}
 
 		return;
@@ -436,7 +456,7 @@ void MagnetProtocol::read(filesize_t size)
 
 void MagnetProtocol::seek(filesize_t offset)
 {
-	kDebug() << offset;
+	kDebug(7019) << offset;
 
 	if (m_dbusHandler->seek(offset))
 	{
@@ -451,26 +471,26 @@ void MagnetProtocol::seek(filesize_t offset)
 
 void MagnetProtocol::setPath(const QString& path)
 {
-	kDebug() << path;
+	kDebug(7019) << path;
 	m_path = path;
 }
 
 void MagnetProtocol::setFiles(const QStringList& files)
 {
-	kDebug() << files;
+	kDebug(7019) << files;
 	m_files = files;
 }
 
 
 void MagnetProtocol::setSize(qint64 size)
 {
-	kDebug() << size;
+	kDebug(7019) << size;
 	m_size = size;
 }
 
 void MagnetProtocol::setNumFiles(int n)
 {
-	kDebug() << n;
+	kDebug(7019) << n;
 	m_numFiles = n;
 }
 
diff --git a/kio-magnet/magnet.protocol b/kio-magnet/magnet.protocol
index cd0f5e9..53dc2b5 100644
--- a/kio-magnet/magnet.protocol
+++ b/kio-magnet/magnet.protocol
@@ -13,6 +13,6 @@ deleting=false
 moving=false
 Icon=folder-remote
 #maxInstancesPerHost=2
-maxInstances=10
+maxInstances=1
 #X-DocPath=kioslave/magnet/index.html
 Class=:internet


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic