[gnome-photos/wip/rishi/collection: 52/52] app: import
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos/wip/rishi/collection: 52/52] app: import
- Date: Thu, 8 Feb 2018 11:00:20 +0000 (UTC)
commit 9f401ac89f1a20619ae53bd542865198f7418681
Author: Debarshi Ray <debarshir gnome org>
Date: Sat Feb 3 13:05:50 2018 +0100
app: import
src/photos-application.c | 499 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 499 insertions(+)
---
diff --git a/src/photos-application.c b/src/photos-application.c
index 98c483e6..05476075 100644
--- a/src/photos-application.c
+++ b/src/photos-application.c
@@ -36,10 +36,13 @@
#include <glib.h>
#include <glib/gi18n.h>
#include <grilo.h>
+#include <libgd/gd.h>
+#include <libtracker-control/tracker-control.h>
#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 +50,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"
@@ -59,6 +63,7 @@
#include "photos-search-type.h"
#include "photos-search-provider.h"
#include "photos-selection-controller.h"
+#include "photos-set-collection-job.h"
#include "photos-single-item-job.h"
#include "photos-source.h"
#include "photos-source-manager.h"
@@ -174,6 +179,9 @@ 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 _PhotosApplicationImportCopiedData PhotosApplicationImportCopiedData;
+typedef struct _PhotosApplicationImportWaitForFileData PhotosApplicationImportWaitForFileData;
typedef struct _PhotosApplicationRefreshData PhotosApplicationRefreshData;
typedef struct _PhotosApplicationSetBackgroundData PhotosApplicationSetBackgroundData;
@@ -184,6 +192,29 @@ struct _PhotosApplicationCreateData
gchar *miner_name;
};
+struct _PhotosApplicationImportCopiedData
+{
+ PhotosBaseItem *collection;
+ TrackerMinerManager *manager;
+ gchar *collection_urn;
+};
+
+struct _PhotosApplicationImportData
+{
+ PhotosApplication *application;
+ GList *files;
+ PhotosApplicationImportCopiedData copied_data;
+ gint64 ctime_latest;
+};
+
+struct _PhotosApplicationImportWaitForFileData
+{
+ PhotosApplication *application;
+ GFile *file;
+ PhotosBaseItem *collection;
+ gchar *collection_urn;
+};
+
struct _PhotosApplicationRefreshData
{
PhotosApplication *application;
@@ -229,6 +260,72 @@ photos_application_create_data_free (PhotosApplicationCreateData *data)
}
+static PhotosApplicationImportData *
+photos_application_import_data_new (PhotosApplication *application,
+ TrackerMinerManager *manager,
+ GList *files,
+ gint64 ctime_latest)
+{
+ PhotosApplicationImportData *data;
+
+ data = g_slice_new0 (PhotosApplicationImportData);
+ g_application_hold (G_APPLICATION (application));
+ data->application = application;
+ data->copied_data.manager = g_object_ref (manager);
+ 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_clear_object (&data->copied_data.collection);
+ g_object_unref (data->copied_data.manager);
+ g_free (data->copied_data.collection_urn);
+ g_slice_free (PhotosApplicationImportData, data);
+}
+
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (PhotosApplicationImportData, photos_application_import_data_free);
+
+
+static PhotosApplicationImportWaitForFileData *
+photos_application_import_wait_for_file_data_new (PhotosApplication *application,
+ GFile *file,
+ PhotosBaseItem *collection,
+ const gchar *collection_urn)
+{
+ PhotosApplicationImportWaitForFileData *data;
+
+ data = g_slice_new0 (PhotosApplicationImportWaitForFileData);
+ g_application_hold (G_APPLICATION (application));
+ data->application = application;
+ data->file = g_object_ref (file);
+ data->collection = collection == NULL ? NULL : g_object_ref (collection);
+ data->collection_urn = g_strdup (collection_urn);
+ return data;
+}
+
+
+static void
+photos_application_import_wait_for_file_data_free (PhotosApplicationImportWaitForFileData *data)
+{
+ g_application_release (G_APPLICATION (data->application));
+ g_object_unref (data->file);
+ g_clear_object (&data->collection);
+ g_free (data->collection_urn);
+ g_slice_free (PhotosApplicationImportWaitForFileData, data);
+}
+
+
+G_DEFINE_AUTOPTR_CLEANUP_FUNC (PhotosApplicationImportWaitForFileData,
+ photos_application_import_wait_for_file_data_free);
+
+
static PhotosApplicationRefreshData *
photos_application_refresh_data_new (PhotosApplication *application, GomMiner *miner)
{
@@ -1046,17 +1143,419 @@ photos_application_get_state (PhotosSearchContext *context)
}
+static void
+photos_application_import_copy_files (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ g_autoptr (PhotosApplicationImportData) data = (PhotosApplicationImportData *) user_data;
+ PhotosApplication *self = data->application;
+ PhotosBaseItem *collection;
+
+ collection = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr,
+ data->copied_data.collection_urn));
+ if (collection != NULL && gd_main_box_item_get_pulse (GD_MAIN_BOX_ITEM (collection)))
+ photos_base_item_unmark_busy (collection);
+
+ {
+ 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));
+}
+
+
+static void
+photos_application_import_index_file (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ PhotosApplication *self;
+ g_autoptr (GFile) file = G_FILE (user_data);
+ TrackerMinerManager *manager = TRACKER_MINER_MANAGER (source_object);
+
+ self = PHOTOS_APPLICATION (g_application_get_default ());
+
+ {
+ g_autoptr (GError) error = NULL;
+
+ if (!tracker_miner_manager_index_file_for_process_finish (manager, res, &error))
+ {
+ g_autofree gchar *uri = NULL;
+
+ uri = g_file_get_uri (file);
+ g_warning ("Unable to index %s: %s", uri, error->message);
+ }
+ }
+
+ g_application_unmark_busy (G_APPLICATION (self));
+ g_application_release (G_APPLICATION (self));
+}
+
+
+static void
+photos_application_import_single_item (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ PhotosApplication *self = PHOTOS_APPLICATION (user_data);
+ PhotosSingleItemJob *job = PHOTOS_SINGLE_ITEM_JOB (source_object);
+ TrackerSparqlCursor *cursor = NULL; /* TODO: use g_autoptr */
+
+ {
+ g_autoptr (GError) error = NULL;
+
+ cursor = photos_single_item_job_finish (job, res, &error);
+ if (error != NULL)
+ {
+ g_warning ("Unable to query single item: %s", error->message);
+ goto out;
+ }
+ }
+
+ if (cursor == NULL)
+ goto out;
+
+ photos_item_manager_add_item_for_mode (PHOTOS_ITEM_MANAGER (self->state->item_mngr),
+ G_TYPE_NONE,
+ PHOTOS_WINDOW_MODE_COLLECTIONS,
+ cursor);
+
+ out:
+ g_application_unmark_busy (G_APPLICATION (self));
+ g_application_release (G_APPLICATION (self));
+}
+
+
+static void
+photos_application_import_set_collection (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ g_autoptr (PhotosApplicationImportWaitForFileData) wait_for_file_data
+ = (PhotosApplicationImportWaitForFileData *) user_data;
+ PhotosApplication *self = wait_for_file_data->application;
+ PhotosBaseItem *collection;
+ PhotosSetCollectionJob *set_collection_job = PHOTOS_SET_COLLECTION_JOB (source_object);
+
+ {
+ g_autoptr (GError) error = NULL;
+
+ if (!photos_set_collection_job_finish (set_collection_job, res, &error))
+ {
+ g_autofree gchar *uri = NULL;
+
+ uri = g_file_get_uri (wait_for_file_data->file);
+ g_warning ("Unable to set collection for %s: %s", uri, error->message);
+ goto out;
+ }
+ }
+
+ collection = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr,
+ wait_for_file_data->collection_urn));
+ if (collection == NULL)
+ {
+ g_autoptr (PhotosSingleItemJob) single_item_job = NULL;
+
+ single_item_job = photos_single_item_job_new (wait_for_file_data->collection_urn);
+
+ g_application_hold (G_APPLICATION (self));
+ g_application_mark_busy (G_APPLICATION (self));
+ photos_single_item_job_run (single_item_job,
+ self->state,
+ PHOTOS_QUERY_FLAGS_COLLECTIONS,
+ NULL,
+ photos_application_import_single_item,
+ self);
+ }
+ else
+ {
+ photos_base_item_refresh (collection);
+ }
+
+ out:
+ g_application_unmark_busy (G_APPLICATION (self));
+}
+
+
+static void
+photos_application_import_wait_for_file (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+ g_autoptr (PhotosApplicationImportWaitForFileData) wait_for_file_data
+ = (PhotosApplicationImportWaitForFileData *) user_data;
+ PhotosApplication *self = wait_for_file_data->application;
+ g_autoptr (GList) urns = NULL;
+ PhotosItemManager *item_mngr = PHOTOS_ITEM_MANAGER (source_object);
+ g_autoptr (PhotosSetCollectionJob) job = NULL;
+ g_autofree gchar *id = NULL;
+
+ {
+ g_autoptr (GError) error = NULL;
+
+ id = photos_item_manager_wait_for_file_finish (item_mngr, res, &error);
+ if (error != NULL)
+ {
+ g_autofree gchar *uri = NULL;
+
+ uri = g_file_get_uri (wait_for_file_data->file);
+ g_warning ("Unable to detect %s: %s", uri, error->message);
+ goto out;
+ }
+ }
+
+ job = photos_set_collection_job_new (wait_for_file_data->collection_urn, TRUE);
+ urns = g_list_prepend (urns, id);
+
+ g_application_mark_busy (G_APPLICATION (self));
+ photos_set_collection_job_run (job,
+ self->state,
+ urns,
+ NULL,
+ photos_application_import_set_collection,
+ g_steal_pointer (&wait_for_file_data));
+
+ out:
+ return;
+}
+
+
+static void
+photos_application_import_copy_files_copied (GFile *source, GFile *destination, gpointer user_data)
+{
+ PhotosApplication *self;
+ PhotosApplicationImportCopiedData *copied_data = (PhotosApplicationImportCopiedData *) user_data;
+ g_autoptr (PhotosApplicationImportWaitForFileData) wait_for_file_data = NULL;
+ PhotosBaseItem *collection;
+
+ self = PHOTOS_APPLICATION (g_application_get_default ());
+
+ collection = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr,
+ copied_data->collection_urn));
+ if (collection != NULL && !gd_main_box_item_get_pulse (GD_MAIN_BOX_ITEM (collection)))
+ photos_base_item_mark_busy (collection);
+
+ wait_for_file_data = photos_application_import_wait_for_file_data_new (self,
+ destination,
+ copied_data->collection,
+ copied_data->collection_urn);
+ photos_item_manager_wait_for_file_async (PHOTOS_ITEM_MANAGER (self->state->item_mngr),
+ destination,
+ NULL,
+ photos_application_import_wait_for_file,
+ g_steal_pointer (&wait_for_file_data));
+
+ g_application_hold (G_APPLICATION (self));
+ g_application_mark_busy (G_APPLICATION (self));
+ tracker_miner_manager_index_file_for_process_async (copied_data->manager,
+ destination,
+ NULL,
+ photos_application_import_index_file,
+ g_object_ref (destination));
+}
+
+
+static void
+photos_application_import_add_to_collection (PhotosApplication *self, gpointer user_data)
+{
+ g_autoptr (PhotosApplicationImportData) data = (PhotosApplicationImportData *) user_data;
+ g_autoptr (GDateTime) date_created_latest = NULL;
+ g_autoptr (GFile) import_sub_dir = NULL;
+ GList *files = data->files;
+ PhotosApplicationImportCopiedData *copied_data = &data->copied_data;
+ PhotosBaseItem *collection;
+ const gchar *pictures_path;
+ g_autofree gchar *date_created_latest_str = NULL;
+ g_autofree gchar *import_sub_path = 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);
+
+ collection = PHOTOS_BASE_ITEM (photos_base_manager_get_object_by_id (self->state->item_mngr,
+ data->copied_data.collection_urn));
+ if (collection != NULL)
+ {
+ g_assert_null (data->copied_data.collection);
+ data->copied_data.collection = g_object_ref (collection);
+ }
+
+ 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_copied,
+ copied_data,
+ photos_application_import_copy_files,
+ g_steal_pointer (&data));
+}
+
+
+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;
+ }
+ }
+
+ g_assert_null (data->copied_data.collection_urn);
+ data->copied_data.collection_urn = g_steal_pointer (&created_urn);
+
+ photos_application_import_add_to_collection (self, g_steal_pointer (&data));
+
+ 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;
+ PhotosBaseItem *collection;
+ const gchar *name;
+
+ 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)
+ {
+ const gchar *id;
+
+ id = photos_filterable_get_id (PHOTOS_FILTERABLE (collection));
+
+ g_assert_null (data->copied_data.collection_urn);
+ data->copied_data.collection_urn = g_strdup (id);
+
+ photos_application_import_add_to_collection (self, g_steal_pointer (&data));
+ }
+ else
+ {
+ g_assert_not_reached ();
+ }
+
+ g_action_activate (G_ACTION (self->import_cancel_action), NULL);
+ photos_mode_controller_set_window_mode (self->state->mode_cntrlr, PHOTOS_WINDOW_MODE_COLLECTIONS);
+
+ 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;
+ TrackerMinerManager *manager = NULL; /* TODO: use g_autoptr */
+ 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);
+
+ {
+ g_autoptr (GError) error = NULL;
+
+ manager = tracker_miner_manager_new_full (FALSE, &error);
+ if (error != NULL)
+ {
+ g_warning ("Unable to create a TrackerMinerManager, importing from attached devices won't work: %s",
+ error->message);
+ goto out;
+ }
+ }
+
+ 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, manager, files, ctime_latest);
+ g_signal_connect (dialog,
+ "response",
+ G_CALLBACK (photos_application_import_response),
+ g_steal_pointer (&data));
+
+ out:
+ g_clear_object (&manager);
+ g_list_free_full (files, g_object_unref);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]