Commit aa6a2345 authored by Paolo Bacchilega's avatar Paolo Bacchilega
Browse files

organize imported files automatically

and create a catalog with the imported files
parent 4f76bf35
......@@ -159,10 +159,10 @@ catalog_ready_cb (GObject *catalog,
static void
add_data_exec (AddData *add_data)
{
gth_catalog_load_from_file (add_data->catalog_file,
NULL,
catalog_ready_cb,
add_data);
gth_catalog_load_from_file_async (add_data->catalog_file,
NULL,
catalog_ready_cb,
add_data);
}
......
......@@ -243,5 +243,5 @@ dlg_catalog_properties (GthBrowser *browser,
gtk_window_set_transient_for (GTK_WINDOW (data->dialog), GTK_WINDOW (browser));
gtk_window_set_modal (GTK_WINDOW (data->dialog), TRUE);
gth_catalog_load_from_file (file_data->file, NULL, catalog_ready_cb, data);
gth_catalog_load_from_file_async (file_data->file, NULL, catalog_ready_cb, data);
}
......@@ -362,6 +362,20 @@ gth_catalog_get_order (GthCatalog *catalog,
}
void
gth_catalog_set_for_date (GthCatalog *catalog,
GthDateTime *date_time)
{
GFile *catalog_file;
gth_catalog_set_date (catalog, date_time);
catalog_file = gth_catalog_get_file_for_date (date_time);
gth_catalog_set_file (catalog, catalog_file);
g_object_unref (catalog_file);
}
void
gth_catalog_load_from_data (GthCatalog *catalog,
const void *buffer,
......@@ -605,6 +619,138 @@ gth_catalog_cancel (GthCatalog *catalog)
}
static char *
get_display_name (GFile *file,
const char *name,
GthDateTime *date_time)
{
GString *display_name;
char *basename;
display_name = g_string_new ("");
basename = g_file_get_basename (file);
if ((basename == NULL) || (strcmp (basename, "/") == 0)) {
g_string_append (display_name, _("Catalogs"));
}
else {
if ((name == NULL) && ! gth_datetime_valid (date_time)) {
char *name;
char *utf8_name;
name = _g_uri_remove_extension (basename);
utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
g_string_append (display_name, utf8_name);
g_free (utf8_name);
g_free (name);
}
else {
if (name != NULL)
g_string_append (display_name, name);
if (gth_datetime_valid (date_time)) {
char *formatted;
formatted = gth_datetime_strftime (date_time, "%x");
if ((name == NULL) || (strstr (name, formatted) == NULL)) {
if (name != NULL)
g_string_append (display_name, " (");
g_string_append (display_name, formatted);
if (name != NULL)
g_string_append (display_name, ")");
}
g_free (formatted);
}
}
}
return g_string_free (display_name, FALSE);
}
static void
update_standard_attributes (GFile *file,
GFileInfo *info,
const char *name,
GthDateTime *date_time)
{
char *display_name;
if (gth_datetime_valid (date_time)) {
char *sort_order_s;
int sort_order;
sort_order_s = gth_datetime_strftime (date_time, "%Y%m%d");
sort_order = atoi (sort_order_s);
g_file_info_set_sort_order (info, sort_order);
}
else if (g_file_info_get_attribute_boolean (info, "gthumb::no-child"))
g_file_info_set_sort_order (info, 99999999);
display_name = get_display_name (file, name, date_time);
if (display_name != NULL)
g_file_info_set_display_name (info, display_name);
}
void
gth_catalog_update_metadata (GthCatalog *catalog,
GthFileData *file_data)
{
const char *sort_type;
gboolean sort_inverse;
/* sort::type,sort::inverse */
sort_type = gth_catalog_get_order (catalog, &sort_inverse);
if (sort_type != NULL) {
g_file_info_set_attribute_string (file_data->info, "sort::type", sort_type);
g_file_info_set_attribute_boolean (file_data->info, "sort::inverse", sort_inverse);
}
else {
g_file_info_remove_attribute (file_data->info, "sort::type");
g_file_info_remove_attribute (file_data->info, "sort::inverse");
}
/* general::event-date */
if (gth_datetime_valid (catalog->priv->date_time)) {
GObject *metadata;
char *raw;
char *formatted;
char *sort_order_s;
int sort_order;
metadata = (GObject *) gth_metadata_new ();
raw = gth_datetime_to_exif_date (catalog->priv->date_time);
formatted = gth_datetime_strftime (catalog->priv->date_time, "%x");
g_object_set (metadata,
"id", "general::event-date",
"raw", raw,
"formatted", formatted,
NULL);
g_file_info_set_attribute_object (file_data->info, "general::event-date", metadata);
sort_order_s = gth_datetime_strftime (catalog->priv->date_time, "%Y%m%d");
sort_order = atoi (sort_order_s);
g_file_info_set_sort_order (file_data->info, sort_order);
g_free (formatted);
g_free (raw);
g_object_unref (metadata);
}
else
g_file_info_remove_attribute (file_data->info, "general::event-date");
/* standard::display-name,standard::sort-order */
update_standard_attributes (file_data->file,
file_data->info,
catalog->priv->name,
catalog->priv->date_time);
}
/* utils */
......@@ -797,78 +943,6 @@ get_tag_value (const char *buffer,
}
static char *
get_display_name (GFile *file,
const char *name,
GthDateTime *date_time)
{
GString *display_name;
char *basename;
display_name = g_string_new ("");
basename = g_file_get_basename (file);
if ((basename == NULL) || (strcmp (basename, "/") == 0)) {
g_string_append (display_name, _("Catalogs"));
}
else {
if ((name == NULL) && ! gth_datetime_valid (date_time)) {
char *name;
char *utf8_name;
name = _g_uri_remove_extension (basename);
utf8_name = g_filename_to_utf8 (name, -1, NULL, NULL, NULL);
g_string_append (display_name, utf8_name);
g_free (utf8_name);
g_free (name);
}
else {
if (name != NULL)
g_string_append (display_name, name);
if (gth_datetime_valid (date_time)) {
char *formatted;
if (name != NULL)
g_string_append (display_name, " (");
formatted = gth_datetime_strftime (date_time, "%x");
g_string_append (display_name, formatted);
if (name != NULL)
g_string_append (display_name, ")");
g_free (formatted);
}
}
}
return g_string_free (display_name, FALSE);
}
static void
update_standard_attributes (GFile *file,
GFileInfo *info,
const char *name,
GthDateTime *date_time)
{
char *display_name;
display_name = get_display_name (file, name, date_time);
if (gth_datetime_valid (date_time)) {
char *sort_order_s;
int sort_order;
sort_order_s = gth_datetime_strftime (date_time, "%Y%m%d");
sort_order = atoi (sort_order_s);
g_file_info_set_sort_order (info, sort_order);
}
if (display_name != NULL)
g_file_info_set_display_name (info, display_name);
}
void
gth_catalog_update_standard_attributes (GFile *file,
GFileInfo *info)
......@@ -958,10 +1032,10 @@ load__catalog_buffer_ready_cb (void *buffer,
void
gth_catalog_load_from_file (GFile *file,
GCancellable *cancellable,
ReadyCallback ready_func,
gpointer user_data)
gth_catalog_load_from_file_async (GFile *file,
GCancellable *cancellable,
ReadyCallback ready_func,
gpointer user_data)
{
LoadData *load_data;
GFile *gio_file;
......@@ -981,59 +1055,106 @@ gth_catalog_load_from_file (GFile *file,
}
void
gth_catalog_update_metadata (GthCatalog *catalog,
GthFileData *file_data)
GFile *
gth_catalog_get_file_for_date (GthDateTime *date_time)
{
const char *sort_type;
gboolean sort_inverse;
char *year;
char *uri;
GFile *base;
char *display_name;
GFile *catalog_file;
/* sort::type,sort::inverse */
year = gth_datetime_strftime (date_time, "%Y");
uri = g_strconcat ("catalog:///", year, "/", NULL);
base = g_file_new_for_uri (uri);
display_name = gth_datetime_strftime (date_time, "%Y-%m-%d");
catalog_file = _g_file_new_for_display_name (uri, display_name, ".catalog");
sort_type = gth_catalog_get_order (catalog, &sort_inverse);
if (sort_type != NULL) {
g_file_info_set_attribute_string (file_data->info, "sort::type", sort_type);
g_file_info_set_attribute_boolean (file_data->info, "sort::inverse", sort_inverse);
}
else {
g_file_info_remove_attribute (file_data->info, "sort::type");
g_file_info_remove_attribute (file_data->info, "sort::inverse");
}
g_free (display_name);
g_object_unref (base);
g_free (uri);
g_free (year);
/* general::event-date */
return catalog_file;
}
if (gth_datetime_valid (catalog->priv->date_time)) {
GObject *metadata;
char *raw;
char *formatted;
char *sort_order_s;
int sort_order;
metadata = (GObject *) gth_metadata_new ();
raw = gth_datetime_to_exif_date (catalog->priv->date_time);
formatted = gth_datetime_strftime (catalog->priv->date_time, "%x");
g_object_set (metadata,
"id", "general::event-date",
"raw", raw,
"formatted", formatted,
NULL);
g_file_info_set_attribute_object (file_data->info, "general::event-date", metadata);
GthCatalog *
gth_catalog_load_from_file (GFile *file)
{
GthCatalog *catalog;
GFile *gio_file;
void *buffer;
gsize buffer_size;
sort_order_s = gth_datetime_strftime (catalog->priv->date_time, "%Y%m%d");
sort_order = atoi (sort_order_s);
g_file_info_set_sort_order (file_data->info, sort_order);
gio_file = gth_catalog_file_to_gio_file (file);
if (! g_load_file_in_buffer (gio_file, &buffer, &buffer_size, NULL))
return NULL;
g_free (formatted);
g_free (raw);
g_object_unref (metadata);
catalog = gth_hook_invoke_get ("gth-catalog-load-from-data", buffer);
if (catalog != NULL)
gth_catalog_load_from_data (catalog, buffer, buffer_size, NULL);
g_free (buffer);
g_object_unref (gio_file);
return catalog;
}
void
gth_catalog_save (GthCatalog *catalog)
{
GFile *file;
GFile *gio_file;
GFile *gio_parent;
char *data;
gsize size;
GError *error = NULL;
file = gth_catalog_get_file (catalog);
gio_file = gth_catalog_file_to_gio_file (file);
gio_parent = g_file_get_parent (gio_file);
g_file_make_directory_with_parents (gio_parent, NULL, NULL);
data = gth_catalog_to_data (catalog, &size);
if (! g_write_file (gio_file,
FALSE,
G_FILE_CREATE_NONE,
data,
size,
NULL,
&error))
{
g_warning ("%s", error->message);
g_clear_error (&error);
}
else
g_file_info_remove_attribute (file_data->info, "general::event-date");
else {
GFile *parent_parent;
GFile *parent;
GList *list;
parent = g_file_get_parent (file);
parent_parent = g_file_get_parent (parent);
if (parent_parent != NULL) {
list = g_list_append (NULL, parent);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent_parent,
list,
GTH_MONITOR_EVENT_CREATED);
g_list_free (list);
}
/* standard::display-name,standard::sort-order */
list = g_list_append (NULL, file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CREATED);
update_standard_attributes (file_data->file,
file_data->info,
catalog->priv->name,
catalog->priv->date_time);
g_list_free (list);
g_object_unref (parent);
}
g_free (data);
g_object_unref (gio_parent);
g_object_unref (gio_file);
}
......@@ -87,6 +87,8 @@ void gth_catalog_set_order (GthCatalog *catalog,
gboolean inverse);
const char * gth_catalog_get_order (GthCatalog *catalog,
gboolean *inverse);
void gth_catalog_set_for_date (GthCatalog *catalog,
GthDateTime *date_time);
void gth_catalog_load_from_data (GthCatalog *catalog,
const void *buffer,
gsize count,
......@@ -124,9 +126,12 @@ char * gth_catalog_get_relative_path (GFile *file);
GIcon * gth_catalog_get_icon (GFile *file);
void gth_catalog_update_standard_attributes (GFile *file,
GFileInfo *info);
void gth_catalog_load_from_file (GFile *file,
void gth_catalog_load_from_file_async (GFile *file,
GCancellable *cancellable,
ReadyCallback ready_func,
gpointer user_data);
GFile * gth_catalog_get_file_for_date (GthDateTime *date_time);
GthCatalog * gth_catalog_load_from_file (GFile *file);
void gth_catalog_save (GthCatalog *catalog);
#endif /*GTH_CATALOG_H*/
......@@ -371,10 +371,10 @@ read_metadata_info_ready_cb (GList *files,
GFile *gio_file;
gio_file = gth_catalog_file_to_gio_file (read_metadata->file_data->file);
gth_catalog_load_from_file (gio_file,
gth_file_source_get_cancellable (read_metadata->file_source),
read_metadata_catalog_ready_cb,
read_metadata);
gth_catalog_load_from_file_async (gio_file,
gth_file_source_get_cancellable (read_metadata->file_source),
read_metadata_catalog_ready_cb,
read_metadata);
g_object_unref (gio_file);
}
......@@ -763,10 +763,10 @@ copy__file_list_info_ready_cb (GList *files,
}
cod->files = g_list_reverse (cod->files);
gth_catalog_load_from_file (cod->destination->file,
gth_file_source_get_cancellable (cod->file_source),
catalog_ready_cb,
cod);
gth_catalog_load_from_file_async (cod->destination->file,
gth_file_source_get_cancellable (cod->file_source),
catalog_ready_cb,
cod);
}
......@@ -955,10 +955,10 @@ gth_file_source_catalogs_reorder (GthFileSource *file_source,
reorder_data->data = data;
gio_file = gth_file_source_to_gio_file (file_source, destination->file);
gth_catalog_load_from_file (gio_file,
gth_file_source_get_cancellable (file_source),
reorder_catalog_ready_cb,
reorder_data);
gth_catalog_load_from_file_async (gio_file,
gth_file_source_get_cancellable (file_source),
reorder_catalog_ready_cb,
reorder_data);
g_object_unref (gio_file);
}
......
......@@ -85,60 +85,9 @@ save_catalog (gpointer key,
gpointer value,
gpointer user_data)
{
GthOrganizeTask *self = user_data;
GthCatalog *catalog = value;
GFile *file;
GFile *gio_file;
GFile *gio_parent;
char *data;
gsize size;
GError *error = NULL;
file = gth_catalog_get_file (catalog);
gio_file = gth_catalog_file_to_gio_file (file);
gio_parent = g_file_get_parent (gio_file);
g_file_make_directory_with_parents (gio_parent, NULL, NULL);
data = gth_catalog_to_data (catalog, &size);
if (! g_write_file (gio_file,
FALSE,
G_FILE_CREATE_NONE,
data,
size,
gth_task_get_cancellable (GTH_TASK (self)),
&error))
{
g_warning ("%s", error->message);
g_clear_error (&error);
}
else {
GFile *parent_parent;
GFile *parent;
GList *list;
parent = g_file_get_parent (file);
parent_parent = g_file_get_parent (parent);
if (parent_parent != NULL) {
list = g_list_append (NULL, parent);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent_parent,
list,
GTH_MONITOR_EVENT_CREATED);
g_list_free (list);
}
list = g_list_append (NULL, file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CREATED);
g_list_free (list);
g_object_unref (parent);
}
GthCatalog *catalog = value;
g_free (data);
g_object_unref (gio_parent);
g_object_unref (gio_file);
gth_catalog_save (catalog);
}
......@@ -183,7 +132,7 @@ save_catalogs (GthOrganizeTask *self)
}
while (gtk_tree_model_iter_next (GTK_TREE_MODEL (self->priv->results_liststore), &iter));
}
g_hash_table_foreach (self->priv->catalogs, save_catalog, self);
g_hash_table_foreach (self->priv->catalogs, save_catalog, NULL);
gth_task_completed (GTH_TASK (self), NULL);
}
......@@ -260,52 +209,6 @@ done_func (GError *error,
}
static GFile *
get_catalog_file (const char *base_uri,
const char *display_name)
{
GFile *base;
char *name;
char *name_escaped;
GFile *catalog_file;
base = g_file_new_for_uri (base_uri);
name = g_strdup_printf ("%s.catalog", display_name);
name_escaped = _g_utf8_replace (name, "/", ".");
catalog_file = g_file_get_child_for_display_name (base, name_escaped, NULL);
g_free (name_escaped);
g_free (name);
g_object_unref (base);
return catalog_file;
}
static GFile *
get_catalog_file_for_time (GTimeVal *timeval)
{
char *year;
char *uri;
GFile *base;
char *display_name;
GFile *catalog_file;
year = _g_time_val_strftime (timeval, "%Y");
uri = g_strconcat ("catalog:///", year, "/", NULL);
base = g_file_new_for_uri (uri);
display_name = _g_time_val_strftime (timeval, "%Y-%m-%d");
catalog_file = get_catalog_file (uri, display_name);
g_free (display_name);
g_object_unref (base);
g_free (uri);