Fix old wiki links
[platform/upstream/glib.git] / gio / gunixvolumemonitor.c
index 2430e4b..130ca62 100644 (file)
@@ -1,3 +1,5 @@
+/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
+
 /* GIO - GLib Input, Output and Streaming Library
  * 
  * Copyright (C) 2006-2007 Red Hat, Inc.
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General
- * Public License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
- * Boston, MA 02111-1307, USA.
+ * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
  *
  * Author: Alexander Larsson <alexl@redhat.com>
+ *         David Zeuthen <davidz@redhat.com>
  */
 
-#include <config.h>
+#include "config.h"
 
 #include <string.h>
 
 #include <glib.h>
 #include "gunixvolumemonitor.h"
 #include "gunixmounts.h"
+#include "gunixmount.h"
 #include "gunixvolume.h"
-#include "gunixdrive.h"
-#include "gvolumeprivate.h"
-
+#include "gmount.h"
+#include "gmountprivate.h"
+#include "giomodule.h"
 #include "glibintl.h"
 
+
 struct _GUnixVolumeMonitor {
   GNativeVolumeMonitor parent;
 
@@ -41,18 +44,23 @@ struct _GUnixVolumeMonitor {
   GList *last_mountpoints;
   GList *last_mounts;
 
-  GList *drives;
   GList *volumes;
+  GList *mounts;
 };
 
-static void mountpoints_changed (GUnixMountMonitor  *mount_monitor,
-                                gpointer            user_data);
-static void mounts_changed      (GUnixMountMonitor  *mount_monitor,
-                                gpointer            user_data);
-static void update_drives       (GUnixVolumeMonitor *monitor);
-static void update_volumes      (GUnixVolumeMonitor *monitor);
+static void mountpoints_changed      (GUnixMountMonitor  *mount_monitor,
+                                      gpointer            user_data);
+static void mounts_changed           (GUnixMountMonitor  *mount_monitor,
+                                      gpointer            user_data);
+static void update_volumes           (GUnixVolumeMonitor *monitor);
+static void update_mounts            (GUnixVolumeMonitor *monitor);
 
-G_DEFINE_TYPE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
+#define g_unix_volume_monitor_get_type _g_unix_volume_monitor_get_type
+G_DEFINE_TYPE_WITH_CODE (GUnixVolumeMonitor, g_unix_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR,
+                         g_io_extension_point_implement (G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
+                                                        g_define_type_id,
+                                                        "unix",
+                                                        0));
 
 static void
 g_unix_volume_monitor_finalize (GObject *object)
@@ -66,58 +74,93 @@ g_unix_volume_monitor_finalize (GObject *object)
                                        
   g_object_unref (monitor->mount_monitor);
 
-  g_list_foreach (monitor->last_mounts, (GFunc)g_unix_mount_free, NULL);
-  g_list_free (monitor->last_mounts);
+  g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
+  g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
+
+  g_list_free_full (monitor->volumes, g_object_unref);
+  g_list_free_full (monitor->mounts, g_object_unref);
+
+  G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize (object);
+}
+
+static void
+g_unix_volume_monitor_dispose (GObject *object)
+{
+  GUnixVolumeMonitor *monitor;
+
+  monitor = G_UNIX_VOLUME_MONITOR (object);
 
-  g_list_foreach (monitor->volumes, (GFunc)g_object_unref, NULL);
-  g_list_free (monitor->volumes);
-  g_list_foreach (monitor->drives, (GFunc)g_object_unref, NULL);
-  g_list_free (monitor->drives);
+  g_list_free_full (monitor->volumes, g_object_unref);
+  monitor->volumes = NULL;
   
-  if (G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize)
-    (*G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->finalize) (object);
+  g_list_free_full (monitor->mounts, g_object_unref);
+  monitor->mounts = NULL;
+  
+  G_OBJECT_CLASS (g_unix_volume_monitor_parent_class)->dispose (object);
 }
 
 static GList *
-get_mounted_volumes (GVolumeMonitor *volume_monitor)
+get_mounts (GVolumeMonitor *volume_monitor)
 {
   GUnixVolumeMonitor *monitor;
-  GList *l;
   
   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
 
-  l = g_list_copy (monitor->volumes);
-  g_list_foreach (l, (GFunc)g_object_ref, NULL);
-
-  return l;
+  return g_list_copy_deep (monitor->mounts, (GCopyFunc) g_object_ref, NULL);
 }
 
 static GList *
-get_connected_drives (GVolumeMonitor *volume_monitor)
+get_volumes (GVolumeMonitor *volume_monitor)
 {
   GUnixVolumeMonitor *monitor;
-  GList *l;
   
   monitor = G_UNIX_VOLUME_MONITOR (volume_monitor);
 
-  l = g_list_copy (monitor->drives);
-  g_list_foreach (l, (GFunc)g_object_ref, NULL);
+  return g_list_copy_deep (monitor->volumes, (GCopyFunc) g_object_ref, NULL);
+}
 
-  return l;
+static GList *
+get_connected_drives (GVolumeMonitor *volume_monitor)
+{
+  return NULL;
 }
 
 static GVolume *
-get_volume_for_mountpoint (const char *mountpoint)
+get_volume_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
+{
+  return NULL;
+}
+
+static GMount *
+get_mount_for_uuid (GVolumeMonitor *volume_monitor, const char *uuid)
+{
+  return NULL;
+}
+
+static gboolean
+is_supported (void)
+{
+  return TRUE;
+}
+
+static GMount *
+get_mount_for_mount_path (const char *mount_path,
+                          GCancellable *cancellable)
 {
+  GUnixMountEntry *mount_entry;
   GUnixMount *mount;
-  GUnixVolume *volume;
 
-  mount = g_get_unix_mount_at (mountpoint, NULL);
-  
-  /* TODO: Set drive? */
-  volume = g_unix_volume_new (mount, NULL);
+  mount_entry = g_unix_mount_at (mount_path, NULL);
+
+  if (!mount_entry)
+    return NULL;
 
-  return G_VOLUME (volume);
+  /* TODO: Set mountable volume? */
+  mount = _g_unix_mount_new (NULL, mount_entry, NULL);
+
+  g_unix_mount_free (mount_entry);
+
+  return G_MOUNT (mount);
 }
 
 static void
@@ -128,34 +171,38 @@ g_unix_volume_monitor_class_init (GUnixVolumeMonitorClass *klass)
   GNativeVolumeMonitorClass *native_class = G_NATIVE_VOLUME_MONITOR_CLASS (klass);
   
   gobject_class->finalize = g_unix_volume_monitor_finalize;
+  gobject_class->dispose = g_unix_volume_monitor_dispose;
 
-  monitor_class->get_mounted_volumes = get_mounted_volumes;
+  monitor_class->get_mounts = get_mounts;
+  monitor_class->get_volumes = get_volumes;
   monitor_class->get_connected_drives = get_connected_drives;
+  monitor_class->get_volume_for_uuid = get_volume_for_uuid;
+  monitor_class->get_mount_for_uuid = get_mount_for_uuid;
+  monitor_class->is_supported = is_supported;
 
-  native_class->priority = 0;
-  native_class->get_volume_for_mountpoint = get_volume_for_mountpoint;
+  native_class->get_mount_for_mount_path = get_mount_for_mount_path;
 }
 
 static void
 mountpoints_changed (GUnixMountMonitor *mount_monitor,
-                    gpointer user_data)
+                    gpointer           user_data)
 {
   GUnixVolumeMonitor *unix_monitor = user_data;
 
-  /* Update both to make sure drives are created before volumes */
-  update_drives (unix_monitor);
+  /* Update both to make sure volumes are created before mounts */
   update_volumes (unix_monitor);
+  update_mounts (unix_monitor);
 }
 
 static void
 mounts_changed (GUnixMountMonitor *mount_monitor,
-               gpointer user_data)
+               gpointer           user_data)
 {
   GUnixVolumeMonitor *unix_monitor = user_data;
 
-  /* Update both to make sure drives are created before volumes */
-  update_drives (unix_monitor);
+  /* Update both to make sure volumes are created before mounts */
   update_volumes (unix_monitor);
+  update_mounts (unix_monitor);
 }
 
 static void
@@ -165,25 +212,19 @@ g_unix_volume_monitor_init (GUnixVolumeMonitor *unix_monitor)
   unix_monitor->mount_monitor = g_unix_mount_monitor_new ();
 
   g_signal_connect (unix_monitor->mount_monitor,
-                   "mounts_changed", G_CALLBACK (mounts_changed),
+                   "mounts-changed", G_CALLBACK (mounts_changed),
                    unix_monitor);
   
   g_signal_connect (unix_monitor->mount_monitor,
-                   "mountpoints_changed", G_CALLBACK (mountpoints_changed),
+                   "mountpoints-changed", G_CALLBACK (mountpoints_changed),
                    unix_monitor);
                    
