Skip to content

Commit

Permalink
Adding support for udisks2
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Barringer committed Nov 14, 2012
1 parent 396f67a commit 7886562
Show file tree
Hide file tree
Showing 20 changed files with 705 additions and 47 deletions.
79 changes: 75 additions & 4 deletions MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,15 @@
#include <QProgressDialog>
#include <QtDBus>
#include <QFile>
#include <QDebug>

#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>

#ifdef USEUDISKS2
#include "udisks2_interface.h"
#endif


#include "MainWindow.h"
Expand Down Expand Up @@ -86,7 +90,16 @@ MainWindow::MainWindow (Platform *platform,
"DeviceRemoved",
this,
SLOT(deviceRemoved(QDBusMessage)));
#elif USEUDISKS2
qDebug() << "Using udisks2";
org::freedesktop::DBus::ObjectManager manager("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", QDBusConnection::systemBus());
QDBusConnection::systemBus().connect("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", "org.freedesktop.DBus.ObjectManager", "InterfacesAdded",
this, SLOT(deviceInserted(QDBusObjectPath,QVariantMapMap)));
QDBusConnection::systemBus().connect("org.freedesktop.UDisks2", "/org/freedesktop/UDisks2", "org.freedesktop.DBus.ObjectManager", "InterfacesRemoved",
this, SLOT(deviceRemoved(QDBusObjectPath,QStringList)));

#else
qDebug() << "Using udisks";
dbusConnection.connect("",
"/org/freedesktop/UDisks",
"org.freedesktop.UDisks",
Expand Down Expand Up @@ -313,6 +326,61 @@ MainWindow::checkIso(const QString &fileName)
return(true);
}

// UDisks2 insertion handler
void MainWindow::deviceInserted(const QDBusObjectPath &object_path,
const QVariantMapMap &interfaces_and_properties)
{
Q_UNUSED(interfaces_and_properties);

QRegExp reg("[0-9]+$");
QString path = object_path.path();

if (!path.startsWith("/org/freedesktop/UDisks2/block_devices"))
return;

if (path.contains(reg))
return;

DeviceItem *device = pPlatform->getNewDevice(path);
if (device != NULL)
if (deviceComboBox->findText(device->getDisplayString()) == -1)
addMenuItem(device->getDisplayString());

}

// UDisks2 removal handler
void MainWindow::deviceRemoved(const QDBusObjectPath &object_path,
const QStringList &interfaces)
{
Q_UNUSED(interfaces);

QRegExp reg("[0-9]+$");
QString path = object_path.path();

if (!path.startsWith("/org/freedesktop/UDisks2/block_devices"))
return;

if (path.contains(reg))
return;

QString udi = path.mid(path.lastIndexOf("/") + 1);
QLinkedList<DeviceItem *> list = pPlatform->getDeviceList();
QLinkedList<DeviceItem *>::iterator i;
for (i = list.begin(); i != list.end(); ++i)
{
if ((*i)->getUDI() == udi)
{
if (removeMenuItem((*i)->getDisplayString()) != -1)
{
pPlatform->removeDeviceFromList(path);
break;
}
}
}

}

// UDisks & HAL insertion handler
void
MainWindow::deviceInserted(QDBusMessage message)
{
Expand All @@ -333,10 +401,10 @@ MainWindow::deviceInserted(QDBusMessage message)
}
}

// UDisks & HAL removal handler
void
MainWindow::deviceRemoved(QDBusMessage message)
{
int index;
QString devicePath;
#ifdef USEHAL
devicePath = message.arguments().at(0).toString();
Expand Down Expand Up @@ -554,22 +622,25 @@ AboutLabel::AboutLabel(QWidget *parent)
void
AboutLabel::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
QMessageBox about(QMessageBox::Information, "About SUSE Studio Imagewriter",
"The <b>SUSE Studio Imagewriter</b> is (C) 2010 Novell, Inc.<br><br>\
It is cheerfully released under the GPL v2 license. You can find the source code in the Kiwi project: http://kiwi.berlios.de.<br><br>\
It was written by Matt Barringer &lt;mbarringer@suse.de&gt;. Please send complaints directly to him.");
"The <b>SUSE Studio Imagewriter</b> is (C) 2012, SUSE Linux Products GmbH<br><br>\
It is cheerfully released under the GPL v2 license. You can find the source code on github: https://github.com/mbarringer/imagewriter<br><br>\
It was written by Matt Barringer &lt;matt@incoherent.de&gt;. Please send complaints directly to him.");
about.exec();
}

void
AboutLabel::enterEvent(QEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::PointingHandCursor);
}

void
AboutLabel::leaveEvent(QEvent *event)
{
Q_UNUSED(event);
setCursor(Qt::ArrowCursor);
}

4 changes: 4 additions & 0 deletions MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
#include <QWidget>
#include <QDBusMessage>

#include "MetaTypes.h"
#include "Platform.h"
#include "DeviceItem.h"

#define DROPDOWN_DIRECTIVE "Insert a USB device"

class MainWindow : public QWidget
Expand All @@ -48,6 +50,8 @@ public slots:
void selectImage();
void deviceInserted(QDBusMessage message);
void deviceRemoved(QDBusMessage message);
void deviceInserted(const QDBusObjectPath &object_path, const QVariantMapMap &interfaces_and_properties);
void deviceRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces);

private slots:
void write();
Expand Down
16 changes: 16 additions & 0 deletions MetaTypes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef METATYPES_H
#define METATYPES_H

