[nautilus] Move clipboard information to NautilusClipboardMonitor.



commit 03ff6b4d60473db94f351b9b3b2c0ee9490556cc
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat May 15 15:36:38 2010 +0200

    Move clipboard information to NautilusClipboardMonitor.
    
    So that we can use that information from more than one view at the same
    time.

 libnautilus-private/nautilus-clipboard-monitor.c |   98 ++++++++++++++++++---
 libnautilus-private/nautilus-clipboard-monitor.h |   13 +++
 src/file-manager/fm-directory-view.c             |   77 ++++++++---------
 3 files changed, 132 insertions(+), 56 deletions(-)
---
diff --git a/libnautilus-private/nautilus-clipboard-monitor.c b/libnautilus-private/nautilus-clipboard-monitor.c
index 9e5de9b..5db5d29 100644
--- a/libnautilus-private/nautilus-clipboard-monitor.c
+++ b/libnautilus-private/nautilus-clipboard-monitor.c
@@ -24,11 +24,11 @@
 
 #include <config.h>
 #include "nautilus-clipboard-monitor.h"
+#include "nautilus-file.h"
 
 #include <eel/eel-debug.h>
 #include <eel/eel-gtk-macros.h>
 #include <eel/eel-glib-extensions.h>
-#include <eel/eel-glib-extensions.h>
 #include <gtk/gtk.h>
 
 /* X11 has a weakness when it comes to clipboard handling,
@@ -48,18 +48,17 @@
 
 enum {
 	CLIPBOARD_CHANGED,
+	CLIPBOARD_INFO,
 	LAST_SIGNAL
 };
 
-static guint signals[LAST_SIGNAL];
+struct NautilusClipboardMonitorDetails {
+	NautilusClipboardInfo *info;
+};
 
-static void nautilus_clipboard_monitor_init       (gpointer              object,
-						   gpointer              klass);
-static void nautilus_clipboard_monitor_class_init (gpointer              klass);
+static guint signals[LAST_SIGNAL];
 
-EEL_CLASS_BOILERPLATE (NautilusClipboardMonitor,
-		       nautilus_clipboard_monitor,
-		       G_TYPE_OBJECT)
+G_DEFINE_TYPE (NautilusClipboardMonitor, nautilus_clipboard_monitor, G_TYPE_OBJECT);
 
 static NautilusClipboardMonitor *clipboard_monitor = NULL;
 
@@ -97,12 +96,48 @@ nautilus_clipboard_monitor_emit_changed (void)
 	g_signal_emit (monitor, signals[CLIPBOARD_CHANGED], 0);
 }
 
+static NautilusClipboardInfo *
+nautilus_clipboard_info_new (GList *files,
+                             gboolean cut)
+{
+	NautilusClipboardInfo *info;
+
+	info = g_slice_new0 (NautilusClipboardInfo);
+	info->files = nautilus_file_list_copy (files);
+	info->cut = cut;
+
+	return info;
+}
+
+static NautilusClipboardInfo *
+nautilus_clipboard_info_copy (NautilusClipboardInfo *info)
+{
+	NautilusClipboardInfo *new_info;
+
+	new_info = NULL;
+
+	if (info != NULL) {
+		new_info = nautilus_clipboard_info_new (info->files,
+			                                info->cut);
+	}
+
+	return new_info;
+}
+
 static void
-nautilus_clipboard_monitor_init (gpointer object, gpointer klass)
+nautilus_clipboard_info_free (NautilusClipboardInfo *info)
 {
-	NautilusClipboardMonitor *monitor;
+	nautilus_file_list_free (info->files);
 
-	monitor = NAUTILUS_CLIPBOARD_MONITOR (object);
+	g_slice_free (NautilusClipboardInfo, info);
+}
+
+static void
+nautilus_clipboard_monitor_init (NautilusClipboardMonitor *monitor)
+{
+	monitor->details = 
+		G_TYPE_INSTANCE_GET_PRIVATE (monitor, NAUTILUS_TYPE_CLIPBOARD_MONITOR,
+		                             NautilusClipboardMonitorDetails);
 }	
 
 static void
@@ -112,16 +147,20 @@ clipboard_monitor_finalize (GObject *object)
 
 	monitor = NAUTILUS_CLIPBOARD_MONITOR (object);
 
-	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
+	if (monitor->details->info != NULL) {
+		nautilus_clipboard_info_free (monitor->details->info);
+		monitor->details->info = NULL;
+	}
+
+	G_OBJECT_CLASS (nautilus_clipboard_monitor_parent_class)->finalize (object);
 }
 
 static void
-nautilus_clipboard_monitor_class_init (gpointer klass)
+nautilus_clipboard_monitor_class_init (NautilusClipboardMonitorClass *klass)
 {
 	GObjectClass *object_class;
 
 	object_class = G_OBJECT_CLASS (klass);
-	
 	object_class->finalize = clipboard_monitor_finalize;
 
 	signals[CLIPBOARD_CHANGED] =
@@ -132,6 +171,37 @@ nautilus_clipboard_monitor_class_init (gpointer klass)
 		              NULL, NULL,
 		              g_cclosure_marshal_VOID__VOID,
 		              G_TYPE_NONE, 0);
+	signals[CLIPBOARD_INFO] =
+		g_signal_new ("clipboard_info",
+		              G_TYPE_FROM_CLASS (klass),
+		              G_SIGNAL_RUN_LAST,
+		              G_STRUCT_OFFSET (NautilusClipboardMonitorClass, clipboard_info),
+		              NULL, NULL,
+		              g_cclosure_marshal_VOID__POINTER,
+		              G_TYPE_NONE,
+		              1, G_TYPE_POINTER);
 
+	g_type_class_add_private (klass, sizeof (NautilusClipboardMonitorDetails));
 }
 
+void
+nautilus_clipboard_monitor_set_clipboard_info (NautilusClipboardMonitor *monitor,
+                                               NautilusClipboardInfo *info)
+{
+	if (monitor->details->info != NULL) {
+		nautilus_clipboard_info_free (monitor->details->info);
+		monitor->details->info = NULL;
+	}
+
+	monitor->details->info = nautilus_clipboard_info_copy (info);
+
+	g_signal_emit (monitor, signals[CLIPBOARD_INFO], 0, monitor->details->info);
+	
+	nautilus_clipboard_monitor_emit_changed ();
+}
+
+NautilusClipboardInfo *
+nautilus_clipboard_monitor_get_clipboard_info (NautilusClipboardMonitor *monitor)
+{
+	return monitor->details->info;
+}
diff --git a/libnautilus-private/nautilus-clipboard-monitor.h b/libnautilus-private/nautilus-clipboard-monitor.h
index 2a48078..cc79259 100644
--- a/libnautilus-private/nautilus-clipboard-monitor.h
+++ b/libnautilus-private/nautilus-clipboard-monitor.h
@@ -40,20 +40,33 @@
   (G_TYPE_INSTANCE_GET_CLASS ((obj), NAUTILUS_TYPE_CLIPBOARD_MONITOR, NautilusClipboardMonitorClass))
 
 typedef struct NautilusClipboardMonitorDetails NautilusClipboardMonitorDetails;
+typedef struct NautilusClipboardInfo NautilusClipboardInfo;
 
 typedef struct {
 	GObject parent_slot;
+
+	NautilusClipboardMonitorDetails *details;
 } NautilusClipboardMonitor;
 
 typedef struct {
 	GObjectClass parent_slot;
   
 	void (* clipboard_changed) (NautilusClipboardMonitor *monitor);
+	void (* clipboard_info) (NautilusClipboardMonitor *monitor,
+	                         NautilusClipboardInfo *info);
 } NautilusClipboardMonitorClass;
 
+struct NautilusClipboardInfo {
+	GList *files;
+	gboolean cut;
+};
+
 GType   nautilus_clipboard_monitor_get_type (void);
 
 NautilusClipboardMonitor *   nautilus_clipboard_monitor_get (void);
+void nautilus_clipboard_monitor_set_clipboard_info (NautilusClipboardMonitor *monitor,
+                                                    NautilusClipboardInfo *info);
+NautilusClipboardInfo * nautilus_clipboard_monitor_get_clipboard_info (NautilusClipboardMonitor *monitor);
 void nautilus_clipboard_monitor_emit_changed (void);
 
 #endif /* NAUTILUS_CLIPBOARD_MONITOR_H */
