[mutter/wip/carlosg/clipboard-manager: 2/6] core: Add memory-based selection source



commit 687eff09aec37f33a42910b2aa5a1044febff6d1
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Nov 19 19:50:21 2018 +0100

    core: Add memory-based selection source
    
    This is a simple implementation of a MetaSelectionSource, able to hold a
    single mimetype, provided as GBytes.

 src/Makefile.am                         |   2 +
 src/core/meta-memory-selection-source.c | 112 ++++++++++++++++++++++++++++++++
 src/core/meta-memory-selection-source.h |  36 ++++++++++
 src/meson.build                         |   2 +
 4 files changed, 152 insertions(+)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 13a1c5394..d5c81791a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -289,6 +289,8 @@ libmutter_@LIBMUTTER_API_VERSION@_la_SOURCES =      \
        meta/meta-inhibit-shortcuts-dialog.h    \
        core/meta-inhibit-shortcuts-dialog-default.c    \
        core/meta-inhibit-shortcuts-dialog-default-private.h \
+       core/meta-memory-selection-source.c     \
+       core/meta-memory-selection-source.h     \
        core/delete.c                           \
        core/display.c                          \
        core/display-private.h                  \
diff --git a/src/core/meta-memory-selection-source.c b/src/core/meta-memory-selection-source.c
new file mode 100644
index 000000000..0fc9957ac
--- /dev/null
+++ b/src/core/meta-memory-selection-source.c
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2018 Red Hat
+ *
+ * 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: Carlos Garnacho <carlosg gnome org>
+ */
+#include "config.h"
+
+#include "core/meta-memory-selection-source.h"
+
+struct _MetaMemorySelectionSource
+{
+  MetaSelectionSource parent_instance;
+  char *mimetype;
+  GBytes *content;
+};
+
+G_DEFINE_TYPE (MetaMemorySelectionSource,
+               meta_memory_selection_source,
+               META_TYPE_SELECTION_SOURCE)
+
+void
+meta_memory_selection_source_read_async (MetaSelectionSource *source,
+                                         const gchar         *mimetype,
+                                         GCancellable        *cancellable,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
+{
+  MetaMemorySelectionSource *source_mem = META_MEMORY_SELECTION_SOURCE (source);
+  GInputStream *stream;
+  GTask *task;
+
+  if (g_strcmp0 (mimetype, source_mem->mimetype) != 0)
+    {
+      g_task_report_new_error (source, callback, user_data,
+                               meta_memory_selection_source_read_async,
+                               G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Mimetype not in selection");
+      return;
+    }
+
+  task = g_task_new (source, cancellable, callback, user_data);
+  g_task_set_source_tag (task, meta_memory_selection_source_read_async);
+
+  stream = g_memory_input_stream_new_from_bytes (source_mem->content);
+  g_task_return_pointer (task, stream, g_object_unref);
+}
+
+GInputStream *
+meta_memory_selection_source_read_finish (MetaSelectionSource  *source,
+                                          GAsyncResult         *result,
+                                          GError              **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, source), FALSE);
+  g_return_val_if_fail (g_task_get_source_tag (G_TASK (result)) ==
+                        meta_memory_selection_source_read_async, FALSE);
+
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+GList *
+meta_memory_selection_source_get_mimetypes (MetaSelectionSource  *source)
+{
+  MetaMemorySelectionSource *source_mem = META_MEMORY_SELECTION_SOURCE (source);
+
+  return g_list_prepend (NULL, g_strdup (source_mem->mimetype));
+}
+
+static void
+meta_memory_selection_source_class_init (MetaMemorySelectionSourceClass *klass)
+{
+  MetaSelectionSourceClass *source_class = META_SELECTION_SOURCE_CLASS (klass);
+
+  source_class->read_async = meta_memory_selection_source_read_async;
+  source_class->read_finish = meta_memory_selection_source_read_finish;
+  source_class->get_mimetypes = meta_memory_selection_source_get_mimetypes;
+}
+
+static void
+meta_memory_selection_source_init (MetaMemorySelectionSource *source)
+{
+}
+
+MetaSelectionSource *
+meta_memory_selection_source_new (const gchar *mimetype,
+                                  GBytes      *content)
+{
+  MetaMemorySelectionSource *source;
+
+  g_return_val_if_fail (mimetype != NULL, NULL);
+  g_return_val_if_fail (content != NULL, NULL);
+
+  source = g_object_new (META_TYPE_MEMORY_SELECTION_SOURCE, NULL);
+  source->mimetype = g_strdup (mimetype);
+  source->content = g_bytes_ref (content);
+
+  return META_SELECTION_SOURCE (source);
+}
diff --git a/src/core/meta-memory-selection-source.h b/src/core/meta-memory-selection-source.h
new file mode 100644
index 000000000..a20bbd361
--- /dev/null
+++ b/src/core/meta-memory-selection-source.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2018 Red Hat
+ *
+ * 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: Carlos Garnacho <carlosg gnome org>
+ */
+
+#ifndef META_MEMORY_SELECTION_SOURCE_H
+#define META_MEMORY_SELECTION_SOURCE_H
+
+#include "core/meta-selection-source.h"
+
+#define META_TYPE_MEMORY_SELECTION_SOURCE (meta_memory_selection_source_get_type ())
+G_DECLARE_FINAL_TYPE (MetaMemorySelectionSource,
+                      meta_memory_selection_source,
+                      META, MEMORY_SELECTION_SOURCE,
+                      MetaSelectionSource)
+
+MetaSelectionSource * meta_memory_selection_source_new (const gchar *mimetype,
+                                                        GBytes      *content);
+
+#endif /* META_MEMORY_SELECTION_SOURCE_H */
diff --git a/src/meson.build b/src/meson.build
index d81599704..7af37a55e 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -329,6 +329,8 @@ mutter_sources = [
   'core/meta-inhibit-shortcuts-dialog.c',
   'core/meta-inhibit-shortcuts-dialog-default.c',
   'core/meta-inhibit-shortcuts-dialog-default-private.h',
+  'core/meta-memory-selection-source.c',
+  'core/meta-memory-selection-source.h',
   'core/meta-selection.c',
   'core/meta-selection.h',
   'core/meta-selection-source.c',


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]