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



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

    app: import

 src/photos-application.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 81 insertions(+)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index 98c483e6..117a1062 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -1046,17 +1046,98 @@ 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 (PhotosApplication *self)
 {
+  g_autoptr (GDateTime) date_created_latest = NULL;
+  g_autoptr (GFile) import_sub_dir = NULL;
+  GList *files = NULL;
+  GList *l;
+  GList *selection;
   GMount *mount;
   PhotosSource *source;
+  const gchar *pictures_path;
+  g_autofree gchar *date_created_latest_str = NULL;
+  g_autofree gchar *import_sub_path = NULL;
+  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));
+    }
+
+  date_created_latest = g_date_time_new_from_unix_local (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 (files,
+                                 import_sub_dir,
+                                 G_PRIORITY_DEFAULT,
+                                 NULL,
+                                 photos_application_import_copy_files,
+                                 self);
+
+  g_list_free_full (files, g_object_unref);
 }
 
 


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