diff --git a/src/file-manager/fm-directory-view.c b/src/file-manager/fm-directory-view.c
index c91e070..b4f10ba 100644
--- a/src/file-manager/fm-directory-view.c
+++ b/src/file-manager/fm-directory-view.c
@@ -596,7 +596,6 @@ fm_directory_view_get_nautilus_window_slot (FMDirectoryView  *view)
 	return view->details->slot;
 }
 
-
 /* Returns the GtkWindow that this directory view occupies, or NULL
  * if at the moment this directory view is not in a GtkWindow or the
  * GtkWindow cannot be determined. Primarily used for parenting dialogs.
@@ -5754,14 +5753,8 @@ create_popup_menu (FMDirectoryView *view, const char *popup_path)
 	return GTK_MENU (menu);
 }
 
-typedef struct {
-	char **file_uris;
-        guint n_file_uris;
-	gboolean cut;
-} ClipboardInfo;
-
 static char *
-convert_file_list_to_string (ClipboardInfo *info,
+convert_file_list_to_string (NautilusClipboardInfo *info,
 			     gboolean format_for_text,
                              gsize *len)
 {
@@ -5769,6 +5762,7 @@ convert_file_list_to_string (ClipboardInfo *info,
 	char *uri, *tmp;
 	GFile *f;
         guint i;
+	GList *l;
 
 	if (format_for_text) {
 		uris = g_string_new (NULL);
@@ -5776,9 +5770,9 @@ convert_file_list_to_string (ClipboardInfo *info,
 		uris = g_string_new (info->cut ? "cut" : "copy");
 	}
 
-        for (i = 0; i < info->n_file_uris; ++i) {
-		uri = info->file_uris[i];
-		
+        for (i = 0, l = info->files; l != NULL; l = l->next, i++) {
+		uri = nautilus_file_get_uri (l->data);
+
 		if (format_for_text) {
 			f = g_file_new_for_uri (uri);
 			tmp = g_file_get_parse_name (f);
@@ -5792,13 +5786,15 @@ convert_file_list_to_string (ClipboardInfo *info,
 			}
 
 			/* skip newline for last element */
