[gnome-photos/wip/rishi/online-miners: 23/24] Add PhotosOnlineMiner




commit eb18efd9f8ab82fd8b0ae24fff238ab6a1a86d26
Author: Debarshi Ray <debarshir gnome org>
Date:   Wed Mar 24 02:08:32 2021 +0100

    Add PhotosOnlineMiner
    
    https://gitlab.gnome.org/GNOME/gnome-photos/-/issues/83

 src/photos-online-miner.c | 396 ++++++++++++++++++++++++++++++++++++++++++++++
 src/photos-online-miner.h |  35 ++++
 2 files changed, 431 insertions(+)
---
diff --git a/src/photos-online-miner.c b/src/photos-online-miner.c
new file mode 100644
index 00000000..8148d249
--- /dev/null
+++ b/src/photos-online-miner.c
@@ -0,0 +1,396 @@
+/*
+ * 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 <locale.h>
+#include <stdlib.h>
+
+#include <glib/gi18n.h>
+
+#include "photos-debug.h"
+#include "photos-error.h"
+#include "photos-online-miner-dbus.h"
+#include "photos-online-miner.h"
+
+
+typedef struct _PhotosOnlineMinerPrivate PhotosOnlineMinerPrivate;
+
+struct _PhotosOnlineMinerPrivate
+{
+  GApplication parent_instance;
+  GDBusConnection *connection;
+  GHashTable *cancellables;
+  PhotosOnlineMinerDBus *skeleton;
+  gchar *address;
+};
+
+
+G_DEFINE_ABSTRACT_TYPE (PhotosOnlineMiner, photos_online_miner, 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 *ONLINE_MINER_PATH = "/org/gnome/Photos/OnlineMiner";
+
+
+static gboolean
+photos_online_miner_authorize_authenticated_peer (PhotosOnlineMiner *self,
+                                                  GIOStream *iostream,
+                                                  GCredentials *credentials)
+{
+  g_autoptr (GCredentials) own_credentials = NULL;
+  gboolean ret_val = FALSE;
+
+  if (credentials == NULL)
+    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:
+  return ret_val;
+}
+
+
+static gboolean
+photos_online_miner_handle_cancel (PhotosOnlineMiner *self, GDBusMethodInvocation *invocation, guint serial)
+{
+  GCancellable *cancellable;
+  GDBusConnection *connection;
+  GDBusMethodInvocation *invocation_ongoing;
+  GHashTableIter iter;
+
+  g_return_val_if_fail (PHOTOS_IS_ONLINE_MINER_GOOGLE (self), FALSE);
+  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
+
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner handling Cancel for %u", serial);
+  g_application_hold (G_APPLICATION (self));
+
+  connection = g_dbus_method_invocation_get_connection (invocation);
+
+  g_hash_table_iter_init (&iter, self->cancellables);
+  while (g_hash_table_iter_next (&iter, (gpointer *) &invocation_ongoing, (gpointer *) &cancellable))
+    {
+      GDBusConnection *connection_ongoing;
+      GDBusMessage *message_ongoing;
+      guint32 serial_ongoing;
+
+      connection_ongoing = g_dbus_method_invocation_get_connection (invocation_ongoing);
+      message_ongoing = g_dbus_method_invocation_get_message (invocation_ongoing);
+      serial_ongoing = g_dbus_message_get_serial (message_ongoing);
+
+      if (connection == connection_ongoing && (guint32) serial == serial_ongoing)
+        {
+          g_cancellable_cancel (cancellable);
+          photos_online_miner_dbus_complete_cancel (self->skeleton, invocation);
+          goto out;
+        }
+    }
+
+  g_dbus_method_invocation_return_error_literal (invocation, PHOTOS_ERROR, 0, "Invalid serial");
+
+ out:
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner completed Cancel");
+  g_application_release (G_APPLICATION (self));
+  return TRUE;
+}
+
+
+static void
+photos_online_miner_handle_insert_shared_content_insert_shared_content (GObject *source_object,
+                                                                        GAsyncResult *res,
+                                                                        gpointer user_data)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (source_object);
+  g_autoptr (GDBusMethodInvocation) invocation = G_DBUS_METHOD_INVOCATION (user_data);
+
+  /* { */
+  /*   g_autoptr (GError) error = NULL; */
+
+  /*   if (!photos_online_miner_google_insert_shared_content_finish (self, res, &error)) */
+  /*     { */
+  /*       g_dbus_method_invocation_take_error (invocation, g_steal_pointer (&error)); */
+  /*       goto out; */
+  /*     } */
+  /* } */
+
+  photos_online_miner_dbus_complete_insert_shared_content (self->skeleton, invocation);
+
+ out:
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner completed InsertSharedContent");
+  g_application_release (G_APPLICATION (self));
+  g_hash_table_remove (self->cancellables, invocation);
+}
+
+
+static gboolean
+photos_online_miner_handle_insert_shared_content (PhotosOnlineMiner *self,
+                                                  GDBusMethodInvocation *invocation,
+                                                  const gchar *account_id,
+                                                  const gchar *shared_id,
+                                                  const gchar *source_urn)
+{
+  g_autoptr (GCancellable) cancellable = NULL;
+
+  g_return_val_if_fail (PHOTOS_IS_ONLINE_MINER (self), FALSE);
+  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
+  g_return_val_if_fail (account_id != NULL && account_id[0] != '\0', FALSE);
+  g_return_val_if_fail (shared_id != NULL && shared_id[0] != '\0', FALSE);
+  g_return_val_if_fail (source_urn != NULL && source_urn[0] != '\0', FALSE);
+
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner handling InsertSharedContent");
+
+  cancellable = g_cancellable_new ();
+  g_hash_table_insert (self->cancellables, g_object_ref (invocation), g_object_ref (cancellable));
+
+  g_application_hold (G_APPLICATION (self));
+
+  return TRUE;
+}
+
+
+static void
+photos_online_miner_handle_refresh_db_refresh_db (GObject *source_object, GAsyncResult *res, gpointer 
user_data)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (source_object);
+  g_autoptr (GDBusMethodInvocation) invocation = G_DBUS_METHOD_INVOCATION (user_data);
+
+  /* { */
+  /*   g_autoptr (GError) error = NULL; */
+
+  /*   if (!photos_online_miner_google_refresh_db_finish (self, res, &error)) */
+  /*     { */
+  /*       g_dbus_method_invocation_take_error (invocation, g_steal_pointer (&error)); */
+  /*       goto out; */
+  /*     } */
+  /* } */
+
+  photos_online_miner_dbus_complete_refresh_db (self->skeleton, invocation);
+
+ out:
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner completed RefreshDB");
+  g_application_release (G_APPLICATION (self));
+  g_hash_table_remove (self->cancellables, invocation);
+}
+
+
+static gboolean
+photos_online_miner_handle_refresh_db (PhotosOnlineMiner *self, GDBusMethodInvocation *invocation)
+{
+  g_autoptr (GCancellable) cancellable = NULL;
+
+  g_return_val_if_fail (PHOTOS_IS_ONLINE_MINER (self), FALSE);
+  g_return_val_if_fail (G_IS_DBUS_METHOD_INVOCATION (invocation), FALSE);
+
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner handling RefreshDB");
+
+  cancellable = g_cancellable_new ();
+  g_hash_table_insert (self->cancellables, g_object_ref (invocation), g_object_ref (cancellable));
+
+  g_application_hold (G_APPLICATION (self));
+
+  return TRUE;
+}
+
+
+static gboolean
+photos_online_miner_google_dbus_register (GApplication *application,
+                                          GDBusConnection *connection,
+                                          const gchar *object_path,
+                                          GError **error)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (application);
+  g_autoptr (GDBusAuthObserver) observer = NULL;
+  gboolean ret_val = FALSE;
+
+  g_return_val_if_fail (self->skeleton == NULL, FALSE);
+
+  if (!G_APPLICATION_CLASS (photos_online_miner_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_online_miner_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;
+
+  self->skeleton = photos_online_miner_dbus_skeleton_new ();
+  g_signal_connect_swapped (self->skeleton, "handle-cancel", G_CALLBACK (photos_online_miner_handle_cancel), 
self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-insert-shared-content",
+                            G_CALLBACK (photos_online_miner_handle_insert_shared_content),
+                            self);
+  g_signal_connect_swapped (self->skeleton,
+                            "handle-refresh-db",
+                            G_CALLBACK (photos_online_miner_handle_refresh_db),
+                            self);
+
+  if (!g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (self->skeleton),
+                                         self->connection,
+                                         ONLINE_MINER_PATH,
+                                         error))
+    {
+      g_clear_object (&self->skeleton);
+      goto out;
+    }
+
+  ret_val = TRUE;
+
+ out:
+  return ret_val;
+}
+
+
+static void
+photos_online_miner_dbus_unregister (GApplication *application,
+                                     GDBusConnection *connection,
+                                     const gchar *object_path)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (application);
+
+  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_APPLICATION_CLASS (photos_online_miner_parent_class)->dbus_unregister (application, connection, 
object_path);
+}
+
+
+static gint
+photos_online_miner_handle_local_options (GApplication *application, GVariantDict *options)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (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_online_miner_shutdown (GApplication *application)
+{
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner exiting");
+  G_APPLICATION_CLASS (photos_online_miner_parent_class)->shutdown (application);
+}
+
+
+static void
+photos_online_miner_startup (GApplication *application)
+{
+  G_APPLICATION_CLASS (photos_online_miner_parent_class)->startup (application);
+  photos_debug (PHOTOS_DEBUG_ONLINE_MINER, "PhotosOnlineMiner ready");
+}
+
+
+static void
+photos_online_miner_dispose (GObject *object)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (object);
+
+  g_assert (self->skeleton == NULL);
+
+  g_clear_object (&self->connection);
+  g_clear_pointer (&self->cancellables, g_hash_table_unref);
+
+  G_OBJECT_CLASS (photos_online_miner_parent_class)->dispose (object);
+}
+
+
+static void
+photos_online_miner_finalize (GObject *object)
+{
+  PhotosOnlineMiner *self = PHOTOS_ONLINE_MINER (object);
+
+  g_free (self->address);
+
+  G_OBJECT_CLASS (photos_online_miner_parent_class)->finalize (object);
+}
+
+
+static void
+photos_online_miner_init (PhotosOnlineMiner *self)
+{
+  setlocale (LC_ALL, "");
+
+  bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
+  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+  textdomain (GETTEXT_PACKAGE);
+
+  g_application_set_flags (G_APPLICATION (self), G_APPLICATION_IS_SERVICE | G_APPLICATION_NON_UNIQUE);
+  g_application_set_inactivity_timeout (G_APPLICATION (self), INACTIVITY_TIMEOUT);
+
+  self->cancellables = g_hash_table_new_full (g_direct_hash, g_direct_equal, g_object_unref, g_object_unref);
+  g_application_add_main_option_entries (G_APPLICATION (self), COMMAND_LINE_OPTIONS);
+}
+
+
+static void
+photos_online_miner_class_init (PhotosOnlineMinerClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GApplicationClass *application_class = G_APPLICATION_CLASS (class);
+
+  object_class->dispose = photos_online_miner_dispose;
+  object_class->finalize = photos_online_miner_finalize;
+  application_class->dbus_register = photos_online_miner_dbus_register;
+  application_class->dbus_unregister = photos_online_miner_dbus_unregister;
+  application_class->handle_local_options = photos_online_miner_handle_local_options;
+  application_class->shutdown = photos_online_miner_shutdown;
+  application_class->startup = photos_online_miner_startup;
+}
diff --git a/src/photos-online-miner.h b/src/photos-online-miner.h
new file mode 100644
index 00000000..d6dc9fd3
--- /dev/null
+++ b/src/photos-online-miner.h
@@ -0,0 +1,35 @@
+/*
+ * 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/>.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_ONLINE_MINER_GOOGLE (photos_online_miner_google_get_type ())
+G_DECLARE_DERIVABLE_TYPE (PhotosOnlineMiner, photos_online_miner, PHOTOS, ONLINE_MINER, GApplication);
+
+struct _PhotosOnlineMinerClass
+{
+  GApplicationClass parent_class;
+
+  /* virtual methods */
+};
+
+G_END_DECLS


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