1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
caja-clipboard-monitor.c: catch clipboard changes.
Copyright (C) 2004 Red Hat, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Alexander Larsson <alexl@redhat.com>
*/
#include <config.h>
#include "caja-clipboard-monitor.h"
#include "caja-file.h"
#include <eel/eel-debug.h>
#include <eel/eel-gtk-macros.h>
#include <eel/eel-glib-extensions.h>
#include <gtk/gtk.h>
/* X11 has a weakness when it comes to clipboard handling,
* there is no way to get told when the owner of the clipboard
* changes. This is often needed, for instance to set the
* sensitivity of the paste menu item. We work around this
* internally in an app by telling the clipboard monitor when
* we changed the clipboard. Unfortunately this doesn't give
* us perfect results, we still don't catch changes made by
* other clients
*
* This is fixed with the XFIXES extensions, which recent versions
* of Gtk+ supports as the owner_change signal on GtkClipboard. We
* use this now, but keep the old code since not all X servers support
* XFIXES.
*/
enum
{
CLIPBOARD_CHANGED,
CLIPBOARD_INFO,
LAST_SIGNAL
};
struct CajaClipboardMonitorDetails
{
CajaClipboardInfo *info;
};
static guint signals[LAST_SIGNAL];
static GdkAtom copied_files_atom;
G_DEFINE_TYPE (CajaClipboardMonitor, caja_clipboard_monitor, G_TYPE_OBJECT);
static CajaClipboardMonitor *clipboard_monitor = NULL;
static void
destroy_clipboard_monitor (void)
{
if (clipboard_monitor != NULL)
{
g_object_unref (clipboard_monitor);
}
}
CajaClipboardMonitor *
caja_clipboard_monitor_get (void)
{
GtkClipboard *clipboard;
if (clipboard_monitor == NULL)
{
clipboard_monitor = CAJA_CLIPBOARD_MONITOR (g_object_new (CAJA_TYPE_CLIPBOARD_MONITOR, NULL));
eel_debug_call_at_shutdown (destroy_clipboard_monitor);
clipboard = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
g_signal_connect (clipboard, "owner_change",
G_CALLBACK (caja_clipboard_monitor_emit_changed), NULL);
}
return clipboard_monitor;
}
void
caja_clipboard_monitor_emit_changed (void)
{
CajaClipboardMonitor *monitor;
monitor = caja_clipboard_monitor_get ();
g_signal_emit (monitor, signals[CLIPBOARD_CHANGED], 0);
}
static CajaClipboardInfo *
caja_clipboard_info_new (GList *files,
gboolean cut)
{
CajaClipboardInfo *info;
info = g_slice_new0 (CajaClipboardInfo);
info->files = caja_file_list_copy (files);
info->cut = cut;
return info;
}
static CajaClipboardInfo *
caja_clipboard_info_copy (CajaClipboardInfo *info)
{
CajaClipboardInfo *new_info;
new_info = NULL;
if (info != NULL)
{
new_info = caja_clipboard_info_new (info->files,
info->cut);
}
return new_info;
}
static void
caja_clipboard_info_free (CajaClipboardInfo *info)
{
caja_file_list_free (info->files);
g_slice_free (CajaClipboardInfo, info);
}
static void
caja_clipboard_monitor_init (CajaClipboardMonitor *monitor)
{
monitor->details =
G_TYPE_INSTANCE_GET_PRIVATE (monitor, CAJA_TYPE_CLIPBOARD_MONITOR,
CajaClipboardMonitorDetails);
}
static void
clipboard_monitor_finalize (GObject *object)
{
CajaClipboardMonitor *monitor;
monitor = CAJA_CLIPBOARD_MONITOR (object);
if (monitor->details->info != NULL)
{
caja_clipboard_info_free (monitor->details->info);
monitor->details->info = NULL;
}
G_OBJECT_CLASS (caja_clipboard_monitor_parent_class)->finalize (object);
}
static void
caja_clipboard_monitor_class_init (CajaClipboardMonitorClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = clipboard_monitor_finalize;
copied_files_atom = gdk_atom_intern ("x-special/mate-copied-files", FALSE);
signals[CLIPBOARD_CHANGED] =
g_signal_new ("clipboard_changed",
G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (CajaClipboardMonitorClass, clipboard_changed),
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 (CajaClipboardMonitorClass, clipboard_info),
NULL, NULL,
g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE,
1, G_TYPE_POINTER);
g_type_class_add_private (klass, sizeof (CajaClipboardMonitorDetails));
}
void
caja_clipboard_monitor_set_clipboard_info (CajaClipboardMonitor *monitor,
CajaClipboardInfo *info)
{
if (monitor->details->info != NULL)
{
caja_clipboard_info_free (monitor->details->info);
monitor->details->info = NULL;
}
monitor->details->info = caja_clipboard_info_copy (info);
g_signal_emit (monitor, signals[CLIPBOARD_INFO], 0, monitor->details->info);
caja_clipboard_monitor_emit_changed ();
}
CajaClipboardInfo *
caja_clipboard_monitor_get_clipboard_info (CajaClipboardMonitor *monitor)
{
return monitor->details->info;
}
void
caja_clear_clipboard_callback (GtkClipboard *clipboard,
gpointer user_data)
{
caja_clipboard_monitor_set_clipboard_info
(caja_clipboard_monitor_get (), NULL);
}
static char *
convert_file_list_to_string (CajaClipboardInfo *info,
gboolean format_for_text,
gsize *len)
{
GString *uris;
char *uri, *tmp;
GFile *f;
guint i;
GList *l;
if (format_for_text)
{
uris = g_string_new (NULL);
}
else
{
uris = g_string_new (info->cut ? "cut" : "copy");
}
for (i = 0, l = info->files; l != NULL; l = l->next, i++)
{
uri = caja_file_get_uri (l->data);
if (format_for_text)
{
f = g_file_new_for_uri (uri);
tmp = g_file_get_parse_name (f);
g_object_unref (f);
if (tmp != NULL)
{
g_string_append (uris, tmp);
g_free (tmp);
}
else
{
g_string_append (uris, uri);
}
/* skip newline for last element */
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;
return g_string_free (uris, FALSE);
}
void
caja_get_clipboard_callback (GtkClipboard *clipboard,
GtkSelectionData *selection_data,
guint info,
gpointer user_data)
{
char **uris;
GList *l;
int i;
CajaClipboardInfo *clipboard_info;
GdkAtom target;
clipboard_info =
caja_clipboard_monitor_get_clipboard_info (caja_clipboard_monitor_get ());
target = gtk_selection_data_get_target (selection_data);
if (gtk_targets_include_uri (&target, 1))
{
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] = caja_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 (&target, 1))
{
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, str, len);
g_free (str);
}
}
|