Commit cb44eb32 authored by Carlos Soriano's avatar Carlos Soriano

clipboard: Use text based clipboard only

Now that we have better integration with the shell extension, and it
simplifies regular clipboard handling.

A nice feature is that now you can perform operations on GEdit or any
other text editor by writing the correct commands :D
parent dde5d529
Pipeline #22644 passed with stages
in 7 minutes and 7 seconds
......@@ -33,8 +33,6 @@
#include <gtk/gtk.h>
#include <string.h>
static GdkAtom copied_files_atom;
typedef struct
{
gboolean cut;
......@@ -47,7 +45,7 @@ convert_lines_to_str_list (char **lines)
int i;
GList *result;
if (lines[0] == NULL)
if (lines[0] == NULL || g_strcmp0 (lines[0], "x-special/nautilus-clipboard") != 0)
{
return NULL;
}
......@@ -77,7 +75,8 @@ convert_file_list_to_string (ClipboardInfo *info,
}
else
{
uris = g_string_new (info->cut ? "cut" : "copy");
uris = g_string_new ("x-special/nautilus-clipboard\n");
g_string_append (uris, info->cut ? "cut" : "copy");
}
for (i = 0, l = info->files; l != NULL; l = l->next, i++)
......@@ -125,8 +124,7 @@ get_item_list_from_selection_data (GtkSelectionData *selection_data)
GList *items;
char **lines;
if (gtk_selection_data_get_data_type (selection_data) != copied_files_atom
|| gtk_selection_data_get_length (selection_data) <= 0)
if (gtk_selection_data_get_length (selection_data) <= 0)
{
items = NULL;
}
......@@ -142,12 +140,31 @@ get_item_list_from_selection_data (GtkSelectionData *selection_data)
data[gtk_selection_data_get_length (selection_data)] = '\0';
lines = g_strsplit (data, "\n", 0);
items = convert_lines_to_str_list (lines);
if (items == NULL)
{
return NULL;
}
if (g_strcmp0 (items->data, "x-special/nautilus-clipboard") != 0)
{
return NULL;
}
if (!(g_strcmp0 (items->next->data, "cut") == 0 ||
g_strcmp0 (items->next->data, "copy") == 0))
{
return NULL;
}
g_strfreev (lines);
}
return items;
}
gboolean
nautilus_clipboard_is_data_valid_from_selection_data (GtkSelectionData *selection_data)
{
return nautilus_clipboard_get_uri_list_from_selection_data (selection_data) != NULL;
}
GList *
nautilus_clipboard_get_uri_list_from_selection_data (GtkSelectionData *selection_data)
{
......@@ -156,7 +173,9 @@ nautilus_clipboard_get_uri_list_from_selection_data (GtkSelectionData *selection
items = get_item_list_from_selection_data (selection_data);
if (items)
{
/* Line 0 is "cut" or "copy", so uris start at line 1. */
/* Line 0 is x-special/nautilus-clipboard. */
items = g_list_remove (items, items->data);
/* Line 1 is "cut" or "copy", so uris start at line 2. */
items = g_list_remove (items, items->data);
}
......@@ -180,7 +199,7 @@ nautilus_clipboard_clear_if_colliding_uris (GtkWidget *widget,
collision = FALSE;
data = gtk_clipboard_wait_for_contents (nautilus_clipboard_get (widget),
copied_files_atom);
nautilus_clipboard_get_atom());
if (data == NULL)
{
return;
......@@ -217,7 +236,7 @@ nautilus_clipboard_is_cut_from_selection_data (GtkSelectionData *selection_data)
items = get_item_list_from_selection_data (selection_data);
is_cut_from_selection_data = items != NULL &&
g_strcmp0 ((gchar *) items->data, "cut") == 0;
g_strcmp0 ((gchar *) items->next->data, "cut") == 0;
g_list_free_full (items, g_free);
......@@ -262,17 +281,8 @@ on_get_clipboard (GtkClipboard *clipboard,
char *str;
gsize len;
str = convert_file_list_to_string (clipboard_info, TRUE, &len);
gtk_selection_data_set_text (selection_data, str, len);
g_free (str);
}
else if (target == copied_files_atom)
{
char *str;
gsize len;
str = convert_file_list_to_string (clipboard_info, FALSE, &len);
gtk_selection_data_set (selection_data, copied_files_atom, 8, (guchar *) str, len);
gtk_selection_data_set_text (selection_data, str, len);
g_free (str);
}
}
......@@ -303,7 +313,7 @@ nautilus_clipboard_prepare_for_files (GtkClipboard *clipboard,
clipboard_info->files = nautilus_file_list_copy (files);
target_list = gtk_target_list_new (NULL, 0);
gtk_target_list_add (target_list, copied_files_atom, 0, 0);
gtk_target_list_add (target_list, nautilus_clipboard_get_atom (), 0, 0);
gtk_target_list_add_uri_targets (target_list, 0);
gtk_target_list_add_text_targets (target_list, 0);
......@@ -320,10 +330,5 @@ nautilus_clipboard_prepare_for_files (GtkClipboard *clipboard,
GdkAtom
nautilus_clipboard_get_atom (void)
{
if (!copied_files_atom)
{
copied_files_atom = gdk_atom_intern_static_string ("x-special/gnome-copied-files");
}
return copied_files_atom;
return gdk_atom_intern_static_string ("TEXT");
}
......@@ -33,3 +33,4 @@ void nautilus_clipboard_prepare_for_files (GtkClipboard *clipboard,
GList *files,
gboolean cut);
GdkAtom nautilus_clipboard_get_atom (void);
gboolean nautilus_clipboard_is_data_valid_from_selection_data (GtkSelectionData *selection_data);
......@@ -7048,9 +7048,9 @@ can_paste_into_file (NautilusFile *file)
}
static void
on_clipboard_contents_received (GtkClipboard *clipboard,
GtkSelectionData *selection_data,
gpointer user_data)
on_clipboard_contents_text_received (GtkClipboard *clipboard,
GtkSelectionData *selection_data,
gpointer user_data)
{
NautilusFilesViewPrivate *priv;
NautilusFilesView *view;
......@@ -7060,6 +7060,7 @@ on_clipboard_contents_received (GtkClipboard *clipboard,
gboolean selection_contains_recent;
gboolean selection_contains_starred;
GAction *action;
gboolean is_data_valid;
view = NAUTILUS_FILES_VIEW (user_data);
priv = nautilus_files_view_get_instance_private (view);
......@@ -7072,6 +7073,7 @@ on_clipboard_contents_received (GtkClipboard *clipboard,
return;
}
is_data_valid = nautilus_clipboard_is_data_valid_from_selection_data (selection_data);
settings_show_create_link = g_settings_get_boolean (nautilus_preferences,
NAUTILUS_PREFERENCES_SHOW_CREATE_LINK);
is_read_only = nautilus_files_view_is_read_only (view);
......@@ -7087,62 +7089,25 @@ on_clipboard_contents_received (GtkClipboard *clipboard,
can_link_from_copied_files &&
settings_show_create_link);
g_object_unref (view);
}
static void
on_clipboard_targets_received (GtkClipboard *clipboard,
GdkAtom *targets,
int n_targets,
gpointer user_data)
{
NautilusFilesViewPrivate *priv;
NautilusFilesView *view;
gboolean is_data_copied;
int i;
GAction *action;
view = NAUTILUS_FILES_VIEW (user_data);
priv = nautilus_files_view_get_instance_private (view);
is_data_copied = FALSE;
if (priv->slot == NULL ||
!priv->active)
{
/* We've been destroyed or became inactive since call */
g_object_unref (view);
return;
}
if (targets)
{
for (i = 0; i < n_targets; i++)
{
if (targets[i] == nautilus_clipboard_get_atom ())
{
is_data_copied = TRUE;
}
}
}
action = g_action_map_lookup_action (G_ACTION_MAP (priv->view_action_group),
"paste");
/* Take into account if the action was previously disabled for other reasons,
* like the directory not being writabble */
g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
is_data_copied && g_action_get_enabled (action));
is_data_valid && g_action_get_enabled (action));
action = g_action_map_lookup_action (G_ACTION_MAP (priv->view_action_group),
"paste-into");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
is_data_copied && g_action_get_enabled (action));
is_data_valid && g_action_get_enabled (action));
action = g_action_map_lookup_action (G_ACTION_MAP (priv->view_action_group),
"create-link");
g_simple_action_set_enabled (G_SIMPLE_ACTION (action),
is_data_copied && g_action_get_enabled (action));
is_data_valid && g_action_get_enabled (action));
g_object_unref (view);
}
......@@ -7688,17 +7653,10 @@ real_update_actions_state (NautilusFilesView *view)
!selection_contains_starred &&
priv->templates_present);
/* Actions that are related to the clipboard need request, request the data
* and update them once we have the data */
g_object_ref (view); /* Need to keep the object alive until we get the reply */
gtk_clipboard_request_targets (nautilus_clipboard_get (GTK_WIDGET (view)),
on_clipboard_targets_received,
view);
g_object_ref (view); /* Need to keep the object alive until we get the reply */
gtk_clipboard_request_contents (nautilus_clipboard_get (GTK_WIDGET (view)),
nautilus_clipboard_get_atom (),
on_clipboard_contents_received,
on_clipboard_contents_text_received,
view);
action = g_action_map_lookup_action (G_ACTION_MAP (view_action_group),
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment