X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=blobdiff_plain;ds=sidebyside;f=shell%2Fev-metadata-manager.c;h=e86da9735834ae10cf64b0ffb037b2cf15e7c420;hb=06e8fe2d26f02fd17a4dc19ea10d7ee00dc33243;hp=f5ab0407b62fee42f21ea874c5f60e745f05a628;hpb=6003391d261ef7940bbc14683f60b242fe347adb;p=evince.git diff --git a/shell/ev-metadata-manager.c b/shell/ev-metadata-manager.c index f5ab0407..e86da973 100644 --- a/shell/ev-metadata-manager.c +++ b/shell/ev-metadata-manager.c @@ -59,9 +59,6 @@ struct _EvMetadataManager gboolean values_loaded; /* It is true if the file has been read */ - gboolean modified; /* It is true if the file - has top be written */ - guint timeout_id; GHashTable *items; @@ -72,6 +69,12 @@ static gboolean ev_metadata_manager_save (gpointer data); static EvMetadataManager *ev_metadata_manager = NULL; +/** + * item_free: + * @data: a pointer to a #Item data + * + * It does free the values on the #GHashTable where data points. + */ static void item_free (gpointer data) { @@ -87,26 +90,45 @@ item_free (gpointer data) g_free (item); } +/** + * ev_metadata_arm_timeout + * + * Setup a timeout for saving the metadata to disk. + */ +static void +ev_metadata_arm_timeout(void) +{ + if (ev_metadata_manager->timeout_id) + return; + ev_metadata_manager->timeout_id = + g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, + 2000, /* 2 sec */ + (GSourceFunc)ev_metadata_manager_save, + NULL, + NULL); +} + +/** + * ev_metadata_manager_init: + * + * Creates an EvMetadataManager with default values. + * + * values_loaded -> %FALSE. + * timeout_id -> the id of the event source. + * items -> a new full empty #GHashTable. + */ void ev_metadata_manager_init (void) { ev_metadata_manager = g_new0 (EvMetadataManager, 1); ev_metadata_manager->values_loaded = FALSE; - ev_metadata_manager->modified = FALSE; ev_metadata_manager->items = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, item_free); - - ev_metadata_manager->timeout_id = - g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, - 2000, /* 2 sec */ - (GSourceFunc)ev_metadata_manager_save, - NULL, - NULL); } /* This function must be called before exiting ev */ @@ -116,9 +138,11 @@ ev_metadata_manager_shutdown (void) if (ev_metadata_manager == NULL) return; - g_source_remove (ev_metadata_manager->timeout_id); - - ev_metadata_manager_save (NULL); + if (ev_metadata_manager->timeout_id) { + g_source_remove (ev_metadata_manager->timeout_id); + ev_metadata_manager->timeout_id = 0; + ev_metadata_manager_save (NULL); + } if (ev_metadata_manager->items != NULL) g_hash_table_destroy (ev_metadata_manager->items); @@ -268,7 +292,7 @@ load_values () cur = xmlDocGetRootElement (doc); if (cur == NULL) { - g_message ("The metadata file '%s' is empty", METADATA_FILE); + g_message ("The metadata file “%s” is empty", METADATA_FILE); xmlFreeDoc (doc); return FALSE; @@ -276,7 +300,7 @@ load_values () if (xmlStrcmp (cur->name, (const xmlChar *) "metadata")) { - g_message ("File '%s' is of the wrong type", METADATA_FILE); + g_message ("File “%s” is of the wrong type", METADATA_FILE); xmlFreeDoc (doc); return FALSE; @@ -375,12 +399,12 @@ ev_metadata_manager_set_last (const gchar *key, } item->atime = time (NULL); - ev_metadata_manager->modified = TRUE; + ev_metadata_arm_timeout (); return; } /** - * ev_metadata_manager_set: + * ev_metadata_manager_get: * @uri: Uri to set data for, if @NULL, we return default value * @key: Key to set uri * @value: GValue struct filled up with value @@ -510,7 +534,7 @@ ev_metadata_manager_set (const gchar *uri, item->atime = time (NULL); - ev_metadata_manager->modified = TRUE; + ev_metadata_arm_timeout (); } static void @@ -539,8 +563,12 @@ save_values (const gchar *key, GValue *value, xmlNodePtr parent) string_value = g_strdup_printf ("%d", g_value_get_int (value)); break; case G_TYPE_DOUBLE: - string_value = g_strdup_printf ("%f", g_value_get_double (value)); - break; + { + gchar buf[G_ASCII_DTOSTR_BUF_SIZE]; + g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, g_value_get_double (value)); + string_value = g_strdup_printf ("%s", buf); + } + break; case G_TYPE_BOOLEAN: string_value = g_strdup_printf ("%d", g_value_get_boolean (value)); break; @@ -627,8 +655,7 @@ ev_metadata_manager_save (gpointer data) xmlNodePtr root; gchar *file_name; - if (!ev_metadata_manager->modified) - return TRUE; + ev_metadata_manager->timeout_id = 0; resize_items (); @@ -652,9 +679,7 @@ ev_metadata_manager_save (gpointer data) xmlFreeDoc (doc); - ev_metadata_manager->modified = FALSE; - - return TRUE; + return FALSE; } void