Commit 39dc6a7b authored by Paolo Bacchilega's avatar Paolo Bacchilega

allow to change comment if the "Store metadata in files" option is not active

update the .comment metadata with the embedded metadata only when the embedded metadata is read for
the first time and no .comment data is available.
parent f9d7ad0b
AFTER_EXIV2 = \
comments \
$(NULL)
FLICKR_EXPORTERS = \
23hq \
flicker \
......@@ -37,10 +41,10 @@ SUBDIRS = \
bookmarks \
catalogs \
change_date \
comments \
convert_format \
edit_metadata \
exiv2_tools \
$(AFTER_EXIV2) \
file_manager \
file_viewer \
gstreamer_utils \
......
......@@ -22,6 +22,9 @@ libcomments_la_SOURCES = \
libcomments_la_CFLAGS = $(GTHUMB_CFLAGS) -I$(top_srcdir) -I$(top_builddir)/gthumb
libcomments_la_LDFLAGS = $(EXTENSION_LIBTOOL_FLAGS)
libcomments_la_LIBADD = $(GTHUMB_LIBS)
if ENABLE_EXIV2
libcomments_la_LIBADD += ../exiv2_tools/libexiv2_tools.la
endif
libcomments_la_DEPENDENCIES = $(top_builddir)/gthumb/gthumb$(EXEEXT)
extensioninidir = $(extensiondir)
......
......@@ -603,135 +603,130 @@ gth_comment_update_general_attributes (GthFileData *file_data)
void
gth_comment_synchronize_metadata (GList *file_list)
gth_comment_update_from_general_attributes (GthFileData *file_data)
{
GList *scan;
gboolean write_comment;
GthMetadata *metadata;
GthStringList *comment_categories;
GList *scan;
const char *text;
GthComment *comment;
GthStringList *categories;
for (scan = file_list; scan; scan = scan->next) {
GthFileData *file_data = scan->data;
gboolean write_comment;
GthMetadata *metadata;
GthStringList *comment_categories;
GList *scan;
const char *text;
GthComment *comment;
GthStringList *categories;
write_comment = FALSE;
write_comment = FALSE;
comment = gth_comment_new ();
gth_comment_set_note (comment, g_file_info_get_attribute_string (file_data->info, "comment::note"));
gth_comment_set_caption (comment, g_file_info_get_attribute_string (file_data->info, "comment::caption"));
gth_comment_set_place (comment, g_file_info_get_attribute_string (file_data->info, "comment::place"));
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL)
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::categories");
comment_categories = gth_metadata_get_string_list (metadata);
if (comment_categories != NULL)
for (scan = gth_string_list_get_list (comment_categories); scan; scan = scan->next)
gth_comment_add_category (comment, (char *) scan->data);
gth_comment_set_rating (comment, g_file_info_get_attribute_int32 (file_data->info, "comment::rating"));
/* sync embedded data and .comment data if required */
comment = gth_comment_new ();
gth_comment_set_note (comment, g_file_info_get_attribute_string (file_data->info, "comment::note"));
gth_comment_set_caption (comment, g_file_info_get_attribute_string (file_data->info, "comment::caption"));
gth_comment_set_place (comment, g_file_info_get_attribute_string (file_data->info, "comment::place"));
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL)
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::categories");
comment_categories = gth_metadata_get_string_list (metadata);
if (comment_categories != NULL)
for (scan = gth_string_list_get_list (comment_categories); scan; scan = scan->next)
gth_comment_add_category (comment, (char *) scan->data);
gth_comment_set_rating (comment, g_file_info_get_attribute_int32 (file_data->info, "comment::rating"));
/* sync embedded data and .comment data if required */
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::description");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::note");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_note (comment, gth_metadata_get_formatted (metadata));
write_comment = TRUE;
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::description");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::note");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_note (comment, gth_metadata_get_formatted (metadata));
write_comment = TRUE;
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::title");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::caption");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_caption (comment, gth_metadata_get_formatted (metadata));
write_comment = TRUE;
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::title");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::caption");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_caption (comment, gth_metadata_get_formatted (metadata));
write_comment = TRUE;
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::location");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::place");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_place (comment, gth_metadata_get_formatted (metadata));
write_comment = TRUE;
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::location");
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::datetime");
if (metadata != NULL) {
text = gth_metadata_get_raw (metadata);
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL) {
text = g_file_info_get_attribute_string (file_data->info, "comment::place");
if (! dom_str_equal (gth_metadata_get_formatted (metadata), text)) {
gth_comment_set_place (comment, gth_metadata_get_formatted (metadata));
if (! dom_str_equal (gth_metadata_get_raw (metadata), text)) {
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
write_comment = TRUE;
}
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::datetime");
if (metadata != NULL) {
text = gth_metadata_get_raw (metadata);
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::time");
if (metadata != NULL) {
if (! dom_str_equal (gth_metadata_get_raw (metadata), text)) {
gth_comment_set_time_from_exif_format (comment, gth_metadata_get_raw (metadata));
write_comment = TRUE;
}
}
}
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::tags");
categories = gth_metadata_get_string_list (metadata);
if (categories != NULL) {
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::categories");
comment_categories = gth_metadata_get_string_list (metadata);
if (! gth_string_list_equal (categories, comment_categories)) {
GList *scan;
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "general::tags");
categories = gth_metadata_get_string_list (metadata);
if (categories != NULL) {
metadata = (GthMetadata *) g_file_info_get_attribute_object (file_data->info, "comment::categories");
comment_categories = gth_metadata_get_string_list (metadata);
if (! gth_string_list_equal (categories, comment_categories)) {
GList *scan;
gth_comment_clear_categories (comment);
for (scan = gth_string_list_get_list (categories); scan; scan = scan->next)
gth_comment_add_category (comment, scan->data);
write_comment = TRUE;
}
gth_comment_clear_categories (comment);
for (scan = gth_string_list_get_list (categories); scan; scan = scan->next)
gth_comment_add_category (comment, scan->data);
write_comment = TRUE;
}
}
if (write_comment) {
GFile *comment_file;
GFile *comment_directory;
char *buffer;
gsize size;
comment_file = gth_comment_get_comment_file (file_data->file);
comment_directory = g_file_get_parent (comment_file);
if (! g_file_query_exists (comment_directory, NULL))
g_file_make_directory (comment_directory, NULL, NULL);
buffer = gth_comment_to_data (comment, &size);
_g_file_write (comment_file,
FALSE,
G_FILE_CREATE_NONE,
buffer,
size,
NULL,
NULL);
{
GFile *parent;
GList *list;
parent = g_file_get_parent (file_data->file);
list = g_list_prepend (NULL, file_data->file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CHANGED);
g_list_free (list);
g_object_unref (parent);
}
g_free (buffer);
g_object_unref (comment_directory);
g_object_unref (comment_file);
if (write_comment) {
GFile *comment_file;
GFile *comment_directory;
char *buffer;
gsize size;
comment_file = gth_comment_get_comment_file (file_data->file);
comment_directory = g_file_get_parent (comment_file);
if (! g_file_query_exists (comment_directory, NULL))
g_file_make_directory (comment_directory, NULL, NULL);
buffer = gth_comment_to_data (comment, &size);
_g_file_write (comment_file,
FALSE,
G_FILE_CREATE_NONE,
buffer,
size,
NULL,
NULL);
{
GFile *parent;
GList *list;
parent = g_file_get_parent (file_data->file);
list = g_list_prepend (NULL, file_data->file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CHANGED);
g_list_free (list);
g_object_unref (parent);
}
g_object_unref (comment);
g_free (buffer);
g_object_unref (comment_directory);
g_object_unref (comment_file);
}
g_object_unref (comment);
}
......@@ -82,6 +82,7 @@ GDate * gth_comment_get_date (GthComment *comment
GthTime * gth_comment_get_time_of_day (GthComment *comment);
char * gth_comment_get_time_as_exif_format (GthComment *comment);
void gth_comment_update_general_attributes (GthFileData *file_data);
void gth_comment_synchronize_metadata (GList *file_list /* GthFileData list */);
void gth_comment_update_from_general_attributes
(GthFileData *file_data);
#endif /* GTH_COMMENT_H */
......@@ -176,8 +176,12 @@ metadata_ready_cb (GObject *source_object,
/* Synchronization is done in _g_query_metadata_async if both
* PREF_GENERAL_STORE_METADATA_IN_FILES and PREF_COMMENTS_SYNCHRONIZE
* are true. */
if (! store_metadata_in_files || ! synchronize)
gth_comment_synchronize_metadata (file_data_list);
if (! store_metadata_in_files || ! synchronize) {
GList *scan;
for (scan = file_data_list; scan; scan = scan->next)
gth_comment_update_from_general_attributes ((GthFileData *) scan->data);
}
gth_task_completed (GTH_TASK (self), NULL);
}
......
......@@ -73,6 +73,8 @@ gth_metadata_provider_comment_read (GthMetadataProvider *self,
char *comment_time;
comment = gth_comment_new_for_file (file_data->file, cancellable, NULL);
g_file_info_set_attribute_boolean (file_data->info, "comment::no-comment-file", (comment == NULL));
if (comment == NULL)
return;
......
......@@ -23,6 +23,7 @@
#include <config.h>
#include <gtk/gtk.h>
#include <gthumb.h>
#include <extensions/exiv2_tools/exiv2-utils.h>
#include "callbacks.h"
#include "dlg-comments-preferences.h"
#include "gth-comment.h"
......@@ -83,6 +84,7 @@ comments__read_metadata_ready_cb (GList *file_list,
{
GSettings *settings;
gboolean store_metadata_in_files;
GList *scan;
gboolean synchronize;
settings = g_settings_new (GTHUMB_GENERAL_SCHEMA);
......@@ -90,28 +92,49 @@ comments__read_metadata_ready_cb (GList *file_list,
g_object_unref (settings);
if (! store_metadata_in_files) {
GList *scan;
/* if PREF_GENERAL_STORE_METADATA_IN_FILES is false, avoid to
* synchronize the .comment metadata because the embedded
* metadata is likely to be out-of-date.
* Give priority to the .comment metadata which, if present,
* is the most up-to-date. */
for (scan = file_list; scan; scan = scan->next)
gth_comment_update_general_attributes ((GthFileData *) scan->data);
return;
gboolean can_read_embedded_attributes;
can_read_embedded_attributes = gth_main_extension_is_active ("exiv2_tools");
for (scan = file_list; scan; scan = scan->next) {
GthFileData *file_data = scan->data;
/* If PREF_GENERAL_STORE_METADATA_IN_FILES is false and
* there is no comment file then we are reading
* the image metadata for the first time, in this
* case update the .comment metadata with the
* embedded metadata. */
if (g_file_info_get_attribute_boolean (file_data->info, "comment::no-comment-file")) {
if (can_read_embedded_attributes) {
exiv2_update_general_attributes (file_data->info);
gth_comment_update_from_general_attributes (file_data);
}
}
else
gth_comment_update_general_attributes ((GthFileData *) scan->data);
}
}
else {
/* if PREF_GENERAL_STORE_METADATA_IN_FILES is true, update the .comment
* metadata with the embedded metadata.
*/
settings = g_settings_new (GTHUMB_COMMENTS_SCHEMA);
synchronize = g_settings_get_boolean (settings, PREF_COMMENTS_SYNCHRONIZE);
g_object_unref (settings);
settings = g_settings_new (GTHUMB_COMMENTS_SCHEMA);
synchronize = g_settings_get_boolean (settings, PREF_COMMENTS_SYNCHRONIZE);
g_object_unref (settings);
if (! synchronize)
return;
if (! synchronize)
return;
gth_comment_synchronize_metadata (file_list);
for (scan = file_list; scan; scan = scan->next)
gth_comment_update_from_general_attributes ((GthFileData *) scan->data);
}
}
......
......@@ -542,12 +542,10 @@ clear_studip_comments_from_tagset (GFileInfo *info,
}
static void
set_attributes_from_tagsets (GFileInfo *info)
extern "C"
void
exiv2_update_general_attributes (GFileInfo *info)
{
clear_studip_comments_from_tagset (info, _DESCRIPTION_TAG_NAMES);
clear_studip_comments_from_tagset (info, _TITLE_TAG_NAMES);
set_attribute_from_tagset (info, "general::datetime", _ORIGINAL_DATE_TAG_NAMES);
set_attribute_from_tagset (info, "general::description", _DESCRIPTION_TAG_NAMES);
set_attribute_from_tagset (info, "general::title", _TITLE_TAG_NAMES);
......@@ -574,6 +572,19 @@ set_attributes_from_tagsets (GFileInfo *info)
set_attribute_from_tagset (info, "general::location", _LOCATION_TAG_NAMES);
set_string_list_attribute_from_tagset (info, "general::tags", _KEYWORDS_TAG_NAMES);
set_attribute_from_tagset (info, "general::rating", _RATING_TAG_NAMES);
}
static void
set_attributes_from_tagsets (GFileInfo *info,
gboolean update_general_attributes)
{
clear_studip_comments_from_tagset (info, _DESCRIPTION_TAG_NAMES);
clear_studip_comments_from_tagset (info, _TITLE_TAG_NAMES);
if (update_general_attributes)
exiv2_update_general_attributes (info);
set_attribute_from_tagset (info, "Embedded::Photo::DateTimeOriginal", _ORIGINAL_DATE_TAG_NAMES);
set_attribute_from_tagset (info, "Embedded::Image::Orientation", _ORIENTATION_TAG_NAMES);
}
......@@ -602,7 +613,8 @@ get_exif_default_category (const Exiv2::Exifdatum &md)
static void
exiv2_read_metadata (Exiv2::Image::AutoPtr image,
GFileInfo *info)
GFileInfo *info,
gboolean update_general_attributes)
{
image->readMetadata();
......@@ -707,7 +719,7 @@ exiv2_read_metadata (Exiv2::Image::AutoPtr image,
g_hash_table_unref (table);
}
set_attributes_from_tagsets (info);
set_attributes_from_tagsets (info, update_general_attributes);
}
......@@ -721,6 +733,7 @@ extern "C"
gboolean
exiv2_read_metadata_from_file (GFile *file,
GFileInfo *info,
gboolean update_general_attributes,
GCancellable *cancellable,
GError **error)
{
......@@ -744,7 +757,7 @@ exiv2_read_metadata_from_file (GFile *file,
}
// Set the log level to only show errors (and suppress warnings, informational and debug messages)
Exiv2::LogMsg::setLevel(Exiv2::LogMsg::error);
exiv2_read_metadata (image, info);
exiv2_read_metadata (image, info, update_general_attributes);
}
catch (Exiv2::AnyError& e) {
if (error != NULL)
......@@ -761,6 +774,7 @@ gboolean
exiv2_read_metadata_from_buffer (void *buffer,
gsize buffer_size,
GFileInfo *info,
gboolean update_general_attributes,
GError **error)
{
try {
......@@ -772,7 +786,7 @@ exiv2_read_metadata_from_buffer (void *buffer,
return FALSE;
}
exiv2_read_metadata (image, info);
exiv2_read_metadata (image, info, update_general_attributes);
}
catch (Exiv2::AnyError& e) {
if (error != NULL)
......@@ -809,7 +823,8 @@ exiv2_get_sidecar (GFile *file)
extern "C"
gboolean
exiv2_read_sidecar (GFile *file,
GFileInfo *info)
GFileInfo *info,
gboolean update_general_attributes)
{
try {
char *path;
......@@ -866,7 +881,7 @@ exiv2_read_sidecar (GFile *file,
}
Exiv2::XmpParser::terminate();
set_attributes_from_tagsets (info);
set_attributes_from_tagsets (info, update_general_attributes);
}
catch (Exiv2::AnyError& e) {
std::cerr << "Caught Exiv2 exception '" << e << "'\n";
......
......@@ -38,15 +38,19 @@ extern const char *_RATING_TAG_NAMES[];
gboolean exiv2_read_metadata_from_file (GFile *file,
GFileInfo *info,
gboolean update_general_attributes,
GCancellable *cancellable,
GError **error);
gboolean exiv2_read_metadata_from_buffer (void *buffer,
gsize buffer_size,
GFileInfo *info,
gboolean update_general_attributes,
GError **error);
GFile * exiv2_get_sidecar (GFile *file);
gboolean exiv2_read_sidecar (GFile *file,
GFileInfo *info);
GFileInfo *info,
gboolean update_general_attributes);
void exiv2_update_general_attributes (GFileInfo *info);
gboolean exiv2_supports_writes (const char *mime_type);
gboolean exiv2_write_metadata (GthImageSaveData *data);
gboolean exiv2_write_metadata_to_buffer (void **buffer,
......
......@@ -94,21 +94,34 @@ gth_metadata_provider_exiv2_can_write (GthMetadataProvider *self,
static void
gth_metadata_provider_exiv2_read (GthMetadataProvider *self,
gth_metadata_provider_exiv2_read (GthMetadataProvider *base,
GthFileData *file_data,
const char *attributes,
GCancellable *cancellable)
{
GFile *sidecar;
GthFileData *sidecar_file_data;
GthMetadataProviderExiv2 *self = GTH_METADATA_PROVIDER_EXIV2 (base);
GFile *sidecar;
GthFileData *sidecar_file_data;
gboolean update_general_attributes;
if (! g_content_type_is_a (gth_file_data_get_mime_type (file_data), "image/*"))
return;
/* The embedded metadata is likely to be outdated if the user chooses to
* not store metadata in files. */
if (self->priv->general_settings == NULL)
self->priv->general_settings = g_settings_new (GTHUMB_GENERAL_SCHEMA);
update_general_attributes = g_settings_get_boolean (self->priv->general_settings, PREF_GENERAL_STORE_METADATA_IN_FILES);
/* this function is executed in a secondary thread, so calling
* slow sync functions is not a problem. */
exiv2_read_metadata_from_file (file_data->file, file_data->info, cancellable, NULL);
exiv2_read_metadata_from_file (file_data->file,
file_data->info,
update_general_attributes,
cancellable,
NULL);
/* sidecar data */
......@@ -117,7 +130,9 @@ gth_metadata_provider_exiv2_read (GthMetadataProvider *self,
if (g_file_query_exists (sidecar_file_data->file, cancellable)) {
gth_file_data_update_info (sidecar_file_data, "time::*");
if (g_file_query_exists (sidecar_file_data->file, cancellable))
exiv2_read_sidecar (sidecar_file_data->file, file_data->info);
exiv2_read_sidecar (sidecar_file_data->file,
file_data->info,
update_general_attributes);
}
g_object_unref (sidecar_file_data);
......@@ -142,7 +157,8 @@ gth_metadata_provider_exiv2_write (GthMetadataProvider *base,
if (self->priv->general_settings == NULL)
self->priv->general_settings = g_settings_new (GTHUMB_GENERAL_SCHEMA);
if (! (flags & GTH_METADATA_WRITE_FORCE_EMBEDDED) && ! g_settings_get_boolean (self->priv->general_settings, PREF_GENERAL_STORE_METADATA_IN_FILES))
if (! (flags & GTH_METADATA_WRITE_FORCE_EMBEDDED)
&& ! g_settings_get_boolean (self->priv->general_settings, PREF_GENERAL_STORE_METADATA_IN_FILES))
return;
if (! exiv2_supports_writes (gth_file_data_get_mime_type (file_data)))
......
......@@ -204,7 +204,12 @@ exiv2_jpeg_tran_cb (JpegTranInfo *tran_info)
GFileInfo *info;
info = g_file_info_new ();
if (exiv2_read_metadata_from_buffer (tran_info->in_buffer, tran_info->in_buffer_size, info, NULL)) {
if (exiv2_read_metadata_from_buffer (tran_info->in_buffer,
tran_info->in_buffer_size,
info,
FALSE,
NULL))
{
GthMetadata *metadata;
update_exif_dimensions (info, tran_info->transformation);
......
......@@ -577,7 +577,11 @@ file_buffer_ready_cb (void **buffer,
file_data = self->priv->current->data;
if (gth_main_extension_is_active ("exiv2_tools"))
exiv2_read_metadata_from_buffer (*buffer, count, file_data->info, NULL);
exiv2_read_metadata_from_buffer (*buffer,
count,
file_data->info,
TRUE,
NULL);
destination_file = get_destination_file (self, file_data);
if (destination_file == NULL)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment