summaryrefslogtreecommitdiffstats
path: root/src/st/st-clipboard.c
blob: 4b730c9930122414ab9ff6b89ca6ebf8612532b2 (plain)
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
339
340
341
342
343
344
345
346
347
348
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * st-clipboard.c: clipboard object
 *
 * Copyright 2009 Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU Lesser General Public License,
 * version 2.1, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * SECTION:st-clipboard
 * @short_description: a simple representation of the clipboard
 *
 * #StCliboard is a very simple object representation of the clipboard
 * available to applications. Text is always assumed to be UTF-8 and non-text
 * items are not handled.
 */

#include "config.h"

#include "st-clipboard.h"

#include <meta/display.h>
#include <meta/meta-selection-source-memory.h>
#include <meta/meta-selection.h>

G_DEFINE_TYPE (StClipboard, st_clipboard, G_TYPE_OBJECT)

typedef struct _TransferData TransferData;
struct _TransferData
{
  StClipboard            *clipboard;
  GCallback               callback;
  gpointer                user_data;
  GOutputStream          *stream;
};

const char *supported_mimetypes[] = {
  "text/plain;charset=utf-8",
  "UTF8_STRING",
  "text/plain",
  "STRING",
};

static MetaSelection *meta_selection = NULL;

static void
st_clipboard_class_init (StClipboardClass *klass)
{
}

static void
st_clipboard_init (StClipboard *self)
{
}

/**
 * st_clipboard_get_default:
 *
 * Get the global #StClipboard object that represents the clipboard.
 *
 * Returns: (transfer none): a #StClipboard owned by St and must not be
 * unrefferenced or freed.
 */
StClipboard*
st_clipboard_get_default (void)
{
  static StClipboard *default_clipboard = NULL;

  if (!default_clipboard)
    {
      default_clipboard = g_object_new (ST_TYPE_CLIPBOARD, NULL);
    }

  return default_clipboard;
}

static gboolean
convert_type (StClipboardType    type,
              MetaSelectionType *type_out)
{
  if (type == ST_CLIPBOARD_TYPE_PRIMARY)
    *type_out = META_SELECTION_PRIMARY;
  else if (type == ST_CLIPBOARD_TYPE_CLIPBOARD)
    *type_out = META_SELECTION_CLIPBOARD;
  else
    return FALSE;

  return TRUE;
}

static const char *
pick_mimetype (MetaSelection     *meta_selection,
               MetaSelectionType  selection_type)
{
  const char *selected_mimetype = NULL;
  GList *mimetypes;
  int i;

  mimetypes = meta_selection_get_mimetypes (meta_selection, selection_type);

  for (i = 0; i < G_N_ELEMENTS (supported_mimetypes); i++)
    {
      if (g_list_find_custom (mimetypes, supported_mimetypes[i],
                              (GCompareFunc) g_strcmp0))
        {
          selected_mimetype = supported_mimetypes[i];
          break;
        }
    }

  g_list_free_full (mimetypes, g_free);
  return selected_mimetype;
}

static void
transfer_cb (MetaSelection *selection,
             GAsyncResult  *res,
             TransferData  *data)
{
  gchar *text = NULL;

  if (meta_selection_transfer_finish (selection, res, NULL))
    {
      gsize data_size;

      data_size =
        g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (data->stream));
      text = g_new0 (char, data_size + 1);
      memcpy (text, g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (data->stream)), data_size);
    }

  ((StClipboardCallbackFunc) data->callback) (data->clipboard, text,
                                              data->user_data);
  g_object_unref (data->stream);
  g_free (data);
  g_free (text);
}

static void
transfer_bytes_cb (MetaSelection *selection,
                   GAsyncResult  *res,
                   TransferData  *data)
{
  GBytes *bytes = NULL;

  if (meta_selection_transfer_finish (selection, res, NULL))
    bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (data->stream));

  ((StClipboardContentCallbackFunc) data->callback) (data->clipboard, bytes,
                                                     data->user_data);
  g_object_unref (data->stream);
  g_clear_pointer (&bytes, g_bytes_unref);
}

/**
 * st_clipboard_get_mimetypes:
 * @clipboard: a #StClipboard
 *
 * Gets a list of the mimetypes supported by the default #StClipboard.
 *
 * Returns: (element-type utf8) (transfer full): the supported mimetypes
 */
GList *
st_clipboard_get_mimetypes (StClipboard     *clipboard,
                            StClipboardType  type)
{
  MetaSelectionType selection_type;

  g_return_val_if_fail (ST_IS_CLIPBOARD (clipboard), NULL);
  g_return_val_if_fail (meta_selection != NULL, NULL);

  if (!convert_type (type, &selection_type))
    return NULL;

  return meta_selection_get_mimetypes (meta_selection, selection_type);
}

/**
 * st_clipboard_get_text:
 * @clipboard: A #StCliboard
 * @type: The type of clipboard data you want
 * @callback: (scope async): function to be called when the text is retrieved
 * @user_data: data to be passed to the callback
 *
 * Request the data from the clipboard in text form. @callback is executed
 * when the data is retrieved.
 */
void
st_clipboard_get_text (StClipboard            *clipboard,
                       StClipboardType         type,
                       StClipboardCallbackFunc callback,
                       gpointer                user_data)
{
  MetaSelectionType selection_type;
  TransferData *data;
  const char *mimetype = NULL;

  g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
  g_return_if_fail (meta_selection != NULL);
  g_return_if_fail (callback != NULL);

  if (convert_type (type, &selection_type))
    mimetype = pick_mimetype (meta_selection, selection_type);

  if (!mimetype)
    {
      callback (clipboard, NULL, user_data);
      return;
    }

  data = g_new0 (TransferData, 1);
  data->clipboard = clipboard;
  data->callback = G_CALLBACK (callback);
  data->user_data = user_data;
  data->stream = g_memory_output_stream_new_resizable ();

  meta_selection_transfer_async (meta_selection,
                                 selection_type,
                                 mimetype, -1,
                                 data->stream, NULL,
                                 (GAsyncReadyCallback) transfer_cb,
                                 data);
}

/**
 * st_clipboard_get_content:
 * @clipboard: A #StCliboard
 * @type: The type of clipboard data you want
 * @mimetype: The mimetype to get content for
 * @callback: (scope async): function to be called when the type is retrieved
 * @user_data: data to be passed to the callback
 *
 * Request the data from the clipboard in #GBytes form. @callback is executed
 * when the data is retrieved.
 */
void
st_clipboard_get_content (StClipboard                    *clipboard,
                          StClipboardType                 type,
                          const gchar                    *mimetype,
                          StClipboardContentCallbackFunc  callback,
                          gpointer                        user_data)
{
  MetaSelectionType selection_type;
  TransferData *data;

  g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
  g_return_if_fail (meta_selection != NULL);
  g_return_if_fail (callback != NULL);

  if (!mimetype || !convert_type (type, &selection_type))
    {
      callback (clipboard, NULL, user_data);
      return;
    }

  data = g_new0 (TransferData, 1);
  data->clipboard = clipboard;
  data->callback = G_CALLBACK (callback);
  data->user_data = user_data;
  data->stream = g_memory_output_stream_new_resizable ();

  meta_selection_transfer_async (meta_selection,
                                 selection_type,
                                 mimetype, -1,
                                 data->stream, NULL,
                                 (GAsyncReadyCallback) transfer_bytes_cb,
                                 data);
}

/**
 * st_clipboard_set_content:
 * @clipboard: A #StClipboard
 * @type: The type of clipboard that you want to set
 * @mimetype: content mimetype
 * @bytes: content data
 *
 * Sets the clipboard content to @bytes.
 *
 * @mimetype is a semi-colon separated list of mime-type strings.
 **/
void
st_clipboard_set_content (StClipboard     *clipboard,
                          StClipboardType  type,
                          const gchar     *mimetype,
                          GBytes          *bytes)
{
  MetaSelectionType selection_type;
  MetaSelectionSource *source;

  g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
  g_return_if_fail (meta_selection != NULL);
  g_return_if_fail (bytes != NULL);

  if (!convert_type (type, &selection_type))
    return;

  source = meta_selection_source_memory_new (mimetype, bytes);
  meta_selection_set_owner (meta_selection, selection_type, source);
  g_object_unref (source);
}

/**
 * st_clipboard_set_text:
 * @clipboard: A #StClipboard
 * @type: The type of clipboard that you want to set
 * @text: text to copy to the clipboard
 *
 * Sets text as the current contents of the clipboard.
 */
void
st_clipboard_set_text (StClipboard     *clipboard,
                       StClipboardType  type,
                       const gchar     *text)
{
  GBytes *bytes;

  g_return_if_fail (ST_IS_CLIPBOARD (clipboard));
  g_return_if_fail (meta_selection != NULL);
  g_return_if_fail (text != NULL);

  bytes = g_bytes_new_take (g_strdup (text), strlen (text));
  st_clipboard_set_content (clipboard, type, "text/plain;charset=utf-8", bytes);
  g_bytes_unref (bytes);
}

/**
 * st_clipboard_set_selection: (skip)
 *
 * Sets the #MetaSelection of the default #StClipboard.
 *
 * This function is called during the initialization of GNOME Shell.
 */
void
st_clipboard_set_selection (MetaSelection *selection)
{
  meta_selection = selection;
}