[gnome-settings-daemon] automount: initial port of Nautilus autorun code



commit a71ee05751b826b8116eb691ca82bf6d58e06af2
Author: Tomas Bzatek <tbzatek redhat com>
Date:   Thu Nov 25 15:21:05 2010 +0100

    automount: initial port of Nautilus autorun code
    
    See bug 585545 for further details.

 configure.ac                                       |    1 +
 ...gnome.settings-daemon.plugins.gschema.xml.in.in |   13 +
 plugins/Makefile.am                                |    1 +
 plugins/automount/Makefile.am                      |   82 ++
 .../automount/automount.gnome-settings-plugin.in   |    8 +
 plugins/automount/gsd-automount-manager.c          |  372 +++++
 plugins/automount/gsd-automount-manager.h          |   57 +
 plugins/automount/gsd-automount-plugin.c           |  110 ++
 plugins/automount/gsd-automount-plugin.h           |   59 +
 plugins/automount/nautilus-autorun.c               | 1532 ++++++++++++++++++++
 plugins/automount/nautilus-autorun.h               |   81 +
 plugins/automount/nautilus-open-with-dialog.c      | 1072 ++++++++++++++
 plugins/automount/nautilus-open-with-dialog.h      |   64 +
 plugins/automount/test-automount.c                 |   58 +
 14 files changed, 3510 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 3e63b5d..9e0258e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -439,6 +439,7 @@ Makefile
 gnome-settings-daemon/Makefile
 plugins/Makefile
 plugins/a11y-keyboard/Makefile
+plugins/automount/Makefile
 plugins/background/Makefile
 plugins/clipboard/Makefile
 plugins/common/Makefile
diff --git a/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in b/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
index 1f3729c..cca9e36 100644
--- a/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
+++ b/data/org.gnome.settings-daemon.plugins.gschema.xml.in.in
@@ -1,6 +1,7 @@
 <schemalist>
   <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.settings-daemon.plugins" path="/org/gnome/settings-daemon/plugins/">
     <child name="a11-keyboard" schema="org.gnome.settings-daemon.plugins.a11y-keyboard"/>
+    <child name="automount" schema="org.gnome.settings-daemon.plugins.automount"/>
     <child name="background" schema="org.gnome.settings-daemon.plugins.background"/>
     <child name="clipboard" schema="org.gnome.settings-daemon.plugins.clipboard"/>
     <child name="font" schema="org.gnome.settings-daemon.plugins.font"/>
@@ -21,6 +22,18 @@
       <_description>Priority to use for this plugin in gnome-settings-daemon startup queue</_description>
     </key>
   </schema>
+  <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.settings-daemon.plugins.automount" path="/org/gnome/settings-daemon/plugins/automount/">
+    <key name="active" type="b">
+      <default>true</default>
+      <_summary>Activation of this plugin</_summary>
+      <_description>Whether this plugin would be activated by gnome-settings-daemon or not</_description>
+    </key>
+    <key name="priority" type="i">
+      <default>97</default>
+      <_summary>Priority to use for this plugin</_summary>
+      <_description>Priority to use for this plugin in gnome-settings-daemon startup queue</_description>
+    </key>
+  </schema>
   <schema gettext-domain="@GETTEXT_PACKAGE@" id="org.gnome.settings-daemon.plugins.background" path="/org/gnome/settings-daemon/plugins/background/">
     <key name="active" type="b">
       <default>true</default>
diff --git a/plugins/Makefile.am b/plugins/Makefile.am
index 4d6381a..33e39cb 100644
--- a/plugins/Makefile.am
+++ b/plugins/Makefile.am
@@ -2,6 +2,7 @@ NULL =
 
 enabled_plugins =	\
 	a11y-keyboard	\
+	automount	\
 	background	\
 	clipboard	\
 	datetime	\
diff --git a/plugins/automount/Makefile.am b/plugins/automount/Makefile.am
new file mode 100644
index 0000000..3072c39
--- /dev/null
+++ b/plugins/automount/Makefile.am
@@ -0,0 +1,82 @@
+NULL =
+
+noinst_PROGRAMS = 			\
+	test-automount			\
+	$(NULL)
+
+test_automount_SOURCES = 		\
+	test-automount.c		\
+	gsd-automount-manager.h		\
+	gsd-automount-manager.c		\
+	nautilus-autorun.c		\
+	nautilus-autorun.h		\
+	nautilus-open-with-dialog.c	\
+	nautilus-open-with-dialog.h	\
+	$(NULL)
+
+test_automount_CPPFLAGS = \
+	-I$(top_srcdir)/gnome-settings-daemon		\
+	-DGNOME_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\" \
+	$(AM_CPPFLAGS)
+
+test_automount_CFLAGS = \
+	$(SETTINGS_PLUGIN_CFLAGS)	\
+	$(AM_CFLAGS)
+
+test_automount_LDADD =			\
+	$(top_builddir)/gnome-settings-daemon/libgsd-profile.la	\
+	$(SETTINGS_PLUGIN_LIBS)		\
+	$(X11_LIBS)			\
+	$(NULL)
+
+plugin_LTLIBRARIES = \
+	libautomount.la		\
+	$(NULL)
+
+libautomount_la_SOURCES =		\
+	gsd-automount-plugin.h		\
+	gsd-automount-plugin.c		\
+	gsd-automount-manager.h		\
+	gsd-automount-manager.c		\
+	nautilus-autorun.c		\
+	nautilus-autorun.h		\
+	nautilus-open-with-dialog.c	\
+	nautilus-open-with-dialog.h	\
+	$(NULL)
+
+libautomount_la_CPPFLAGS = \
+	-I$(top_srcdir)/gnome-settings-daemon		\
+	-DGNOME_SETTINGS_LOCALEDIR=\""$(datadir)/locale"\" \
+	$(AM_CPPFLAGS)
+
+libautomount_la_CFLAGS = \
+	$(SETTINGS_PLUGIN_CFLAGS)	\
+	$(AM_CFLAGS)
+
+libautomount_la_LDFLAGS =		\
+	$(GSD_PLUGIN_LDFLAGS)		\
+	$(NULL)
+
+libautomount_la_LIBADD  =		\
+	$(SETTINGS_PLUGIN_LIBS)		\
+	$(NULL)
+
+plugin_in_files =		\
+	automount.gnome-settings-plugin.in	\
+	$(NULL)
+
+plugin_DATA = $(plugin_in_files:.gnome-settings-plugin.in=.gnome-settings-plugin)
+
+EXTRA_DIST = 			\
+	$(plugin_in_files)	\
+	$(NULL)
+
+CLEANFILES = 			\
+	$(plugin_DATA)		\
+	$(NULL)
+
+DISTCLEANFILES =		\
+	$(plugin_DATA)		\
+	$(NULL)
+
+ GSD_INTLTOOL_PLUGIN_RULE@
diff --git a/plugins/automount/automount.gnome-settings-plugin.in b/plugins/automount/automount.gnome-settings-plugin.in
new file mode 100644
index 0000000..6a5362d
--- /dev/null
+++ b/plugins/automount/automount.gnome-settings-plugin.in
@@ -0,0 +1,8 @@
+[GNOME Settings Plugin]
+Module=automount
+IAge=0
+_Name=Automount
+_Description=Automounter plugin
+Authors=
+Copyright=Copyright © 2010
+Website=
diff --git a/plugins/automount/gsd-automount-manager.c b/plugins/automount/gsd-automount-manager.c
new file mode 100644
index 0000000..f02b66a
--- /dev/null
+++ b/plugins/automount/gsd-automount-manager.c
@@ -0,0 +1,372 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Tomas Bzatek <tbzatek redhat com>
+ * Portions of the code have been ripped from nautilus-application.c,
+ * copyright belongs to the particular Nautilus developers.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+
+#include "gnome-settings-profile.h"
+#include "gsd-automount-manager.h"
+#include "nautilus-autorun.h"
+
+#define GSD_AUTOMOUNT_MANAGER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GSD_TYPE_AUTOMOUNT_MANAGER, GsdAutomountManagerPrivate))
+
+struct GsdAutomountManagerPrivate
+{
+        GSettings   *settings;
+
+	GVolumeMonitor *volume_monitor;
+	unsigned int automount_idle_id;
+};
+
+static void     gsd_automount_manager_class_init  (GsdAutomountManagerClass *klass);
+static void     gsd_automount_manager_init        (GsdAutomountManager      *gsd_automount_manager);
+static void     gsd_automount_manager_finalize    (GObject             *object);
+
+G_DEFINE_TYPE (GsdAutomountManager, gsd_automount_manager, G_TYPE_OBJECT)
+
+static gpointer manager_object = NULL;
+
+
+static void
+startup_volume_mount_cb (GObject *source_object,
+			 GAsyncResult *res,
+			 gpointer user_data)
+{
+	g_volume_mount_finish (G_VOLUME (source_object), res, NULL);
+}
+
+static void
+automount_all_volumes (GsdAutomountManager *manager)
+{
+	GList *volumes, *l;
+	GMount *mount;
+	GVolume *volume;
+
+	if (g_settings_get_boolean (manager->priv->settings, "automount")) {
+		/* automount all mountable volumes at start-up */
+		volumes = g_volume_monitor_get_volumes (manager->priv->volume_monitor);
+		for (l = volumes; l != NULL; l = l->next) {
+			volume = l->data;
+
+			if (!g_volume_should_automount (volume) ||
+			    !g_volume_can_mount (volume)) {
+				continue;
+			}
+
+			mount = g_volume_get_mount (volume);
+			if (mount != NULL) {
+				g_object_unref (mount);
+				continue;
+			}
+
+			/* pass NULL as GMountOperation to avoid user interaction */
+			g_volume_mount (volume, 0, NULL, NULL, startup_volume_mount_cb, NULL);
+		}
+		g_list_foreach (volumes, (GFunc) g_object_unref, NULL);
+		g_list_free (volumes);
+	}
+}
+
+static gboolean
+automount_all_volumes_idle_cb (gpointer data)
+{
+	GsdAutomountManager *manager = GSD_AUTOMOUNT_MANAGER (data);
+
+	automount_all_volumes (manager);
+
+	manager->priv->automount_idle_id = 0;
+	return FALSE;
+}
+
+static void
+volume_mount_cb (GObject *source_object,
+		 GAsyncResult *res,
+		 gpointer user_data)
+{
+	GMountOperation *mount_op = user_data;
+	GError *error;
+	char *primary;
+	char *name;
+
+	error = NULL;
+	nautilus_allow_autorun_for_volume_finish (G_VOLUME (source_object));
+	if (!g_volume_mount_finish (G_VOLUME (source_object), res, &error)) {
+		if (error->code != G_IO_ERROR_FAILED_HANDLED) {
+			name = g_volume_get_name (G_VOLUME (source_object));
+			primary = g_strdup_printf (_("Unable to mount %s"), name);
+			g_free (name);
+			show_error_dialog (primary,
+				           error->message,
+					   NULL);
+			g_free (primary);
+		}
+		g_error_free (error);
+	}
+
+	g_object_unref (mount_op);
+}
+
+static void
+nautilus_file_operations_mount_volume (GtkWindow *parent_window,
+				       GVolume *volume,
+				       gboolean allow_autorun)
+{
+	GMountOperation *mount_op;
+
+	mount_op = gtk_mount_operation_new (parent_window);
+	g_mount_operation_set_password_save (mount_op, G_PASSWORD_SAVE_FOR_SESSION);
+
+	if (allow_autorun) {
+		nautilus_allow_autorun_for_volume (volume);
+	}
+	g_volume_mount (volume, 0, mount_op, NULL, volume_mount_cb, mount_op);
+}
+
+static void
+volume_added_callback (GVolumeMonitor *monitor,
+		       GVolume *volume,
+		       GsdAutomountManager *manager)
+{
+	g_print ("volume_added_callback\n");
+
+	if (g_settings_get_boolean (manager->priv->settings, "automount") &&
+	    g_volume_should_automount (volume) &&
+	    g_volume_can_mount (volume)) {
+		nautilus_file_operations_mount_volume (NULL, volume, TRUE);
+	} else {
+		/* Allow nautilus_autorun() to run. When the mount is later
+		 * added programmatically (i.e. for a blank CD),
+		 * nautilus_autorun() will be called by mount_added_callback(). */
+		nautilus_allow_autorun_for_volume (volume);
+		nautilus_allow_autorun_for_volume_finish (volume);
+	}
+}
+
+static void
+autorun_show_window (GMount *mount, gpointer user_data)
+{
+	GFile *location;
+        char *uri;
+        GError *error;
+	char *primary;
+	char *name;
+
+	location = g_mount_get_root (mount);
+        uri = g_file_get_uri (location);
+
+	g_print ("-- mount OK, show window\n");
+
+        error = NULL;
+	/* use default folder handler */
+        if (! gtk_show_uri (NULL, uri, GDK_CURRENT_TIME, &error)) {
+		name = g_mount_get_name (mount);
+		primary = g_strdup_printf (_("Unable to open a folder for %s"), name);
+		g_free (name);
+        	show_error_dialog (primary,
+        		           error->message,
+        			   NULL);
+		g_free (primary);
+        	g_error_free (error);
+        }
+
+        g_free (uri);
+	g_object_unref (location);
+}
+
+static void
+mount_added_callback (GVolumeMonitor *monitor,
+		      GMount *mount,
+		      GsdAutomountManager *manager)
+{
+	g_print ("mount_added_callback\n");
+
+	nautilus_autorun (mount, manager->priv->settings, autorun_show_window, manager);
+}
+
+static void
+setup_automounter (GsdAutomountManager *manager)
+{
+	manager->priv->volume_monitor = g_volume_monitor_get ();
+	g_signal_connect_object (manager->priv->volume_monitor, "mount_added",
+				 G_CALLBACK (mount_added_callback), manager, 0);
+	g_signal_connect_object (manager->priv->volume_monitor, "volume_added",
+				 G_CALLBACK (volume_added_callback), manager, 0);
+
+	manager->priv->automount_idle_id =
+		g_idle_add_full (G_PRIORITY_LOW,
+				 automount_all_volumes_idle_cb,
+				 manager, NULL);
+}
+
+gboolean
+gsd_automount_manager_start (GsdAutomountManager *manager,
+                                       GError              **error)
+{
+        g_debug ("Starting automounting manager");
+        gnome_settings_profile_start (NULL);
+
+        manager->priv->settings = g_settings_new ("org.gnome.media-handling");
+        setup_automounter (manager);
+
+        gnome_settings_profile_end (NULL);
+
+        return TRUE;
+}
+
+void
+gsd_automount_manager_stop (GsdAutomountManager *manager)
+{
+        GsdAutomountManagerPrivate *p = manager->priv;
+
+        g_debug ("Stopping automounting manager");
+
+	if (p->volume_monitor) {
+		g_object_unref (p->volume_monitor);
+		p->volume_monitor = NULL;
+	}
+
+	if (p->automount_idle_id != 0) {
+		g_source_remove (p->automount_idle_id);
+		p->automount_idle_id = 0;
+	}
+
+        if (p->settings != NULL) {
+                g_object_unref (p->settings);
+                p->settings = NULL;
+        }
+}
+
+static void
+gsd_automount_manager_set_property (GObject        *object,
+                                     guint           prop_id,
+                                     const GValue   *value,
+                                     GParamSpec     *pspec)
+{
+        GsdAutomountManager *self;
+
+        self = GSD_AUTOMOUNT_MANAGER (object);
+
+        switch (prop_id) {
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                break;
+        }
+}
+
+static void
+gsd_automount_manager_get_property (GObject        *object,
+                                     guint           prop_id,
+                                     GValue         *value,
+                                     GParamSpec     *pspec)
+{
+        GsdAutomountManager *self;
+
+        self = GSD_AUTOMOUNT_MANAGER (object);
+
+        switch (prop_id) {
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+                break;
+        }
+}
+
+static GObject *
+gsd_automount_manager_constructor (GType                  type,
+                                    guint                  n_construct_properties,
+                                    GObjectConstructParam *construct_properties)
+{
+        GsdAutomountManager      *gsd_automount_manager;
+        GsdAutomountManagerClass *klass;
+
+        klass = GSD_AUTOMOUNT_MANAGER_CLASS (g_type_class_peek (GSD_TYPE_AUTOMOUNT_MANAGER));
+
+        gsd_automount_manager = GSD_AUTOMOUNT_MANAGER (G_OBJECT_CLASS (gsd_automount_manager_parent_class)->constructor (type,
+                                                                                                                        n_construct_properties,
+                                                                                                                        construct_properties));
+
+        return G_OBJECT (gsd_automount_manager);
+}
+
+static void
+gsd_automount_manager_dispose (GObject *object)
+{
+        GsdAutomountManager *gsd_automount_manager;
+
+        gsd_automount_manager = GSD_AUTOMOUNT_MANAGER (object);
+
+        G_OBJECT_CLASS (gsd_automount_manager_parent_class)->dispose (object);
+}
+
+static void
+gsd_automount_manager_class_init (GsdAutomountManagerClass *klass)
+{
+        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
+
+        object_class->get_property = gsd_automount_manager_get_property;
+        object_class->set_property = gsd_automount_manager_set_property;
+        object_class->constructor = gsd_automount_manager_constructor;
+        object_class->dispose = gsd_automount_manager_dispose;
+        object_class->finalize = gsd_automount_manager_finalize;
+
+        g_type_class_add_private (klass, sizeof (GsdAutomountManagerPrivate));
+}
+
+static void
+gsd_automount_manager_init (GsdAutomountManager *manager)
+{
+        manager->priv = GSD_AUTOMOUNT_MANAGER_GET_PRIVATE (manager);
+}
+
+static void
+gsd_automount_manager_finalize (GObject *object)
+{
+        GsdAutomountManager *gsd_automount_manager;
+
+        g_return_if_fail (object != NULL);
+        g_return_if_fail (GSD_IS_AUTOMOUNT_MANAGER (object));
+
+        gsd_automount_manager = GSD_AUTOMOUNT_MANAGER (object);
+
+        g_return_if_fail (gsd_automount_manager->priv != NULL);
+
+        G_OBJECT_CLASS (gsd_automount_manager_parent_class)->finalize (object);
+}
+
+GsdAutomountManager *
+gsd_automount_manager_new (void)
+{
+        if (manager_object != NULL) {
+                g_object_ref (manager_object);
+        } else {
+                manager_object = g_object_new (GSD_TYPE_AUTOMOUNT_MANAGER, NULL);
+                g_object_add_weak_pointer (manager_object,
+                                           (gpointer *) &manager_object);
+        }
+
+        return GSD_AUTOMOUNT_MANAGER (manager_object);
+}
diff --git a/plugins/automount/gsd-automount-manager.h b/plugins/automount/gsd-automount-manager.h
new file mode 100644
index 0000000..e9e4155
--- /dev/null
+++ b/plugins/automount/gsd-automount-manager.h
@@ -0,0 +1,57 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Tomas Bzatek <tbzatek redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __GSD_AUTOMOUNT_MANAGER_H
+#define __GSD_AUTOMOUNT_MANAGER_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define GSD_TYPE_AUTOMOUNT_MANAGER         (gsd_automount_manager_get_type ())
+#define GSD_AUTOMOUNT_MANAGER(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_AUTOMOUNT_MANAGER, GsdAutomountManager))
+#define GSD_AUTOMOUNT_MANAGER_CLASS(k)     (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_AUTOMOUNT_MANAGER, GsdAutomountManagerClass))
+#define GSD_IS_AUTOMOUNT_MANAGER(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_AUTOMOUNT_MANAGER))
+#define GSD_IS_AUTOMOUNT_MANAGER_CLASS(k)  (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_AUTOMOUNT_MANAGER))
+#define GSD_AUTOMOUNT_MANAGER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_AUTOMOUNT_MANAGER, GsdAutomountManagerClass))
+
+typedef struct GsdAutomountManagerPrivate GsdAutomountManagerPrivate;
+
+typedef struct
+{
+        GObject                     parent;
+        GsdAutomountManagerPrivate *priv;
+} GsdAutomountManager;
+
+typedef struct
+{
+        GObjectClass   parent_class;
+} GsdAutomountManagerClass;
+
+GType                   gsd_automount_manager_get_type            (void);
+
+GsdAutomountManager *   gsd_automount_manager_new                 (void);
+gboolean                gsd_automount_manager_start               (GsdAutomountManager *manager,
+                                                                   GError              **error);
+void                    gsd_automount_manager_stop                (GsdAutomountManager *manager);
+
+G_END_DECLS
+
+#endif /* __GSD_AUTOMOUNT_MANAGER_H */
diff --git a/plugins/automount/gsd-automount-plugin.c b/plugins/automount/gsd-automount-plugin.c
new file mode 100644
index 0000000..ac80c7a
--- /dev/null
+++ b/plugins/automount/gsd-automount-plugin.c
@@ -0,0 +1,110 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Tomas Bzatek <tbzatek redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <glib/gi18n-lib.h>
+#include <gmodule.h>
+
+#include "gnome-settings-plugin.h"
+#include "gsd-automount-plugin.h"
+#include "gsd-automount-manager.h"
+
+struct GsdAutomountPluginPrivate {
+        GsdAutomountManager *manager;
+};
+
+#define GSD_AUTOMOUNT_PLUGIN_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN, GsdAutomountPluginPrivate))
+
+GNOME_SETTINGS_PLUGIN_REGISTER (GsdAutomountPlugin, gsd_automount_plugin)
+
+static void
+gsd_automount_plugin_init (GsdAutomountPlugin *plugin)
+{
+        plugin->priv = GSD_AUTOMOUNT_PLUGIN_GET_PRIVATE (plugin);
+
+        g_debug ("Automount plugin initializing");
+
+        plugin->priv->manager = gsd_automount_manager_new ();
+}
+
+static void
+gsd_automount_plugin_finalize (GObject *object)
+{
+        GsdAutomountPlugin *plugin;
+
+        g_return_if_fail (object != NULL);
+        g_return_if_fail (GSD_IS_AUTOMOUNT_PLUGIN (object));
+
+        g_debug ("Automount plugin finalizing");
+
+        plugin = GSD_AUTOMOUNT_PLUGIN (object);
+
+        g_return_if_fail (plugin->priv != NULL);
+
+        if (plugin->priv->manager != NULL) {
+                g_object_unref (plugin->priv->manager);
+        }
+
+        G_OBJECT_CLASS (gsd_automount_plugin_parent_class)->finalize (object);
+}
+
+static void
+impl_activate (GnomeSettingsPlugin *plugin)
+{
+        gboolean res;
+        GError  *error;
+
+        g_debug ("Activating automount plugin");
+
+        error = NULL;
+        res = gsd_automount_manager_start (GSD_AUTOMOUNT_PLUGIN (plugin)->priv->manager, &error);
+        if (! res) {
+                g_warning ("Unable to start automount manager: %s", error->message);
+                g_error_free (error);
+        }
+}
+
+static void
+impl_deactivate (GnomeSettingsPlugin *plugin)
+{
+        g_debug ("Deactivating automount plugin");
+        gsd_automount_manager_stop (GSD_AUTOMOUNT_PLUGIN (plugin)->priv->manager);
+}
+
+static void
+gsd_automount_plugin_class_init (GsdAutomountPluginClass *klass)
+{
+        GObjectClass             *object_class = G_OBJECT_CLASS (klass);
+        GnomeSettingsPluginClass *plugin_class = GNOME_SETTINGS_PLUGIN_CLASS (klass);
+
+        object_class->finalize = gsd_automount_plugin_finalize;
+
+        plugin_class->activate = impl_activate;
+        plugin_class->deactivate = impl_deactivate;
+
+        g_type_class_add_private (klass, sizeof (GsdAutomountPluginPrivate));
+}
+
+static void
+gsd_automount_plugin_class_finalize (GsdAutomountPluginClass *klass)
+{
+}
+
diff --git a/plugins/automount/gsd-automount-plugin.h b/plugins/automount/gsd-automount-plugin.h
new file mode 100644
index 0000000..50aaa6b
--- /dev/null
+++ b/plugins/automount/gsd-automount-plugin.h
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Tomas Bzatek <tbzatek redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __GSD_AUTOMOUNT_PLUGIN_H__
+#define __GSD_AUTOMOUNT_PLUGIN_H__
+
+#include <glib.h>
+#include <glib-object.h>
+#include <gmodule.h>
+
+#include "gnome-settings-plugin.h"
+
+G_BEGIN_DECLS
+
+#define GSD_TYPE_GSD_AUTOMOUNT_PLUGIN            (gsd_automount_plugin_get_type ())
+#define GSD_AUTOMOUNT_PLUGIN(o)                  (G_TYPE_CHECK_INSTANCE_CAST ((o), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN, GsdAutomountPlugin))
+#define GSD_AUTOMOUNT_PLUGIN_CLASS(k)            (G_TYPE_CHECK_CLASS_CAST((k), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN, GsdAutomountPluginClass))
+#define GSD_IS_AUTOMOUNT_PLUGIN(o)               (G_TYPE_CHECK_INSTANCE_TYPE ((o), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN))
+#define GSD_IS_AUTOMOUNT_PLUGIN_CLASS(k)         (G_TYPE_CHECK_CLASS_TYPE ((k), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN))
+#define GSD_AUTOMOUNT_PLUGIN_GET_CLASS(o)        (G_TYPE_INSTANCE_GET_CLASS ((o), GSD_TYPE_GSD_AUTOMOUNT_PLUGIN, GsdAutomountPluginClass))
+
+typedef struct GsdAutomountPluginPrivate GsdAutomountPluginPrivate;
+
+typedef struct
+{
+        GnomeSettingsPlugin           parent;
+        GsdAutomountPluginPrivate    *priv;
+} GsdAutomountPlugin;
+
+typedef struct
+{
+        GnomeSettingsPluginClass parent_class;
+} GsdAutomountPluginClass;
+
+GType   gsd_automount_plugin_get_type             (void) G_GNUC_CONST;
+
+/* All the plugins must implement this function */
+G_MODULE_EXPORT GType register_gnome_settings_plugin (GTypeModule *module);
+
+G_END_DECLS
+
+#endif /* __GSD_AUTOMOUNT_PLUGIN_H__ */
diff --git a/plugins/automount/nautilus-autorun.c b/plugins/automount/nautilus-autorun.c
new file mode 100644
index 0000000..82e2c0c
--- /dev/null
+++ b/plugins/automount/nautilus-autorun.c
@@ -0,0 +1,1532 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+
+/*
+ * Nautilus
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * Nautilus is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: David Zeuthen <davidz redhat com>
+ */
+ 
+#include <config.h>
+#include <string.h>
+#include <glib/gi18n.h>
+#include <gio/gio.h>
+#include <gtk/gtk.h>
+#include <gdk/gdkx.h>
+#include <X11/XKBlib.h>
+#include <gdk/gdkkeysyms.h>
+
+#include "nautilus-autorun.h"
+#include "nautilus-open-with-dialog.h"
+
+enum
+{
+	AUTORUN_ASK,
+	AUTORUN_IGNORE,
+	AUTORUN_APP,
+	AUTORUN_OPEN_FOLDER,
+	AUTORUN_SEP,
+	AUTORUN_OTHER_APP,
+};
+enum
+{
+	COLUMN_AUTORUN_PIXBUF,
+	COLUMN_AUTORUN_NAME,
+	COLUMN_AUTORUN_APP_INFO,
+	COLUMN_AUTORUN_X_CONTENT_TYPE,
+	COLUMN_AUTORUN_ITEM_TYPE,
+};
+
+static gboolean should_autorun_mount (GMount *mount);
+
+static void nautilus_autorun_rebuild_combo_box (GtkWidget *combo_box);
+
+
+
+GtkDialog *
+show_error_dialog (const char *primary_text,
+		   const char *secondary_text,
+		   GtkWindow *parent)
+{
+	GtkWidget *dialog;
+
+	dialog = gtk_message_dialog_new (parent,
+					 0,
+					 GTK_MESSAGE_ERROR,
+					 GTK_BUTTONS_OK,
+					 NULL);
+
+	g_object_set (dialog,
+		      "text", primary_text,
+		      "secondary-text", secondary_text,
+		      NULL);
+
+	gtk_widget_show (GTK_WIDGET (dialog));
+
+	g_signal_connect (dialog, "response",
+			  G_CALLBACK (gtk_widget_destroy), NULL);
+
+	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+
+	return GTK_DIALOG (dialog);
+}
+
+
+/**
+ * eel_g_strv_find
+ *
+ * Get index of string in array of strings.
+ *
+ * @strv: NULL-terminated array of strings.
+ * @find_me: string to search for
+ *
+ * Return value: index of array entry in @strv that
+ * matches @find_me, or -1 if no matching entry.
+ */
+static int
+eel_g_strv_find (char **strv, const char *find_me)
+{
+	int index;
+
+	g_return_val_if_fail (find_me != NULL, -1);
+
+	for (index = 0; strv[index] != NULL; ++index) {
+		if (strcmp (strv[index], find_me) == 0) {
+			return index;
+		}
+	}
+
+	return -1;
+}
+
+
+#define ICON_SIZE_STANDARD 48
+
+static gint
+get_icon_size_for_stock_size (GtkIconSize size)
+{
+	gint w, h;
+
+	if (gtk_icon_size_lookup (size, &w, &h)) {
+		return MAX (w, h);
+	}
+	return ICON_SIZE_STANDARD;
+}
+
+static GdkPixbuf *
+render_icon (GIcon *icon, gint icon_size)
+{
+	GdkPixbuf *pixbuf;
+	GtkIconInfo *info;
+
+	pixbuf = NULL;
+
+	if (G_IS_THEMED_ICON (icon)) {
+		gchar const * const *names;
+
+		info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
+				                       icon,
+				                       icon_size,
+				                       0);
+
+		g_print ("render_icon: info = %p\n", info);
+
+		if (info) {
+			pixbuf = gtk_icon_info_load_icon (info, NULL);
+		        gtk_icon_info_free (info);
+		}
+
+		if (pixbuf == NULL) {
+			names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+			pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+							   *names,
+							   icon_size,
+							   0, NULL);
+		}
+
+		g_print ("render_icon: pixbuf = %p, icon_size = %d\n", pixbuf, icon_size);
+	}
+	else
+	if (G_IS_FILE_ICON (icon)) {
+		GFile *icon_file;
+		gchar *path;
+
+		icon_file = g_file_icon_get_file (G_FILE_ICON (icon));
+		path = g_file_get_path (icon_file);
+		pixbuf = gdk_pixbuf_new_from_file_at_size (path,
+							   icon_size, icon_size, 
+							   NULL);
+		g_free (path);
+		g_object_unref (G_OBJECT (icon_file));
+	}
+
+	return pixbuf;
+}
+
+static void
+nautilus_autorun_get_preferences (const char *x_content_type,
+				  gboolean *pref_start_app,
+				  gboolean *pref_ignore,
+				  gboolean *pref_open_folder)
+{
+        GSettings *settings;
+	char **x_content_start_app;
+	char **x_content_ignore;
+	char **x_content_open_folder;
+
+	g_return_if_fail (pref_start_app != NULL);
+	g_return_if_fail (pref_ignore != NULL);
+	g_return_if_fail (pref_open_folder != NULL);
+
+	/*  FIXME: this is suboptimal, bind to some global object instead */
+        settings = g_settings_new ("org.gnome.media-handling");
+
+	*pref_start_app = FALSE;
+	*pref_ignore = FALSE;
+	*pref_open_folder = FALSE;
+	x_content_start_app = g_settings_get_strv (settings, "autorun-x-content-start-app");
+	x_content_ignore = g_settings_get_strv (settings, "autorun-x-content-ignore");
+	x_content_open_folder = g_settings_get_strv (settings, "autorun-x-content-open-folder");
+	if (x_content_start_app != NULL) {
+		*pref_start_app = eel_g_strv_find (x_content_start_app, x_content_type) != -1;
+	}
+	if (x_content_ignore != NULL) {
+		*pref_ignore = eel_g_strv_find (x_content_ignore, x_content_type) != -1;
+	}
+	if (x_content_open_folder != NULL) {
+		*pref_open_folder = eel_g_strv_find (x_content_open_folder, x_content_type) != -1;
+	}
+	g_strfreev (x_content_ignore);
+	g_strfreev (x_content_start_app);
+	g_strfreev (x_content_open_folder);
+        g_object_unref (settings);
+}
+
+static void
+remove_elem_from_str_array (char **v, const char *s)
+{
+	int n, m;
+
+	if (v == NULL) {
+		return;
+	}
+
+	for (n = 0; v[n] != NULL; n++) {
+		if (strcmp (v[n], s) == 0) {
+			for (m = n + 1; v[m] != NULL; m++) {
+				v[m - 1] = v[m];
+			}
+			v[m - 1] = NULL;
+			n--;
+		}
+	}
+}
+
+static char **
+add_elem_to_str_array (char **v, const char *s)
+{
+	guint len;
+	char **r;
+
+	len = v != NULL ? g_strv_length (v) : 0;
+	r = g_new0 (char *, len + 2);
+	memcpy (r, v, len * sizeof (char *));
+	r[len] = g_strdup (s);
+	r[len+1] = NULL;
+	g_free (v);
+
+	return r;
+}
+
+
+static void
+nautilus_autorun_set_preferences (const char *x_content_type,
+				  gboolean pref_start_app,
+				  gboolean pref_ignore,
+				  gboolean pref_open_folder)
+{
+        GSettings *settings;
+	char **x_content_start_app;
+	char **x_content_ignore;
+	char **x_content_open_folder;
+
+	g_assert (x_content_type != NULL);
+
+	/*  FIXME: this is suboptimal, bind to some global object instead */
+	settings = g_settings_new ("org.gnome.media-handling");
+
+	x_content_start_app = g_settings_get_strv (settings, "autorun-x-content-start-app");
+	x_content_ignore = g_settings_get_strv (settings, "autorun-x-content-ignore");
+	x_content_open_folder = g_settings_get_strv (settings, "autorun-x-content-open-folder");
+
+	remove_elem_from_str_array (x_content_start_app, x_content_type);
+	if (pref_start_app) {
+		x_content_start_app = add_elem_to_str_array (x_content_start_app, x_content_type);
+	}
+	g_settings_set_strv (settings, "autorun-x-content-start-app", (const gchar * const*) x_content_start_app);
+
+	remove_elem_from_str_array (x_content_ignore, x_content_type);
+	if (pref_ignore) {
+		x_content_ignore = add_elem_to_str_array (x_content_ignore, x_content_type);
+	}
+	g_settings_set_strv (settings, "autorun-x-content-ignore", (const gchar * const*) x_content_ignore);
+
+	remove_elem_from_str_array (x_content_open_folder, x_content_type);
+	if (pref_open_folder) {
+		x_content_open_folder = add_elem_to_str_array (x_content_open_folder, x_content_type);
+	}
+	g_settings_set_strv (settings, "autorun-x-content-open-folder", (const gchar * const*) x_content_open_folder);
+
+	g_strfreev (x_content_open_folder);
+	g_strfreev (x_content_ignore);
+	g_strfreev (x_content_start_app);
+        g_object_unref (settings);
+}
+
+static gboolean
+combo_box_separator_func (GtkTreeModel *model,
+                          GtkTreeIter *iter,
+                          gpointer data)
+{
+	char *str;
+
+	gtk_tree_model_get (model, iter,
+			    1, &str,
+			    -1);
+	if (str != NULL) {
+		g_free (str);
+		return FALSE;
+	}
+	return TRUE;
+}
+
+typedef struct
+{
+	guint changed_signal_id;
+	GtkWidget *combo_box;
+
+	char *x_content_type;
+	gboolean include_ask;
+	gboolean include_open_with_other_app;
+
+	gboolean update_settings;
+	NautilusAutorunComboBoxChanged changed_cb;
+	gpointer user_data;
+
+	gboolean other_application_selected;
+} NautilusAutorunComboBoxData;
+
+static void 
+nautilus_autorun_combobox_data_destroy (NautilusAutorunComboBoxData *data)
+{
+	/* signal handler may be automatically disconnected by destroying the widget */
+	if (g_signal_handler_is_connected (G_OBJECT (data->combo_box), data->changed_signal_id)) {
+		g_signal_handler_disconnect (G_OBJECT (data->combo_box), data->changed_signal_id);
+	}
+	g_free (data->x_content_type);
+	g_free (data);
+}
+
+static void
+other_application_selected (NautilusOpenWithDialog *dialog,
+			    GAppInfo *app_info,
+			    NautilusAutorunComboBoxData *data)
+{
+	if (data->changed_cb != NULL) {
+		data->changed_cb (TRUE, FALSE, FALSE, app_info, data->user_data);
+	}
+	if (data->update_settings) {
+		nautilus_autorun_set_preferences (data->x_content_type, TRUE, FALSE, FALSE);
+		g_app_info_set_as_default_for_type (app_info,
+						    data->x_content_type,
+						    NULL);
+		data->other_application_selected = TRUE;
+	}
+
+	/* rebuild so we include and select the new application in the list */
+	nautilus_autorun_rebuild_combo_box (data->combo_box);
+}
+
+static void
+handle_dialog_closure (NautilusAutorunComboBoxData *data)
+{
+	if (!data->other_application_selected) {
+		/* reset combo box so we don't linger on "Open with other Application..." */
+		nautilus_autorun_rebuild_combo_box (data->combo_box);
+	}
+}
+
+static void
+dialog_response_cb (GtkDialog *dialog,
+                    gint response,
+                    NautilusAutorunComboBoxData *data)
+{
+	handle_dialog_closure (data);
+}
+
+static void
+dialog_destroy_cb (GtkWidget *object,
+                   NautilusAutorunComboBoxData *data)
+{
+	handle_dialog_closure (data);
+}
+
+static void 
+combo_box_changed (GtkComboBox *combo_box,
+                   NautilusAutorunComboBoxData *data)
+{
+	GtkTreeIter iter;
+	GtkTreeModel *model;
+	GAppInfo *app_info;
+	char *x_content_type;
+	int type;
+
+	model = NULL;
+	app_info = NULL;
+	x_content_type = NULL;
+
+	if (!gtk_combo_box_get_active_iter (combo_box, &iter)) {
+		goto out;
+	}
+
+	model = gtk_combo_box_get_model (combo_box);
+	if (model == NULL) {
+		goto out;
+	}
+
+	gtk_tree_model_get (model, &iter, 
+			    COLUMN_AUTORUN_APP_INFO, &app_info,
+			    COLUMN_AUTORUN_X_CONTENT_TYPE, &x_content_type,
+			    COLUMN_AUTORUN_ITEM_TYPE, &type,
+			    -1);
+
+	switch (type) {
+	case AUTORUN_ASK:
+		if (data->changed_cb != NULL) {
+			data->changed_cb (TRUE, FALSE, FALSE, NULL, data->user_data);
+		}
+		if (data->update_settings) {
+			nautilus_autorun_set_preferences (x_content_type, FALSE, FALSE, FALSE);
+		}
+		break;
+	case AUTORUN_IGNORE:
+		if (data->changed_cb != NULL) {
+			data->changed_cb (FALSE, TRUE, FALSE, NULL, data->user_data);
+		}
+		if (data->update_settings) {
+			nautilus_autorun_set_preferences (x_content_type, FALSE, TRUE, FALSE);
+		}
+		break;
+	case AUTORUN_OPEN_FOLDER:
+		if (data->changed_cb != NULL) {
+			data->changed_cb (FALSE, FALSE, TRUE, NULL, data->user_data);
+		}
+		if (data->update_settings) {
+			nautilus_autorun_set_preferences (x_content_type, FALSE, FALSE, TRUE);
+		}
+		break;
+
+	case AUTORUN_APP:
+		if (data->changed_cb != NULL) {
+			/* TODO TODO?? */
+			data->changed_cb (TRUE, FALSE, FALSE, app_info, data->user_data);
+		}
+		if (data->update_settings) {
+			nautilus_autorun_set_preferences (x_content_type, TRUE, FALSE, FALSE);
+			g_app_info_set_as_default_for_type (app_info,
+							    x_content_type,
+							    NULL);
+		}
+		break;
+
+	case AUTORUN_OTHER_APP:
+	{
+		GtkWidget *dialog;
+
+		data->other_application_selected = FALSE;
+
+		dialog = nautilus_add_application_dialog_new (NULL, x_content_type);
+		gtk_window_set_transient_for (GTK_WINDOW (dialog),
+					      GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (combo_box))));
+		g_signal_connect (dialog, "application_selected",
+				  G_CALLBACK (other_application_selected),
+				  data);
+		g_signal_connect (dialog, "response",
+				  G_CALLBACK (dialog_response_cb), data);
+		g_signal_connect (dialog, "destroy",
+				  G_CALLBACK (dialog_destroy_cb), data);
+		gtk_widget_show (GTK_WIDGET (dialog));
+
+		break;
+	}
+
+	}
+ 
+out:
+	if (app_info != NULL) {
+		g_object_unref (app_info);
+	}
+	g_free (x_content_type);
+}
+
+static void
+nautilus_autorun_rebuild_combo_box (GtkWidget *combo_box)
+{
+	NautilusAutorunComboBoxData *data;
+	char *x_content_type;
+
+	data = g_object_get_data (G_OBJECT (combo_box), "nautilus_autorun_combobox_data");
+	if (data == NULL) {
+		g_warning ("no 'nautilus_autorun_combobox_data' data!");
+		return;
+	}
+
+	x_content_type = g_strdup (data->x_content_type);
+	nautilus_autorun_prepare_combo_box (combo_box,
+					    x_content_type,
+					    data->include_ask,
+					    data->include_open_with_other_app,
+					    data->update_settings,
+					    data->changed_cb,
+					    data->user_data);
+	g_free (x_content_type);
+}
+
+/* TODO: we need some kind of way to remove user-defined associations,
+ * e.g. the result of "Open with other Application...".
+ *
+ * However, this is a bit hard as
+ * g_app_info_can_remove_supports_type() will always return TRUE
+ * because we now have [Removed Applications] in the file
+ * ~/.local/share/applications/mimeapps.list.
+ *
+ * We need the API outlined in
+ *
+ *  http://bugzilla.gnome.org/show_bug.cgi?id=545350
+ *
+ * to do this.
+ *
+ * Now, there's also the question about what the UI would look like
+ * given this API. Ideally we'd include a small button on the right
+ * side of the combo box that the user can press to delete an
+ * association, e.g.:
+ *
+ *  +-------------------------------------+
+ *  | Ask what to do                      |
+ *  | Do Nothing                          |
+ *  | Open Folder                         |
+ *  +-------------------------------------+
+ *  | Open Rhythmbox Music Player         |
+ *  | Open Audio CD Extractor             |
+ *  | Open Banshee Media Player           |
+ *  | Open Frobnicator App            [x] |
+ *  +-------------------------------------+
+ *  | Open with other Application...      |
+ *  +-------------------------------------+
+ *
+ * where "Frobnicator App" have been set up using "Open with other
+ * Application...". However this is not accessible (which is a
+ * GTK+ issue) but probably not a big deal.
+ *
+ * And we only want show these buttons (e.g. [x]) for associations with
+ * GAppInfo instances that are deletable.
+ */
+
+void
+nautilus_autorun_prepare_combo_box (GtkWidget *combo_box,
+				    const char *x_content_type,
+				    gboolean include_ask,
+				    gboolean include_open_with_other_app,
+				    gboolean update_settings,
+				    NautilusAutorunComboBoxChanged changed_cb,
+				    gpointer user_data)
+{
+	GList *l;
+	GList *app_info_list;
+	GAppInfo *default_app_info;
+	GtkListStore *list_store;
+	GtkTreeIter iter;
+	GdkPixbuf *pixbuf;
+	int icon_size;
+	int set_active;
+	int n;
+	int num_apps;
+	gboolean pref_ask;
+	gboolean pref_start_app;
+	gboolean pref_ignore;
+	gboolean pref_open_folder;
+	NautilusAutorunComboBoxData *data;
+	GtkCellRenderer *renderer;
+	gboolean new_data;
+
+	nautilus_autorun_get_preferences (x_content_type, &pref_start_app, &pref_ignore, &pref_open_folder);
+	pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;
+
+	icon_size = get_icon_size_for_stock_size (GTK_ICON_SIZE_MENU);
+
+	set_active = -1;
+	data = NULL;
+	new_data = TRUE;
+
+	app_info_list = g_app_info_get_all_for_type (x_content_type);
+	default_app_info = g_app_info_get_default_for_type (x_content_type, FALSE);
+	num_apps = g_list_length (app_info_list);
+
+	list_store = gtk_list_store_new (5,
+					 GDK_TYPE_PIXBUF,
+					 G_TYPE_STRING,
+					 G_TYPE_APP_INFO,
+					 G_TYPE_STRING,
+					 G_TYPE_INT);
+
+	/* no apps installed */
+	if (num_apps == 0) {
+		gtk_list_store_append (list_store, &iter);
+		pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						   GTK_STOCK_DIALOG_ERROR,
+						   icon_size,
+						   0,
+						   NULL);
+
+		/* TODO: integrate with PackageKit-gnome to find applications */
+
+		gtk_list_store_set (list_store, &iter, 
+				    COLUMN_AUTORUN_PIXBUF, pixbuf, 
+				    COLUMN_AUTORUN_NAME, _("No applications found"), 
+				    COLUMN_AUTORUN_APP_INFO, NULL, 
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK,
+				    -1);
+		g_object_unref (pixbuf);
+	} else {	
+		if (include_ask) {
+			gtk_list_store_append (list_store, &iter);
+			pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+							   GTK_STOCK_DIALOG_QUESTION,
+							   icon_size,
+							   0,
+							   NULL);
+			gtk_list_store_set (list_store, &iter, 
+					    COLUMN_AUTORUN_PIXBUF, pixbuf, 
+					    COLUMN_AUTORUN_NAME, _("Ask what to do"), 
+					    COLUMN_AUTORUN_APP_INFO, NULL, 
+					    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+					    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_ASK,
+					    -1);
+			g_object_unref (pixbuf);
+		}
+		
+		gtk_list_store_append (list_store, &iter);
+		pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						   GTK_STOCK_CLOSE,
+						   icon_size,
+						   0,
+						   NULL);
+		gtk_list_store_set (list_store, &iter, 
+				    COLUMN_AUTORUN_PIXBUF, pixbuf, 
+				    COLUMN_AUTORUN_NAME, _("Do Nothing"), 
+				    COLUMN_AUTORUN_APP_INFO, NULL, 
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_IGNORE,
+				    -1);
+		g_object_unref (pixbuf);		
+
+		gtk_list_store_append (list_store, &iter);
+		pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						   "folder-open",
+						   icon_size,
+						   0,
+						   NULL);
+		gtk_list_store_set (list_store, &iter, 
+				    COLUMN_AUTORUN_PIXBUF, pixbuf, 
+				    COLUMN_AUTORUN_NAME, _("Open Folder"), 
+				    COLUMN_AUTORUN_APP_INFO, NULL, 
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OPEN_FOLDER,
+				    -1);
+		g_object_unref (pixbuf);		
+
+		gtk_list_store_append (list_store, &iter);
+		gtk_list_store_set (list_store, &iter, 
+				    COLUMN_AUTORUN_PIXBUF, NULL, 
+				    COLUMN_AUTORUN_NAME, NULL, 
+				    COLUMN_AUTORUN_APP_INFO, NULL, 
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, NULL,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_SEP,
+				    -1);
+
+		for (l = app_info_list, n = include_ask ? 4 : 3; l != NULL; l = l->next, n++) {
+			GIcon *icon;
+			
+			char *open_string;
+			GAppInfo *app_info = l->data;
+			
+			/* we deliberately ignore should_show because some apps might want
+			 * to install special handlers that should be hidden in the regular
+			 * application launcher menus
+			 */
+			
+			icon = g_app_info_get_icon (app_info);
+			pixbuf = render_icon (icon, icon_size);
+			
+			open_string = g_strdup_printf (_("Open %s"), g_app_info_get_display_name (app_info));
+
+			gtk_list_store_append (list_store, &iter);
+			gtk_list_store_set (list_store, &iter,
+					    COLUMN_AUTORUN_PIXBUF, pixbuf,
+					    COLUMN_AUTORUN_NAME, open_string,
+					    COLUMN_AUTORUN_APP_INFO, app_info,
+					    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+					    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_APP,
+					    -1);
+			if (pixbuf != NULL) {
+				g_object_unref (pixbuf);
+			}
+			g_free (open_string);
+			
+			if (g_app_info_equal (app_info, default_app_info)) {
+				set_active = n;
+			}
+		}
+	}
+
+	if (include_open_with_other_app) {
+		gtk_list_store_append (list_store, &iter);
+		gtk_list_store_set (list_store, &iter,
+				    COLUMN_AUTORUN_PIXBUF, NULL,
+				    COLUMN_AUTORUN_NAME, NULL,
+				    COLUMN_AUTORUN_APP_INFO, NULL,
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, NULL,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_SEP,
+				    -1);
+
+		gtk_list_store_append (list_store, &iter);
+		pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						   "application-x-executable",
+						   icon_size,
+						   0,
+						   NULL);
+		gtk_list_store_set (list_store, &iter,
+				    COLUMN_AUTORUN_PIXBUF, pixbuf,
+				    COLUMN_AUTORUN_NAME, _("Open with other Application..."),
+				    COLUMN_AUTORUN_APP_INFO, NULL,
+				    COLUMN_AUTORUN_X_CONTENT_TYPE, x_content_type,
+				    COLUMN_AUTORUN_ITEM_TYPE, AUTORUN_OTHER_APP,
+				    -1);
+		g_object_unref (pixbuf);
+	}
+
+	if (default_app_info != NULL) {
+		g_object_unref (default_app_info);
+	}
+	g_list_foreach (app_info_list, (GFunc) g_object_unref, NULL);
+	g_list_free (app_info_list);
+
+	gtk_combo_box_set_model (GTK_COMBO_BOX (combo_box), GTK_TREE_MODEL (list_store));
+	g_object_unref (G_OBJECT (list_store));
+
+	gtk_cell_layout_clear (GTK_CELL_LAYOUT (combo_box));
+
+	renderer = gtk_cell_renderer_pixbuf_new ();
+	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, FALSE);
+	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
+					"pixbuf", COLUMN_AUTORUN_PIXBUF,
+					NULL);
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
+	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), renderer,
+					"text", COLUMN_AUTORUN_NAME,
+					NULL);
+	gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo_box), combo_box_separator_func, NULL, NULL);
+
+	if (num_apps == 0) {
+		gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
+		gtk_widget_set_sensitive (combo_box, FALSE);
+	} else {
+		gtk_widget_set_sensitive (combo_box, TRUE);
+		if (pref_ask && include_ask) {
+			gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
+		} else if (pref_ignore) {
+			gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), include_ask ? 1 : 0);
+		} else if (pref_open_folder) {
+			gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), include_ask ? 2 : 1);
+		} else if (set_active != -1) {
+			gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), set_active);
+		} else {
+			gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), include_ask ? 1 : 0);
+		}
+
+		/* See if we have an old data around */
+		data = g_object_get_data (G_OBJECT (combo_box), "nautilus_autorun_combobox_data");
+		if (data) {
+			new_data = FALSE;
+			g_free (data->x_content_type);
+		} else {
+			data = g_new0 (NautilusAutorunComboBoxData, 1);
+		}
+	
+		data->x_content_type = g_strdup (x_content_type);
+		data->include_ask = include_ask;
+		data->include_open_with_other_app = include_open_with_other_app;
+		data->update_settings = update_settings;
+		data->changed_cb = changed_cb;
+		data->user_data = user_data;
+		data->combo_box = combo_box;
+		if (data->changed_signal_id == 0) {
+			data->changed_signal_id = g_signal_connect (G_OBJECT (combo_box),
+								    "changed",
+								    G_CALLBACK (combo_box_changed),
+								    data);
+		}
+	}
+
+	if (new_data) {
+		g_object_set_data_full (G_OBJECT (combo_box),
+					"nautilus_autorun_combobox_data",
+					data,
+					(GDestroyNotify) nautilus_autorun_combobox_data_destroy);
+	}
+}
+
+static gboolean
+is_shift_pressed (void)
+{
+	gboolean ret;
+	XkbStateRec state;
+	Bool status;
+
+	ret = FALSE;
+
+        gdk_error_trap_push ();
+	status = XkbGetState (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
+			      XkbUseCoreKbd, &state);
+        gdk_error_trap_pop_ignored ();
+
+	if (status == Success) {
+		ret = state.mods & ShiftMask;
+	}
+
+	return ret;
+}
+
+enum {
+	AUTORUN_DIALOG_RESPONSE_EJECT = 0
+};
+
+typedef struct
+{
+	GtkWidget *dialog;
+
+	GMount *mount;
+	gboolean should_eject;
+
+	gboolean selected_ignore;
+	gboolean selected_open_folder;
+	GAppInfo *selected_app;
+
+	gboolean remember;
+
+	char *x_content_type;
+
+	NautilusAutorunOpenWindow open_window_func;
+	gpointer user_data;
+} AutorunDialogData;
+
+
+void
+nautilus_autorun_launch_for_mount (GMount *mount, GAppInfo *app_info)
+{
+	GFile *root;
+	GdkAppLaunchContext *launch_context;
+	GError *error;
+	gboolean result;
+	GList *list;
+	gchar *uri_scheme;
+	gchar *uri;
+
+	root = g_mount_get_root (mount);
+	list = g_list_append (NULL, root);
+
+	launch_context = gdk_app_launch_context_new ();
+
+	error = NULL;
+	result = g_app_info_launch (app_info,
+				    list,
+				    G_APP_LAUNCH_CONTEXT (launch_context),
+				    &error);
+
+	g_object_unref (launch_context);
+
+	if (!result) {
+		if (error->domain == G_IO_ERROR &&
+		    error->code == G_IO_ERROR_NOT_SUPPORTED) {
+			uri = g_file_get_uri (root);
+			uri_scheme = g_uri_parse_scheme (uri);
+			
+			/* FIXME: Present user a dialog to choose another app when the last one failed to handle a file */
+			g_warning ("Cannot open location: %s\n", error->message);
+
+			g_free (uri_scheme);
+			g_free (uri);
+		} else {
+			g_warning ("Cannot open app: %s\n", error->message);
+		}
+		g_error_free (error);
+	}
+
+	g_list_free (list);
+	g_object_unref (root);
+}
+
+static void autorun_dialog_mount_unmounted (GMount *mount, AutorunDialogData *data);
+
+static void
+autorun_dialog_destroy (AutorunDialogData *data)
+{
+	g_signal_handlers_disconnect_by_func (G_OBJECT (data->mount),
+					      G_CALLBACK (autorun_dialog_mount_unmounted),
+					      data);
+
+	gtk_widget_destroy (GTK_WIDGET (data->dialog));
+	if (data->selected_app != NULL) {
+		g_object_unref (data->selected_app);
+	}
+	g_object_unref (data->mount);
+	g_free (data->x_content_type);
+	g_free (data);
+}
+
+static void
+autorun_dialog_mount_unmounted (GMount *mount, AutorunDialogData *data)
+{
+	/* remove the dialog if the media is unmounted */
+	autorun_dialog_destroy (data);
+}
+
+static void
+unmount_mount_callback (GObject *source_object,
+			GAsyncResult *res,
+			gpointer user_data)
+{
+	GError *error;
+	char *primary;
+	gboolean unmounted;
+	gboolean should_eject;
+	GtkWidget *dialog;
+
+
+	should_eject = user_data != NULL;
+
+	error = NULL;
+	if (should_eject) {
+		unmounted = g_mount_eject_with_operation_finish (G_MOUNT (source_object),
+								 res, &error);
+	} else {
+		unmounted = g_mount_unmount_with_operation_finish (G_MOUNT (source_object),
+								   res, &error);
+	}
+	
+	if (! unmounted) {
+		if (error->code != G_IO_ERROR_FAILED_HANDLED) {
+			if (should_eject) {
+				primary = g_strdup_printf (_("Unable to eject %p"), source_object);
+			} else {
+				primary = g_strdup_printf (_("Unable to unmount %p"), source_object);
+			}
+
+			dialog = gtk_message_dialog_new (NULL,
+							 0,
+							 GTK_MESSAGE_INFO,
+							 GTK_BUTTONS_OK,
+							 "%s",
+							 primary);
+			gtk_message_dialog_format_secondary_markup (GTK_MESSAGE_DIALOG (dialog), error->message);
+			
+			gtk_widget_show (GTK_WIDGET (dialog));
+			g_signal_connect (dialog, "response",
+					  G_CALLBACK (gtk_widget_destroy), NULL);
+			g_free (primary);
+		}
+	}
+
+	if (error != NULL) {
+		g_error_free (error);
+	}
+}
+
+static void
+do_unmount (GMount *mount, gboolean should_eject, GtkWindow *window)
+{
+	GMountOperation *mount_op;
+
+	mount_op = gtk_mount_operation_new (window);
+	if (should_eject) {
+		g_mount_eject_with_operation (mount,
+					      0,
+					      mount_op,
+					      NULL,
+					      unmount_mount_callback,
+					      (gpointer) 1);
+	} else {
+		g_mount_unmount_with_operation (mount,
+						0,
+						mount_op,
+						NULL,
+						unmount_mount_callback,
+						(gpointer) 0);
+	}
+	g_object_unref (mount_op);
+}
+
+static void
+autorun_dialog_response (GtkDialog *dialog, gint response, AutorunDialogData *data)
+{
+	switch (response) {
+	case AUTORUN_DIALOG_RESPONSE_EJECT:
+		do_unmount (data->mount, data->should_eject, GTK_WINDOW (dialog));
+		break;
+
+	case GTK_RESPONSE_NONE:
+		/* window was closed */
+		break;
+	case GTK_RESPONSE_CANCEL:
+		break;
+	case GTK_RESPONSE_OK:
+		/* do the selected action */
+
+		if (data->remember) {
+			/* make sure we don't ask again */
+			nautilus_autorun_set_preferences (data->x_content_type, TRUE, data->selected_ignore, data->selected_open_folder);
+			if (!data->selected_ignore && !data->selected_open_folder && data->selected_app != NULL) {
+				g_app_info_set_as_default_for_type (data->selected_app,
+								    data->x_content_type,
+								    NULL);
+			}
+		} else {
+			/* make sure we do ask again */
+			nautilus_autorun_set_preferences (data->x_content_type, FALSE, FALSE, FALSE);
+		}
+
+		if (!data->selected_ignore && !data->selected_open_folder && data->selected_app != NULL) {
+			nautilus_autorun_launch_for_mount (data->mount, data->selected_app);
+		} else if (!data->selected_ignore && data->selected_open_folder) {
+			if (data->open_window_func != NULL)
+				data->open_window_func (data->mount, data->user_data);
+		}
+		break;
+	}
+
+	autorun_dialog_destroy (data);
+}
+
+static void
+autorun_combo_changed (gboolean selected_ask,
+		       gboolean selected_ignore,
+		       gboolean selected_open_folder,
+		       GAppInfo *selected_app,
+		       gpointer user_data)
+{
+	AutorunDialogData *data = user_data;
+
+	if (data->selected_app != NULL) {
+		g_object_unref (data->selected_app);
+	}
+	data->selected_app = selected_app != NULL ? g_object_ref (selected_app) : NULL;
+	data->selected_ignore = selected_ignore;
+	data->selected_open_folder = selected_open_folder;
+}
+
+
+static void
+autorun_always_toggled (GtkToggleButton *togglebutton, AutorunDialogData *data)
+{
+	data->remember = gtk_toggle_button_get_active (togglebutton);
+}
+
+static gboolean
+combo_box_enter_ok (GtkWidget *togglebutton, GdkEventKey *event, GtkDialog *dialog)
+{
+	if (event->keyval == GDK_KEY_KP_Enter || event->keyval == GDK_KEY_Return) {
+		gtk_dialog_response (dialog, GTK_RESPONSE_OK);
+		return TRUE;
+	}
+	return FALSE;
+}
+
+/* returns TRUE if a folder window should be opened */
+static gboolean
+do_autorun_for_content_type (GMount *mount, const char *x_content_type, NautilusAutorunOpenWindow open_window_func, gpointer user_data)
+{
+	AutorunDialogData *data;
+	GtkWidget *dialog;
+	GtkWidget *hbox;
+	GtkWidget *vbox;
+	GtkWidget *label;
+	GtkWidget *combo_box;
+	GtkWidget *always_check_button;
+	GtkWidget *eject_button;
+	GtkWidget *image;
+	char *markup;
+	char *content_description;
+	char *mount_name;
+	GIcon *icon;
+	GdkPixbuf *pixbuf;
+	int icon_size;
+	gboolean user_forced_dialog;
+	gboolean pref_ask;
+	gboolean pref_start_app;
+	gboolean pref_ignore;
+	gboolean pref_open_folder;
+	char *media_greeting;
+	gboolean ret;
+
+	ret = FALSE;
+	mount_name = NULL;
+
+	g_print ("do_autorun_for_content_type: x_content_type = '%s'\n", x_content_type);
+
+	if (g_content_type_is_a (x_content_type, "x-content/win32-software")) {
+		/* don't pop up the dialog anyway if the content type says
+ 		 * windows software.
+ 		 */
+		goto out;
+	}
+
+	user_forced_dialog = is_shift_pressed ();
+
+	nautilus_autorun_get_preferences (x_content_type, &pref_start_app, &pref_ignore, &pref_open_folder);
+	pref_ask = !pref_start_app && !pref_ignore && !pref_open_folder;
+
+	if (user_forced_dialog) {
+		goto show_dialog;
+	}
+
+	if (!pref_ask && !pref_ignore && !pref_open_folder) {
+		GAppInfo *app_info;
+		app_info = g_app_info_get_default_for_type (x_content_type, FALSE);
+		if (app_info != NULL) {
+			nautilus_autorun_launch_for_mount (mount, app_info);
+		}
+		goto out;
+	}
+
+	if (pref_open_folder) {
+		ret = TRUE;
+		goto out;
+	}
+
+	if (pref_ignore) {
+		goto out;
+	}
+
+show_dialog:
+
+	mount_name = g_mount_get_name (mount);
+
+	dialog = gtk_dialog_new ();
+
+	hbox = gtk_hbox_new (FALSE, 12);
+	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), hbox, TRUE, TRUE, 0);
+	gtk_container_set_border_width (GTK_CONTAINER (hbox), 12);
+
+	icon = g_mount_get_icon (mount);
+	g_print ("icon = %p\n", icon);
+	icon_size = get_icon_size_for_stock_size (GTK_ICON_SIZE_DIALOG);
+	g_print ("icon_size = %d\n", icon_size);
+	image = gtk_image_new_from_gicon (icon, GTK_ICON_SIZE_DIALOG);
+	pixbuf = render_icon (icon, icon_size);
+	g_print ("pixbuf = %p\n", pixbuf);
+	gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
+	gtk_box_pack_start (GTK_BOX (hbox), image, TRUE, TRUE, 0);
+	/* also use the icon on the dialog */
+	gtk_window_set_title (GTK_WINDOW (dialog), mount_name);
+	/* FIXME: icon is not shown for unknown reason */
+	gtk_window_set_icon (GTK_WINDOW (dialog), pixbuf);
+	gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
+	g_object_unref (icon);
+	if (pixbuf) {
+		g_object_unref (pixbuf);
+	}
+	vbox = gtk_vbox_new (FALSE, 12);
+	gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
+
+	label = gtk_label_new (NULL);
+
+
+	/* Customize greeting for well-known x-content types */
+	if (strcmp (x_content_type, "x-content/audio-cdda") == 0) {
+		media_greeting = _("You have just inserted an Audio CD.");
+	} else if (strcmp (x_content_type, "x-content/audio-dvd") == 0) {
+		media_greeting = _("You have just inserted an Audio DVD.");
+	} else if (strcmp (x_content_type, "x-content/video-dvd") == 0) {
+		media_greeting = _("You have just inserted a Video DVD.");
+	} else if (strcmp (x_content_type, "x-content/video-vcd") == 0) {
+		media_greeting = _("You have just inserted a Video CD.");
+	} else if (strcmp (x_content_type, "x-content/video-svcd") == 0) {
+		media_greeting = _("You have just inserted a Super Video CD.");
+	} else if (strcmp (x_content_type, "x-content/blank-cd") == 0) {
+		media_greeting = _("You have just inserted a blank CD.");
+	} else if (strcmp (x_content_type, "x-content/blank-dvd") == 0) {
+		media_greeting = _("You have just inserted a blank DVD.");
+	} else if (strcmp (x_content_type, "x-content/blank-cd") == 0) {
+		media_greeting = _("You have just inserted a blank Blu-Ray disc.");
+	} else if (strcmp (x_content_type, "x-content/blank-cd") == 0) {
+		media_greeting = _("You have just inserted a blank HD DVD.");
+	} else if (strcmp (x_content_type, "x-content/image-photocd") == 0) {
+		media_greeting = _("You have just inserted a Photo CD.");
+	} else if (strcmp (x_content_type, "x-content/image-picturecd") == 0) {
+		media_greeting = _("You have just inserted a Picture CD.");
+	} else if (strcmp (x_content_type, "x-content/image-dcf") == 0) {
+		media_greeting = _("You have just inserted a medium with digital photos.");
+	} else if (strcmp (x_content_type, "x-content/audio-player") == 0) {
+		media_greeting = _("You have just inserted a digital audio player.");
+	} else if (g_content_type_is_a (x_content_type, "x-content/software")) {
+		media_greeting = _("You have just inserted a medium with software intended to be automatically started.");
+	} else {
+		/* fallback to generic greeting */
+		media_greeting = _("You have just inserted a medium.");
+	}
+	markup = g_strdup_printf ("<big><b>%s %s</b></big>", media_greeting, _("Choose what application to launch."));
+	gtk_label_set_markup (GTK_LABEL (label), markup);
+	g_free (markup);
+	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+	gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
+
+	label = gtk_label_new (NULL);
+	content_description = g_content_type_get_description (x_content_type);
+	markup = g_strdup_printf (_("Select how to open \"%s\" and whether to perform this action in the future for other media of type \"%s\"."), mount_name, content_description);
+	g_free (content_description);
+	gtk_label_set_markup (GTK_LABEL (label), markup);
+	g_free (markup);
+	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
+	gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
+	gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
+	
+	data = g_new0 (AutorunDialogData, 1);
+	data->dialog = dialog;
+	data->mount = g_object_ref (mount);
+	data->remember = !pref_ask;
+	data->selected_ignore = pref_ignore;
+	data->x_content_type = g_strdup (x_content_type);
+	data->selected_app = g_app_info_get_default_for_type (x_content_type, FALSE);
+	data->open_window_func = open_window_func;
+	data->user_data = user_data;
+
+	combo_box = gtk_combo_box_new ();
+	nautilus_autorun_prepare_combo_box (combo_box, x_content_type, FALSE, TRUE, FALSE, autorun_combo_changed, data);
+	g_signal_connect (G_OBJECT (combo_box),
+			  "key-press-event",
+			  G_CALLBACK (combo_box_enter_ok),
+			  dialog);
+
+	gtk_box_pack_start (GTK_BOX (vbox), combo_box, TRUE, TRUE, 0);
+
+	always_check_button = gtk_check_button_new_with_mnemonic (_("_Always perform this action"));
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (always_check_button), data->remember);
+	g_signal_connect (G_OBJECT (always_check_button),
+			  "toggled",
+			  G_CALLBACK (autorun_always_toggled),
+			  data);
+	gtk_box_pack_start (GTK_BOX (vbox), always_check_button, TRUE, TRUE, 0);
+
+	gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
+				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+				GTK_STOCK_OK, GTK_RESPONSE_OK,
+				NULL);
+	gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+	
+	if (g_mount_can_eject (mount)) {
+		GtkWidget *eject_image;
+		eject_button = gtk_button_new_with_mnemonic (_("_Eject"));
+		pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+						   "media-eject",
+						   get_icon_size_for_stock_size (GTK_ICON_SIZE_BUTTON),
+						   0,
+						   NULL);
+		eject_image = gtk_image_new_from_pixbuf (pixbuf);
+		g_object_unref (pixbuf);
+		gtk_button_set_image (GTK_BUTTON (eject_button), eject_image);
+		data->should_eject = TRUE;
+	} else {
+		eject_button = gtk_button_new_with_mnemonic (_("_Unmount"));
+		data->should_eject = FALSE;
+	}
+	gtk_dialog_add_action_widget (GTK_DIALOG (dialog), eject_button, AUTORUN_DIALOG_RESPONSE_EJECT);
+	gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (gtk_dialog_get_action_area (GTK_DIALOG (dialog))), eject_button, TRUE);
+
+	/* show the dialog */
+	gtk_widget_show_all (dialog);
+
+	g_signal_connect (G_OBJECT (dialog),
+			  "response",
+			  G_CALLBACK (autorun_dialog_response),
+			  data);
+
+	g_signal_connect (G_OBJECT (data->mount),
+			  "unmounted",
+			  G_CALLBACK (autorun_dialog_mount_unmounted),
+			  data);
+
+out:
+	g_free (mount_name);
+	return ret;
+}
+
+typedef struct {
+	GMount *mount;
+	NautilusAutorunOpenWindow open_window_func;
+	gpointer user_data;
+	GSettings *settings;
+} AutorunData;
+
+static void
+autorun_guessed_content_type_callback (GObject *source_object,
+				       GAsyncResult *res,
+				       gpointer user_data)
+{
+	GError *error;
+	char **guessed_content_type;
+	AutorunData *data = user_data;
+	gboolean open_folder;
+
+	open_folder = FALSE;
+
+	error = NULL;
+	guessed_content_type = g_mount_guess_content_type_finish (G_MOUNT (source_object), res, &error);
+	g_object_set_data_full (source_object,
+				"nautilus-content-type-cache",
+				g_strdupv (guessed_content_type),
+				(GDestroyNotify)g_strfreev);
+	if (error != NULL) {
+		g_warning ("Unable to guess content type for mount: %s", error->message);
+		g_error_free (error);
+	} else {
+		g_print ("guessed_content_type = %p\n", guessed_content_type);
+		if (guessed_content_type != NULL && g_strv_length (guessed_content_type) > 0) {
+			int n;
+			for (n = 0; guessed_content_type[n] != NULL; n++) {
+				if (do_autorun_for_content_type (data->mount, guessed_content_type[n], 
+								 data->open_window_func, data->user_data)) {
+					open_folder = TRUE;
+				}
+			}
+			g_strfreev (guessed_content_type);
+		} else {
+			if (g_settings_get_boolean (data->settings, "automount-open")) {
+				open_folder = TRUE;
+			}
+		}
+	}
+
+	/* only open the folder once.. */
+	if (open_folder && data->open_window_func != NULL) {
+		data->open_window_func (data->mount, data->user_data);
+	}
+
+	g_object_unref (data->mount);
+	g_object_unref (data->settings);
+	g_free (data);
+}
+
+void
+nautilus_autorun (GMount *mount, GSettings *settings, NautilusAutorunOpenWindow open_window_func, gpointer user_data)
+{
+	AutorunData *data;
+
+	if (!should_autorun_mount (mount) ||
+	    g_settings_get_boolean (settings, "autorun-never")) {
+		g_print ("Shouldn't automount || autorun-never!\n");
+		return;
+	}
+
+	data = g_new0 (AutorunData, 1);
+	data->mount = g_object_ref (mount);
+	data->open_window_func = open_window_func;
+	data->user_data = user_data;
+	data->settings = g_object_ref (settings);
+
+	g_mount_guess_content_type (mount,
+				    FALSE,
+				    NULL,
+				    autorun_guessed_content_type_callback,
+				    data);
+}
+
+typedef struct {
+	NautilusAutorunGetContent callback;
+	gpointer user_data;
+} GetContentTypesData;
+
+static void
+get_types_cb (GObject *source_object,
+	      GAsyncResult *res,
+	      gpointer user_data)
+{
+	GetContentTypesData *data;
+	char **types;
+
+	data = user_data;
+	types = g_mount_guess_content_type_finish (G_MOUNT (source_object), res, NULL);
+
+	g_object_set_data_full (source_object,
+				"nautilus-content-type-cache",
+				g_strdupv (types),
+				(GDestroyNotify)g_strfreev);
+
+	if (data->callback) {
+		data->callback (types, data->user_data);
+	}
+	g_strfreev (types);
+	g_free (data);
+}
+
+void
+nautilus_autorun_get_x_content_types_for_mount_async (GMount *mount,
+						      NautilusAutorunGetContent callback,
+						      GCancellable *cancellable,
+						      gpointer user_data)
+{
+	char **cached;
+	GetContentTypesData *data;
+	
+	if (mount == NULL) {
+		if (callback) {
+			callback (NULL, user_data);
+		}
+		return;
+	}
+
+	cached = g_object_get_data (G_OBJECT (mount), "nautilus-content-type-cache");
+	if (cached != NULL) {
+		if (callback) {
+			callback (cached, user_data);
+		}
+		return;
+	}
+
+	data = g_new (GetContentTypesData, 1);
+	data->callback = callback;
+	data->user_data = user_data;
+
+	g_mount_guess_content_type (mount,
+				    FALSE,
+				    cancellable,
+				    get_types_cb,
+				    data);
+}
+
+
+char **
+nautilus_autorun_get_cached_x_content_types_for_mount (GMount      *mount)
+{
+	char **cached;
+	
+	if (mount == NULL) {
+		return NULL;
+	}
+
+	cached = g_object_get_data (G_OBJECT (mount), "nautilus-content-type-cache");
+	if (cached != NULL) {
+		return g_strdupv (cached);
+	}
+
+	return NULL;
+}
+
+static gboolean
+remove_allow_volume (gpointer data)
+{
+	GVolume *volume = data;
+
+	g_object_set_data (G_OBJECT (volume), "nautilus-allow-autorun", NULL);
+	return FALSE;
+}
+
+void
+nautilus_allow_autorun_for_volume (GVolume *volume)
+{
+	g_object_set_data (G_OBJECT (volume), "nautilus-allow-autorun", GINT_TO_POINTER (1));
+}
+
+#define INHIBIT_AUTORUN_SECONDS 10
+
+void
+nautilus_allow_autorun_for_volume_finish (GVolume *volume)
+{
+	if (g_object_get_data (G_OBJECT (volume), "nautilus-allow-autorun") != NULL) {
+		g_timeout_add_seconds_full (0,
+					    INHIBIT_AUTORUN_SECONDS,
+					    remove_allow_volume,
+					    g_object_ref (volume),
+					    g_object_unref);
+	}
+}
+
+static gboolean
+should_skip_native_mount_root (GFile *root)
+{
+	char *path;
+	gboolean should_skip;
+
+	/* skip any mounts in hidden directory hierarchies */
+	path = g_file_get_path (root);
+	should_skip = strstr (path, "/.") != NULL;
+	g_free (path);
+
+	return should_skip;
+}
+
+static gboolean
+should_autorun_mount (GMount *mount)
+{
+	GFile *root;
+	GVolume *enclosing_volume;
+	gboolean ignore_autorun;
+
+	ignore_autorun = TRUE;
+	enclosing_volume = g_mount_get_volume (mount);
+	if (enclosing_volume != NULL) {
+		if (g_object_get_data (G_OBJECT (enclosing_volume), "nautilus-allow-autorun") != NULL) {
+			ignore_autorun = FALSE;
+			g_object_set_data (G_OBJECT (enclosing_volume), "nautilus-allow-autorun", NULL);
+		}
+	}
+
+	if (ignore_autorun) {
+		if (enclosing_volume != NULL) {
+			g_object_unref (enclosing_volume);
+		}
+		return FALSE;
+	}
+	
+	root = g_mount_get_root (mount);
+
+	/* only do autorun on local files or files where g_volume_should_automount() returns TRUE */
+	ignore_autorun = TRUE;
+	if ((g_file_is_native (root) && !should_skip_native_mount_root (root)) || 
+	    (enclosing_volume != NULL && g_volume_should_automount (enclosing_volume))) {
+		ignore_autorun = FALSE;
+	}
+	if (enclosing_volume != NULL) {
+		g_object_unref (enclosing_volume);
+	}
+	g_object_unref (root);
+
+	return !ignore_autorun;
+}
diff --git a/plugins/automount/nautilus-autorun.h b/plugins/automount/nautilus-autorun.h
new file mode 100644
index 0000000..796d816
--- /dev/null
+++ b/plugins/automount/nautilus-autorun.h
@@ -0,0 +1,81 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+
+/*
+ * Nautilus
+ *
+ * Copyright (C) 2008 Red Hat, Inc.
+ *
+ * Nautilus is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Author: David Zeuthen <davidz redhat com>
+ */
+
+/* TODO:
+ *
+ * - automount all user-visible media on startup
+ *  - but avoid doing autorun for these
+ * - unmount all the media we've automounted on shutdown
+ * - finish x-content / * types
+ *  - finalize the semi-spec
+ *  - add probing/sniffing code
+ * - clean up code
+ * - implement missing features
+ *  - "Open Folder when mounted"
+ *  - Autorun spec (e.g. $ROOT/.autostart)
+ *
+ */
+
+#ifndef NAUTILUS_AUTORUN_H
+#define NAUTILUS_AUTORUN_H
+
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+typedef void (*NautilusAutorunComboBoxChanged) (gboolean selected_ask,
+						gboolean selected_ignore,
+						gboolean selected_open_folder,
+						GAppInfo *selected_app,
+						gpointer user_data);
+
+typedef void (*NautilusAutorunOpenWindow) (GMount *mount, gpointer user_data);
+typedef void (*NautilusAutorunGetContent) (char **content, gpointer user_data);
+
+void nautilus_autorun_prepare_combo_box (GtkWidget *combo_box, 
+					 const char *x_content_type, 
+					 gboolean include_ask,
+					 gboolean include_open_with_other_app,
+					 gboolean update_settings,
+					 NautilusAutorunComboBoxChanged changed_cb,
+					 gpointer user_data);
+
+void nautilus_autorun (GMount *mount, GSettings *settings, NautilusAutorunOpenWindow open_window_func, gpointer user_data);
+
+char **nautilus_autorun_get_cached_x_content_types_for_mount (GMount       *mount);
+
+void nautilus_autorun_get_x_content_types_for_mount_async (GMount *mount,
+							   NautilusAutorunGetContent callback,
+							   GCancellable *cancellable,
+							   gpointer user_data);
+
+void nautilus_autorun_launch_for_mount (GMount *mount, GAppInfo *app_info);
+
+void nautilus_allow_autorun_for_volume (GVolume *volume);
+void nautilus_allow_autorun_for_volume_finish (GVolume *volume);
+
+GtkDialog * show_error_dialog (const char *primary_text,
+		               const char *secondary_text,
+		               GtkWindow *parent);
+
+#endif /* NAUTILUS_AUTORUN_H */
diff --git a/plugins/automount/nautilus-open-with-dialog.c b/plugins/automount/nautilus-open-with-dialog.c
new file mode 100644
index 0000000..6089721
--- /dev/null
+++ b/plugins/automount/nautilus-open-with-dialog.c
@@ -0,0 +1,1072 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+
+/*
+   eel-open-with-dialog.c: an open-with dialog
+
+   Copyright (C) 2004 Novell, Inc.
+   Copyright (C) 2007 Red Hat, Inc.
+
+   The Gnome Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Authors: Dave Camp <dave novell com>
+            Alexander Larsson <alexl redhat com>
+*/
+
+#include <config.h>
+#include "nautilus-open-with-dialog.h"
+#include "nautilus-autorun.h"
+
+#include <string.h>
+#include <glib/gi18n-lib.h>
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+#define sure_string(s)                    ((const char *)((s)!=NULL?(s):""))
+#define DESKTOP_ENTRY_GROUP		  "Desktop Entry"
+
+struct _NautilusOpenWithDialogDetails {
+	GAppInfo *selected_app_info;
+	
+	char *content_type;
+	char *extension;
+
+	GtkWidget *label;
+	GtkWidget *entry;
+	GtkWidget *button;
+	GtkWidget *checkbox;
+
+	GtkWidget *desc_label;
+
+	GtkWidget *open_label;
+
+	GtkWidget     *program_list;
+	GtkListStore  *program_list_store;
+	GSList	      *add_icon_paths;
+	gint	       add_items_idle_id;
+	gint	       add_icons_idle_id;
+
+	gboolean add_mode;
+};
+
+enum {
+        COLUMN_APP_INFO,
+        COLUMN_ICON,
+        COLUMN_GICON,
+        COLUMN_NAME,
+        COLUMN_COMMENT,
+        COLUMN_EXEC,
+        NUM_COLUMNS
+};
+
+enum {
+	RESPONSE_OPEN,
+	RESPONSE_REMOVE
+};
+
+enum {
+	APPLICATION_SELECTED,
+	LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+G_DEFINE_TYPE (NautilusOpenWithDialog, nautilus_open_with_dialog, GTK_TYPE_DIALOG); 
+
+static void
+nautilus_open_with_dialog_finalize (GObject *object)
+{
+	NautilusOpenWithDialog *dialog;
+
+	dialog = NAUTILUS_OPEN_WITH_DIALOG (object);
+
+	if (dialog->details->add_icons_idle_id) {
+		g_source_remove (dialog->details->add_icons_idle_id);
+	}
+
+	if (dialog->details->add_items_idle_id) {
+		g_source_remove (dialog->details->add_items_idle_id);
+	}
+
+	if (dialog->details->selected_app_info) {
+		g_object_unref (dialog->details->selected_app_info);
+	}
+	g_free (dialog->details->content_type);
+	g_free (dialog->details->extension);
+
+	g_free (dialog->details);
+
+	G_OBJECT_CLASS (nautilus_open_with_dialog_parent_class)->finalize (object);
+}
+
+/* An application is valid if:
+ *
+ * 1) The file exists
+ * 2) The user has permissions to run the file
+ */
+static gboolean
+check_application (NautilusOpenWithDialog *dialog)
+{
+	char *command;
+	char *path = NULL;
+	char **argv = NULL;
+	int argc;
+	GError *error = NULL;
+	gint retval = TRUE;
+
+	command = NULL;
+	if (dialog->details->selected_app_info != NULL) {
+		command = g_strdup (g_app_info_get_executable (dialog->details->selected_app_info));
+	}
+
+	if (command == NULL) {
+		command = g_strdup (gtk_entry_get_text (GTK_ENTRY (dialog->details->entry)));
+	}
+	
+	g_shell_parse_argv (command, &argc, &argv, &error);
+	if (error) {
+		show_error_dialog (_("Could not run application"),
+				   error->message,
+				   GTK_WINDOW (dialog));
+		g_error_free (error);
+		retval = FALSE;
+		goto cleanup;
+	}
+
+	path = g_find_program_in_path (argv[0]);
+	if (!path) {
+		char *error_message;
+
+		error_message = g_strdup_printf (_("Could not find '%s'"),
+						 argv[0]);
+
+		show_error_dialog (_("Could not find application"),
+				   error_message,
+				   GTK_WINDOW (dialog));
+		g_free (error_message);
+		retval = FALSE;
+		goto cleanup;
+	}
+
+ cleanup:
+	g_strfreev (argv);
+	g_free (path);
+	g_free (command);
+
+	return retval;
+}
+
+/* Only called for non-desktop files */
+static char *
+get_app_name (const char *commandline, GError **error)
+{
+	char *basename;
+	char *unquoted;
+	char **argv;
+	int argc;
+
+	if (!g_shell_parse_argv (commandline,
+				 &argc, &argv, error)) {
+		return NULL;
+	}
+	
+	unquoted = g_shell_unquote (argv[0], NULL);
+	if (unquoted) {
+		basename = g_path_get_basename (unquoted);
+	} else {
+		basename = g_strdup (argv[0]);
+	}
+
+	g_free (unquoted);
+	g_strfreev (argv);
+
+	return basename;
+}
+
+/* This will check if the application the user wanted exists will return that
+ * application.  If it doesn't exist, it will create one and return that.
+ * It also sets the app info as the default for this type.
+ */
+static GAppInfo *
+add_or_find_application (NautilusOpenWithDialog *dialog)
+{
+	GAppInfo *app;
+	char *app_name;
+	const char *commandline;
+	GError *error;
+	gboolean success, should_set_default;
+	char *message;
+	GList *applications;
+
+	error = NULL;
+	app = NULL;
+	if (dialog->details->selected_app_info) {
+		app = g_object_ref (dialog->details->selected_app_info);
+	} else {
+		commandline = gtk_entry_get_text (GTK_ENTRY (dialog->details->entry));
+		app_name = get_app_name (commandline, &error);
+		if (app_name != NULL) {
+			app = g_app_info_create_from_commandline (commandline,
+								  app_name,
+								  G_APP_INFO_CREATE_NONE,
+								  &error);
+			g_free (app_name);
+		}
+	}
+
+	if (app == NULL) {
+		message = g_strdup_printf (_("Could not add application to the application database: %s"), error->message);
+		show_error_dialog (_("Could not add application"),
+				   message,
+				   GTK_WINDOW (dialog));
+		g_free (message);
+		g_error_free (error);
+		return NULL;
+	}
+
+	should_set_default = (dialog->details->add_mode) ||
+		(!dialog->details->add_mode &&
+		 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->details->checkbox)));
+	success = TRUE;
+
+	if (should_set_default) {
+		if (dialog->details->content_type) {
+			success = g_app_info_set_as_default_for_type (app,
+								      dialog->details->content_type,
+								      &error);
+		} else {
+			success = g_app_info_set_as_default_for_extension (app,
+									   dialog->details->extension,
+									   &error);
+		}
+	} else {
+		applications = g_app_info_get_all_for_type (dialog->details->content_type);
+		if (dialog->details->content_type && applications != NULL) {
+			/* we don't care about reporting errors here */
+			g_app_info_add_supports_type (app,
+						      dialog->details->content_type,
+						      NULL);
+		}
+
+		if (applications != NULL) {
+			g_list_foreach (applications, (GFunc) g_object_unref, NULL);
+			g_list_free (applications);
+		}
+	}
+
+	if (!success && should_set_default) {
+		message = g_strdup_printf (_("Could not set application as the default: %s"), error->message);
+		show_error_dialog (_("Could not set as default application"),
+				   message,
+				   GTK_WINDOW (dialog));
+		g_free (message);
+		g_error_free (error);
+	}
+
+	return app;
+}
+
+static void
+emit_application_selected (NautilusOpenWithDialog *dialog,
+			   GAppInfo *application)
+{
+	g_signal_emit (G_OBJECT (dialog), signals[APPLICATION_SELECTED], 0,
+		       application);
+}
+
+static void
+response_cb (NautilusOpenWithDialog *dialog,
+	     int response_id,
+	     gpointer data)
+{
+	GAppInfo *application;
+
+	switch (response_id) {
+	case RESPONSE_OPEN:
+		if (check_application (dialog)) {
+			application = add_or_find_application (dialog);
+
+			if (application) {
+				emit_application_selected (dialog, application);
+				g_object_unref (application);
+
+				gtk_widget_destroy (GTK_WIDGET (dialog));
+			}
+		}
+
+		break;
+	case RESPONSE_REMOVE:
+		if (dialog->details->selected_app_info != NULL) {
+			if (g_app_info_delete (dialog->details->selected_app_info)) {
+				GtkTreeModel *model;
+				GtkTreeIter iter;
+				GAppInfo *info, *selected;
+
+				selected = dialog->details->selected_app_info;
+				dialog->details->selected_app_info = NULL;
+
+				model = GTK_TREE_MODEL (dialog->details->program_list_store);
+				if (gtk_tree_model_get_iter_first (model, &iter)) {
+					do {
+						gtk_tree_model_get (model, &iter,
+			    					    COLUMN_APP_INFO, &info,
+			    					    -1);
+						if (g_app_info_equal (selected, info)) {
+							gtk_list_store_remove (dialog->details->program_list_store, &iter);
+							break;
+						}
+					} while (gtk_tree_model_iter_next (model, &iter));
+				}
+
+				g_object_unref (selected);
+			}
+		}
+		break;
+	case GTK_RESPONSE_NONE:
+	case GTK_RESPONSE_DELETE_EVENT:
+	case GTK_RESPONSE_CANCEL:
+		gtk_widget_destroy (GTK_WIDGET (dialog));
+		break;
+	default :
+		g_assert_not_reached ();
+	}
+
+}
+
+
+static void
+nautilus_open_with_dialog_class_init (NautilusOpenWithDialogClass *class)
+{
+	GObjectClass *gobject_class;
+
+	gobject_class = G_OBJECT_CLASS (class);
+	gobject_class->finalize = nautilus_open_with_dialog_finalize;
+
+	signals[APPLICATION_SELECTED] =
+		g_signal_new ("application_selected",
+			      G_TYPE_FROM_CLASS (class),
+			      G_SIGNAL_RUN_LAST,
+			      G_STRUCT_OFFSET (NautilusOpenWithDialogClass,
+					       application_selected),
+			      NULL, NULL,
+			      g_cclosure_marshal_VOID__POINTER,
+			      G_TYPE_NONE,
+			      1, G_TYPE_POINTER);
+}
+
+static void
+chooser_response_cb (GtkFileChooser *chooser,
+		     int response,
+		     gpointer user_data)
+{
+	NautilusOpenWithDialog *dialog;
+
+	dialog = NAUTILUS_OPEN_WITH_DIALOG (user_data);
+
+	if (response == GTK_RESPONSE_OK) {
+		char *filename;
+
+		filename = gtk_file_chooser_get_filename (chooser);
+
+		if (filename) {
+			char *quoted_text;
+
+			quoted_text = g_shell_quote (filename);
+
+			gtk_entry_set_text (GTK_ENTRY (dialog->details->entry),
+					    quoted_text);
+			gtk_editable_set_position (GTK_EDITABLE (dialog->details->entry), -1);
+			g_free (quoted_text);
+			g_free (filename);
+		}
+	}
+
+	gtk_widget_destroy (GTK_WIDGET (chooser));
+}
+
+static void
+browse_clicked_cb (GtkWidget *button,
+		   gpointer user_data)
+{
+	NautilusOpenWithDialog *dialog;
+	GtkWidget *chooser;
+
+	dialog = NAUTILUS_OPEN_WITH_DIALOG (user_data);
+
+	chooser = gtk_file_chooser_dialog_new (_("Select an Application"),
+					       GTK_WINDOW (dialog),
+					       GTK_FILE_CHOOSER_ACTION_OPEN,
+					       GTK_STOCK_CANCEL,
+					       GTK_RESPONSE_CANCEL,
+					       GTK_STOCK_OPEN,
+					       GTK_RESPONSE_OK,
+					       NULL);
+	gtk_window_set_destroy_with_parent (GTK_WINDOW (chooser), TRUE);
+	g_signal_connect (chooser, "response",
+			  G_CALLBACK (chooser_response_cb), dialog);
+	gtk_dialog_set_default_response (GTK_DIALOG (chooser),
+					 GTK_RESPONSE_OK);
+	gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (chooser), TRUE);
+	gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (chooser),
+					      FALSE);
+	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
+					     "/usr/bin");
+
+	gtk_widget_show (chooser);
+}
+
+static void
+entry_changed_cb (GtkWidget *entry,
+		  NautilusOpenWithDialog *dialog)
+{
+	/* We are writing in the entry, so we are not using a known appinfo anymore */
+	if (dialog->details->selected_app_info != NULL) {
+		g_object_unref (dialog->details->selected_app_info);
+		dialog->details->selected_app_info = NULL;
+	}
+
+	if (gtk_entry_get_text (GTK_ENTRY (dialog->details->entry))[0] == '\000') {
+		gtk_widget_set_sensitive (dialog->details->button, FALSE);
+	} else {
+		gtk_widget_set_sensitive (dialog->details->button, TRUE);
+	}
+}
+
+static GdkPixbuf *
+get_pixbuf_for_icon (GIcon *icon)
+{
+	GdkPixbuf  *pixbuf;
+	char *filename;
+
+	pixbuf = NULL;
+	if (G_IS_FILE_ICON (icon)) {
+		filename = g_file_get_path (g_file_icon_get_file (G_FILE_ICON (icon)));
+		if (filename) {
+			pixbuf = gdk_pixbuf_new_from_file_at_size (filename, 24, 24, NULL);
+		}
+		g_free (filename);
+	} else if (G_IS_THEMED_ICON (icon)) {
+		const char * const *names;
+		char *icon_no_extension;
+		char *p;
+		
+		names = g_themed_icon_get_names (G_THEMED_ICON (icon));
+		
+		if (names != NULL && names[0] != NULL) {
+			icon_no_extension = g_strdup (names[0]);
+			p = strrchr (icon_no_extension, '.');
+			if (p &&
+			    (strcmp (p, ".png") == 0 ||
+			     strcmp (p, ".xpm") == 0 ||
+			     strcmp (p, ".svg") == 0)) {
+				*p = 0;
+			}
+			pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
+							   icon_no_extension, 24, 0, NULL);
+			g_free (icon_no_extension);
+		}
+	}
+	return pixbuf;
+}
+
+static gboolean
+nautilus_open_with_dialog_add_icon_idle (NautilusOpenWithDialog *dialog)
+{
+	GtkTreeIter   iter;
+	GtkTreePath  *path;
+	GdkPixbuf    *pixbuf;
+	GIcon *icon;
+	gboolean      long_operation;
+
+	long_operation = FALSE;
+	do {
+		if (!dialog->details->add_icon_paths) {
+			dialog->details->add_icons_idle_id = 0;
+			return FALSE;
+		}
+
+		path = dialog->details->add_icon_paths->data;
+		dialog->details->add_icon_paths->data = NULL;
+		dialog->details->add_icon_paths = g_slist_delete_link (dialog->details->add_icon_paths,
+								       dialog->details->add_icon_paths);
+
+		if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (dialog->details->program_list_store),
+					      &iter, path)) {
+			gtk_tree_path_free (path);
+			continue;
+		}
+		
+		gtk_tree_path_free (path);
+
+		gtk_tree_model_get (GTK_TREE_MODEL (dialog->details->program_list_store), &iter,
+				    COLUMN_GICON, &icon, -1);
+
+		if (icon == NULL) {
+			continue;
+		}
+
+		pixbuf = get_pixbuf_for_icon (icon);
+		if (pixbuf) {
+			long_operation = TRUE;
+			gtk_list_store_set (dialog->details->program_list_store, &iter, COLUMN_ICON, pixbuf, -1);
+			g_object_unref (pixbuf);
+		}
+		
+	/* don't go back into the main loop if this wasn't very hard to do */
+	} while (!long_operation);
+
+	return TRUE;
+}
+
+
+static gboolean
+nautilus_open_with_search_equal_func (GtkTreeModel *model,
+				 int column,
+				 const char *key,
+				 GtkTreeIter *iter,
+				 gpointer user_data)
+{
+	char *normalized_key;
+	char *name, *normalized_name;
+	char *path, *normalized_path;
+	char *basename, *normalized_basename;
+	gboolean ret;
+
+	if (key != NULL) {
+		normalized_key = g_utf8_casefold (key, -1);
+		g_assert (normalized_key != NULL);
+
+		ret = TRUE;
+
+		gtk_tree_model_get (model, iter,
+				    COLUMN_NAME, &name,
+				    COLUMN_EXEC, &path,
+				    -1);
+
+		if (name != NULL) {
+			normalized_name = g_utf8_casefold (name, -1);
+			g_assert (normalized_name != NULL);
+
+			if (strncmp (normalized_name, normalized_key, strlen (normalized_key)) == 0) {
+				ret = FALSE;
+			}
+
+			g_free (normalized_name);
+		}
+
+		if (ret && path != NULL) {
+			normalized_path = g_utf8_casefold (path, -1);
+			g_assert (normalized_path != NULL);
+
+			basename = g_path_get_basename (path);
+			g_assert (basename != NULL);
+
+			normalized_basename = g_utf8_casefold (basename, -1);
+			g_assert (normalized_basename != NULL);
+
+			if (strncmp (normalized_path, normalized_key, strlen (normalized_key)) == 0 ||
+			    strncmp (normalized_basename, normalized_key, strlen (normalized_key)) == 0) {
+				ret = FALSE;
+			}
+
+			g_free (basename);
+			g_free (normalized_basename);
+			g_free (normalized_path);
+		}
+
+		g_free (name);
+		g_free (path);
+		g_free (normalized_key);
+
+		return ret;
+	} else {
+		return TRUE;
+	}
+}
+
+
+
+static gboolean
+nautilus_open_with_dialog_add_items_idle (NautilusOpenWithDialog *dialog)
+{
+	GtkCellRenderer   *renderer;
+	GtkTreeViewColumn *column;
+	GtkTreeModel      *sort;
+	GList             *all_applications;
+	GList             *l;
+
+	/* create list store */
+	dialog->details->program_list_store = gtk_list_store_new (NUM_COLUMNS,
+								  G_TYPE_APP_INFO,
+								  GDK_TYPE_PIXBUF,
+								  G_TYPE_ICON,
+								  G_TYPE_STRING,
+								  G_TYPE_STRING,
+								  G_TYPE_STRING);
+	sort = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (dialog->details->program_list_store));
+	all_applications = g_app_info_get_all ();
+	
+	for (l = all_applications; l; l = l->next) {
+		GAppInfo *app = l->data;
+		GtkTreeIter     iter;
+		GtkTreePath    *path;
+
+		if (!g_app_info_supports_uris (app) &&
+		    !g_app_info_supports_files (app))
+			continue;
+
+		gtk_list_store_append (dialog->details->program_list_store, &iter);
+		gtk_list_store_set (dialog->details->program_list_store, &iter,
+				    COLUMN_APP_INFO,  app,
+				    COLUMN_ICON,      NULL,
+				    COLUMN_GICON,     g_app_info_get_icon (app),
+				    COLUMN_NAME,      g_app_info_get_display_name (app),
+				    COLUMN_COMMENT,   g_app_info_get_description (app),
+				    COLUMN_EXEC,      g_app_info_get_executable,
+				    -1);
+
+		path = gtk_tree_model_get_path (GTK_TREE_MODEL (dialog->details->program_list_store), &iter);
+		if (path != NULL) {
+			dialog->details->add_icon_paths = g_slist_prepend (dialog->details->add_icon_paths, path);
+		}
+	}
+	g_list_free (all_applications);
+
+	gtk_tree_view_set_model (GTK_TREE_VIEW (dialog->details->program_list), 
+				 GTK_TREE_MODEL (sort));
+	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (sort),
+					      COLUMN_NAME, GTK_SORT_ASCENDING);
+	gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (dialog->details->program_list),
+					     nautilus_open_with_search_equal_func,
+					     NULL, NULL);
+
+	renderer = gtk_cell_renderer_pixbuf_new ();
+	column = gtk_tree_view_column_new ();
+	gtk_tree_view_column_pack_start (column, renderer, FALSE);
+	gtk_tree_view_column_set_attributes (column, renderer,
+                                             "pixbuf", COLUMN_ICON,
+                                             NULL);
+
+	renderer = gtk_cell_renderer_text_new ();
+	gtk_tree_view_column_pack_start (column, renderer, TRUE);
+	gtk_tree_view_column_set_attributes (column, renderer,
+                                             "text", COLUMN_NAME,
+                                             NULL);
+	gtk_tree_view_column_set_sort_column_id (column, COLUMN_NAME);
+	gtk_tree_view_append_column (GTK_TREE_VIEW (dialog->details->program_list), column);
+
+	dialog->details->add_icon_paths = g_slist_reverse (dialog->details->add_icon_paths);
+
+	if (!dialog->details->add_icons_idle_id) {
+		dialog->details->add_icons_idle_id =
+			g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, (GSourceFunc) nautilus_open_with_dialog_add_icon_idle,
+					 dialog, NULL);
+	}
+
+	dialog->details->add_items_idle_id = 0;
+	return FALSE;
+}
+
+static void
+program_list_selection_changed (GtkTreeSelection  *selection,
+				NautilusOpenWithDialog *dialog)
+{
+	GtkTreeModel     *model;
+	GtkTreeIter       iter;
+	GAppInfo *info;
+		
+	if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
+		gtk_widget_set_sensitive (dialog->details->button, FALSE);
+		gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
+					   	   RESPONSE_REMOVE,
+					   	   FALSE);
+		return;
+	}
+
+	info = NULL;
+	gtk_tree_model_get (model, &iter,
+			    COLUMN_APP_INFO, &info,
+			    -1);
+	
+	if (info == NULL) {
+		return;
+	}
+
+	gtk_entry_set_text (GTK_ENTRY (dialog->details->entry),
+			    sure_string (g_app_info_get_executable (info)));
+	gtk_label_set_text (GTK_LABEL (dialog->details->desc_label),
+			    sure_string (g_app_info_get_description (info)));
+	gtk_widget_set_sensitive (dialog->details->button, TRUE);
+	gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
+					   RESPONSE_REMOVE,
+					   g_app_info_can_delete (info));
+
+	if (dialog->details->selected_app_info) {
+		g_object_unref (dialog->details->selected_app_info);
+	}
+	
+	dialog->details->selected_app_info = info;
+}
+
+static void
+program_list_selection_activated (GtkTreeView       *view,
+				  GtkTreePath       *path,
+				  GtkTreeViewColumn *column,
+				  NautilusOpenWithDialog *dialog)
+{
+	GtkTreeSelection *selection;
+
+	/* update the entry with the info from the selection */
+	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));	
+	program_list_selection_changed (selection, dialog);
+	
+	gtk_dialog_response (GTK_DIALOG (&dialog->parent), RESPONSE_OPEN);
+}
+
+static void
+expander_toggled (GtkWidget *expander, NautilusOpenWithDialog *dialog)
+{
+	if (gtk_expander_get_expanded (GTK_EXPANDER (expander)) == TRUE) {
+		gtk_widget_grab_focus (dialog->details->entry);
+		gtk_window_resize (GTK_WINDOW (dialog), 400, 1);
+	} else {
+		GtkTreeSelection *selection;
+
+		gtk_widget_grab_focus (dialog->details->program_list);
+		selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));
+		program_list_selection_changed (selection, dialog);
+	}
+}
+
+static void
+nautilus_open_with_dialog_init (NautilusOpenWithDialog *dialog)
+{
+	GtkWidget *hbox;
+	GtkWidget *vbox;
+	GtkWidget *vbox2;
+	GtkWidget *label;
+	GtkWidget *align;
+	GtkWidget *scrolled_window;
+	GtkWidget *expander;
+	GtkTreeSelection *selection;
+
+	dialog->details = g_new0 (NautilusOpenWithDialogDetails, 1);
+
+	gtk_window_set_title (GTK_WINDOW (dialog), _("Open With"));
+	gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
+	gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
+	gtk_window_set_destroy_with_parent (GTK_WINDOW (dialog), TRUE);
+
+	gtk_box_set_spacing (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), 2);
+
+	vbox = gtk_vbox_new (FALSE, 12);
+	gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
+
+	vbox2 = gtk_vbox_new (FALSE, 6);
+	gtk_box_pack_start (GTK_BOX (vbox), vbox2, TRUE, TRUE, 0);
+
+	dialog->details->label = gtk_label_new ("");
+	gtk_misc_set_alignment (GTK_MISC (dialog->details->label), 0.0, 0.5);
+	gtk_label_set_line_wrap (GTK_LABEL (dialog->details->label), TRUE);
+	gtk_box_pack_start (GTK_BOX (vbox2), dialog->details->label,
+			    FALSE, FALSE, 0);
+	gtk_widget_show (dialog->details->label);
+
+
+	scrolled_window = gtk_scrolled_window_new (NULL, NULL);
+	gtk_widget_set_size_request (scrolled_window, 400, 300);
+	
+	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
+					     GTK_SHADOW_IN);
+	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
+					GTK_POLICY_AUTOMATIC,
+					GTK_POLICY_AUTOMATIC);
+	dialog->details->program_list = gtk_tree_view_new ();
+	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (dialog->details->program_list),
+					   FALSE);
+	gtk_container_add (GTK_CONTAINER (scrolled_window), dialog->details->program_list);
+	
+	gtk_box_pack_start (GTK_BOX (vbox2), scrolled_window, TRUE, TRUE, 0);
+
+	dialog->details->desc_label = gtk_label_new (_("Select an application to view its description."));
+	gtk_misc_set_alignment (GTK_MISC (dialog->details->desc_label), 0.0, 0.5);
+	gtk_label_set_justify (GTK_LABEL (dialog->details->desc_label), GTK_JUSTIFY_LEFT);
+	gtk_label_set_line_wrap (GTK_LABEL (dialog->details->desc_label), TRUE);
+	gtk_label_set_single_line_mode (GTK_LABEL (dialog->details->desc_label), FALSE);
+	gtk_box_pack_start (GTK_BOX (vbox2), dialog->details->desc_label, FALSE, FALSE, 0);
+	
+	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->details->program_list));
+	gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
+	g_signal_connect (selection, "changed",
+			  G_CALLBACK (program_list_selection_changed),
+			  dialog);
+	g_signal_connect (dialog->details->program_list, "row-activated",
+			  G_CALLBACK (program_list_selection_activated),
+			  dialog);
+
+	dialog->details->add_items_idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
+						     (GSourceFunc) nautilus_open_with_dialog_add_items_idle,
+						      dialog, NULL);
+
+	
+	gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), vbox, TRUE, TRUE, 0);
+	gtk_widget_show_all (vbox);
+
+
+	expander = gtk_expander_new_with_mnemonic (_("_Use a custom command"));
+	gtk_box_pack_start (GTK_BOX (vbox), expander, FALSE, FALSE, 0);
+	g_signal_connect_after (expander, "activate", G_CALLBACK (expander_toggled), dialog);
+
+	gtk_widget_show (expander);
+
+	hbox = gtk_hbox_new (FALSE, 6);
+	gtk_container_add (GTK_CONTAINER (expander), hbox);
+	gtk_widget_show (hbox);
+
+	dialog->details->entry = gtk_entry_new ();
+	gtk_entry_set_activates_default (GTK_ENTRY (dialog->details->entry), TRUE);
+
+	gtk_box_pack_start (GTK_BOX (hbox), dialog->details->entry,
+			    TRUE, TRUE, 0);
+	gtk_widget_show (dialog->details->entry);
+
+	dialog->details->button = gtk_button_new_with_mnemonic (_("_Browse..."));
+	g_signal_connect (dialog->details->button, "clicked",
+			  G_CALLBACK (browse_clicked_cb), dialog);
+	gtk_box_pack_start (GTK_BOX (hbox), dialog->details->button, FALSE, FALSE, 0);
+	gtk_widget_show (dialog->details->button);
+
+	/* Add remember this application checkbox - only visible in open mode */
+	dialog->details->checkbox = gtk_check_button_new ();
+	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->details->checkbox), TRUE);
+	gtk_button_set_use_underline (GTK_BUTTON (dialog->details->checkbox), TRUE);
+	gtk_widget_show (GTK_WIDGET (dialog->details->checkbox));
+	gtk_box_pack_start (GTK_BOX (vbox), dialog->details->checkbox, FALSE, FALSE, 0);
+
+        gtk_dialog_add_button (GTK_DIALOG (dialog),
+                               GTK_STOCK_REMOVE,
+			       RESPONSE_REMOVE);
+	gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog),
+			       		   RESPONSE_REMOVE,
+					   FALSE);
+
+	gtk_dialog_add_button (GTK_DIALOG (dialog),
+			       GTK_STOCK_CANCEL,
+			       GTK_RESPONSE_CANCEL);
+
+
+	/* Create a custom stock icon */
+	dialog->details->button = gtk_button_new ();
+
+	/* Hook up the entry to the button */
+	gtk_widget_set_sensitive (dialog->details->button, FALSE);
+	g_signal_connect (G_OBJECT (dialog->details->entry), "changed",
+			  G_CALLBACK (entry_changed_cb), dialog);
+
+	hbox = gtk_hbox_new (FALSE, 2);
+	gtk_widget_show (hbox);
+
+	label = gtk_label_new_with_mnemonic (_("_Open"));
+	gtk_label_set_mnemonic_widget (GTK_LABEL (label), GTK_WIDGET (dialog->details->button));
+	gtk_widget_show (label);
+	dialog->details->open_label = label;
+
+	gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
+
+	align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
+	gtk_widget_show (align);
+
+	gtk_widget_show (dialog->details->button);
+	gtk_widget_set_can_default (dialog->details->button, TRUE);
+
+
+	gtk_container_add (GTK_CONTAINER (align), hbox);
+	gtk_container_add (GTK_CONTAINER (dialog->details->button), align);
+
+	gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
+				      dialog->details->button, RESPONSE_OPEN);
+
+
+	gtk_dialog_set_default_response (GTK_DIALOG (dialog),
+					 RESPONSE_OPEN);
+
+	g_signal_connect (dialog, "response",
+			  G_CALLBACK (response_cb),
+			  dialog);
+}
+
+static char *
+get_extension (const char *basename)
+{
+	char *p;
+
+	p = strrchr (basename, '.');
+
+	if (p && *(p + 1) != '\0') {
+		return g_strdup (p + 1);
+	} else {
+		return NULL;
+	}
+}
+
+static void
+set_uri_and_type (NautilusOpenWithDialog *dialog,
+		  const char *uri,
+		  const char *mime_type,
+		  const char *passed_extension,
+		  gboolean add_mode)
+{
+	char *label;
+	char *emname;
+	char *name, *extension;
+	char *description;
+	char *checkbox_text;
+	
+	name = NULL;
+	extension = NULL;
+	
+	if (uri != NULL) {
+		GFile *file;
+
+		file = g_file_new_for_uri (uri);
+		name = g_file_get_basename (file);
+		g_object_unref (file);
+	}
+	if (passed_extension == NULL && name != NULL) {
+		extension = get_extension (name);
+	} else {
+		extension = g_strdup (passed_extension);
+	}
+
+	if (extension != NULL &&
+	    g_content_type_is_unknown (mime_type)) {
+		dialog->details->extension = g_strdup (extension);
+
+		if (name != NULL) {
+			emname = g_strdup_printf ("<i>%s</i>", name);
+			if (add_mode) {
+				/* first %s is a filename and second %s is a file extension */
+				label = g_strdup_printf (_("Open %s and other %s document with:"),
+							 emname, dialog->details->extension);
+			} else {
+				/* the %s here is a file name */
+				label = g_strdup_printf (_("Open %s with:"), emname);
+				checkbox_text = g_strdup_printf (_("_Remember this application for %s documents"), 
+								 dialog->details->extension);
+
+				gtk_button_set_label (GTK_BUTTON (dialog->details->checkbox), checkbox_text);
+				g_free (checkbox_text);
+			}
+			g_free (emname);
+		} else {
+			/* Only in add mode - the %s here is a file extension */
+			label = g_strdup_printf (_("Open all %s documents with:"),
+						 dialog->details->extension);
+		}
+		g_free (extension);
+	} else {
+		dialog->details->content_type = g_strdup (mime_type);
+		description = g_content_type_get_description (mime_type);
+
+		if (description == NULL) {
+			description = g_strdup (_("Unknown"));
+		}
+
+		if (name != NULL) {
+			emname = g_strdup_printf ("<i>%s</i>", name);
+			if (add_mode) {
+				/* First %s is a filename, second is a description
+				 * of the type, eg "plain text document" */
+				label = g_strdup_printf (_("Open %s and other \"%s\" files with:"), 
+							 emname, description);
+			} else {
+				/* %s is a filename */
+				label = g_strdup_printf (_("Open %s with:"), emname);
+				/* %s is a file type description */
+				checkbox_text = g_strdup_printf (_("_Remember this application for \"%s\" files"),
+								 description);
+
+				gtk_button_set_label (GTK_BUTTON (dialog->details->checkbox), checkbox_text);
+				g_free (checkbox_text);
+			}
+			g_free (emname);
+		} else {
+			/* Only in add mode */
+			label = g_strdup_printf (_("Open all \"%s\" files with:"), description);
+		}
+
+		g_free (description);
+	}
+
+	dialog->details->add_mode = add_mode;
+	if (add_mode) {
+		gtk_widget_hide (dialog->details->checkbox);
+
+		gtk_label_set_text_with_mnemonic (GTK_LABEL (dialog->details->open_label),
+						  _("_Add"));
+		gtk_window_set_title (GTK_WINDOW (dialog), _("Add Application"));
+	}
+
+	gtk_label_set_markup (GTK_LABEL (dialog->details->label), label);
+
+	g_free (label);
+	g_free (name);
+}
+
+
+static GtkWidget *
+real_nautilus_open_with_dialog_new (const char *uri,
+				    const char *mime_type,
+				    const char *extension,
+				    gboolean add_mode)
+{
+	GtkWidget *dialog;
+
+	dialog = gtk_widget_new (NAUTILUS_TYPE_OPEN_WITH_DIALOG, NULL);
+
+	set_uri_and_type (NAUTILUS_OPEN_WITH_DIALOG (dialog), uri, mime_type, extension, add_mode);
+
+	return dialog;
+}
+
+GtkWidget *
+nautilus_open_with_dialog_new (const char *uri,
+			       const char *mime_type,
+			       const char *extension)
+{
+	return real_nautilus_open_with_dialog_new (uri, mime_type, extension, FALSE);
+}
+
+GtkWidget * 
+nautilus_add_application_dialog_new (const char *uri,
+				     const char *mime_type)
+{
+	NautilusOpenWithDialog *dialog;
+	
+	dialog = NAUTILUS_OPEN_WITH_DIALOG (real_nautilus_open_with_dialog_new (uri, mime_type, NULL, TRUE));
+
+	return GTK_WIDGET (dialog);
+}
+
+GtkWidget * 
+nautilus_add_application_dialog_new_for_multiple_files (const char *extension,
+							const char *mime_type)
+{
+	NautilusOpenWithDialog *dialog;
+	
+	dialog = NAUTILUS_OPEN_WITH_DIALOG (real_nautilus_open_with_dialog_new (NULL, mime_type, extension, TRUE));
+	
+	return GTK_WIDGET (dialog);
+}
+
diff --git a/plugins/automount/nautilus-open-with-dialog.h b/plugins/automount/nautilus-open-with-dialog.h
new file mode 100644
index 0000000..e2bf4e7
--- /dev/null
+++ b/plugins/automount/nautilus-open-with-dialog.h
@@ -0,0 +1,64 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+
+/*
+   nautilus-open-with-dialog.c: an open-with dialog
+ 
+   Copyright (C) 2004 Novell, Inc.
+ 
+   The Gnome Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Library General Public License as
+   published by the Free Software Foundation; either version 2 of the
+   License, or (at your option) any later version.
+
+   The Gnome Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Library General Public License for more details.
+
+   You should have received a copy of the GNU Library General Public
+   License along with the Gnome Library; see the file COPYING.LIB.  If not,
+   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+   Boston, MA 02111-1307, USA.
+
+   Authors: Dave Camp <dave novell com>
+*/
+
+#ifndef NAUTILUS_OPEN_WITH_DIALOG_H
+#define NAUTILUS_OPEN_WITH_DIALOG_H
+
+#include <gtk/gtk.h>
+#include <gio/gio.h>
+
+#define NAUTILUS_TYPE_OPEN_WITH_DIALOG         (nautilus_open_with_dialog_get_type ())
+#define NAUTILUS_OPEN_WITH_DIALOG(obj)         (G_TYPE_CHECK_INSTANCE_CAST ((obj), NAUTILUS_TYPE_OPEN_WITH_DIALOG, NautilusOpenWithDialog))
+#define NAUTILUS_OPEN_WITH_DIALOG_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_OPEN_WITH_DIALOG, NautilusOpenWithDialogClass))
+#define NAUTILUS_IS_OPEN_WITH_DIALOG(obj)      (G_TYPE_INSTANCE_CHECK_TYPE ((obj), NAUTILUS_TYPE_OPEN_WITH_DIALOG)
+
+typedef struct _NautilusOpenWithDialog        NautilusOpenWithDialog;
+typedef struct _NautilusOpenWithDialogClass   NautilusOpenWithDialogClass;
+typedef struct _NautilusOpenWithDialogDetails NautilusOpenWithDialogDetails;
+
+struct _NautilusOpenWithDialog {
+	GtkDialog parent;
+	NautilusOpenWithDialogDetails *details;
+};
+
+struct _NautilusOpenWithDialogClass {
+	GtkDialogClass parent_class;
+
+	void (*application_selected) (NautilusOpenWithDialog *dialog,
+				      GAppInfo *application);
+};
+
+GType      nautilus_open_with_dialog_get_type (void);
+GtkWidget* nautilus_open_with_dialog_new      (const char *uri,
+					       const char *mime_type,
+					       const char *extension);
+GtkWidget* nautilus_add_application_dialog_new (const char *uri,
+						const char *mime_type);
+GtkWidget* nautilus_add_application_dialog_new_for_multiple_files (const char *extension,
+								   const char *mime_type);
+
+
+
+#endif /* NAUTILUS_OPEN_WITH_DIALOG_H */
diff --git a/plugins/automount/test-automount.c b/plugins/automount/test-automount.c
new file mode 100644
index 0000000..8594a53
--- /dev/null
+++ b/plugins/automount/test-automount.c
@@ -0,0 +1,58 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2010 Tomas Bzatek <tbzatek redhat com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
+
+#include "config.h"
+
+#include <stdlib.h>
+#include <locale.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+
+#include "gsd-automount-manager.h"
+
+static gboolean
+idle (GsdAutomountManager *manager)
+{
+        gsd_automount_manager_start (manager, NULL);
+        return FALSE;
+}
+
+int
+main (int argc, char *argv[])
+{
+        GsdAutomountManager *manager;
+
+        bindtextdomain (GETTEXT_PACKAGE, GNOME_SETTINGS_LOCALEDIR);
+        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+        textdomain (GETTEXT_PACKAGE);
+
+        setlocale (LC_ALL, "");
+
+        gtk_init (&argc, &argv);
+
+        manager = gsd_automount_manager_new ();
+        g_idle_add ((GSourceFunc)idle, manager);
+
+        gtk_main ();
+
+        return 0;
+}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]