[gnome-photos/wip/rishi/thumbnailer: 3/6] Add an out-of-process custom thumbnailer



commit 623489624f3af80c4d731cfa3539dd675b560124
Author: Debarshi Ray <debarshir gnome org>
Date:   Thu Feb 9 20:24:37 2017 +0100

    Add an out-of-process custom thumbnailer

 src/Makefile.am                 |   39 ++++++
 src/photos-thumbnailer-dbus.xml |   33 +++++
 src/photos-thumbnailer-main.c   |   54 ++++++++
 src/photos-thumbnailer.c        |  277 +++++++++++++++++++++++++++++++++++++++
 src/photos-thumbnailer.h        |   35 +++++
 5 files changed, 438 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index c5a9bce..528e740 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,5 +1,7 @@
 bin_PROGRAMS = gnome-photos
 
+libexec_PROGRAMS = gnome-photos-thumbnailer
+
 gnome_photos_built_sources = \
        photos-about-data.c \
        photos-about-data.h \
@@ -263,8 +265,26 @@ gnome_photos_SOURCES = \
        photos-main.c \
        $(NULL)
 
+gnome_photos_thumbnailer_built_sources = \
+       photos-thumbnailer-dbus.c \
+       photos-thumbnailer-dbus.h \
+       $(NULL)
+
+nodist_gnome_photos_thumbnailer_SOURCES = \
+       $(gnome_photos_thumbnailer_built_sources) \
+       $(NULL)
+
+gnome_photos_thumbnailer_SOURCES = \
+       photos-debug.c \
+       photos-debug.h \
+       photos-thumbnailer.c \
+       photos-thumbnailer.h \
+       photos-thumbnailer-main.c \
+       $(NULL)
+
 BUILT_SOURCES = \
        $(gnome_photos_built_sources) \
+       $(gnome_photos_thumbnailer_built_sources) \
        $(NULL)
 
 EXTRA_DIST = \
@@ -283,6 +303,7 @@ EXTRA_DIST = \
        photos-selection-toolbar.ui \
        photos-share-dialog.ui \
        photos-gom-miner.xml \
+       photos-thumbnailer-dbus.xml \
        photos-tracker-extract-priority.xml \
        photos-tracker-resources.xml \
        photos-dleyna-renderer-device.xml \
@@ -345,6 +366,16 @@ gnome_photos_LDADD = \
        $(top_builddir)/libgd/libgd.la \
        $(NULL)
 
+gnome_photos_thumbnailer_LDFLAGS = \
+       $(WARN_LDFLAGS) \
+       $(NULL)
+
+gnome_photos_thumbnailer_LDADD = \
+       $(GEGL_LIBS) \
+       $(GIO_LIBS) \
+       $(GLIB_LIBS) \
+       $(NULL)
+
 CLEANFILES = \
        $(BUILT_SOURCES) \
        stamp-photos-about-data.h \
@@ -491,6 +522,14 @@ photos-shell-search-provider2.h photos-shell-search-provider2.c: org.gnome.Shell
                --interface-prefix org.gnome.Shell. \
                $<
 
+photos-thumnailer-dbus.h photos-thumbnailer-dbus.c: photos-thumbnailer-dbus.xml
+       $(AM_V_GEN)gdbus-codegen \
+               --c-namespace Photos \
+               --generate-c-code photos-thumbnailer-dbus \
+               --interface-prefix org.gnome.Photos. \
+               --annotate "org.gnome.Photos.Thumbnailer" org.gtk.GDBus.C.Name ThumbnailerDBus \
+               $<
+
 photos-tracker-extract-priority.h photos-tracker-extract-priority.c: photos-tracker-extract-priority.xml
        $(AM_V_GEN)gdbus-codegen \
                --c-namespace Tracker \
diff --git a/src/photos-thumbnailer-dbus.xml b/src/photos-thumbnailer-dbus.xml
new file mode 100644
index 0000000..ecff8a8
--- /dev/null
+++ b/src/photos-thumbnailer-dbus.xml
@@ -0,0 +1,33 @@
+<!DOCTYPE node PUBLIC
+"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
+"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd";>
+
+<!--
+ Photos - access, organize and share your photos on GNOME
+ Copyright © 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ 02110-1301, USA.
+-->
+
+<node name="/" xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd";>
+  <interface name="org.gnome.Photos.Thumbnailer">
+    <method name="GenerateThumbnail">
+      <arg name="uri" type="s" direction="in" />
+      <arg name="mime_type" type="s" direction="in" />
+      <arg name="size" type="i" direction="in" />
+    </method>
+  </interface>
+</node>
diff --git a/src/photos-thumbnailer-main.c b/src/photos-thumbnailer-main.c
new file mode 100644
index 0000000..1f9c517
--- /dev/null
+++ b/src/photos-thumbnailer-main.c
@@ -0,0 +1,54 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <locale.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include "photos-debug.h"
+#include "photos-thumbnailer.h"
+
+
+gint
+main (gint argc, gchar *argv[])
+{
+  GApplication *app;
+  gint exit_status = 0;
+
+  setlocale (LC_ALL, "");
+
+  photos_debug_init ();
+
+  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+  textdomain (GETTEXT_PACKAGE);
+
+  g_set_prgname (PACKAGE_TARNAME "-thumbnailer");
+
+  app = photos_thumbnailer_new ();
+  exit_status = g_application_run (app, argc, argv);
+
+  g_object_unref (app);
+  return exit_status;
+}
diff --git a/src/photos-thumbnailer.c b/src/photos-thumbnailer.c
new file mode 100644
index 0000000..fb919fb
--- /dev/null
+++ b/src/photos-thumbnailer.c
@@ -0,0 +1,277 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <stdlib.h>
+
+#include <gegl.h>
+#include <glib/gi18n.h>
+
+#include "photos-debug.h"
+#include "photos-thumbnailer.h"
+#include "photos-thumbnailer-dbus.h"
+
+
+struct _PhotosThumbnailer
+{
+  GApplication parent_instance;
+  GDBusConnection *connection;
+  PhotosThumbnailerDBus *skeleton;
+  gchar *address;
+};
+
+
+G_DEFINE_TYPE (PhotosThumbnailer, photos_thumbnailer, G_TYPE_APPLICATION);
+
+
+enum
+{
+  INACTIVITY_TIMEOUT = 12000 /* ms */
+};
+
+static const GOptionEntry COMMAND_LINE_OPTIONS[] =
+{
+  { "address", 0, G_OPTION_FLAG_NONE, G_OPTION_ARG_STRING, NULL, N_("D-Bus address to use"), NULL},
+  { NULL }
+};
+
+static const gchar *THUMBNAILER_PATH = "/org/gnome/Photos/Thumbnailer";
+
+
+static gboolean
+photos_thumbnailer_authorize_authenticated_peer (PhotosThumbnailer *self,
+                                                 GIOStream *iostream,
+                                                 GCredentials *credentials)
+{
+  GCredentials *own_credentials = NULL;
+  GError *error;
+  gboolean ret_val = FALSE;
+
+  if (credentials == NULL)
+    goto out;
+
+  own_credentials = g_credentials_new ();
+
+  error = NULL;
+  if (!g_credentials_is_same_user (credentials, own_credentials, &error))
+    {
+      g_warning ("Unable to authorize peer: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
+  ret_val = TRUE;
+
+ out:
+  g_clear_object (&own_credentials);
+  return ret_val;
+}
+
+
+static gboolean
+photos_thumbnailer_generate_thumbnail (PhotosThumbnailer *self,
+                                       GDBusMethodInvocation *invocation,
+                                       const gchar *uri,
+                                       const gchar *mime_type,
+                                       const gchar *size)
+{
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Handling GenerateThumbnail for %s", uri);
+
+  g_application_hold (G_APPLICATION (self));
+
+  photos_thumbnailer_dbus_complete_generate_thumbnail (self->skeleton, invocation);
+
+  g_application_release (G_APPLICATION (self));
+  return TRUE;
+}
+
+
+static gboolean
+photos_thumbnailer_dbus_register (GApplication *application,
+                                  GDBusConnection *connection,
+                                  const gchar *object_path,
+                                  GError **error)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
+  GDBusAuthObserver *observer = NULL;
+  gboolean ret_val = FALSE;
+
+  if (!G_APPLICATION_CLASS (photos_thumbnailer_parent_class)->dbus_register (application,
+                                                                             connection,
+                                                                             object_path,
+                                                                             error))
+    goto out;
+
+  observer = g_dbus_auth_observer_new ();
+  g_signal_connect_swapped (observer,
+                            "authorize-authenticated-peer",
+                            G_CALLBACK (photos_thumbnailer_authorize_authenticated_peer),
+                            self);
+
+  self->connection = g_dbus_connection_new_for_address_sync (self->address,
+                                                             G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
+                                                             observer,
+                                                             NULL,
+                                                             error);
+  if (self->connection == NULL)
+    goto out;
+
+  g_message ("exporting skeleton");
+  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
+                                         self->connection,
+                                         THUMBNAILER_PATH,
+                                         error))
+    {
+      g_clear_object (&self->skeleton);
+      goto out;
+    }
+
+  g_message ("dbus register: %s %s", g_dbus_connection_get_unique_name (self->connection), object_path);
+
+  ret_val = TRUE;
+
+ out:
+  g_clear_object (&observer);
+  return ret_val;
+}
+
+
+static void
+photos_thumbnailer_dbus_unregister (GApplication *application,
+                                    GDBusConnection *connection,
+                                    const gchar *object_path)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
+
+  g_message ("dbus unregister: %li", g_get_monotonic_time ());
+
+  if (self->skeleton != NULL)
+    {
+      g_dbus_interface_skeleton_unexport_from_connection (G_DBUS_INTERFACE_SKELETON (self->skeleton),
+                                                          self->connection);
+      g_clear_object (&self->skeleton);
+    }
+
+  g_message ("dbus unregister: chain up");
+  G_APPLICATION_CLASS (photos_thumbnailer_parent_class)->dbus_unregister (application, connection, 
object_path);
+}
+
+
+static gint
+photos_thumbnailer_handle_local_options (GApplication *application, GVariantDict *options)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
+  gint ret_val = EXIT_FAILURE;
+
+  if (g_variant_dict_lookup (options, "address", "s", &self->address))
+    ret_val = -1;
+
+  return ret_val;
+}
+
+
+static void
+photos_thumbnailer_shutdown (GApplication *application)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
+
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Thumbnailer exiting");
+
+  G_APPLICATION_CLASS (photos_thumbnailer_parent_class)->shutdown (application);
+}
+
+
+static void
+photos_thumbnailer_startup (GApplication *application)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (application);
+
+  G_APPLICATION_CLASS (photos_thumbnailer_parent_class)->startup (application);
+
+  gegl_init (NULL, NULL);
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Thumbnailer ready");
+}
+
+
+static void
+photos_thumbnailer_dispose (GObject *object)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (object);
+
+  g_clear_object (&self->connection);
+  g_clear_object (&self->skeleton);
+
+  G_OBJECT_CLASS (photos_thumbnailer_parent_class)->dispose (object);
+}
+
+
+static void
+photos_thumbnailer_finalize (GObject *object)
+{
+  PhotosThumbnailer *self = PHOTOS_THUMBNAILER (object);
+
+  g_free (self->address);
+
+  if (g_application_get_is_registered (G_APPLICATION (self)))
+    gegl_exit ();
+
+  G_OBJECT_CLASS (photos_thumbnailer_parent_class)->finalize (object);
+}
+
+
+static void
+photos_thumbnailer_init (PhotosThumbnailer *self)
+{
+  self->skeleton = photos_thumbnailer_dbus_skeleton_new ();
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-generate-thumbnail",
+                            G_CALLBACK (photos_thumbnailer_generate_thumbnail),
+                            self);
+
+  g_application_add_main_option_entries (G_APPLICATION (self), COMMAND_LINE_OPTIONS);
+}
+
+
+static void
+photos_thumbnailer_class_init (PhotosThumbnailerClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GApplicationClass *application_class = G_APPLICATION_CLASS (class);
+
+  object_class->dispose = photos_thumbnailer_dispose;
+  object_class->finalize = photos_thumbnailer_finalize;
+  application_class->dbus_register = photos_thumbnailer_dbus_register;
+  application_class->dbus_unregister = photos_thumbnailer_dbus_unregister;
+  application_class->handle_local_options = photos_thumbnailer_handle_local_options;
+  application_class->shutdown = photos_thumbnailer_shutdown;
+  application_class->startup = photos_thumbnailer_startup;
+}
+
+
+GApplication *
+photos_thumbnailer_new (void)
+{
+  return g_object_new (PHOTOS_TYPE_THUMBNAILER,
+                       "flags", G_APPLICATION_IS_SERVICE | G_APPLICATION_NON_UNIQUE,
+                       "inactivity-timeout", INACTIVITY_TIMEOUT,
+                       NULL);
+}
diff --git a/src/photos-thumbnailer.h b/src/photos-thumbnailer.h
new file mode 100644
index 0000000..f4d287a
--- /dev/null
+++ b/src/photos-thumbnailer.h
@@ -0,0 +1,35 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2017 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., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_THUMBNAILER_H
+#define PHOTOS_THUMBNAILER_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_THUMBNAILER (photos_thumbnailer_get_type ())
+G_DECLARE_FINAL_TYPE (PhotosThumbnailer, photos_thumbnailer, PHOTOS, THUMBNAILER, GApplication);
+
+GApplication          *photos_thumbnailer_new                    (void);
+
+G_END_DECLS
+
+#endif /* PHOTOS_THUMBNAILER_H */


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