[gnome-photos/wip/rishi/collection: 53/53] app: import



commit 7a669c5e21f64f31395cd61c3da466933e1e8a13
Author: Debarshi Ray <debarshir gnome org>
Date:   Sat Feb 3 13:05:50 2018 +0100

    app: import

 src/photos-application.c | 195 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 195 insertions(+)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index 98c483e6..5dc001d7 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -40,6 +40,7 @@
 #include "photos-application.h"
 #include "photos-base-item.h"
 #include "photos-camera-cache.h"
+#include "photos-create-collection-job.h"
 #include "photos-debug.h"
 #include "photos-dlna-renderers-dialog.h"
 #include "photos-export-dialog.h"
@@ -47,6 +48,7 @@
 #include "photos-filterable.h"
 #include "photos-gegl.h"
 #include "photos-glib.h"
+#include "photos-import-dialog.h"
 #include "photos-item-manager.h"
 #include "photos-main-window.h"
 #include "photos-properties-dialog.h"
@@ -174,6 +176,7 @@ static const gchar *DESKTOP_KEY_PRIMARY_COLOR = "primary-color";
 static const gchar *DESKTOP_KEY_SECONDARY_COLOR = "secondary-color";
 
 typedef struct _PhotosApplicationCreateData PhotosApplicationCreateData;
+typedef struct _PhotosApplicationImportData PhotosApplicationImportData;
 typedef struct _PhotosApplicationRefreshData PhotosApplicationRefreshData;
 typedef struct _PhotosApplicationSetBackgroundData PhotosApplicationSetBackgroundData;
 
@@ -184,6 +187,13 @@ struct _PhotosApplicationCreateData
   gchar *miner_name;
 };
 
+struct _PhotosApplicationImportData
+{
+  PhotosApplication *application;
+  GList *files;
+  gint64 ctime_latest;
+};
+
 struct _PhotosApplicationRefreshData
 {
   PhotosApplication *application;
@@ -229,6 +239,32 @@ photos_application_create_data_free (PhotosApplicationCreateData *data)
 }
 
 
+static PhotosApplicationImportData *
+photos_application_import_data_new (PhotosApplication *application, GList *files, gint64 ctime_latest)
+{
+  PhotosApplicationImportData *data;
+
+  data = g_slice_new0 (PhotosApplicationImportData);
+  g_application_hold (G_APPLICATION (application));
+  data->application = application;
+  data->files = g_list_copy_deep (files, (GCopyFunc) g_object_ref, NULL);
+  data->ctime_latest = ctime_latest;
+  return data;
+}
+
+
+static void
+photos_application_import_data_free (PhotosApplicationImportData *data)
+{
+  g_application_release (G_APPLICATION (data->application));
+  g_list_free_full (data->files, g_object_unref);
+  g_slice_free (PhotosApplicationImportData, data);
+}
+
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (PhotosApplicationImportData, photos_application_import_data_free);
+
+
 static PhotosApplicationRefreshData *
 photos_application_refresh_data_new (PhotosApplication *application, GomMiner *miner)
 {
@@ -1046,17 +1082,176 @@ photos_application_get_state (PhotosSearchContext *context)
 }
 
 