-  update_drives (unix_monitor);
   update_volumes (unix_monitor);
-
+  update_mounts (unix_monitor);
 }
 
-/**
- * g_unix_volume_monitor_new:
- * 
- * Returns:  a new #GVolumeMonitor.
- **/
 GVolumeMonitor *
-g_unix_volume_monitor_new (void)
+_g_unix_volume_monitor_new (void)
 {
   GUnixVolumeMonitor *monitor;
 
@@ -193,8 +234,11 @@ g_unix_volume_monitor_new (void)
 }
 
 static void
-diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
-                  GList **added, GList **removed)
+diff_sorted_lists (GList         *list1, 
+                   GList         *list2, 
+                   GCompareFunc   compare,
+                  GList        **added, 
+                   GList        **removed)
 {
   int order;
   
@@ -233,56 +277,49 @@ diff_sorted_lists (GList *list1, GList *list2, GCompareFunc compare,
     }
 }
 
-/**
- * g_unix_volume_lookup_drive_for_mountpoint: 
- * @monitor:
- * @mountpoint:
- * 
- * Returns:  #GUnixDrive for the given @mountpoint.
- **/
-GUnixDrive *
-g_unix_volume_monitor_lookup_drive_for_mountpoint (GUnixVolumeMonitor *monitor,
-                                                  const char *mountpoint)
+GUnixVolume *
+_g_unix_volume_monitor_lookup_volume_for_mount_path (GUnixVolumeMonitor *monitor,
+                                                     const char         *mount_path)
 {
   GList *l;
 
-  for (l = monitor->drives; l != NULL; l = l->next)
+  for (l = monitor->volumes; l != NULL; l = l->next)
     {
-      GUnixDrive *drive = l->data;
+      GUnixVolume *volume = l->data;
 
-      if (g_unix_drive_has_mountpoint (drive, mountpoint))
-       return drive;
+      if (_g_unix_volume_has_mount_path (volume, mount_path))
+       return volume;
     }
   
   return NULL;
 }
 
-static GUnixVolume *
-find_volume_by_mountpoint (GUnixVolumeMonitor *monitor,
-                          const char *mountpoint)
+static GUnixMount *
+find_mount_by_mountpath (GUnixVolumeMonitor *monitor,
+                         const char *mount_path)
 {
   GList *l;
 
-  for (l = monitor->volumes; l != NULL; l = l->next)
+  for (l = monitor->mounts; l != NULL; l = l->next)
     {
-      GUnixVolume *volume = l->data;
+      GUnixMount *mount = l->data;
 
-      if (g_unix_volume_has_mountpoint (volume, mountpoint))
-       return volume;
+      if (_g_unix_mount_has_mount_path (mount, mount_path))
+       return mount;
     }
   
   return NULL;
 }
 
 static void
-update_drives (GUnixVolumeMonitor *monitor)
+update_volumes (GUnixVolumeMonitor *monitor)
 {
   GList *new_mountpoints;
   GList *removed, *added;
   GList *l;
-  GUnixDrive *drive;
+  GUnixVolume *volume;
   
-  new_mountpoints = g_get_unix_mount_points (NULL);
+  new_mountpoints = g_unix_mount_points_get (NULL);
   
   new_mountpoints = g_list_sort (new_mountpoints, (GCompareFunc) g_unix_mount_point_compare);
   
@@ -294,14 +331,15 @@ update_drives (GUnixVolumeMonitor *monitor)
     {
       GUnixMountPoint *mountpoint = l->data;
       
-      drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
-                                                                g_unix_mount_point_get_mount_path (mountpoint));
-      if (drive)
+      volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor,
+                                                                    g_unix_mount_point_get_mount_path (mountpoint));
+      if (volume)
        {
-         g_unix_drive_disconnected (drive);
-         monitor->drives = g_list_remove (monitor->drives, drive);
-         g_signal_emit_by_name (monitor, "drive_disconnected", drive);
-         g_object_unref (drive);
+         _g_unix_volume_disconnected (volume);
+         monitor->volumes = g_list_remove (monitor->volumes, volume);
+         g_signal_emit_by_name (monitor, "volume-removed", volume);
+         g_signal_emit_by_name (volume, "removed");
+         g_object_unref (volume);
        }
     }
   
@@ -309,33 +347,31 @@ update_drives (GUnixVolumeMonitor *monitor)
     {
       GUnixMountPoint *mountpoint = l->data;
       
-      drive = g_unix_drive_new (G_VOLUME_MONITOR (monitor), mountpoint);
-      if (drive)
+      volume = _g_unix_volume_new (G_VOLUME_MONITOR (monitor), mountpoint);
+      if (volume)
        {
-         monitor->drives = g_list_prepend (monitor->drives, drive);
-         g_signal_emit_by_name (monitor, "drive_connected", drive);
+         monitor->volumes = g_list_prepend (monitor->volumes, volume);
+         g_signal_emit_by_name (monitor, "volume-added", volume);
        }
     }
   
   g_list_free (added);
   g_list_free (removed);
-  g_list_foreach (monitor->last_mountpoints,
-                 (GFunc)g_unix_mount_point_free, NULL);
-  g_list_free (monitor->last_mountpoints);
+  g_list_free_full (monitor->last_mountpoints, (GDestroyNotify) g_unix_mount_point_free);
   monitor->last_mountpoints = new_mountpoints;
 }
 
 static void
-update_volumes (GUnixVolumeMonitor *monitor)
+update_mounts (GUnixVolumeMonitor *monitor)
 {
   GList *new_mounts;
   GList *removed, *added;
   GList *l;
+  GUnixMount *mount;
   GUnixVolume *volume;
-  GUnixDrive *drive;
   const char *mount_path;
   
-  new_mounts = g_get_unix_mounts (NULL);
+  new_mounts = g_unix_mounts_get (NULL);
   
   new_mounts = g_list_sort (new_mounts, (GCompareFunc) g_unix_mount_compare);
   
@@ -345,38 +381,36 @@ update_volumes (GUnixVolumeMonitor *monitor)
   
   for (l = removed; l != NULL; l = l->next)
     {
-      GUnixMount *mount = l->data;
+      GUnixMountEntry *mount_entry = l->data;
       
-      volume = find_volume_by_mountpoint (monitor, g_unix_mount_get_mount_path (mount));
-      if (volume)
+      mount = find_mount_by_mountpath (monitor, g_unix_mount_get_mount_path (mount_entry));
+      if (mount)
        {
-         g_unix_volume_unmounted (volume);
-         monitor->volumes = g_list_remove (monitor->volumes, volume);
-         g_signal_emit_by_name (monitor, "volume_unmounted", volume);
-         g_object_unref (volume);
+         _g_unix_mount_unmounted (mount);
+         monitor->mounts = g_list_remove (monitor->mounts, mount);
+         g_signal_emit_by_name (monitor, "mount-removed", mount);
+         g_signal_emit_by_name (mount, "unmounted");
+         g_object_unref (mount);
        }
     }
   
   for (l = added; l != NULL; l = l->next)
     {
-      GUnixMount *mount = l->data;
+      GUnixMountEntry *mount_entry = l->data;
 
-      mount_path = g_unix_mount_get_mount_path (mount);
+      mount_path = g_unix_mount_get_mount_path (mount_entry);
       
-      drive = g_unix_volume_monitor_lookup_drive_for_mountpoint (monitor,
-                                                                mount_path);
-      volume = g_unix_volume_new (mount, drive);
-      if (volume)
+      volume = _g_unix_volume_monitor_lookup_volume_for_mount_path (monitor, mount_path);
+      mount = _g_unix_mount_new (G_VOLUME_MONITOR (monitor), mount_entry, volume);
+      if (mount)
        {
-         monitor->volumes = g_list_prepend (monitor->volumes, volume);
-         g_signal_emit_by_name (monitor, "volume_mounted", volume);
+         monitor->mounts = g_list_prepend (monitor->mounts, mount);
+         g_signal_emit_by_name (monitor, "mount-added", mount);
        }
     }
   
   g_list_free (added);
   g_list_free (removed);
-  g_list_foreach (monitor->last_mounts,
-                 (GFunc)g_unix_mount_free, NULL);
-  g_list_free (monitor->last_mounts);
+  g_list_free_full (monitor->last_mounts, (GDestroyNotify) g_unix_mount_free);
   monitor->last_mounts = new_mounts;
 }