[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-02 18:39:02
Message-ID: 20100902183902.4CFD9AC884 () svn ! kde ! org
[Download RAW message or body]

SVN commit 1171112 by weilbach:

check and start ktorrent if it is not running. cleanup.


 M  +1 -1      dbusthread.cpp  
 M  +33 -3     kio_torrent.cpp  
 M  +3 -0      kio_torrent.h  
 M  +72 -29    ktorrentdbusinterface.cpp  
 M  +7 -3      ktorrentdbusinterface.h  


--- trunk/playground/ioslaves/kio_torrent/dbusthread.cpp #1171111:1171112
@@ -34,7 +34,7 @@
 void DBusThread::run()
 {
     kDebug();
-    m_interface->initializeDBus();
+    m_interface->init();
     kDebug() << " starting event loop.";
     exec();
     kDebug() << " event loop finished.";
--- trunk/playground/ioslaves/kio_torrent/kio_torrent.cpp #1171111:1171112
@@ -34,7 +34,9 @@
 
 using namespace KIO;
 
-extern "C" { int KDE_EXPORT kdemain(int argc, char **argv); }
+extern "C" {
+    int KDE_EXPORT kdemain(int argc, char **argv);
+}
 
 int kdemain( int argc, char **argv )
 {
@@ -56,6 +58,8 @@
 
 TorrentProtocol::TorrentProtocol( const QByteArray &pool, const QByteArray &app ) : \
SlaveBase("torrent", pool, app )  , m_finishStat(false)
+	, m_numFiles(0)
+	, m_size(-1)
 {
     kDebug();
     m_ktorrentDBusInterface = new KTorrentDBusInterface(this);
@@ -63,8 +67,8 @@
 
 TorrentProtocol::~TorrentProtocol()
 {
+    kDebug();
     delete m_ktorrentDBusInterface;
-    kDebug();
 }
 
 void TorrentProtocol::stat( const KUrl& url )
@@ -81,14 +85,33 @@
     }
 }
 
+void TorrentProtocol::listDir(const KUrl& url)
+{
+    kDebug() << url.url();
+    error(KIO::ERR_IS_FILE,url.url());
+/*    m_ktorrentDBusInterface->load( url );
+    while ( true ) {
+        if (m_numFiles) {
+	    totalSize( m_numFiles);
+            finished();
+            break;
+        }
+        usleep(100);
+    }*/
+    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_size!=-1){
+	  totalSize( m_size );
+	  m_size = -1;
+	}
         usleep (100);
     }
-    totalSize( m_size );
     
     KMimeType::Ptr mt = KMimeType::findByUrl( m_path, 0, true /* local URL */ );
     emit mimeType( mt->name() );
@@ -126,3 +149,10 @@
   kDebug();
   m_size = size;
 }
+
+void TorrentProtocol::setNumFiles(int n)
+{
+    kDebug();
+    m_numFiles = n;
+}
+
--- trunk/playground/ioslaves/kio_torrent/kio_torrent.h #1171111:1171112
@@ -33,8 +33,10 @@
         virtual ~TorrentProtocol();
 
         virtual void stat( const KUrl & url );
+    virtual void listDir( const KUrl & url );
         virtual void get( const KUrl & url );
 	virtual void setSize( qlonglong );
+    virtual void setNumFiles( int );
 	virtual void setPath( const QString& );
 	virtual void finishStat();
 	
@@ -44,6 +46,7 @@
 	bool m_finishStat;
 	QString m_path;
 	qlonglong m_size;
+    int m_numFiles;
 };
 
 #endif // KIO_TORRENT_H
--- trunk/playground/ioslaves/kio_torrent/ktorrentdbusinterface.cpp #1171111:1171112
@@ -22,11 +22,9 @@
 #include "dbusthread.h"
 #include "ktorrentgroupinterface.h"
 
-#include <stdlib.h>
-#include <unistd.h>
-
 #include <QtCore/QMutex>
 #include <QtCore/QThread>
+#include <QtCore/QTimer>
 #include <QtDBus/QDBusInterface>
 #include <QtDBus/QDBusConnection>
 #include <QtDBus/QDBusConnectionInterface>
@@ -37,43 +35,85 @@
 #include <KUrl>
 #include <kdebug.h>
 
+const int timeout = 5000;
+const int repeat = 6;
+
 KTorrentDBusInterface::KTorrentDBusInterface(TorrentProtocol* slave) :