+static void
+photos_application_import_copy_files (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    if (!photos_utils_copy_files_finish (res, &error))
+      {
+        g_warning ("Unable to copy files: %s", error->message);
+        goto out;
+      }
+  }
+
+ out:
+  g_application_unmark_busy (G_APPLICATION (self));
+  g_application_release (G_APPLICATION (self));
+}
+
+
+static void
+photos_application_import_create_collection_executed (GObject *source_object,
+                                                      GAsyncResult *res,
+                                                      gpointer user_data)
+{
+  g_autoptr (PhotosApplicationImportData) data = (PhotosApplicationImportData *) user_data;
+  PhotosApplication *self = data->application;
+  PhotosCreateCollectionJob *col_job = PHOTOS_CREATE_COLLECTION_JOB (source_object);
+  g_autofree gchar *created_urn = NULL;
+
+  {
+    g_autoptr (GError) error = NULL;
+
+    created_urn = photos_create_collection_job_finish (col_job, res, &error);
+    if (error != NULL)
+      {
+        g_warning ("Unable to create collection: %s", error->message);
+        goto out;
+      }
+  }
+
+ out:
+  g_application_unmark_busy (G_APPLICATION (self));
+  return;
+}
+
+
+static void
+photos_application_import_response (GtkDialog *dialog, gint response_id, gpointer user_data)
+{
+  g_autoptr (PhotosApplicationImportData) data = (PhotosApplicationImportData *) user_data;
+  PhotosApplication *self = data->application;
+  g_autoptr (GDateTime) date_created_latest = NULL;
+  g_autoptr (GFile) import_sub_dir = NULL;
+  PhotosBaseItem *collection;
+  const gchar *name;
+  const gchar *pictures_path;
+  g_autofree gchar *date_created_latest_str = NULL;
+  g_autofree gchar *import_sub_path = NULL;
+
+  g_assert_true (PHOTOS_IS_IMPORT_DIALOG (dialog));
+
+  if (response_id != GTK_RESPONSE_OK)
+    goto out;
+
+  collection = photos_import_dialog_get_collection (PHOTOS_IMPORT_DIALOG (dialog));
+  name = photos_import_dialog_get_name (PHOTOS_IMPORT_DIALOG (dialog));
+  g_assert_true (PHOTOS_IS_BASE_ITEM (collection) && name == NULL || collection == NULL && name != NULL);
+
+  if (name != NULL)
+    {
+      g_autoptr (PhotosCreateCollectionJob) col_job = NULL;
+
+      col_job = photos_create_collection_job_new (name);
+
+      g_application_mark_busy (G_APPLICATION (self));
+      photos_create_collection_job_run (col_job,
+                                        NULL,
+                                        photos_application_import_create_collection_executed,
+                                        g_steal_pointer (&data));
+    }
+  else if (collection != NULL)
+    {
+    }
+
+  date_created_latest = g_date_time_new_from_unix_local (data->ctime_latest);
+
+  /* Translators: this is the default sub-directory where photos will
+   * be imported.
+   */
+  date_created_latest_str = g_date_time_format (date_created_latest, _("%-d %B %Y"));
+
+  pictures_path = g_get_user_special_dir (G_USER_DIRECTORY_PICTURES);
+  import_sub_path = g_build_filename (pictures_path, PHOTOS_IMPORT_SUBPATH, date_created_latest_str, NULL);
+  g_mkdir_with_parents (import_sub_path, 0777);
+
+  import_sub_dir = g_file_new_for_path (import_sub_path);
+
+  g_application_hold (G_APPLICATION (self));
+  g_application_mark_busy (G_APPLICATION (self));
+  photos_utils_copy_files_async (data->files,
+                                 import_sub_dir,
+                                 G_PRIORITY_DEFAULT,
+                                 NULL,
+                                 photos_application_import_copy_files,
+                                 self);
+
+ out:
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+
 static void
 photos_application_import (PhotosApplication *self)
 {
+  GList *files = NULL;
+  GList *l;
+  GList *selection;
   GMount *mount;
+  GtkWidget *dialog;
+  g_autoptr (PhotosApplicationImportData) data = NULL;
   PhotosSource *source;
+  gint64 ctime_latest = -1;
 
   source = PHOTOS_SOURCE (photos_base_manager_get_active_object (self->state->src_mngr));
   g_return_if_fail (PHOTOS_IS_SOURCE (source));
 
   mount = photos_source_get_mount (source);
   g_return_if_fail (G_IS_MOUNT (mount));
+
+  g_return_if_fail (photos_utils_get_selection_mode ());
+
+  selection = photos_selection_controller_get_selection (self->sel_cntrlr);
+  g_return_if_fail (selection != NULL);
+
+  for (l = selection; l != NULL; l = l->next)
+    {
+      g_autoptr (GFile) file = NULL;
+      PhotosBaseItem *item;
+      const gchar *uri;
+      const gchar *urn = (gchar *) l->data;
+      gint64 ctime;
+
+      item = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr, urn));
+
+      ctime = photos_base_item_get_date_created (item);
+      if (ctime < 0)
+        ctime = photos_base_item_get_mtime (item);
+
+      if (ctime > ctime_latest)
+        ctime_latest = ctime;
+
+      uri = photos_base_item_get_uri (item);
+      file = g_file_new_for_uri (uri);
+      files = g_list_prepend (files, g_object_ref (file));
+    }
+
+  g_assert_cmpint (ctime_latest, >=, 0);
+
+  dialog = photos_import_dialog_new (GTK_WINDOW (self->main_window), ctime_latest);
+  gtk_widget_show_all (dialog);
+
+  data = photos_application_import_data_new (self, files, ctime_latest);
+  g_signal_connect (dialog,
+                    "response",
+                    G_CALLBACK (photos_application_import_response),
+                    g_steal_pointer (&data));
+
+  g_list_free_full (files, g_object_unref);
 }
 
 


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