[gnome-photos/wip/rishi/online-miners] Add PhotosOnlineMinerManager



commit 59f307ab5dd60a486e9b7fad3834b4025b6f523a
Author: Debarshi Ray <debarshir gnome org>
Date:   Fri Mar 19 21:28:22 2021 +0100

    Add PhotosOnlineMinerManager

 src/meson.build                   |   1 +
 src/photos-online-miner-manager.c | 304 ++++++++++++++++++++++++++++++++++++++
 src/photos-online-miner-manager.h |  33 +++++
 3 files changed, 338 insertions(+)
---
diff --git a/src/meson.build b/src/meson.build
index 156c9891..02f34ee9 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -157,6 +157,7 @@ sources = common_sources + files(
   'photos-offset-import-controller.c',
   'photos-offset-overview-controller.c',
   'photos-offset-search-controller.c',
+  'photos-online-miner-manager.c',
   'photos-online-miner-process.c',
   'photos-organize-collection-dialog.c',
   'photos-organize-collection-model.c',
diff --git a/src/photos-online-miner-manager.c b/src/photos-online-miner-manager.c
new file mode 100644
index 00000000..f1abc0a6
--- /dev/null
+++ b/src/photos-online-miner-manager.c
@@ -0,0 +1,304 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2021 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "config.h"
+
+#include "photos-debug.h"
+#include "photos-online-miner-manager.h"
+#include "photos-online-miner-process.h"
+#include "photos-utils.h"
+
+
+struct _PhotosOnlineMinerManager
+{
+  GObject parent_instance;
+  GDBusServer *dbus_server;
+  GError *initialization_error;
+  GHashTable *provider_type_to_processes;
+  gboolean is_initialized;
+};
+
+static void photos_online_miner_manager_initable_iface_init (GInitableIface *iface);
+
+
+G_DEFINE_TYPE_WITH_CODE (PhotosOnlineMinerManager, photos_online_miner_manager, G_TYPE_OBJECT,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE, 
photos_online_miner_manager_initable_iface_init));
+
+
+G_LOCK_DEFINE_STATIC (init_lock);
+
+
+static gboolean
+photos_online_miner_manager_authorize_authenticated_peer (PhotosOnlineMinerManager *self,
+                                                          GIOStream *iostream,
+                                                          GCredentials *credentials)
+{
+  g_autoptr (GCredentials) own_credentials = NULL;
+  gboolean ret_val = FALSE;
+  g_autofree gchar *str = NULL;
+
+  g_mutex_lock (&self->mutex);
+
+  str = g_credentials_to_string (credentials);
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Received authorization request: %s", str);
+
+  if (self->connection != NULL)
+    {
+      g_warning ("Unable to authorize peer: Connection exists");
+      goto out;
+    }
+
+  if (credentials == NULL)
+    {
+      g_warning ("Unable to authorize peer: Credentials not found");
+      goto out;
+    }
+
+  own_credentials = g_credentials_new ();
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    if (!g_credentials_is_same_user (credentials, own_credentials, &error))
+      {
+        g_warning ("Unable to authorize peer: %s", error->message);
+        goto out;
+      }
+  }
+
+  ret_val = TRUE;
+
+ out:
+  g_mutex_unlock (&self->mutex);
+  return ret_val;
+}
+
+
+static gboolean
+photos_online_miner_manager_new_connection (PhotosOnlineMinerManager *self, GDBusConnection *connection)
+{
+  g_mutex_lock (&self->mutex);
+
+  g_assert_null (self->connection);
+
+  g_assert (self->thumbnailer_error == NULL);
+  g_assert (self->thumbnailer == NULL);
+
+  photos_debug (PHOTOS_DEBUG_THUMBNAILER, "Received new connection");
+
+  self->connection = g_object_ref (connection);
+  g_signal_connect_swapped (self->connection,
+                            "closed",
+                            G_CALLBACK (photos_online_miner_manager_connection_closed),
+                            self);
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    self->thumbnailer = photos_thumbnailer_dbus_proxy_new_sync (self->connection,
+                                                                G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES
+                                                                | G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                                                NULL,
+                                                                THUMBNAILER_PATH,
+                                                                NULL,
+                                                                &error);
+    if (error != NULL)
+      self->thumbnailer_error = g_error_copy (error);
+  }
+
+  g_cond_signal (&self->cond);
+  g_mutex_unlock (&self->mutex);
+
+  return TRUE;
+}
+
+
+static GObject *
+photos_online_miner_manager_constructor (GType type, guint n_construct_params, GObjectConstructParam 
*construct_params)
+{
+  static GObject *self = NULL;
+
+  if (self == NULL)
+    {
+      self = G_OBJECT_CLASS (photos_online_miner_manager_parent_class)->constructor (type,
+                                                                                     n_construct_params,
+                                                                                     construct_params);
+      g_object_add_weak_pointer (self, (gpointer) &self);
+      return self;
+    }
+
+  return g_object_ref (self);
+}
+
+
+static void
+photos_online_miner_manager_dispose (GObject *object)
+{
+  PhotosOnlineMinerManager *self = PHOTOS_ONLINE_MINER_MANAGER (object);
+
+  if (self->dbus_server != NULL)
+    {
+      g_dbus_server_stop (self->dbus_server);
+      g_clear_object (&self->dbus_server);
+    }
+
+  g_clear_pointer (&self->provider_type_to_processes, g_hash_table_unref);
+
+  G_OBJECT_CLASS (photos_online_miner_manager_parent_class)->dispose (object);
+}
+
+
+static void
+photos_online_miner_manager_finalize (GObject *object)
+{
+  PhotosOnlineMinerManager *self = PHOTOS_ONLINE_MINER_MANAGER (object);
+
+  g_clear_error (&self->initialization_error);
+
+  G_OBJECT_CLASS (photos_online_miner_manager_parent_class)->finalize (object);
+}
+
+
+static void
+photos_online_miner_manager_init (PhotosOnlineMinerManager *self)
+{
+  self->provider_type_to_processes = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
+}
+
+
+static void
+photos_online_miner_manager_class_init (PhotosOnlineMinerManagerClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+
+  object_class->constructor = photos_online_miner_manager_constructor;
+  object_class->dispose = photos_online_miner_manager_dispose;
+  object_class->finalize = photos_online_miner_manager_finalize;
+}
+
+
+static gboolean
+photos_online_miner_manager_initable_init (GInitable *initable, GCancellable *cancellable, GError **error)
+{
+  PhotosOnlineMinerManager *self = PHOTOS_ONLINE_MINER_MANAGER (initable);
+  g_autoptr (GDBusAuthObserver) observer = NULL;
+  GIOExtensionPoint *extension_point;
+  GList *extensions;
+  GList *l;
+  gboolean ret_val = FALSE;
+  const gchar *tmp_dir;
+  g_autofree gchar *address = NULL;
+  g_autofree gchar *guid = NULL;
+
+  G_LOCK (init_lock);
+
+  if (self->is_initialized)
+    {
+      if (self->dbus_server != NULL)
+        ret_val = TRUE;
+      else
+        g_assert_nonnull (self->initialization_error);
+
+      goto out;
+    }
+
+  g_assert_no_error (self->initialization_error);
+
+  tmp_dir = g_get_tmp_dir ();
+  address = g_strdup_printf ("unix:tmpdir=%s", tmp_dir);
+
+  guid = g_dbus_generate_guid ();
+
+  observer = g_dbus_auth_observer_new ();
+  g_signal_connect_swapped (observer,
+                            "authorize-authenticated-peer",
+                            G_CALLBACK (photos_online_miner_manager_authorize_authenticated_peer),
+                            self);
+
+  self->dbus_server = g_dbus_server_new_sync (address,
+                                              G_DBUS_SERVER_FLAGS_NONE,
+                                              guid,
+                                              observer,
+                                              cancellable,
+                                              &self->initialization_error);
+  if (G_UNLIKELY (self->initialization_error != NULL))
+    goto out;
+
+  g_signal_connect_swapped (self->dbus_server,
+                            "new-connection",
+                            G_CALLBACK (photos_online_miner_manager_new_connection),
+                            self);
+
+  g_dbus_server_start (self->dbus_server);
+
+  extension_point = g_io_extension_point_lookup (PHOTOS_BASE_ITEM_EXTENSION_POINT_NAME);
+  extensions = g_io_extension_point_get_extensions (extension_point);
+  for (l = extensions; l != NULL; l = l->next)
+    {
+      GIOExtension *extension = (GIOExtension *) l->data;
+      PhotosApplicationCreateData *data;
+      const gchar *extension_name;
+
+      extension_name = g_io_extension_get_name (extension);
+      if (g_strcmp0 (extension_name, "local") != 0)
+        {
+          g_autoptr (PhotosOnlineMinerProcess) process = NULL;
+          const gchar *address;
+          gboolean key_didnt_exist;
+
+          address = g_dbus_server_get_client_address (self->dbus_server);
+          process = photos_online_miner_process_new (address, extension_name);
+          key_didnt_exist = g_hash_table_insert (self->provider_type_to_processes,
+                                                 g_strdup (extension_name),
+                                                 g_object_ref (process));
+          g_assert (key_didnt_exist);
+        }
+    }
+
+  ret_val = TRUE;
+
+ out:
+  self->is_initialized = TRUE;
+  if (!ret_val)
+    {
+      g_assert_nonnull (self->initialization_error);
+      g_propagate_error (error, g_error_copy (self->initialization_error));
+    }
+
+  G_UNLOCK (init_lock);
+
+  return ret_val;
+}
+
+
+static void
+photos_online_miner_manager_initable_iface_init (GInitableIface *iface)
+{
+  iface->init = photos_online_miner_manager_initable_init;
+}
+
+
+PhotosOnlineMinerManager *
+photos_online_miner_manager_dup_singleton (GCancellable *cancellable, GError **error)
+{
+  g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  return g_initable_new (PHOTOS_TYPE_ONLINE_MINER_MANAGER, cancellable, error, NULL);
+}
diff --git a/src/photos-online-miner-manager.h b/src/photos-online-miner-manager.h
new file mode 100644
index 00000000..77f2ebe4
--- /dev/null
+++ b/src/photos-online-miner-manager.h
@@ -0,0 +1,33 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2021 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef PHOTOS_ONLINE_MINER_MANAGER_H
+#define PHOTOS_ONLINE_MINER_MANAGER_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_ONLINE_MINER_MANAGER (photos_online_miner_manager_get_type ())
+G_DECLARE_FINAL_TYPE (PhotosOnlineMinerManager, photos_online_miner_manager, PHOTOS, ONLINE_MINER_MANAGER, 
GObject);
+
+PhotosOnlineMinerManager  *photos_online_miner_manager_dup_singleton  (GCancellable *cancellable, GError 
**error);
+
+G_END_DECLS
+
+#endif /* PHOTOS_ONLINE_MINER_MANAGER_H */


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