-      m_torrentInt( 0 )
-    , m_coreInt( 0 )
+        m_coreInt( 0 )
+        , m_torrentInt( 0 )
+        , m_slave( slave )
+        , m_file( -1 )
+        , m_passedTime( 0 )
 {
     kDebug() << "Thread: " << thread();
-    m_mutex.lock();
-    m_slave = slave;
-    m_file = -1;
     m_thread = new DBusThread(this);
     m_thread->start();
     moveToThread(m_thread);
 }
 
+void KTorrentDBusInterface::init()
+{
+    kDebug() << "Thread: " << thread();
+    m_mutex.lock();
+    m_coreInt = new org::ktorrent::core("org.ktorrent.ktorrent", "/core",
+                                        QDBusConnection::sessionBus());
+
+    if (!m_coreInt->isValid()) {
+        kDebug() << "Could not initialize org.ktorrent.ktorrent /core. KTorrent \
seems to be not running."; +        m_process = new KProcess();
+        if (m_process->startDetached("ktorrent")==-2) {
+            m_slave->error(KIO::ERR_COULD_NOT_CONNECT, i18n("Cannot start process \
for KTorrent. This should not happen, even if KTorrent is not installed.")); +        \
return; +        }
+        m_process->waitForStarted();
+        QTimer::singleShot(timeout,this,SLOT(initializeDBus()));
+    } else {
+        initializeDBus();
+    }
+}
+
 void KTorrentDBusInterface::initializeDBus()
 {
-    kDebug() << "Thread: " << thread();
+    kDebug();
+
+    if (!m_coreInt->isValid()) {
+        delete m_coreInt;
+        m_coreInt = new org::ktorrent::core("org.ktorrent.ktorrent", "/core", \
QDBusConnection::sessionBus()); +        if (!m_coreInt->isValid()) {
+            if (m_passedTime<repeat*timeout) {
+                QTimer::singleShot(timeout,this,SLOT(initializeDBus()));
+                m_passedTime+=timeout;
+            } else {
+                m_slave->error(KIO::ERR_COULD_NOT_CONNECT, i18n("Could not connect \
to KTorrent via DBus after %1 seconds. Is it installed?").arg(m_passedTime)); +       \
return; +            }
+        }
+    } else {
+        setupDBus();
+    }
+}
+
+void KTorrentDBusInterface::setupDBus()
+{
+    kDebug();
     QDBusConnection bus = QDBusConnection::sessionBus();
-    m_coreInt = new org::ktorrent::core("org.ktorrent.ktorrent", "/core",
-                            bus,
-                            this);
     QDBusReply<QStringList>groupList = m_coreInt->groups();
     if (!groupList.isValid()) {
-        kError() << "Could not get the group list, do you have a compatible KTorrent \
version running?"; +        m_slave->error(KIO::ERR_COULD_NOT_CONNECT,i18n("Could not \
get the group list, do you have a compatible KTorrent version running?")); +        \
return;  } else {
-        kDebug() << groupList.value();
         if (!groupList.value().contains("kio_torrent")) {
             m_coreInt->addGroup("kio_torrent");
         }
-        org::ktorrent::group groupInt("org.ktorrent.ktorrent", "/group/kio_torrent", \
bus, this ); //"org.ktorrent.group"); +        org::ktorrent::group \
groupInt("org.ktorrent.ktorrent", "/group/kio_torrent", bus );  KStandardDirs *dirs = \
                new KStandardDirs();
         groupInt.setDefaultSaveLocation(dirs->saveLocation("data","kio_torrent"));
     }
     
-    bus.connect("org.ktorrent.ktorrent","/core","org.ktorrent.core","torrentAdded",this,SLOT(slotTorrentAdded(const \
                QString &)));
-    bus.connect("org.ktorrent.ktorrent","/core","org.ktorrent.core","finished",this,SLOT(slotFinished(const \
                QString &)));
-    bus.connect("org.ktorrent.ktorrent","/core","org.ktorrent.core","torrentRemoved",this,SLOT(slotTorrentRemoved(const \
                QString &)));
-    bus.connect("org.ktorrent.ktorrent","/core","org.ktorrent.core","torrentStoppedByError",this,SLOT(slotTorrentStoppedByError(const \
QString &, const QString &))); +    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&)));  \
m_mutex.unlock();  }
 
@@ -83,18 +123,17 @@
     m_mutex.lock();
     KUrl url = u;
     