#include <QtCore>
#include <QtDBus>

typedef QMap<QString,QVariantMap> QVariantMapMap;
Q_DECLARE_METATYPE(QVariantMapMap)

typedef QMap<QDBusObjectPath, QVariantMapMap> DBUSManagerStruct;
Q_DECLARE_METATYPE(DBUSManagerStruct)

typedef QList<QByteArray> ByteArrayList;
Q_DECLARE_METATYPE(ByteArrayList)

#endif // METATYPES_H
5 changes: 4 additions & 1 deletion Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@
#include <sys/mount.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#include <fcntl.h>
#include <errno.h>

#define BLOCKSIZE 1048576
#define _GNU_SOURCE

Platform::Platform(bool kioskMode)
Platform::Platform(bool kioskMode, bool unsafe)
{
mKioskMode = kioskMode;
mUnsafe = unsafe;
}

bool
Expand Down
11 changes: 6 additions & 5 deletions Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,19 @@ class Platform
{

public:
Platform(bool kioskMode = false);
Platform(bool kioskMode = false, bool unsafe = false);
bool removeDeviceFromList(const QString &displayName);
DeviceItem *findDeviceInList(const QString &displayName);
void writeData(QString path, QString fileName, qint64 deviceSize);
QLinkedList<DeviceItem *> getDeviceList() { return itemList; }

virtual void findDevices(bool unsafe = false) {}
virtual bool isMounted(QString path) { return false; }
virtual bool unmountDevice(QString path) { return false; }
virtual DeviceItem *getNewDevice(QString devicePath) { return(NULL); }
virtual void findDevices() {}
virtual bool isMounted(QString path) { Q_UNUSED(path); return false; }
virtual bool unmountDevice(QString path) { Q_UNUSED(path); return false; }
virtual DeviceItem *getNewDevice(QString devicePath) { Q_UNUSED(devicePath); return(NULL); }

protected:
bool mUnsafe;
bool mKioskMode;
DeviceItem *pDevice;
QLinkedList<DeviceItem *> itemList;
Expand Down
8 changes: 4 additions & 4 deletions PlatformHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@
#include "DeviceItem.h"
#include "PlatformHal.h"

PlatformHal::PlatformHal(bool kioskMode)
: Platform(kioskMode)
PlatformHal::PlatformHal(bool kioskMode, bool unsafe)
: Platform(kioskMode, unsafe)
{
}

// Figure out which devices we should allow a user to write to.
void
PlatformHal::findDevices(bool unsafe)
PlatformHal::findDevices()
{
char **drives;
int drive_count, i;
Expand All @@ -56,7 +56,7 @@ PlatformHal::findDevices(bool unsafe)

// We want to only write to USB drives, unless the user specifies
// the unsafe flag on the command line
if (unsafe)
if (mUnsafe)
drives = libhal_manager_find_device_string_match(context,
"storage.drive_type",
"disk",
Expand Down
4 changes: 2 additions & 2 deletions PlatformHal.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class PlatformHal : public Platform
{

public:
PlatformHal(bool kioskMode = false);
void findDevices(bool unsafe = false);
PlatformHal(bool kioskMode = false, bool unsafe = false);
void findDevices();
bool isMounted(QString path);
void writeData(QString path, QString fileName, qint64 deviceSize);
bool unmountDevice(QString path);
Expand Down
9 changes: 5 additions & 4 deletions PlatformUdisks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
#include "DeviceItem.h"
#include "PlatformUdisks.h"

PlatformUdisks::PlatformUdisks(bool kioskMode)
: Platform(kioskMode)
PlatformUdisks::PlatformUdisks(bool kioskMode, bool unsafe)
: Platform(kioskMode, unsafe)
{
}

Expand Down Expand Up @@ -65,7 +65,7 @@ PlatformUdisks::udiskEnabled()

// Figure out which devices we should allow a user to write to.
void
PlatformUdisks::findDevices(bool unsafe)
PlatformUdisks::findDevices()
{
int i = 0;
if (!udiskEnabled())
Expand All @@ -75,6 +75,7 @@ PlatformUdisks::findDevices(bool unsafe)
msgBox.exec();
return;
}

// First get the list of disks
QDBusConnection connection = QDBusConnection::systemBus();
QDBusMessage message;
Expand All @@ -98,7 +99,7 @@ PlatformUdisks::findDevices(bool unsafe)

QStringList diskList;
// Safe mode (the default) only handles USB devices
if (!unsafe)
if (!mUnsafe)
{
for (i = 0; i < devList.size(); ++i)
if (isUSB(devList.at(i)))
Expand Down
7 changes: 3 additions & 4 deletions PlatformUdisks.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class PlatformUdisks : public Platform
{

public:
PlatformUdisks(bool kioskMode = false);
void findDevices(bool unsafe = false);
PlatformUdisks(bool kioskMode = false, bool unsafe = false);
void findDevices();
bool isMounted(QString path);
void writeData(QString path, QString fileName, qint64 deviceSize);
bool unmountDevice(QString path);
DeviceItem *getNewDevice(QString devicePath);

private:
protected:
bool udiskEnabled();
bool isUSB(const QString &udiskPath);
bool isPartitionMounted(const QString &partitionPath);
Expand All @@ -52,7 +52,6 @@ class PlatformUdisks : public Platform
QString getVendor(const QString &devicePath);
bool getIsRemovable(const QString &devicePath);
long long getSize(const QString &devicePath);

};

#endif
Loading

0 comments on commit 7886562

Please sign in to comment.