diff options
Diffstat (limited to 'src/nautilus-gdu.c')
-rw-r--r-- | src/nautilus-gdu.c | 271 |
1 files changed, 222 insertions, 49 deletions
diff --git a/src/nautilus-gdu.c b/src/nautilus-gdu.c index 8b361a9..a0611d0 100644 --- a/src/nautilus-gdu.c +++ b/src/nautilus-gdu.c @@ -1,6 +1,6 @@ /* * nautilus-gdu.c - * + * * Copyright (C) 2008-2009 Red Hat, Inc. * * This library is free software; you can redistribute it and/or @@ -18,7 +18,7 @@ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * Author: Tomas Bzatek <tbzatek@redhat.com> - * + * */ #ifdef HAVE_CONFIG_H @@ -26,22 +26,12 @@ #endif #include "nautilus-gdu.h" - -#include <libnautilus-extension/nautilus-menu-provider.h> +#include "nautilus-gdu-window.h" #include <glib/gi18n-lib.h> -#include <gtk/gtkicontheme.h> -#include <gtk/gtkwidget.h> -#include <gtk/gtkmain.h> - +#include <gio/gio.h> #include <gdu/gdu.h> -#include <gdu-gtk/gdu-gtk.h> -#include <errno.h> -#include <fcntl.h> -#include <string.h> /* for strcmp */ -#include <unistd.h> /* for chdir */ -#include <sys/stat.h> static void nautilus_gdu_instance_init (NautilusGDU *cvs); @@ -50,23 +40,136 @@ static void nautilus_gdu_class_init (NautilusGDUClass *class); static GType nautilus_gdu_type = 0; + + +static GduPresentable * +find_presentable_from_mount_path (char *mount_path) +{ + GduPool *pool; + GList *presentables, *presentables_w; + GduPresentable *presentable = NULL; + GduPresentable *pres_w; + GduDevice *device; + const char *device_mount; + GFile *file1, *file2; + + g_return_val_if_fail (mount_path != NULL, NULL); + g_return_val_if_fail (strlen (mount_path) > 1, NULL); + + pool = gdu_pool_new (); + presentables = gdu_pool_get_presentables (pool); + + presentables_w = presentables; + while (presentable == NULL && presentables_w != NULL) { + pres_w = presentables_w->data; + if (pres_w) { + device = gdu_presentable_get_device (pres_w); + if (device) { + device_mount = gdu_device_get_mount_path (device); + if (device_mount && strlen (device_mount) > 1) { + g_print ("find_presentable_from_path: found mount '%s', matching with '%s'\n", device_mount, mount_path); + /* compare via GFile routines */ + file1 = g_file_new_for_commandline_arg (mount_path); + file2 = g_file_new_for_path (device_mount); + if (g_file_equal (file1, file2)) + presentable = g_object_ref (pres_w); + g_object_unref (file1); + g_object_unref (file2); + } + g_object_unref (device); + } + g_object_unref (pres_w); + } + presentables_w = g_list_next (presentables_w); + } + + if (presentables) + g_list_free (presentables); + g_object_unref (pool); + + if (presentable) + g_print ("find_presentable_from_path: found presentable '%s'\n", gdu_presentable_get_name (presentable)); + else + g_print ("find_presentable_from_path: no presentable found\n"); + + return presentable; +} + +/* caller must unref the returned object */ +GduPresentable * +find_presentable_from_file (NautilusFileInfo *nautilus_file) +{ + GduPresentable *presentable = NULL; + GFile *file; + GFileInfo *info; + GError *error; + GFileType file_type; + char *mount_path = NULL; + + g_return_val_if_fail (nautilus_file != NULL, NULL); + file = nautilus_file_info_get_location (nautilus_file); + g_return_val_if_fail (file != NULL, NULL); + file_type = nautilus_file_info_get_file_type (nautilus_file); + +#if 0 + /* is file native? (not local, but at least locally mounted) */ + if (! g_file_is_native (file)) { + g_object_unref (file); + return NULL; + } +#endif + + /* first try to find mount target from a mountable */ + if (file_type == G_FILE_TYPE_MOUNTABLE || file_type == G_FILE_TYPE_SHORTCUT) { + mount_path = nautilus_file_info_get_activation_uri (nautilus_file); + if (mount_path == NULL) { + error = NULL; + + /* TODO: retrieve HAL/DeviceKit device ID for non-mounted devices */ + info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI, G_FILE_QUERY_INFO_NONE, NULL, &error); + if (info) { + mount_path = g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI); + g_object_unref (info); + } + if (error) { + g_warning ("unable to query info: %s\n", error->message); + g_clear_error (&error); + } + } + } + + /* try to guess mount from a path (e.g. /media/disk) */ + if (mount_path == NULL && file_type == G_FILE_TYPE_DIRECTORY) { + error = NULL; + info = g_file_query_info (file, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, G_FILE_QUERY_INFO_NONE, NULL, &error); + if (info) { + if (g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT)) + mount_path = g_file_get_path (file); + g_object_unref (info); + } + if (error) { + g_warning ("unable to query info: %s\n", error->message); + g_clear_error (&error); + } + } + + if (mount_path) { + presentable = find_presentable_from_mount_path (mount_path); + g_free (mount_path); + } + g_object_unref (file); + + return presentable; +} + + static void format_callback (NautilusMenuItem *item, - NautilusFileInfo *file_info) + GduPresentable *presentable) { - GtkWidget *dialog; - - g_print ("format_callback\n"); - - dialog = gtk_dialog_new_with_buttons (_("Format disk"), NULL, GTK_DIALOG_NO_SEPARATOR, - GTK_STOCK_OK, - GTK_RESPONSE_ACCEPT, - GTK_STOCK_CANCEL, - GTK_RESPONSE_REJECT, - NULL); - gtk_window_set_default_size (GTK_WINDOW (dialog), 300, 400); - gtk_dialog_run (GTK_DIALOG (dialog)); - gtk_widget_destroy (dialog); + g_return_if_fail (GDU_IS_PRESENTABLE (presentable)); + + nautilus_gdu_spawn_dialog (presentable); } static GList * @@ -74,20 +177,9 @@ nautilus_gdu_get_background_items (NautilusMenuProvider *provider, GtkWidget *window, NautilusFileInfo *file_info) { - NautilusMenuItem *item; - g_print ("nautilus_gdu_get_background_items\n"); - - item = nautilus_menu_item_new ("NautilusGDU::format", _("_Format..."), _("Open dummy dialog"), "palimpsest"); - g_object_set_data (G_OBJECT (item), "NautilusGDU::screen", gtk_widget_get_screen (window)); - g_object_set_data_full (G_OBJECT (item), "file-info", - g_object_ref (file_info), - (GDestroyNotify) g_object_unref); - g_signal_connect (item, "activate", - G_CALLBACK (format_callback), - file_info); - - return g_list_append (NULL, item); + /* not used */ + return NULL; } GList * @@ -96,22 +188,102 @@ nautilus_gdu_get_file_items (NautilusMenuProvider *provider, GList *files) { NautilusMenuItem *item; + GTimeVal start_time, stop_time; - g_print ("nautilus_gdu_get_file_items\n"); + g_get_current_time (&start_time); - if (g_list_length (files) != 1 /* || - nautilus_file_info_get_file_type (files->data) != G_FILE_TYPE_MOUNTABLE */ ) { + if (g_list_length (files) != 1) { return NULL; } + +#if 0 + g_print ("nautilus_gdu_get_file_items: item[0] uri '%s', type = %d, activation_uri '%s', name '%s'\n", + nautilus_file_info_get_uri (files->data), + nautilus_file_info_get_file_type (files->data), + nautilus_file_info_get_activation_uri (files->data), + nautilus_file_info_get_name (files->data)); + + GError *error = NULL; + GMount *mount; + mount = g_file_find_enclosing_mount (nautilus_file_info_get_location(files->data), NULL, &error); + g_print ("nautilus_gdu_get_file_items: mount = %p\n", mount); + if (error) { + g_print ("nautilus_gdu_get_file_items: error = %s\n", error->message); + } + + GFileInfo *info; + + error = NULL; + info = g_file_query_info (nautilus_file_info_get_location (files->data), + G_FILE_ATTRIBUTE_STANDARD_TARGET_URI "," G_FILE_ATTRIBUTE_ID_FILE "," + G_FILE_ATTRIBUTE_ID_FILESYSTEM "," G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE "," + G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI "," G_FILE_ATTRIBUTE_UNIX_DEVICE "," + G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT, G_FILE_QUERY_INFO_NONE, NULL, &error); + if (info) { + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_STANDARD_TARGET_URI = %s\n", g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_STANDARD_TARGET_URI)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_ID_FILE = %s\n", g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_ID_FILE)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_ID_FILESYSTEM = %s\n", g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_ID_FILESYSTEM)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE = %d\n", g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_MOUNTABLE_UNIX_DEVICE)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI = %s\n", g_file_info_get_attribute_as_string (info, G_FILE_ATTRIBUTE_MOUNTABLE_HAL_UDI)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_UNIX_DEVICE = %d\n", g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE)); + g_print ("nautilus_gdu_get_file_items: G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT = %d\n", g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_UNIX_IS_MOUNTPOINT)); + } + if (error) { + g_print ("nautilus_gdu_get_file_items: error = %s\n", error->message); + } + + + /* GDU stuff */ + GduPool *pool; + GList *presentables; + GduPresentable *presentable; + GduDevice *device; + + pool = gdu_pool_new (); + presentables = gdu_pool_get_presentables (pool); + + + + while (presentables != NULL) { + presentable = presentables->data; + device = gdu_presentable_get_device (presentable); + g_print ("nautilus_gdu_get_file_items: found presentable name '%s', mounted as %s, \n device %s, object_path %s, uuid %s\n", + gdu_presentable_get_name (presentable), gdu_device_get_mount_path (device), gdu_device_get_device_file (device), + gdu_device_get_object_path (device), gdu_device_id_get_uuid (device)); + g_object_unref (device); + g_object_unref (presentable); + presentables = g_list_next (presentables); + } + + + if (presentables) + g_list_free (presentables); + g_object_unref (pool); + +#else + GduPresentable *presentable; +#endif + + + presentable = find_presentable_from_file (files->data); + + g_get_current_time (&stop_time); + g_print ("I spent %ld ms searching for DK presentable\n", (stop_time.tv_sec - start_time.tv_sec) * 1000 + (stop_time.tv_usec - start_time.tv_usec) / 1000); + + if (! presentable) + return NULL; + item = nautilus_menu_item_new ("NautilusGDU::format", _("_Format..."), _("Open dummy dialog"), "palimpsest"); - g_object_set_data (G_OBJECT (item), "NautilusGDU::screen", gtk_widget_get_screen (window)); +// g_object_set_data (G_OBJECT (item), "NautilusGDU::screen", gtk_widget_get_screen (window)); +/* g_object_set_data_full (G_OBJECT (item), "file-info", g_object_ref (files->data), (GDestroyNotify) g_object_unref); +*/ g_signal_connect (item, "activate", G_CALLBACK (format_callback), - files->data); + presentable); return g_list_append (NULL, item); } @@ -123,6 +295,7 @@ nautilus_gdu_get_toolbar_items (NautilusMenuProvider *provider, NautilusFileInfo *current_folder) { g_print ("nautilus_gdu_get_toolbar_items\n"); + /* not used */ return NULL; } @@ -135,7 +308,7 @@ nautilus_gdu_menu_provider_iface_init (NautilusMenuProviderIface *iface) iface->get_toolbar_items = nautilus_gdu_get_toolbar_items; } -static void +static void nautilus_gdu_instance_init (NautilusGDU *cvs) { } @@ -146,7 +319,7 @@ nautilus_gdu_class_init (NautilusGDUClass *class) } GType -nautilus_gdu_get_type (void) +nautilus_gdu_get_type (void) { return nautilus_gdu_type; } @@ -159,7 +332,7 @@ nautilus_gdu_register_type (GTypeModule *module) (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) nautilus_gdu_class_init, - NULL, + NULL, NULL, sizeof (NautilusGDU), 0, |