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

List:       kde-devel
Subject:    Re: QDBus connection problem
From:       Christian Weilbach <christian_weilbach () web ! de>
Date:       2010-05-11 17:05:00
Message-ID: 201005111905.00798.christian_weilbach () web ! de
[Download RAW message or body]

Am Donnerstag, 29. April 2010 schrieb Thiago Macieira:
> Em Quinta-feira 29. Abril 2010, ās 16.28.35, Duns Ens escreveu:
> > I would like to get some clarification on a DBus connection problem I
> > have. I  am trying to wrap a kio slave around ktorrents dbus interface
> > to allow easy usage of torrent shares in all of KDE.
> > 
> > Now my problem is that I need to have a QObject to connect to the dbus
> > signals. I have inherited my slave from both QObject and SlaveBase but
> > although the dbus calls go out to ktorrent and internal signal slot
> > communication works the dbus signal connection from KTorrent does not
> > work.  dbus-monitor shows that the message is emitted on the specified
> > interface.
> > 
> > Maybe I am doing sth. wrong with inheriting QObject for a kio slave? Or
> > do I  need a QApplication with app.exec() in the main function to start
> > some event loop?
> 
> You need an event loop to receive D-Bus signals.
> 
> IOSlaves have no event loop.
> 
> You need to start a thread and receive the signal there. Be careful with
> cross-thread synchronisation issues. Also be sure of creating the D-Bus
> connection in the thread.

Ok I have done that. But it told me in the debug output: 

QEventLoop: Cannot be used without QApplication

So I have added a QApplication in kdemain now, but I guess I'd have to call 
app.exec() instead of slave.dispatchLoop() to get an event loop here. I am a 
bit confused, no matter what I did I neither received one of ktorrents dbus 
signals nor did i get some debug errors. If I've missed the relevant 
information on google, please point me to it, so I don't have to bug you. 
At the moment I'm only experimenting to get the signals from dbus so the code 
is only a quick hack.

Cheers,
duns

["dbusthread.h" (text/x-chdr)]

