nautilus-3.6.3/libnautilus-private/nautilus-clipboard.c

No issues found

  1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
  2 
  3 /* nautilus-clipboard.c
  4  *
  5  * Nautilus Clipboard support.  For now, routines to support component cut
  6  * and paste.
  7  *
  8  * Copyright (C) 1999, 2000  Free Software Foundaton
  9  * Copyright (C) 2000, 2001  Eazel, Inc.
 10  *
 11  * This program is free software; you can redistribute it and/or
 12  * modify it under the terms of the GNU Library General Public License as
 13  * published by the Free Software Foundation; either version 2 of the
 14  * License, or (at your option) any later version.
 15  *
 16  * This program is distributed in the hope that it will be useful,
 17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 19  * Library General Public License for more details.
 20  *
 21  * You should have received a copy of the GNU Library General Public
 22  * License along with this program; if not, write to the
 23  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 24  * Boston, MA 02111-1307, USA.
 25  *
 26  * Authors: Rebecca Schulman <rebecka@eazel.com>,
 27  *          Darin Adler <darin@bentspoon.com>
 28  */
 29 
 30 #include <config.h>
 31 #include "nautilus-clipboard.h"
 32 #include "nautilus-file-utilities.h"
 33 
 34 #include <glib/gi18n.h>
 35 #include <gtk/gtk.h>
 36 #include <string.h>
 37 
 38 typedef struct _TargetCallbackData TargetCallbackData;
 39 
 40 typedef void (* SelectAllCallback)    (gpointer target);
 41 typedef void (* ConnectCallbacksFunc) (GObject            *object,
 42 				       TargetCallbackData *target_data);
 43 
 44 static void selection_changed_callback            (GtkWidget *widget,
 45 						   gpointer callback_data);
 46 static void owner_change_callback (GtkClipboard        *clipboard,
 47 				   GdkEventOwnerChange *event,
 48 				   gpointer callback_data);
 49 struct _TargetCallbackData {
 50 	GtkUIManager *ui_manager;
 51 	GtkActionGroup *action_group;
 52 	gboolean shares_selection_changes;
 53 
 54 	SelectAllCallback select_all_callback;
 55 
 56 	ConnectCallbacksFunc connect_callbacks;
 57 	ConnectCallbacksFunc disconnect_callbacks;
 58 };
 59 
 60 static void
 61 cut_callback (gpointer target)
 62 {
 63 	g_assert (target != NULL);
 64 
 65 	g_signal_emit_by_name (target, "cut-clipboard");
 66 }
 67 
 68 static void
 69 copy_callback (gpointer target)
 70 {
 71 	g_assert (target != NULL);
 72 
 73 	g_signal_emit_by_name (target, "copy-clipboard");
 74 }
 75 
 76 static void
 77 paste_callback (gpointer target)
 78 {
 79 	g_assert (target != NULL);
 80 
 81 	g_signal_emit_by_name (target, "paste-clipboard");
 82 }
 83 
 84 static void
 85 editable_select_all_callback (gpointer target)
 86 {
 87 	GtkEditable *editable;
 88 
 89 	editable = GTK_EDITABLE (target);
 90 	g_assert (editable != NULL);
 91 
 92 	gtk_editable_set_position (editable, -1);
 93 	gtk_editable_select_region (editable, 0, -1);
 94 }
 95 
 96 static void
 97 action_cut_callback (GtkAction *action,
 98 		     gpointer callback_data)
 99 {
100 	cut_callback (callback_data);
101 }
102 
103 static void
104 action_copy_callback (GtkAction *action,
105 		      gpointer callback_data)
106 {
107 	copy_callback (callback_data);
108 }
109 
110 static void
111 action_paste_callback (GtkAction *action,
112 		       gpointer callback_data)
113 {
114 	paste_callback (callback_data);
115 }
116 
117 static void
118 action_select_all_callback (GtkAction *action,
119 			    gpointer callback_data)
120 {
121 	TargetCallbackData *target_data;
122 
123 	g_assert (callback_data != NULL);
124 
125 	target_data = g_object_get_data (callback_data, "Nautilus:clipboard_target_data");
126 	g_assert (target_data != NULL);
127 
128 	target_data->select_all_callback (callback_data);
129 }
130 
131 static void
132 received_clipboard_contents (GtkClipboard     *clipboard,
133 			     GtkSelectionData *selection_data,
134 			     gpointer          data)
135 {
136 	GtkActionGroup *action_group;
137 	GtkAction *action;
138 
139 	action_group = data;
140 
141 	action = gtk_action_group_get_action (action_group,
142 					      "Paste");
143 	if (action != NULL) {
144 		gtk_action_set_sensitive (action,
145 					  gtk_selection_data_targets_include_text (selection_data));
146 	}
147 
148 	g_object_unref (action_group);
149 }
150 
151 
152 static void
153 set_paste_sensitive_if_clipboard_contains_data (GtkActionGroup *action_group)
154 {
155 	GtkAction *action;
156 	if (gdk_display_supports_selection_notification (gdk_display_get_default ())) {
157 		gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
158 						gdk_atom_intern ("TARGETS", FALSE),
159 						received_clipboard_contents,
160 						g_object_ref (action_group));
161 	} else {
162 		/* If selection notification isn't supported, always activate Paste */
163 		action = gtk_action_group_get_action (action_group,
164 						      "Paste");
165 		gtk_action_set_sensitive (action, TRUE);
166 	}
167 }
168 
169 static void
170 set_clipboard_menu_items_sensitive (GtkActionGroup *action_group)
171 {
172 	GtkAction *action;
173 
174 	action = gtk_action_group_get_action (action_group,
175 					      "Cut");
176 	gtk_action_set_sensitive (action, TRUE);
177 	action = gtk_action_group_get_action (action_group,
178 					      "Copy");
179 	gtk_action_set_sensitive (action, TRUE);
180 }
181 
182 static void
183 set_clipboard_menu_items_insensitive (GtkActionGroup *action_group)
184 {
185 	GtkAction *action;
186 
187 	action = gtk_action_group_get_action (action_group,
188 					      "Cut");
189 	gtk_action_set_sensitive (action, FALSE);
190 	action = gtk_action_group_get_action (action_group,
191 					      "Copy");
192 	gtk_action_set_sensitive (action, FALSE);
193 }
194 
195 static gboolean
196 clipboard_items_are_merged_in (GtkWidget *widget)
197 {
198 	return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
199 						   "Nautilus:clipboard_menu_items_merged"));
200 }
201 
202 static void
203 set_clipboard_items_are_merged_in (GObject *widget_as_object,
204 				   gboolean merged_in)
205 {
206 	g_object_set_data (widget_as_object,
207 			   "Nautilus:clipboard_menu_items_merged",
208 			   GINT_TO_POINTER (merged_in));
209 }
210 
211 static void
212 editable_connect_callbacks (GObject *object,
213 			    TargetCallbackData *target_data)
214 {
215 	g_signal_connect_after (object, "selection_changed",
216 				G_CALLBACK (selection_changed_callback), target_data);
217 	selection_changed_callback (GTK_WIDGET (object),
218 				    target_data);
219 }
220 
221 static void
222 editable_disconnect_callbacks (GObject *object,
223 			       TargetCallbackData *target_data)
224 {
225 	g_signal_handlers_disconnect_matched (object,
226 					      G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
227 					      0, 0, NULL,
228 					      G_CALLBACK (selection_changed_callback),
229 					      target_data);
230 }
231 
232 static void
233 merge_in_clipboard_menu_items (GObject *widget_as_object,
234 			       TargetCallbackData *target_data)
235 {
236 	gboolean add_selection_callback;
237 
238 	g_assert (target_data != NULL);
239 	
240 	add_selection_callback = target_data->shares_selection_changes;
241 
242 	gtk_ui_manager_insert_action_group (target_data->ui_manager,
243 					    target_data->action_group, 0);
244 
245 	set_paste_sensitive_if_clipboard_contains_data (target_data->action_group);
246 	
247 	g_signal_connect (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), "owner_change",
248 			  G_CALLBACK (owner_change_callback), target_data);
249 	
250 	if (add_selection_callback) {
251 		target_data->connect_callbacks (widget_as_object, target_data);
252 	} else {
253 		/* If we don't use sensitivity, everything should be on */
254 		set_clipboard_menu_items_sensitive (target_data->action_group);
255 	}
256 	set_clipboard_items_are_merged_in (widget_as_object, TRUE);
257 }
258 
259 static void
260 merge_out_clipboard_menu_items (GObject *widget_as_object,
261 				TargetCallbackData *target_data)
262 
263 {
264 	gboolean selection_callback_was_added;
265 
266 	g_assert (target_data != NULL);
267 
268 	gtk_ui_manager_remove_action_group (target_data->ui_manager,
269 					    target_data->action_group);
270 
271 	g_signal_handlers_disconnect_matched (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
272 					      G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
273 					      0, 0, NULL,
274 					      G_CALLBACK (owner_change_callback),
275 					      target_data);
276 	
277 	selection_callback_was_added = target_data->shares_selection_changes;
278 
279 	if (selection_callback_was_added) {
280 		target_data->disconnect_callbacks (widget_as_object, target_data);
281 	}
282 	set_clipboard_items_are_merged_in (widget_as_object, FALSE);
283 }
284 
285 static gboolean
286 focus_changed_callback (GtkWidget *widget,
287 			GdkEventAny *event,
288 			gpointer callback_data)
289 {
290 	/* Connect the component to the container if the widget has focus. */
291 	if (gtk_widget_has_focus (widget)) {
292 		if (!clipboard_items_are_merged_in (widget)) {
293 			merge_in_clipboard_menu_items (G_OBJECT (widget), callback_data);
294 		}
295 	} else {
296 		if (clipboard_items_are_merged_in (widget)) {
297 			merge_out_clipboard_menu_items (G_OBJECT (widget), callback_data);
298 		}
299 	}
300 
301 	return FALSE;
302 }
303 
304 static void
305 selection_changed_callback (GtkWidget *widget,
306 			    gpointer callback_data)
307 {
308 	TargetCallbackData *target_data;
309 	GtkEditable *editable;
310 	int start, end;
311 
312 	target_data = (TargetCallbackData *) callback_data;
313 	g_assert (target_data != NULL);
314 
315 	editable = GTK_EDITABLE (widget);
316 	g_assert (editable != NULL);
317 
318 	if (gtk_editable_get_selection_bounds (editable, &start, &end) && start != end) {
319 		set_clipboard_menu_items_sensitive (target_data->action_group);
320 	} else {
321 		set_clipboard_menu_items_insensitive (target_data->action_group);
322 	}
323 }
324 
325 static void
326 owner_change_callback (GtkClipboard        *clipboard,
327 		       GdkEventOwnerChange *event,
328 		       gpointer callback_data)
329 {
330 	TargetCallbackData *target_data;
331 
332 	g_assert (callback_data != NULL);
333 	target_data = callback_data;
334 
335 	set_paste_sensitive_if_clipboard_contains_data (target_data->action_group);
336 }
337 
338 static void
339 target_destroy_callback (GtkWidget *object,
340 			 gpointer callback_data)
341 {
342 	g_assert (callback_data != NULL);
343 
344 	if (clipboard_items_are_merged_in (object)) {
345 		merge_out_clipboard_menu_items (G_OBJECT (object), callback_data);
346 	}
347 }
348 
349 static void
350 target_data_free (TargetCallbackData *target_data)
351 {
352 	g_object_unref (target_data->action_group);
353 	g_free (target_data);
354 }
355 
356 static const GtkActionEntry clipboard_entries[] = {
357   /* name, stock id */      { "Cut", GTK_STOCK_CUT,
358   /* label, accelerator */    NULL, NULL,
359   /* tooltip */               N_("Cut the selected text to the clipboard"),
360                               G_CALLBACK (action_cut_callback) },
361   /* name, stock id */      { "Copy", GTK_STOCK_COPY,
362   /* label, accelerator */    NULL, NULL,
363   /* tooltip */               N_("Copy the selected text to the clipboard"),
364                               G_CALLBACK (action_copy_callback) },
365   /* name, stock id */      { "Paste", GTK_STOCK_PASTE,
366   /* label, accelerator */    NULL, NULL,
367   /* tooltip */               N_("Paste the text stored on the clipboard"),
368                               G_CALLBACK (action_paste_callback) },
369   /* name, stock id */      { "Select All", NULL,
370   /* label, accelerator */    N_("Select _All"), "<control>A",
371   /* tooltip */               N_("Select all the text in a text field"),
372                               G_CALLBACK (action_select_all_callback) },
373 };
374 
375 static TargetCallbackData *
376 initialize_clipboard_component_with_callback_data (GtkEditable *target,
377 						   GtkUIManager *ui_manager,
378 						   gboolean shares_selection_changes,
379 						   SelectAllCallback select_all_callback,
380 						   ConnectCallbacksFunc connect_callbacks,
381 						   ConnectCallbacksFunc disconnect_callbacks)
382 {
383 	GtkActionGroup *action_group;
384 	TargetCallbackData *target_data;
385 
386 	action_group = gtk_action_group_new ("ClipboardActions");
387 	gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
388 	gtk_action_group_add_actions (action_group, 
389 				      clipboard_entries, G_N_ELEMENTS (clipboard_entries),
390 				      target);
391 	
392 	/* Do the actual connection of the UI to the container at
393 	 * focus time, and disconnect at both focus and destroy
394 	 * time.
395 	 */
396 	target_data = g_new (TargetCallbackData, 1);
397 	target_data->ui_manager = ui_manager;
398 	target_data->action_group = action_group;
399 	target_data->shares_selection_changes = shares_selection_changes;
400 	target_data->select_all_callback = select_all_callback;
401 	target_data->connect_callbacks = connect_callbacks;
402 	target_data->disconnect_callbacks = disconnect_callbacks;
403 
404 	return target_data;
405 }
406 
407 static void
408 nautilus_clipboard_real_set_up (gpointer target,
409 				GtkUIManager *ui_manager,
410 				gboolean shares_selection_changes,
411 				SelectAllCallback select_all_callback,
412 				ConnectCallbacksFunc connect_callbacks,
413 				ConnectCallbacksFunc disconnect_callbacks)
414 {
415 	TargetCallbackData *target_data;
416 
417 	if (g_object_get_data (G_OBJECT (target), "Nautilus:clipboard_target_data") != NULL) {
418 		return;
419 	}
420 
421 	target_data = initialize_clipboard_component_with_callback_data
422 		(target, 
423 		 ui_manager,
424 		 shares_selection_changes,
425 		 select_all_callback,
426 		 connect_callbacks,
427 		 disconnect_callbacks);
428 
429 	g_signal_connect (target, "focus_in_event",
430 			  G_CALLBACK (focus_changed_callback), target_data);
431 	g_signal_connect (target, "focus_out_event",
432 			  G_CALLBACK (focus_changed_callback), target_data);
433 	g_signal_connect (target, "destroy",
434 			  G_CALLBACK (target_destroy_callback), target_data);
435 
436 	g_object_set_data_full (G_OBJECT (target), "Nautilus:clipboard_target_data",
437 				target_data, (GDestroyNotify) target_data_free);
438 
439 	/* Call the focus changed callback once to merge if the window is
440 	 * already in focus.
441 	 */
442 	focus_changed_callback (GTK_WIDGET (target), NULL, target_data);
443 }
444 
445 void
446 nautilus_clipboard_set_up_editable (GtkEditable *target,
447 				    GtkUIManager *ui_manager,
448 				    gboolean shares_selection_changes)
449 {
450 	g_return_if_fail (GTK_IS_EDITABLE (target));
451 	g_return_if_fail (GTK_IS_UI_MANAGER (ui_manager));
452 
453 	nautilus_clipboard_real_set_up (target, ui_manager,
454 					shares_selection_changes,
455 					editable_select_all_callback,
456 					editable_connect_callbacks,
457 					editable_disconnect_callbacks);
458 }
459 
460 static GList *
461 convert_lines_to_str_list (char **lines, gboolean *cut)
462 {
463 	int i;
464 	GList *result;
465 
466 	if (cut) {
467 		*cut = FALSE;
468 	}
469 
470 	if (lines[0] == NULL) {
471 		return NULL;
472 	}
473 
474 	if (strcmp (lines[0], "cut") == 0) {
475 		if (cut) {
476 			*cut = TRUE;
477 		}
478 	} else if (strcmp (lines[0], "copy") != 0) {
479 		return NULL;
480 	}
481 
482 	result = NULL;
483 	for (i = 1; lines[i] != NULL; i++) {
484 		result = g_list_prepend (result, g_strdup (lines[i]));
485 	}
486 	return g_list_reverse (result);
487 }
488 
489 GList*
490 nautilus_clipboard_get_uri_list_from_selection_data (GtkSelectionData *selection_data,
491 						     gboolean *cut,
492 						     GdkAtom copied_files_atom)
493 {
494 	GList *items;
495 	char **lines;
496 
497 	if (gtk_selection_data_get_data_type (selection_data) != copied_files_atom
498 	    || gtk_selection_data_get_length (selection_data) <= 0) {
499 		items = NULL;
500 	} else {
501 		gchar *data;
502 		/* Not sure why it's legal to assume there's an extra byte
503 		 * past the end of the selection data that it's safe to write
504 		 * to. But gtk_editable_selection_received does this, so I
505 		 * think it is OK.
506 		 */
507 		data = (gchar *) gtk_selection_data_get_data (selection_data);
508 		data[gtk_selection_data_get_length (selection_data)] = '\0';
509 		lines = g_strsplit (data, "\n", 0);
510 		items = convert_lines_to_str_list (lines, cut);
511 		g_strfreev (lines);
512 	}
513 	
514 	return items;
515 }
516 
517 GtkClipboard *
518 nautilus_clipboard_get (GtkWidget *widget)
519 {
520 	return gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (widget)),
521 					      GDK_SELECTION_CLIPBOARD);
522 }
523 
524 void
525 nautilus_clipboard_clear_if_colliding_uris (GtkWidget *widget,
526 					    const GList *item_uris,
527 					    GdkAtom copied_files_atom)
528 {
529 	GtkSelectionData *data;
530 	GList *clipboard_item_uris, *l;
531 	gboolean collision;
532 
533 	collision = FALSE;
534 	data = gtk_clipboard_wait_for_contents (nautilus_clipboard_get (widget),
535 						copied_files_atom);
536 	if (data == NULL) {
537 		return;
538 	}
539 
540 	clipboard_item_uris = nautilus_clipboard_get_uri_list_from_selection_data (data, NULL,
541 										   copied_files_atom);
542 
543 	for (l = (GList *) item_uris; l; l = l->next) {
544 		if (g_list_find_custom ((GList *) item_uris, l->data,
545 					(GCompareFunc) g_strcmp0)) {
546 			collision = TRUE;
547 			break;
548 		}
549 	}
550 	
551 	if (collision) {
552 		gtk_clipboard_clear (nautilus_clipboard_get (widget));
553 	}
554 	
555 	if (clipboard_item_uris) {
556 		g_list_free_full (clipboard_item_uris, g_free);
557 	}
558 }