[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: playground/ioslaves/kio_torrent
From: Christian Weilbach <christian () whiletaker ! homeip ! net>
Date: 2010-09-04 13:31:29
Message-ID: 20100904133129.F0FC3AC884 () svn ! kde ! org
[Download RAW message or body]
SVN commit 1171573 by weilbach:
further cleanups.
M +2 -0 CMakeLists.txt
M +96 -21 kio_torrent.cpp
M +10 -5 kio_torrent.h
M +5 -3 ktorrentdbusinterface.cpp
--- trunk/playground/ioslaves/kio_torrent/CMakeLists.txt #1171572:1171573
@@ -19,6 +19,8 @@
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE4_ENABLE_EXCEPTIONS}")
add_definitions( ${KDE4_DEFINITIONS} )
+configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/file/config.h )
+
include_directories( ${KDE4_INCLUDES} )
########### kio_torrent ###############
--- trunk/playground/ioslaves/kio_torrent/kio_torrent.cpp #1171572:1171573
@@ -32,6 +32,9 @@
#include <KMimeType>
#include <kdebug.h>
+const int finished_sleep = 100;
+const long max_ipc_size = 1024*32;
+
using namespace KIO;
extern "C" {
@@ -57,9 +60,10 @@
}
TorrentProtocol::TorrentProtocol( const QByteArray &pool, const QByteArray &app ) : \
SlaveBase("torrent", pool, app )
- , m_finishStat(false)
+ , m_finish(false)
+ , m_size(-1)
+ , m_position(0)
, m_numFiles(0)
- , m_size(-1)
{
kDebug();
m_ktorrentDBusInterface = new KTorrentDBusInterface(this);
@@ -75,21 +79,24 @@
{
kDebug() << url.url();
m_ktorrentDBusInterface->load( url );
- while ( true ) {
- if (m_finishStat) {
- m_finishStat = false;
+ while ( !m_finish ) {
+ usleep (finished_sleep);
+ }
+
+ UDSEntry entry;
+ entry.insert( KIO::UDSEntry::UDS_NAME, KUrl(m_path).fileName() );
+ entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG ); // a file
+ entry.insert( KIO::UDSEntry::UDS_ACCESS, S_IRUSR | S_IRGRP | S_IROTH ); // \
readable by everybody +
+ statEntry(entry);
finished();
- break;
}
- usleep (100);
- }
-}
-void TorrentProtocol::listDir(const KUrl& url)
+/*void TorrentProtocol::listDir(const KUrl& url)
{
kDebug() << url.url();
error(KIO::ERR_IS_FILE,url.url());
-/* m_ktorrentDBusInterface->load( url );
+ m_ktorrentDBusInterface->load( url );
while ( true ) {
if (m_numFiles) {
totalSize( m_numFiles);
@@ -97,62 +104,130 @@
break;
}
usleep(100);
+ }
+ KIO::SlaveBase::listDir(url);
}*/
- KIO::SlaveBase::listDir(url);
-}
void TorrentProtocol::get( const KUrl& url )
{
kDebug() << url.url() << "path: " << m_path;
m_ktorrentDBusInterface->load(url);
- while ( m_path.isEmpty() ) {
+ if ( m_finish ) {
+ totalSize( m_size );
+ } else {
+ while ( !m_finish ) {
if(m_size!=-1){
totalSize( m_size );
m_size = -1;
}
usleep (100);
}
+ }
KMimeType::Ptr mt = KMimeType::findByUrl( m_path, 0, true /* local URL */ );
- emit mimeType( mt->name() );
+ kDebug() << "mimetype: " << mt->name();
+ emit mimeType( "unknown" );
QFile file(m_path);
if (!file.open(QIODevice::ReadOnly)) {
error(KIO::ERR_CANNOT_OPEN_FOR_READING,
i18n("File exits in KTorrent, but cannot open it on disk at path \
\"%1\". Have you removed the file manually?").arg(m_path));
- finished();
+ close();
return;
}
+ int ps = 0;
while (!file.atEnd()) {
- QByteArray d = file.read(1024*32);
+ QByteArray d = file.read(max_ipc_size);
data(d);
+ ps += m_size - ps > max_ipc_size ? max_ipc_size : m_size;
+ processedSize(ps);
}
+ kDebug() << "reading ended.";
file.close();
finished();
}
-void TorrentProtocol::finishStat()
+void TorrentProtocol::finish()
{
kDebug();
- m_finishStat = true;
+ m_finish = true;
}
+void TorrentProtocol::open(const KUrl& url, QIODevice::OpenMode mode)
+{
+ kDebug() << url.url() << "path: " << m_path;
+ m_ktorrentDBusInterface->load(url);
+ while ( !m_finish ) {
+ if (m_size!=-1) {
+ totalSize( m_size );
+ m_size = -1;
+ }
+ usleep (100);
+ }
+
+ KMimeType::Ptr mt = KMimeType::findByUrl( m_path, 0, true /* local URL */ );
+ emit mimeType( mt->name() );
+
+ QFile file(m_path);
+ if (!file.open(QIODevice::ReadOnly)) {
+ error(KIO::ERR_CANNOT_OPEN_FOR_READING,
+ i18n("File exits in KTorrent, but cannot open it on disk at path \
\"%1\". Have you removed the file manually?").arg(m_path)); + close();
+ return;
+ }
+
+ m_position = 0;
+ position( 0 );
+ emit opened();
+}
+
+void TorrentProtocol::read(filesize_t size)
+{
+ kDebug() << size;
+ while ( m_path.isEmpty() ) {
+ usleep (finished_sleep);
+ }
+
+ QFile file(m_path);
+ if (!file.open(QIODevice::ReadOnly)) {
+ error(KIO::ERR_CANNOT_OPEN_FOR_READING,
+ i18n("File exits in KTorrent, but cannot open it on disk at path \
\"%1\". Have you removed the file manually?").arg(m_path)); + close();
+ return;
+ }
+ file.seek(m_position);
+ QByteArray d = file.read(size);
+ data(d);
+ file.close();
+}
+
+void TorrentProtocol::seek(filesize_t offset)
+{
+ kDebug() << offset;
+ m_position=offset;
+ position(offset);
+}
+
+
void TorrentProtocol::setPath(const QString& path)
{
kDebug() << path;
+ if(m_path!=path)
m_path = path;
}
void TorrentProtocol::setSize(qlonglong size)
{
- kDebug();
+ kDebug() << size;
+ if(m_size!=size)
m_size = size;
}
void TorrentProtocol::setNumFiles(int n)
{
- kDebug();
+ kDebug() << n;
m_numFiles = n;
}
+#include "kio_torrent.moc"
--- trunk/playground/ioslaves/kio_torrent/kio_torrent.h #1171572:1171573
@@ -25,27 +25,32 @@
#include <kio/slavebase.h>
class KTorrentDBusInterface;
+class QMutex;
-class TorrentProtocol : public KIO::SlaveBase
+class TorrentProtocol : public QObject, public KIO::SlaveBase
{
+ Q_OBJECT
public:
TorrentProtocol( const QByteArray &pool, const QByteArray &app );
virtual ~TorrentProtocol();
virtual void stat( const KUrl & url );
- virtual void listDir( const KUrl & url );
+// virtual void listDir( const KUrl & url );
virtual void get( const KUrl & url );
+ virtual void open( const KUrl &url, QIODevice::OpenMode mode );
+ virtual void read( KIO::filesize_t );
+ virtual void seek( KIO::filesize_t );
virtual void setSize( qlonglong );
virtual void setNumFiles( int );
virtual void setPath( const QString& );
- virtual void finishStat();
+ virtual void finish();
private:
KUrl m_url;
KTorrentDBusInterface* m_ktorrentDBusInterface;
- bool m_finishStat;
+ bool m_finish;
QString m_path;
- qlonglong m_size;
+ qlonglong m_size, m_position;
int m_numFiles;
};
--- trunk/playground/ioslaves/kio_torrent/ktorrentdbusinterface.cpp #1171572:1171573
@@ -46,6 +46,7 @@
, m_passedTime( 0 )
{
kDebug() << "Thread: " << thread();
+ m_mutex.lock();
m_thread = new DBusThread(this);
m_thread->start();
moveToThread(m_thread);
@@ -54,7 +55,6 @@
void KTorrentDBusInterface::init()
{
kDebug() << "Thread: " << thread();
- m_mutex.lock();
m_coreInt = new org::ktorrent::core("org.ktorrent.ktorrent", "/core",
QDBusConnection::sessionBus());
@@ -136,12 +136,14 @@
kDebug() << "Torrent " + name.value() + "("+ m_tor + ") already loaded in \
KTorrent."; if (m_file==-1&&m_torrentInt->bytesLeftToDownload().value()==0) {
m_slave->setPath(m_torrentInt->pathOnDisk());
+ m_slave->setSize(m_torrentInt->totalSize());
} else {
m_slave->setPath(m_torrentInt->filePathOnDisk(m_file));
+ m_slave->setSize(m_torrentInt->fileSize(m_file));
}
m_torrentInt->setDoNotDownload(m_file, false);
- m_slave->finishStat();
+ m_slave->finish();
} else {
QString u = url.url().replace("torrent:","magnet:");
if ( m_file != -1)
@@ -206,7 +208,7 @@
m_fileSize = size.value();
kDebug() << "fileSize: " << m_fileSize;
m_slave->setSize(m_fileSize);
- m_slave->finishStat();
+ m_slave->finish();
}
void KTorrentDBusInterface::slotTorrentRemoved(const QString& tor)
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic