来者有分!在ubuntu环境下,用Qt怎么实现一个usb盘检测工具?
各位大侠,我目前的工作需要,在ubuntu环境下实现一个u盘插拔识别工具;
我选择了qt creator;在网上找了一段代码,是管理U盘的,我将代码加入到我的项目中,
编译运行后,发现插拔usb盘,我找到的代码没有反应,求助各位了,帮我看看了,来者有分。
#ifndef HALDEVICE_H
#define HALDEVICE_H
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <QObject>
#include <QtDBus/QDBusConnection>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusObjectPath>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusVariant>
#include <QTimer>
#include <QtDBus/QDBusMetaType>
#include <QtDBus/QDBusArgument>
#include <QFile>
#include <QDBusInterface>
//#include "filebrowser.h"
#ifdef DEBUG_LOG
#define LOG(...) printf(__VA_ARGS__)
#else
#define LOG( format, ... ) "\n"
#endif
#define DBUS_HAL_SERVICE "org.freedesktop.Hal"
#define DBUS_HAL_PATH "/org/freedesktop/Hal/Manager"
#define DBUS_HAL_INTERFACE "org.freedesktop.Hal.Manager"
#define DBUS_HAL_DEVICE_INTERFACE "org.freedesktop.Hal.Device"
struct ChangeStruct
{
QString propertyName;
bool added;
bool removed;
};
class haldevice : public QObject
{
Q_OBJECT
signals:
void mountedStatusChanged(bool);
void readyBrowserUsb(QString path);
public:
// usbdevice(QObject *parent = 0);
haldevice(QDBusInterface *interface,const QString &deviceUdi);
~haldevice();
public slots:
void propertyModified(int numChanges,const QList<ChangeStruct> &changes);
void proMountedStatusChanged(bool);
void proReadyBrowserUsb(QString path);
public:
const QString& udi() const;
const QString& parentUdi() const;
const QString& blockName() const;
const QString& category() const;
const QString& bus() const;
int majorNumber() const;
bool isMounted() const;
bool isMountedByDBus() const;
const QString& currentMountPointByDBus();
void setParentUdi(QString);
void setBlockName(QString);
void setCurrentMountPoint(QString);
void setCategory(QString);
void setBus(QString);
void setIsMounted(bool);
void setMajorNumber(int);
private:
QString deviceUdi;
QString deviceParentUdi;
QString deviceBlockName;
QString deviceCurrentMountPoint;
QString deviceCategory;
QString deviceBus;
bool deviceIsMounted;
int major;
QDBusInterface *informationCenter;
};
#endif
#ifndef UABMANAGER_H
#define UABMANAGER_H
#include "haldevice.h"
#include <QtCore/QMap>
#include <QDBusInterface>
class usbmanager : public QObject
{
Q_OBJECT
public:
usbmanager(QObject *parent = 0);
public slots:
void slotDeviceAdded(QString strdev);
void slotDeviceRemoved(QString strdev);
void scan();
private:
QMap<QString,haldevice*> mDeviceMap;
void getDevice(QDBusInterface *interface,const QString &udi);
bool isVolume(QString value);
bool isDisk(QString value);
bool isBlockDev(QString value);
bool isValidMajor(int value);
bool isUsb(QString value);
// Create interface to get information from Hal
QDBusInterface *interfaceHal;
};
#endif