From bb4bb28c049f0480af23e4adfc1cc6f76c77eec5 Mon Sep 17 00:00:00 2001 From: David Sansome Date: Sat, 4 Sep 2010 12:34:01 +0000 Subject: [PATCH] Automatically mount GIO volumes that get plugged in. Nautilus already does this for us in Gnome, this makes it work as expected in KDE too. --- src/devices/giolister.cpp | 61 +++++++++++++++++++++++++++++---------- src/devices/giolister.h | 9 ++++-- 2 files changed, 51 insertions(+), 19 deletions(-) diff --git a/src/devices/giolister.cpp b/src/devices/giolister.cpp index 8c1ed5ea6..789ff08eb 100644 --- a/src/devices/giolister.cpp +++ b/src/devices/giolister.cpp @@ -40,13 +40,42 @@ GioLister::GioLister() { } +template +void OperationFinished(F f, GObject *object, GAsyncResult *result) { + T* obj = reinterpret_cast(object); + GError* error = NULL; + + f(obj, result, &error); + + if (error) { + qDebug() << "Unmount error:" << error->message; + g_error_free(error); + } +} + +void GioLister::VolumeMountFinished(GObject* object, GAsyncResult* result, gpointer) { + OperationFinished(boost::bind( + g_volume_mount_finish, _1, _2, _3), object, result); +} + void GioLister::Init() { monitor_ = g_volume_monitor_get(); + // Mount any volumes that aren't already mounted + GList* const volumes = g_volume_monitor_get_volumes(monitor_); + for (GList* p=volumes; p; p=p->next) { + GVolume* volume = static_cast(p->data); + if (g_volume_can_mount(volume) && !g_volume_get_mount(volume)) { + g_volume_mount(volume, G_MOUNT_MOUNT_NONE, NULL, NULL, + (GAsyncReadyCallback) VolumeMountFinished, NULL); + } + g_object_unref(volume); + } + // Get things that are already mounted - GList* mounts = g_volume_monitor_get_mounts(monitor_); - for (; mounts ; mounts=mounts->next) { - GMount* mount = static_cast(mounts->data); + GList* const mounts = g_volume_monitor_get_mounts(monitor_); + for (GList* p=mounts; p; p=p->next) { + GMount* mount = static_cast(p->data); MountAdded(mount); g_object_unref(mount); @@ -54,6 +83,7 @@ void GioLister::Init() { g_list_free(mounts); // Connect signals from the monitor + g_signal_connect(monitor_, "volume-added", G_CALLBACK(VolumeAddedCallback), this); g_signal_connect(monitor_, "mount-added", G_CALLBACK(MountAddedCallback), this); g_signal_connect(monitor_, "mount-changed", G_CALLBACK(MountChangedCallback), this); g_signal_connect(monitor_, "mount-removed", G_CALLBACK(MountRemovedCallback), this); @@ -130,6 +160,10 @@ QList GioLister::MakeDeviceUrls(const QString &id) { return ret; } +void GioLister::VolumeAddedCallback(GVolumeMonitor*, GVolume* v, gpointer d) { + static_cast(d)->VolumeAdded(v); +} + void GioLister::MountAddedCallback(GVolumeMonitor*, GMount* m, gpointer d) { static_cast(d)->MountAdded(m); } @@ -142,6 +176,14 @@ void GioLister::MountRemovedCallback(GVolumeMonitor*, GMount* m, gpointer d) { static_cast(d)->MountRemoved(m); } +void GioLister::VolumeAdded(GVolume* volume) { + if (g_volume_can_mount(volume) && !g_volume_get_mount(volume)) { + g_volume_mount(volume, G_MOUNT_MOUNT_NONE, NULL, NULL, + (GAsyncReadyCallback) VolumeMountFinished, NULL); + } + g_object_unref(volume); +} + void GioLister::MountAdded(GMount *mount) { MountInfo info = ReadMountInfo(mount); if (!info.is_suitable()) @@ -274,19 +316,6 @@ QString GioLister::FindUniqueIdByMount(GMount *mount) const { return QString(); } -template -void OperationFinished(F f, GObject *object, GAsyncResult *result) { - T* obj = reinterpret_cast(object); - GError* error = NULL; - - f(obj, result, &error); - - if (error) { - qDebug() << "Unmount error:" << error->message; - g_error_free(error); - } -} - void GioLister::VolumeEjectFinished(GObject *object, GAsyncResult *result, gpointer) { OperationFinished(boost::bind( g_volume_eject_with_operation_finish, _1, _2, _3), object, result); diff --git a/src/devices/giolister.h b/src/devices/giolister.h index 70250ce32..22b9f1ccf 100644 --- a/src/devices/giolister.h +++ b/src/devices/giolister.h @@ -76,17 +76,20 @@ private: QString filesystem_type; }; + void VolumeAdded(GVolume* volume); void MountAdded(GMount* mount); void MountChanged(GMount* mount); void MountRemoved(GMount* mount); + static void VolumeAddedCallback(GVolumeMonitor*, GVolume*, gpointer); static void MountAddedCallback(GVolumeMonitor*, GMount*, gpointer); static void MountChangedCallback(GVolumeMonitor*, GMount*, gpointer); static void MountRemovedCallback(GVolumeMonitor*, GMount*, gpointer); - static void VolumeEjectFinished(GObject *object, GAsyncResult *result, gpointer); - static void MountEjectFinished(GObject *object, GAsyncResult *result, gpointer); - static void MountUnmountFinished(GObject *object, GAsyncResult *result, gpointer); + static void VolumeMountFinished(GObject* object, GAsyncResult* result, gpointer); + static void VolumeEjectFinished(GObject* object, GAsyncResult* result, gpointer); + static void MountEjectFinished(GObject* object, GAsyncResult* result, gpointer); + static void MountUnmountFinished(GObject* object, GAsyncResult* result, gpointer); static QString ConvertAndFree(char* str); static MountInfo ReadMountInfo(GMount* mount);