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.

This commit is contained in:
David Sansome 2010-09-04 12:34:01 +00:00
parent 2d9e8806cd
commit bb4bb28c04
2 changed files with 51 additions and 19 deletions

View File

@ -40,13 +40,42 @@ GioLister::GioLister()
{
}
template <typename T, typename F>
void OperationFinished(F f, GObject *object, GAsyncResult *result) {
T* obj = reinterpret_cast<T*>(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<GVolume>(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<GVolume*>(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<GMount*>(mounts->data);
GList* const mounts = g_volume_monitor_get_mounts(monitor_);
for (GList* p=mounts; p; p=p->next) {
GMount* mount = static_cast<GMount*>(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<QUrl> GioLister::MakeDeviceUrls(const QString &id) {
return ret;
}
void GioLister::VolumeAddedCallback(GVolumeMonitor*, GVolume* v, gpointer d) {
static_cast<GioLister*>(d)->VolumeAdded(v);
}
void GioLister::MountAddedCallback(GVolumeMonitor*, GMount* m, gpointer d) {
static_cast<GioLister*>(d)->MountAdded(m);
}
@ -142,6 +176,14 @@ void GioLister::MountRemovedCallback(GVolumeMonitor*, GMount* m, gpointer d) {
static_cast<GioLister*>(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 <typename T, typename F>
void OperationFinished(F f, GObject *object, GAsyncResult *result) {
T* obj = reinterpret_cast<T*>(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<GVolume>(boost::bind(
g_volume_eject_with_operation_finish, _1, _2, _3), object, result);

View File

@ -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);