1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 
3 /* caja-clipboard.c
4  *
5  * Caja 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., 51 Franklin St, Fifth Floor,
24  * Boston, MA 02110-1301, USA.
25  *
26  * Authors: Rebecca Schulman <rebecka@eazel.com>,
27  *          Darin Adler <darin@bentspoon.com>
28  */
29 
30 #include <config.h>
31 #include "caja-clipboard.h"
32 #include "caja-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 {
51     GtkUIManager *ui_manager;
52     GtkActionGroup *action_group;
53     gboolean shares_selection_changes;
54 
55     SelectAllCallback select_all_callback;
56 
57     ConnectCallbacksFunc connect_callbacks;
58     ConnectCallbacksFunc disconnect_callbacks;
59 };
60 
61 static void
cut_callback(gpointer target)62 cut_callback (gpointer target)
63 {
64     g_assert (target != NULL);
65 
66     g_signal_emit_by_name (target, "cut-clipboard");
67 }
68 
69 static void
copy_callback(gpointer target)70 copy_callback (gpointer target)
71 {
72     g_assert (target != NULL);
73 
74     g_signal_emit_by_name (target, "copy-clipboard");
75 }
76 
77 static void
paste_callback(gpointer target)78 paste_callback (gpointer target)
79 {
80     g_assert (target != NULL);
81 
82     g_signal_emit_by_name (target, "paste-clipboard");
83 }
84 
85 static void
editable_select_all_callback(gpointer target)86 editable_select_all_callback (gpointer target)
87 {
88     GtkEditable *editable;
89 
90     editable = GTK_EDITABLE (target);
91     g_assert (editable != NULL);
92 
93     gtk_editable_set_position (editable, -1);
94     gtk_editable_select_region (editable, 0, -1);
95 }
96 
97 static void
text_view_select_all_callback(gpointer target)98 text_view_select_all_callback (gpointer target)
99 {
100     g_assert (GTK_IS_TEXT_VIEW (target));
101 
102     g_signal_emit_by_name (target, "select-all", TRUE);
103 }
104 
105 static void
action_cut_callback(GtkAction * action,gpointer callback_data)106 action_cut_callback (GtkAction *action,
107                      gpointer callback_data)
108 {
109     cut_callback (callback_data);
110 }
111 
112 static void
action_copy_callback(GtkAction * action,gpointer callback_data)113 action_copy_callback (GtkAction *action,
114                       gpointer callback_data)
115 {
116     copy_callback (callback_data);
117 }
118 
119 static void
action_paste_callback(GtkAction * action,gpointer callback_data)120 action_paste_callback (GtkAction *action,
121                        gpointer callback_data)
122 {
123     paste_callback (callback_data);
124 }
125 
126 static void
action_select_all_callback(GtkAction * action,gpointer callback_data)127 action_select_all_callback (GtkAction *action,
128                             gpointer callback_data)
129 {
130     TargetCallbackData *target_data;
131 
132     g_assert (callback_data != NULL);
133 
134     target_data = g_object_get_data (callback_data, "Caja:clipboard_target_data");
135     g_assert (target_data != NULL);
136 
137     target_data->select_all_callback (callback_data);
138 }
139 
140 static void
received_clipboard_contents(GtkClipboard * clipboard,GtkSelectionData * selection_data,gpointer data)141 received_clipboard_contents (GtkClipboard     *clipboard,
142                              GtkSelectionData *selection_data,
143                              gpointer          data)
144 {
145     GtkActionGroup *action_group;
146     GtkAction *action;
147 
148     action_group = data;
149 
150     G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
151     action = gtk_action_group_get_action (action_group,
152                                           "Paste");
153     if (action != NULL)
154     {
155         gtk_action_set_sensitive (action,
156                                   gtk_selection_data_targets_include_text (selection_data));
157     }
158     G_GNUC_END_IGNORE_DEPRECATIONS;
159 
160     g_object_unref (action_group);
161 }
162 
163 
164 static void
set_paste_sensitive_if_clipboard_contains_data(GtkActionGroup * action_group)165 set_paste_sensitive_if_clipboard_contains_data (GtkActionGroup *action_group)
166 {
167     if (gdk_display_supports_selection_notification (gdk_display_get_default ()))
168     {
169         gtk_clipboard_request_contents (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
170                                         gdk_atom_intern ("TARGETS", FALSE),
171                                         received_clipboard_contents,
172                                         g_object_ref (action_group));
173     }
174     else
175     {
176         GtkAction *action;
177 
178         /* If selection notification isn't supported, always activate Paste */
179         G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
180         action = gtk_action_group_get_action (action_group,
181                                               "Paste");
182         gtk_action_set_sensitive (action, TRUE);
183         G_GNUC_END_IGNORE_DEPRECATIONS;
184     }
185 }
186 
187 static void
set_clipboard_menu_items_sensitive(GtkActionGroup * action_group)188 set_clipboard_menu_items_sensitive (GtkActionGroup *action_group)
189 {
190     GtkAction *action;
191 
192     G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
193     action = gtk_action_group_get_action (action_group,
194                                           "Cut");
195     gtk_action_set_sensitive (action, TRUE);
196     action = gtk_action_group_get_action (action_group,
197                                           "Copy");
198     gtk_action_set_sensitive (action, TRUE);
199     G_GNUC_END_IGNORE_DEPRECATIONS;
200 }
201 
202 static void
set_clipboard_menu_items_insensitive(GtkActionGroup * action_group)203 set_clipboard_menu_items_insensitive (GtkActionGroup *action_group)
204 {
205     GtkAction *action;
206 
207     G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
208     action = gtk_action_group_get_action (action_group,
209                                           "Cut");
210     gtk_action_set_sensitive (action, FALSE);
211     action = gtk_action_group_get_action (action_group,
212                                           "Copy");
213     gtk_action_set_sensitive (action, FALSE);
214     G_GNUC_END_IGNORE_DEPRECATIONS;
215 }
216 
217 static gboolean
clipboard_items_are_merged_in(GtkWidget * widget)218 clipboard_items_are_merged_in (GtkWidget *widget)
219 {
220     return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget),
221                             "Caja:clipboard_menu_items_merged"));
222 }
223 
224 static void
set_clipboard_items_are_merged_in(GObject * widget_as_object,gboolean merged_in)225 set_clipboard_items_are_merged_in (GObject *widget_as_object,
226                                    gboolean merged_in)
227 {
228     g_object_set_data (widget_as_object,
229                        "Caja:clipboard_menu_items_merged",
230                        GINT_TO_POINTER (merged_in));
231 }
232 
233 static void
editable_connect_callbacks(GObject * object,TargetCallbackData * target_data)234 editable_connect_callbacks (GObject *object,
235                             TargetCallbackData *target_data)
236 {
237     g_signal_connect_after (object, "selection_changed",
238                             G_CALLBACK (selection_changed_callback), target_data);
239     selection_changed_callback (GTK_WIDGET (object),
240                                 target_data);
241 }
242 
243 static void
editable_disconnect_callbacks(GObject * object,TargetCallbackData * target_data)244 editable_disconnect_callbacks (GObject *object,
245                                TargetCallbackData *target_data)
246 {
247     g_signal_handlers_disconnect_matched (object,
248                                           G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
249                                           0, 0, NULL,
250                                           G_CALLBACK (selection_changed_callback),
251                                           target_data);
252 }
253 
254 static void
text_buffer_update_sensitivity(GtkTextBuffer * buffer,TargetCallbackData * target_data)255 text_buffer_update_sensitivity (GtkTextBuffer *buffer,
256                                 TargetCallbackData *target_data)
257 {
258     g_assert (GTK_IS_TEXT_BUFFER (buffer));
259     g_assert (target_data != NULL);
260 
261     if (gtk_text_buffer_get_selection_bounds (buffer, NULL, NULL))
262     {
263         set_clipboard_menu_items_sensitive (target_data->action_group);
264     }
265     else
266     {
267         set_clipboard_menu_items_insensitive (target_data->action_group);
268     }
269 }
270 
271 static void
text_buffer_delete_range(GtkTextBuffer * buffer,GtkTextIter * iter1,GtkTextIter * iter2,TargetCallbackData * target_data)272 text_buffer_delete_range (GtkTextBuffer *buffer,
273                           GtkTextIter   *iter1,
274                           GtkTextIter   *iter2,
275                           TargetCallbackData *target_data)
276 {
277     text_buffer_update_sensitivity (buffer, target_data);
278 }
279 
280 static void
text_buffer_mark_set(GtkTextBuffer * buffer,GtkTextIter * iter,GtkTextMark * mark,TargetCallbackData * target_data)281 text_buffer_mark_set (GtkTextBuffer *buffer,
282                       GtkTextIter *iter,
283                       GtkTextMark *mark,
284                       TargetCallbackData *target_data)
285 {
286     /* anonymous marks with NULL names refer to cursor moves */
287     if (gtk_text_mark_get_name (mark) != NULL)
288     {
289         text_buffer_update_sensitivity (buffer, target_data);
290     }
291 }
292 
293 static void
text_view_connect_callbacks(GObject * object,TargetCallbackData * target_data)294 text_view_connect_callbacks (GObject *object,
295                              TargetCallbackData *target_data)
296 {
297     GtkTextBuffer *buffer;
298 
299     buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (object));
300     g_assert (buffer);
301 
302     g_signal_connect_after (buffer, "mark-set",
303                             G_CALLBACK (text_buffer_mark_set), target_data);
304     g_signal_connect_after (buffer, "delete-range",
305                             G_CALLBACK (text_buffer_delete_range), target_data);
306     text_buffer_update_sensitivity (buffer, target_data);
307 }
308 
309 static void
text_view_disconnect_callbacks(GObject * object,TargetCallbackData * target_data)310 text_view_disconnect_callbacks (GObject *object,
311                                 TargetCallbackData *target_data)
312 {
313     GtkTextBuffer *buffer;
314 
315     buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (object));
316     g_assert (buffer);
317 
318     g_signal_handlers_disconnect_matched (buffer,
319                                           G_SIGNAL_MATCH_DATA,
320                                           0, 0, NULL, NULL,
321                                           target_data);
322 }
323 
324 static void
merge_in_clipboard_menu_items(GObject * widget_as_object,TargetCallbackData * target_data)325 merge_in_clipboard_menu_items (GObject *widget_as_object,
326                                TargetCallbackData *target_data)
327 {
328     gboolean add_selection_callback;
329 
330     g_assert (target_data != NULL);
331 
332     add_selection_callback = target_data->shares_selection_changes;
333 
334     gtk_ui_manager_insert_action_group (target_data->ui_manager,
335                                         target_data->action_group, 0);
336 
337     set_paste_sensitive_if_clipboard_contains_data (target_data->action_group);
338 
339     g_signal_connect (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD), "owner_change",
340                       G_CALLBACK (owner_change_callback), target_data);
341 
342     if (add_selection_callback)
343     {
344         target_data->connect_callbacks (widget_as_object, target_data);
345     }
346     else
347     {
348         /* If we don't use sensitivity, everything should be on */
349         set_clipboard_menu_items_sensitive (target_data->action_group);
350     }
351     set_clipboard_items_are_merged_in (widget_as_object, TRUE);
352 }
353 
354 static void
merge_out_clipboard_menu_items(GObject * widget_as_object,TargetCallbackData * target_data)355 merge_out_clipboard_menu_items (GObject *widget_as_object,
356                                 TargetCallbackData *target_data)
357 
358 {
359     gboolean selection_callback_was_added;
360 
361     g_assert (target_data != NULL);
362 
363     gtk_ui_manager_remove_action_group (target_data->ui_manager,
364                                         target_data->action_group);
365 
366     g_signal_handlers_disconnect_matched (gtk_clipboard_get (GDK_SELECTION_CLIPBOARD),
367                                           G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA,
368                                           0, 0, NULL,
369                                           G_CALLBACK (owner_change_callback),
370                                           target_data);
371 
372     selection_callback_was_added = target_data->shares_selection_changes;
373 
374     if (selection_callback_was_added)
375     {
376         target_data->disconnect_callbacks (widget_as_object, target_data);
377     }
378     set_clipboard_items_are_merged_in (widget_as_object, FALSE);
379 }
380 
381 static gboolean
focus_changed_callback(GtkWidget * widget,GdkEventAny * event,gpointer callback_data)382 focus_changed_callback (GtkWidget *widget,
383                         GdkEventAny *event,
384                         gpointer callback_data)
385 {
386     /* Connect the component to the container if the widget has focus. */
387     if (gtk_widget_has_focus (widget))
388     {
389         if (!clipboard_items_are_merged_in (widget))
390         {
391             merge_in_clipboard_menu_items (G_OBJECT (widget), callback_data);
392         }
393     }
394     else
395     {
396         if (clipboard_items_are_merged_in (widget))
397         {
398             merge_out_clipboard_menu_items (G_OBJECT (widget), callback_data);
399         }
400     }
401 
402     return FALSE;
403 }
404 
405 static void
selection_changed_callback(GtkWidget * widget,gpointer callback_data)406 selection_changed_callback (GtkWidget *widget,
407                             gpointer callback_data)
408 {
409     TargetCallbackData *target_data;
410     GtkEditable *editable;
411     int start, end;
412 
413     target_data = (TargetCallbackData *) callback_data;
414     g_assert (target_data != NULL);
415 
416     editable = GTK_EDITABLE (widget);
417     g_assert (editable != NULL);
418 
419     if (gtk_editable_get_selection_bounds (editable, &start, &end) && start != end)
420     {
421         set_clipboard_menu_items_sensitive (target_data->action_group);
422     }
423     else
424     {
425         set_clipboard_menu_items_insensitive (target_data->action_group);
426     }
427 }
428 
429 static void
owner_change_callback(GtkClipboard * clipboard,GdkEventOwnerChange * event,gpointer callback_data)430 owner_change_callback (GtkClipboard        *clipboard,
431                        GdkEventOwnerChange *event,
432                        gpointer callback_data)
433 {
434     TargetCallbackData *target_data;
435 
436     g_assert (callback_data != NULL);
437     target_data = callback_data;
438 
439     set_paste_sensitive_if_clipboard_contains_data (target_data->action_group);
440 }
441 
442 static void
target_destroy_callback(GtkWidget * object,gpointer callback_data)443 target_destroy_callback (GtkWidget *object,
444                          gpointer callback_data)
445 {
446     g_assert (callback_data != NULL);
447 
448     if (clipboard_items_are_merged_in (object))
449     {
450         merge_out_clipboard_menu_items (G_OBJECT (object), callback_data);
451     }
452 }
453 
454 static void
target_data_free(TargetCallbackData * target_data)455 target_data_free (TargetCallbackData *target_data)
456 {
457     g_object_unref (target_data->action_group);
458     g_free (target_data);
459 }
460 
461 static const GtkActionEntry clipboard_entries[] =
462 {
463     /* name, icon name */      { "Cut", "edit-cut",
464         /* label, accelerator */    N_("Cu_t"), NULL,
465         /* tooltip */               N_("Cut the selected text to the clipboard"),
466         G_CALLBACK (action_cut_callback)
467     },
468     /* name, icon name */      { "Copy", "edit-copy",
469         /* label, accelerator */    N_("_Copy"), NULL,
470         /* tooltip */               N_("Copy the selected text to the clipboard"),
471         G_CALLBACK (action_copy_callback)
472     },
473     /* name, icon name */      { "Paste", "edit-paste",
474         /* label, accelerator */    N_("_Paste"), NULL,
475         /* tooltip */               N_("Paste the text stored on the clipboard"),
476         G_CALLBACK (action_paste_callback)
477     },
478     /* name, icon name */      { "Select All", NULL,
479         /* label, accelerator */    N_("Select _All"), "<control>A",
480         /* tooltip */               N_("Select all the text in a text field"),
481         G_CALLBACK (action_select_all_callback)
482     },
483 };
484 
485 static TargetCallbackData *
initialize_clipboard_component_with_callback_data(GtkEditable * target,GtkUIManager * ui_manager,gboolean shares_selection_changes,SelectAllCallback select_all_callback,ConnectCallbacksFunc connect_callbacks,ConnectCallbacksFunc disconnect_callbacks)486 initialize_clipboard_component_with_callback_data (GtkEditable *target,
487         GtkUIManager *ui_manager,
488         gboolean shares_selection_changes,
489         SelectAllCallback select_all_callback,
490         ConnectCallbacksFunc connect_callbacks,
491         ConnectCallbacksFunc disconnect_callbacks)
492 {
493     GtkActionGroup *action_group;
494     TargetCallbackData *target_data;
495 
496     G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
497     action_group = gtk_action_group_new ("ClipboardActions");
498 #ifdef ENABLE_NLS
499     gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
500 #endif /* ENABLE_NLS */
501     gtk_action_group_add_actions (action_group,
502                                   clipboard_entries, G_N_ELEMENTS (clipboard_entries),
503                                   target);
504     G_GNUC_END_IGNORE_DEPRECATIONS;
505 
506     /* Do the actual connection of the UI to the container at
507      * focus time, and disconnect at both focus and destroy
508      * time.
509      */
510     target_data = g_new (TargetCallbackData, 1);
511     target_data->ui_manager = ui_manager;
512     target_data->action_group = action_group;
513     target_data->shares_selection_changes = shares_selection_changes;
514     target_data->select_all_callback = select_all_callback;
515     target_data->connect_callbacks = connect_callbacks;
516     target_data->disconnect_callbacks = disconnect_callbacks;
517 
518     return target_data;
519 }
520 
521 static void
caja_clipboard_real_set_up(gpointer target,GtkUIManager * ui_manager,gboolean shares_selection_changes,SelectAllCallback select_all_callback,ConnectCallbacksFunc connect_callbacks,ConnectCallbacksFunc disconnect_callbacks)522 caja_clipboard_real_set_up (gpointer target,
523                             GtkUIManager *ui_manager,
524                             gboolean shares_selection_changes,
525                             SelectAllCallback select_all_callback,
526                             ConnectCallbacksFunc connect_callbacks,
527                             ConnectCallbacksFunc disconnect_callbacks)
528 {
529     TargetCallbackData *target_data;
530 
531     if (g_object_get_data (G_OBJECT (target), "Caja:clipboard_target_data") != NULL)
532     {
533         return;
534     }
535 
536     target_data = initialize_clipboard_component_with_callback_data
537                   (target,
538                    ui_manager,
539                    shares_selection_changes,
540                    select_all_callback,
541                    connect_callbacks,
542                    disconnect_callbacks);
543 
544     g_signal_connect (target, "focus_in_event",
545                       G_CALLBACK (focus_changed_callback), target_data);
546     g_signal_connect (target, "focus_out_event",
547                       G_CALLBACK (focus_changed_callback), target_data);
548     g_signal_connect (target, "destroy",
549                       G_CALLBACK (target_destroy_callback), target_data);
550 
551     g_object_set_data_full (G_OBJECT (target), "Caja:clipboard_target_data",
552                             target_data, (GDestroyNotify) target_data_free);
553 
554     /* Call the focus changed callback once to merge if the window is
555      * already in focus.
556      */
557     focus_changed_callback (GTK_WIDGET (target), NULL, target_data);
558 }
559 
560 void
caja_clipboard_set_up_editable(GtkEditable * target,GtkUIManager * ui_manager,gboolean shares_selection_changes)561 caja_clipboard_set_up_editable (GtkEditable *target,
562                                 GtkUIManager *ui_manager,
563                                 gboolean shares_selection_changes)
564 {
565     g_return_if_fail (GTK_IS_EDITABLE (target));
566     g_return_if_fail (GTK_IS_UI_MANAGER (ui_manager));
567 
568     caja_clipboard_real_set_up (target, ui_manager,
569                                 shares_selection_changes,
570                                 editable_select_all_callback,
571                                 editable_connect_callbacks,
572                                 editable_disconnect_callbacks);
573 }
574 
575 void
caja_clipboard_set_up_text_view(GtkTextView * target,GtkUIManager * ui_manager)576 caja_clipboard_set_up_text_view (GtkTextView *target,
577                                  GtkUIManager *ui_manager)
578 {
579     g_return_if_fail (GTK_IS_TEXT_VIEW (target));
580     g_return_if_fail (GTK_IS_UI_MANAGER (ui_manager));
581 
582     caja_clipboard_real_set_up (target, ui_manager, TRUE,
583                                 text_view_select_all_callback,
584                                 text_view_connect_callbacks,
585                                 text_view_disconnect_callbacks);
586 }
587 
588 static GList *
convert_lines_to_str_list(char ** lines,gboolean * cut)589 convert_lines_to_str_list (char **lines, gboolean *cut)
590 {
591     int i;
592     GList *result;
593 
594     if (cut)
595     {
596         *cut = FALSE;
597     }
598 
599     if (lines[0] == NULL)
600     {
601         return NULL;
602     }
603 
604     if (strcmp (lines[0], "cut") == 0)
605     {
606         if (cut)
607         {
608             *cut = TRUE;
609         }
610     }
611     else if (strcmp (lines[0], "copy") != 0)
612     {
613         return NULL;
614     }
615 
616     result = NULL;
617     for (i = 1; lines[i] != NULL; i++)
618     {
619         result = g_list_prepend (result, g_strdup (lines[i]));
620     }
621     return g_list_reverse (result);
622 }
623 
624 GList*
caja_clipboard_get_uri_list_from_selection_data(GtkSelectionData * selection_data,gboolean * cut,GdkAtom copied_files_atom)625 caja_clipboard_get_uri_list_from_selection_data (GtkSelectionData *selection_data,
626         gboolean *cut,
627         GdkAtom copied_files_atom)
628 {
629     GList *items;
630 
631     if (gtk_selection_data_get_data_type (selection_data) != copied_files_atom
632             || gtk_selection_data_get_length (selection_data) <= 0)
633     {
634         items = NULL;
635     }
636     else
637     {
638         char **lines;
639         guchar *data;
640         /* Not sure why it's legal to assume there's an extra byte
641          * past the end of the selection data that it's safe to write
642          * to. But gtk_editable_selection_received does this, so I
643          * think it is OK.
644          */
645         data = (guchar *) gtk_selection_data_get_data (selection_data);
646         data[gtk_selection_data_get_length (selection_data)] = '\0';
647         lines = g_strsplit (data, "\n", 0);
648         items = convert_lines_to_str_list (lines, cut);
649         g_strfreev (lines);
650     }
651 
652     return items;
653 }
654 
655 GtkClipboard *
caja_clipboard_get(GtkWidget * widget)656 caja_clipboard_get (GtkWidget *widget)
657 {
658     return gtk_clipboard_get_for_display (gtk_widget_get_display (GTK_WIDGET (widget)),
659                                           GDK_SELECTION_CLIPBOARD);
660 }
661 
662 void
caja_clipboard_clear_if_colliding_uris(GtkWidget * widget,const GList * item_uris,GdkAtom copied_files_atom)663 caja_clipboard_clear_if_colliding_uris (GtkWidget *widget,
664                                         const GList *item_uris,
665                                         GdkAtom copied_files_atom)
666 {
667     GtkSelectionData *data;
668     GList *clipboard_item_uris, *l;
669     gboolean collision;
670 
671     collision = FALSE;
672     data = gtk_clipboard_wait_for_contents (caja_clipboard_get (widget),
673                                             copied_files_atom);
674     if (data == NULL) {
675         return;
676     }
677 
678     clipboard_item_uris = caja_clipboard_get_uri_list_from_selection_data (data, NULL,
679                           copied_files_atom);
680 
681     for (l = (GList *) item_uris; l; l = l->next) {
682         if (g_list_find_custom ((GList *) item_uris, l->data,
683                                 (GCompareFunc) g_strcmp0)) {
684             collision = TRUE;
685             break;
686         }
687     }
688 
689     if (collision) {
690         gtk_clipboard_clear (caja_clipboard_get (widget));
691     }
692 
693     if (clipboard_item_uris) {
694         g_list_free_full (clipboard_item_uris, g_free);
695     }
696 }
697