Thanks Thiago.
We would like to polish the existing implementation attached as we had faced 
some
problems earlier.

Please let me know if the calls made are correct or not.

Best Regards,
Ramakanth
________________________________________
From: 
interest-bounces+ramakanthreddy_kesireddy=mahindrasatyam....@qt-project.org 
[interest-bounces+ramakanthreddy_kesireddy=mahindrasatyam....@qt-project.org] 
on behalf of Thiago Macieira [thiago.macie...@intel.com]
Sent: Thursday, June 05, 2014 11:42 AM
To: interest@qt-project.org
Subject: Re: [Interest] QtDBus Example to read files from USB

Em qui 05 jun 2014, às 06:02:58, Ramakanthreddy Kesireddy escreveu:
> Hi,
>
> Is there any Qt DBus example to read the files from USB?

D-Bus is used to send and receive messages. It has nothing to do with USB.

You may have a service in your system that can read files from USB. I don't
know of any program that can do that for you. But if you do, please study its
documentation and figure out what calls you need to place. If you don't know
how to make the calls, we can help you. You just need to provide the
interfaces, names of the methods and what parameters are necessary.
--
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

________________________________

DISCLAIMER:
This email (including any attachments) is intended for the sole use of the 
intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE 
COMPANY INFORMATION. Any review or reliance by others or copying or 
distribution or forwarding of any or all of the contents in this message is 
STRICTLY PROHIBITED. If you are not the intended recipient, please contact the 
sender by email and delete all copies; your cooperation in this regard is 
appreciated.


============================================================================================================================
Disclaimer:  This message and the information contained herein is proprietary 
and confidential and subject to the Tech Mahindra policy statement, you may 
review the policy at http://www.techmahindra.com/Disclaimer.html externally 
http://tim.techmahindra.com/tim/disclaimer.html internally within TechMahindra.
============================================================================================================================

#ifndef USB_H
#define USB_H

#include <QObject>
#include <QDBusObjectPath>
#include <QDBusConnection>


class USB : public QObject
{
    Q_OBJECT

public:
    static USB *instance();
    ~USB();

    bool getUsbDetails();
    QList<QString> mountedPaths;

private slots:
    void deviceAdded(QDBusObjectPath dev);
    void deviceRemoved(QDBusObjectPath dev);
    void extractMountedPaths();

signals:
    void deviceConnected();

private:

    static USB* m_instance;

    USB(QObject *parent = 0);

    void emumuratedDevices();
    bool isDeviceMounted(QString path);
    QString usbDeviceMountedPath(QString path);

    QDBusConnection cnx;

    QList<QString> fileSystemPaths;

    QThread *mThread;
};

#endif // USB_H
#include "usb.h"
#include <QDBusMessage>
#include <QTimer>
#include <QDBusPendingReply>
#include <QDBusInterface>
#include <QDirIterator>
#include <QDebug>

USB* USB::m_instance = NULL;

USB* USB::instance()
{
    if(!m_instance) {
        m_instance = new USB();
    }

    return m_instance;
}

bool USB::getUsbDetails()
{
    cnx.connect("org.freedesktop.UDisks","/org/freedesktop/UDisks","org.freedesktop.UDisks","DeviceAdded",this, SLOT(deviceAdded(QDBusObjectPath)));
    cnx.connect("org.freedesktop.UDisks","/org/freedesktop/UDisks","org.freedesktop.UDisks","DeviceRemoved",this, SLOT(deviceRemoved(QDBusObjectPath)));

    qDebug() <<"Cnx Connected??";
    if( cnx.isConnected() )
    {
        qDebug() <<"Cnx Name:"<< cnx.name();
        extractMountedPaths();
        emit deviceConnected();

        return true;
    }
    else {
        return false;
    }
}

USB::USB(QObject *parent):
    QObject(parent),
    cnx( QDBusConnection::connectToBus( QDBusConnection::SystemBus, "org.freedesktop.DBus" ) )
{

}