-			if (i + 1 < info->n_file_uris) {
+			if (i + 1 < g_list_length (info->files)) {
 				g_string_append_c (uris, '\n');
 			}
 		} else {
 			g_string_append_c (uris, '\n');
 			g_string_append (uris, uri);
 		}
+
+		g_free (uri);
 	}
 
         *len = uris->len;
@@ -5811,10 +5807,28 @@ get_clipboard_callback (GtkClipboard     *clipboard,
 			guint             info,
 			gpointer          user_data)
 {
-	ClipboardInfo *clipboard_info = user_data;
+	char **uris;
+	GList *l;
+	int i;
+	NautilusClipboardInfo *clipboard_info;
+
+	clipboard_info =
+		nautilus_clipboard_monitor_get_clipboard_info (nautilus_clipboard_monitor_get ());
 
         if (gtk_targets_include_uri (&selection_data->target, 1)) {
-                gtk_selection_data_set_uris (selection_data, clipboard_info->file_uris);
+		uris = g_malloc ((g_list_length (clipboard_info->files) + 1) * sizeof (char *));
+		i = 0;
+
+		for (l = clipboard_info->files; l != NULL; l = l->next) {
+			uris[i] = nautilus_file_get_uri (l->data);
+			i++;
+		}
+
+		uris[i] = NULL;
+
+		gtk_selection_data_set_uris (selection_data, uris);
+
+		g_strfreev (uris);
         } else if (gtk_targets_include_text (&selection_data->target, 1)) {
                 char *str;
                 gsize len;
@@ -5836,30 +5850,8 @@ static void
 clear_clipboard_callback (GtkClipboard *clipboard,
 			  gpointer      user_data)
 {
-	ClipboardInfo *info = user_data;
-
-        g_strfreev (info->file_uris);
-	g_slice_free (ClipboardInfo, info);
-}
-
-static ClipboardInfo *
-convert_file_list_to_uris (GList *files,
-                           gboolean cut)
-{
-        ClipboardInfo *info;
-        guint i;
-
-	info = g_slice_new (ClipboardInfo);
-        info->cut = cut;
-        info->n_file_uris = g_list_length (files);
-        info->file_uris = g_new (char *, info->n_file_uris + 1);
-
-        for (i = 0; files != NULL; files = files->next, ++i)
-                info->file_uris[i] = nautilus_file_get_uri (files->data);
-
-        info->file_uris[info->n_file_uris] = NULL;
-
-        return info;
+	nautilus_clipboard_monitor_set_clipboard_info (nautilus_clipboard_monitor_get (),
+	                                               NULL);
 }
 	
 static void
@@ -5869,12 +5861,13 @@ copy_or_cut_files (FMDirectoryView *view,
 {
 	int count;
 	char *status_string, *name;
-	ClipboardInfo *info;
+	NautilusClipboardInfo info;
         GtkTargetList *target_list;
         GtkTargetEntry *targets;
         int n_targets;
 
-	info = convert_file_list_to_uris (clipboard_contents, cut);
+	info.files = clipboard_contents;
+	info.cut = cut;
 
         target_list = gtk_target_list_new (NULL, 0);
         gtk_target_list_add (target_list, copied_files_atom, 0, 0);
@@ -5887,10 +5880,10 @@ copy_or_cut_files (FMDirectoryView *view,
 	gtk_clipboard_set_with_data (nautilus_clipboard_get (GTK_WIDGET (view)),
 				     targets, n_targets,
 				     get_clipboard_callback, clear_clipboard_callback,
-				     info);
+				     NULL);
         gtk_target_table_free (targets, n_targets);
 
-	nautilus_clipboard_monitor_emit_changed ();
+	nautilus_clipboard_monitor_set_clipboard_info (nautilus_clipboard_monitor_get (), &info);
 
 	count = g_list_length (clipboard_contents);
 	if (count == 1) {



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