+    // TODO parse correctly + error handling
     QString xt = url.queryItemValue("xt");
     QStringList args = xt.split(":");
     m_tor = args[2].toLower();
     m_file = args.count()>4 ? args[4].toUInt() : -1;
-    kDebug() << m_file;
     
     m_torrentInt = new org::ktorrent::torrent("org.ktorrent.ktorrent", \
                "/torrent/"+m_tor,
-                            QDBusConnection::sessionBus(),
-                            this);
+            QDBusConnection::sessionBus());
     QDBusReply<QString>name = m_torrentInt->name();
     if(name.isValid()){
-      kDebug() << "Torrent " + m_tor + " already loaded in KTorrent.";
+        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());
       } else {
@@ -107,8 +146,6 @@
       QString u = url.url().replace("torrent:","magnet:");
       if( m_file != -1)
         u.remove(":flid:"+args[4]);
-      if( m_file == -1 ) // HACK
-        u.resize(u.count()-1); // remove trailing &
       
       kDebug() << "Load torrent in KTorrent: " << u;
       m_coreInt->loadSilently(u, "kio_torrent");
@@ -128,8 +165,8 @@
       else
         path = m_torrentInt->filePathOnDisk(m_file);
       if(!path.isValid()){
-	kDebug() << "Error getting path.";
         m_slave->error(KIO::ERR_SLAVE_DEFINED, i18n("Could not fetch path from \
KTorrent.")); +            delete m_coreInt;
 	return;
       }
       QString p = path.value();
@@ -144,14 +181,16 @@
       return;
     
     QDBusReply<uint>numFiles = m_torrentInt->numFiles();
-    uint n = numFiles.value();
+    int n = numFiles.value();
     if(m_file==-1&&n!=0){
       m_slave->error(KIO::ERR_IS_DIRECTORY, i18n("Torrent \"%1\" contains a \
directory but no path to file given.").arg(m_torrentInt->name())); +        delete \
m_coreInt;  return;
     }
     
     // disable all files except the one needed
     kDebug() << "Number of files: " << n;
+    m_slave->setNumFiles(n);
     for(int i=0; i<n; i++){
       kDebug() << "Set file path: " << m_torrentInt->filePath(i) << " to " << \
(i!=m_file);  m_torrentInt->setDoNotDownload(i, (i!=m_file) );
@@ -173,16 +212,20 @@
 void KTorrentDBusInterface::slotTorrentRemoved(const QString& tor)
 {
     kDebug()<< tor;
-    if( m_tor == tor)
+    if ( m_tor == tor) {
       m_slave->error(KIO::ERR_DOES_NOT_EXIST,i18n("Torrent has been removed from \
KTorrent.")); +        delete m_coreInt;
 }
+}
 
 void KTorrentDBusInterface::slotTorrentStoppedByError(const QString& tor, const \
QString& error)  {
     kDebug()<< tor << error;
-    if( m_tor == tor)
+    if ( m_tor == tor) {
       m_slave->error(KIO::ERR_CANNOT_OPEN_FOR_WRITING,error);
+        delete m_coreInt;
 }
+}
 
 
 #include "ktorrentdbusinterface.moc"
--- trunk/playground/ioslaves/kio_torrent/ktorrentdbusinterface.h #1171111:1171112
@@ -26,6 +26,7 @@
 #include <QtCore/QMutex>
 #include <QtDBus/QDBusConnection>
 #include <KUrl>
+#include <KProcess>
 
 class TorrentProtocol;
 class DBusThread;
@@ -36,24 +37,27 @@
     Q_OBJECT
     public:
         KTorrentDBusInterface( TorrentProtocol* );
-	void load(const KUrl&);
-	void initializeDBus();
+    virtual void load(const KUrl&);
+    virtual void init();
 	
     private slots:
+    void initializeDBus();
         void slotFinished(const QString&);
         void slotTorrentAdded(const QString&);
         void slotTorrentRemoved(const QString&);
 	void slotTorrentStoppedByError( const QString&, const QString&);
     private:
+    void setupDBus();
         org::ktorrent::core* m_coreInt;
         org::ktorrent::torrent* m_torrentInt;
+    KProcess* m_process;
 	QDBusConnection* m_bus;
         KUrl m_url;
 	QMutex m_mutex;
 	QThread* m_thread;
 	TorrentProtocol* m_slave;
 	QString m_tor;
-	int m_file;
+    int m_file, m_passedTime;
 	qulonglong m_fileSize;
 };
 


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

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