/*  This file is part of the KDE libraries
    Copyright (C) 2010 Duns Ens < dunsens AT web D0T de >

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef DBUSTHREAD_H
#define DBUSTHREAD_H

#include <QtCore/QThread>
#include <QtDBus/QDBusInterface>
#include <KUrl>

class DBusThread : public QThread
{
    Q_OBJECT
    public:
        DBusThread();
        virtual ~DBusThread();
        void run();
        void get(const KUrl&);

    private slots:
        void slotFinished(const QString&);
        void slotTorrentAdded(const QString&);
    private:
        QDBusInterface* m_coreKtInterface;
        KUrl m_url;
};

#endif // DBUSTHREAD_H

["dbusthread.cpp" (text/x-c++src)]

/*  This file is part of the KDE libraries
    Copyright (C) 2010 Duns Ens < dunsens AT web D0T de >

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "dbusthread.h"


#include <QtCore/QThread>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusConnectionInterface>
#include <QtDBus/QDBusReply>

#include <kcomponentdata.h>
#include <kstandarddirs.h>
#include <KUrl>
#include <kdebug.h>

DBusThread::DBusThread() : QThread()
{
    kDebug();
}

void DBusThread::run()
{
    kDebug();
    m_coreKtInterface = new QDBusInterface("org.ktorrent.ktorrent", "/core", \
"org.freedesktop.DBus.Introspectable");  QDBusReply<QString>ktReply = \
m_coreKtInterface->call("Introspect");  if (!ktReply.isValid()) {
        kDebug() << "KTorrent DBus interface connection failed.";
        return;
    }
    delete m_coreKtInterface;

    m_coreKtInterface = new QDBusInterface("org.ktorrent.ktorrent", "/core", \
"org.ktorrent.core");  QDBusReply<QStringList>groupList = \
m_coreKtInterface->call("groups");  if (!groupList.isValid()) {
        kError() << "Could not get the group list, do you have an incompatible \
KTorrent version running?";  } else {
        if (!groupList.value().contains("kio_torrent")) {
            m_coreKtInterface->call("addGroup","kio_torrent");
        }
        QDBusInterface groupInt("org.ktorrent.ktorrent", "/group/kio_torrent", \
"org.ktorrent.group");  KStandardDirs *dirs = new KStandardDirs();
        groupInt.call("setDefaultSaveLocation",dirs->saveLocation("data","kio_torrent"));
  }

    QDBusConnection ktConnection = QDBusConnection::sessionBus();
    if (!ktConnection.connect("org.ktorrent.ktorrent", "/core", "org.ktorrent.core", \
"finished", "const QString&", this, SLOT(slotFinished(const QString&)))) {  kDebug() \
<< "Could not connect to finished().";  }
    if (!ktConnection.connect("org.ktorrent.ktorrent", "/core", "org.ktorrent.core", \
"torrentAdded", "const QString&", this, SLOT(slotTorrentAdded(const QString&)))) {  \
kDebug() << "Could not connect to torrentAdded().";  }
    exec();
    kDebug() << " event loop finished.";
}

DBusThread::~DBusThread()
{
    kDebug();
    delete m_coreKtInterface;
}

void DBusThread::get( const KUrl& url )
{
    kDebug() << url.url();
    m_url = url;
    QString httpUrl = url.url();
    httpUrl.replace("torrent:","http:");
    QStringList splitUrl = httpUrl.split(QRegExp(".torrent(/)"));
    m_coreKtInterface->call("loadSilently", splitUrl[0]+".torrent","kio_torrent");
}

void DBusThread::slotFinished(const QString& tor)
{
    kDebug()<< tor;
}

void DBusThread::slotTorrentAdded(const QString& tor)
{
    kDebug()<< tor;
}

#include "dbusthread.moc"


["kio_torrent.cpp" (text/x-c++src)]

/*  This file is part of the KDE libraries
    Copyright (C) 2010 Duns Ens < dunsens AT web D0T de >

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "kio_torrent.h"
#include "dbusthread.h"

#include <stdlib.h>
#include <unistd.h>

#include <QApplication>

#include <kcomponentdata.h>
#include <kstandarddirs.h>
#include <KUrl>
#include <kdebug.h>

using namespace KIO;

extern "C" { int KDE_EXPORT kdemain(int argc, char **argv); }

int kdemain( int argc, char **argv )
{
    kDebug() << "Starting" << getpid();
    KComponentData componentData("kio_torrent");
    QApplication app( argc, argv );

    if (argc != 4) {
        fprintf(stderr, "Usage: kio_torrent protocol domain-socket1 \
domain-socket2\n");  exit(-1);
    }

    TorrentProtocol slave(argv[2], argv[3]);
    slave.dispatchLoop();

    kDebug() << "Done";
    return ( 0 );
}

TorrentProtocol::TorrentProtocol( const QByteArray &pool, const QByteArray &app ) : \
SlaveBase("torrent", pool, app ) {
    kDebug();
    m_dbusThread = new DBusThread();
    m_dbusThread->start();
}

TorrentProtocol::~TorrentProtocol()
{
    delete m_dbusThread;
    kDebug();
}

void TorrentProtocol::listDir( const KUrl& url )
{
    kDebug() << url.url();
    finished();
}

void TorrentProtocol::stat( const KUrl& url )
{
    kDebug() << url.url();
    finished();
}

void TorrentProtocol::get( const KUrl& url )
{
    kDebug() << url.url();
    m_dbusThread->get( url );
    while (1){ sleep(1000); }
    finished();
}


["kio_torrent.h" (text/x-chdr)]

/*  This file is part of the KDE libraries
    Copyright (C) 2010 Duns Ens < dunsens AT web D0T de >

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef KIO_TORRENT_H
#define KIO_TORRENT_H

#include <KUrl>
#include <kio/global.h>
#include <kio/slavebase.h>

class DBusThread;

class TorrentProtocol : public KIO::SlaveBase
{
    public:
        TorrentProtocol( const QByteArray &pool, const QByteArray &app );
        virtual ~TorrentProtocol();

        virtual void listDir( const KUrl & url );
        virtual void stat( const KUrl & url );
        virtual void get( const KUrl & url );
    private:
        KUrl m_url;
        DBusThread* m_dbusThread;
};

#endif // KIO_TORRENT_H


>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


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

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