USB::~USB()
{
    QDBusConnection::disconnectFromBus(cnx.name());
    delete m_instance;
    mThread->wait();
    mThread->quit();
    mThread->deleteLater();
}

void USB::deviceAdded(QDBusObjectPath dev)
{    
    qDebug() << "device added path: "<<dev.path();
    QTimer::singleShot(1000, this, SLOT(extractMountedPaths()));
}

void USB::deviceRemoved(QDBusObjectPath dev)
{
    qDebug() << "device Removed path: "<<dev.path();
    extractMountedPaths();
}

void USB::extractMountedPaths()
{
    fileSystemPaths.clear();
    mountedPaths.clear();
    emumuratedDevices();

    bool deviceMounted;

    for(int index=0;index<fileSystemPaths.count();++index)
    {
        deviceMounted = isDeviceMounted(fileSystemPaths.at(index));
        if(deviceMounted)
        {
            mountedPaths.append( usbDeviceMountedPath(fileSystemPaths.at(index)) );
        }
    }

    emit deviceConnected();

}

void USB::emumuratedDevices()
{
    QDBusMessage Reply;
    QDBusInterface Connection ( "org.freedesktop.UDisks", "/org/freedesktop/UDisks" , "org.freedesktop.UDisks", QDBusConnection::systemBus());
    qDebug() <<"Is Connection Valid ? "<<Connection.isValid();
    Reply = Connection.call ( "EnumerateDevices" );
    if ( Reply.type() == QDBusMessage::ErrorMessage )
    {
        qDebug()<<"QDBusMessage::ErrorMessage";
    }
    else if ( Reply.type() != QDBusMessage::ReplyMessage )
    {
        qDebug()<<"QDBusMessage::ReplyMessage";
    }

    QDBusArgument dbusarg = qvariant_cast<QDBusArgument> ( Reply.arguments() [0] );

    dbusarg.beginArray();
    while ( !dbusarg.atEnd() )
    {
        QVariant variant = dbusarg.asVariant();
        QDBusObjectPath dbusobjectpath = qvariant_cast<QDBusObjectPath>(variant);
        fileSystemPaths.append(dbusobjectpath.path());
    }
}

bool USB::isDeviceMounted(QString path)
{
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks", path,"org.freedesktop.DBus.Properties","Get");
    QList<QVariant> args;
    args.append("org.freedesktop.UDisks.Device");
    args.append("DeviceIsMounted");
    msg.setArguments(args);

    QDBusMessage reply;
    reply = cnx.call(msg);

    QString sig;
    sig = 'v';
    bool ismounted = false;

    if (!reply.signature().compare(sig)) {
        if (reply.arguments().length() == 1) {
            bool ismounted;
            //not sure, if its the best way to handle those variants
            QVariant var = reply.arguments().at(0);
            QDBusVariant dbusVariant = qvariant_cast<QDBusVariant> (var);
            QVariant result = dbusVariant.variant();
            ismounted = result.toBool();
            return ismounted;
        }
    }
    return ismounted;
}

QString USB::usbDeviceMountedPath(QString path)
{
    QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.UDisks", path,"org.freedesktop.DBus.Properties","Get");
    QList<QVariant> args;
    args.append("org.freedesktop.UDisks.Device");
    args.append("DeviceMountPaths");
    msg.setArguments(args);
    QDBusMessage reply = cnx.call(msg);
    QString sig;
    sig = 'v';
    QString mount_path("");

    if (!reply.signature().compare(sig)) {
        if (reply.arguments().length() == 1) {
            QVariant var = reply.arguments().at(0);
            QDBusVariant dbusVariant = qvariant_cast<QDBusVariant> (var);
            QVariant result = dbusVariant.variant();
            mount_path = result.toString();
            return mount_path;
        }
    }
}
_______________________________________________
Interest mailing list
Interest@qt-project.org
http://lists.qt-project.org/mailman/listinfo/interest

Reply via email to