44
43
#define WRITABLE_PATH_KEY "gimp-data-factory-writable-path"
45
/* Data files that have this string in their path are considered
46
* obsolete and are only kept around for backwards compatibility
48
#define GIMP_OBSOLETE_DATA_DIR_NAME "gimp-obsolete-files"
51
typedef void (* GimpDataForeachFunc) (GimpDataFactory *factory,
56
struct _GimpDataFactoryPriv
59
GimpContainer *container;
61
GimpContainer *container_obsolete;
63
gchar *path_property_name;
64
gchar *writable_property_name;
66
const GimpDataFactoryLoaderEntry *loader_entries;
67
gint n_loader_entries;
69
GimpDataNewFunc data_new_func;
70
GimpDataGetStandardFunc data_get_standard_func;
47
74
static void gimp_data_factory_finalize (GObject *object);
49
76
static void gimp_data_factory_data_load (GimpDataFactory *factory,
50
78
GHashTable *cache);
52
80
static gint64 gimp_data_factory_get_memsize (GimpObject *object,
55
static gchar * gimp_data_factory_get_save_dir (GimpDataFactory *factory);
83
static gchar * gimp_data_factory_get_save_dir (GimpDataFactory *factory,
57
86
static void gimp_data_factory_load_data (const GimpDatafileData *file_data,
74
103
object_class->finalize = gimp_data_factory_finalize;
76
105
gimp_object_class->get_memsize = gimp_data_factory_get_memsize;
107
g_type_class_add_private (klass, sizeof (GimpDataFactoryPriv));
80
111
gimp_data_factory_init (GimpDataFactory *factory)
83
factory->container = NULL;
84
factory->path_property_name = NULL;
85
factory->writable_property_name = NULL;
86
factory->loader_entries = NULL;
87
factory->n_loader_entries = 0;
88
factory->data_new_func = NULL;
89
factory->data_get_standard_func = NULL;
113
factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
114
GIMP_TYPE_DATA_FACTORY,
115
GimpDataFactoryPriv);
117
factory->priv->gimp = NULL;
118
factory->priv->container = NULL;
119
factory->priv->container_obsolete = NULL;
120
factory->priv->path_property_name = NULL;
121
factory->priv->writable_property_name = NULL;
122
factory->priv->loader_entries = NULL;
123
factory->priv->n_loader_entries = 0;
124
factory->priv->data_new_func = NULL;
125
factory->priv->data_get_standard_func = NULL;
95
131
GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
97
if (factory->container)
99
g_object_unref (factory->container);
100
factory->container = NULL;
103
if (factory->path_property_name)
105
g_free (factory->path_property_name);
106
factory->path_property_name = NULL;
109
if (factory->writable_property_name)
111
g_free (factory->writable_property_name);
112
factory->writable_property_name = NULL;
133
if (factory->priv->container)
135
g_object_unref (factory->priv->container);
136
factory->priv->container = NULL;
139
if (factory->priv->container_obsolete)
141
g_object_unref (factory->priv->container_obsolete);
142
factory->priv->container_obsolete = NULL;
145
if (factory->priv->path_property_name)
147
g_free (factory->priv->path_property_name);
148
factory->priv->path_property_name = NULL;
151
if (factory->priv->writable_property_name)
153
g_free (factory->priv->writable_property_name);
154
factory->priv->writable_property_name = NULL;
115
157
G_OBJECT_CLASS (parent_class)->finalize (object);
151
195
factory = g_object_new (GIMP_TYPE_DATA_FACTORY, NULL);
153
factory->gimp = gimp;
154
factory->container = gimp_list_new (data_type, TRUE);
155
gimp_list_set_sort_func (GIMP_LIST (factory->container),
156
(GCompareFunc) gimp_data_compare);
158
factory->path_property_name = g_strdup (path_property_name);
159
factory->writable_property_name = g_strdup (writable_property_name);
161
factory->loader_entries = loader_entries;
162
factory->n_loader_entries = n_loader_entries;
164
factory->data_new_func = new_func;
165
factory->data_get_standard_func = get_standard_func;
197
factory->priv->gimp = gimp;
198
factory->priv->container = gimp_list_new (data_type, TRUE);
199
gimp_list_set_sort_func (GIMP_LIST (factory->priv->container),
200
(GCompareFunc) gimp_data_compare);
201
factory->priv->container_obsolete = gimp_list_new (data_type, TRUE);
202
gimp_list_set_sort_func (GIMP_LIST (factory->priv->container_obsolete),
203
(GCompareFunc) gimp_data_compare);
205
factory->priv->path_property_name = g_strdup (path_property_name);
206
factory->priv->writable_property_name = g_strdup (writable_property_name);
208
factory->priv->loader_entries = loader_entries;
209
factory->priv->n_loader_entries = n_loader_entries;
211
factory->priv->data_new_func = new_func;
212
factory->priv->data_get_standard_func = get_standard_func;
171
218
gimp_data_factory_data_init (GimpDataFactory *factory,
219
GimpContext *context,
172
220
gboolean no_data)
174
222
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
223
g_return_if_fail (GIMP_IS_CONTEXT (context));
176
225
/* Freeze and thaw the container even if no_data,
177
226
* this creates the standard data that serves as fallback.
179
gimp_container_freeze (factory->container);
228
gimp_container_freeze (factory->priv->container);
183
if (factory->gimp->be_verbose)
232
if (factory->priv->gimp->be_verbose)
185
const gchar *name = gimp_object_get_name (GIMP_OBJECT (factory));
234
const gchar *name = gimp_object_get_name (factory);
187
236
g_print ("Loading '%s' data\n", name ? name : "???");
190
gimp_data_factory_data_load (factory, NULL);
239
gimp_data_factory_data_load (factory, context, NULL);
193
gimp_container_thaw (factory->container);
242
gimp_container_thaw (factory->priv->container);
246
gimp_data_factory_refresh_cache_add (GimpDataFactory *factory,
198
GimpDataFactory *factory;
200
} GimpDataLoadContext;
250
const gchar *filename = gimp_data_get_filename (data);
254
GHashTable *cache = user_data;
259
gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
261
list = g_hash_table_lookup (cache, filename);
262
list = g_list_prepend (list, data);
264
g_hash_table_insert (cache, (gpointer) filename, list);
203
269
gimp_data_factory_refresh_cache_remove (gpointer key,
218
gimp_data_factory_data_move_to_cache (GimpDataFactory *factory,
222
GHashTable *cache = data;
224
const gchar *filename = GIMP_DATA (object)->filename;
226
g_object_ref (object);
228
gimp_container_remove (factory->container, GIMP_OBJECT (object));
230
list = g_hash_table_lookup (cache, filename);
231
list = g_list_prepend (list, object);
233
g_hash_table_insert (cache, (gpointer) filename, list);
237
gimp_data_factory_data_foreach (GimpDataFactory *factory,
238
void (*callback) (GimpDataFactory *factory,
245
if (gimp_container_is_empty (factory->container))
248
list = GIMP_LIST (factory->container);
252
if (GIMP_DATA (list->list->data)->internal)
254
/* if there are internal objects in the list, skip them */
257
for (glist = list->list; glist; glist = g_list_next (glist))
259
if (glist->next && ! GIMP_DATA (glist->next->data)->internal)
262
callback (factory, glist->next->data, context);
271
callback (factory, list->list->data, context);
284
gimp_data_factory_data_foreach (GimpDataFactory *factory,
285
gboolean skip_internal,
286
GimpDataForeachFunc callback,
289
GList *list = GIMP_LIST (factory->priv->container)->list;
293
while (list && gimp_data_is_internal (GIMP_DATA (list->data)))
294
list = g_list_next (list);
299
GList *next = g_list_next (list);
301
callback (factory, list->data, user_data);
309
GimpDataFactory *factory;
310
GimpContext *context;
312
const gchar *top_directory;
313
} GimpDataLoadContext;
277
316
gimp_data_factory_data_load (GimpDataFactory *factory,
317
GimpContext *context,
278
318
GHashTable *cache)
281
321
gchar *writable_path;
283
g_object_get (factory->gimp->config,
284
factory->path_property_name, &path,
285
factory->writable_property_name, &writable_path,
323
g_object_get (factory->priv->gimp->config,
324
factory->priv->path_property_name, &path,
325
factory->priv->writable_property_name, &writable_path,
288
328
if (path && strlen (path))
290
330
GList *writable_list = NULL;
292
GimpDataLoadContext context;
332
GimpDataLoadContext load_context = { 0, };
294
context.factory = factory;
295
context.cache = cache;
334
load_context.factory = factory;
335
load_context.context = context;
336
load_context.cache = cache;
297
338
tmp = gimp_config_path_expand (path, TRUE, NULL);
328
370
g_free (writable_path);
332
gimp_data_factory_data_reload (GimpDataFactory *factory)
374
gimp_data_factory_data_refresh (GimpDataFactory *factory,
375
GimpContext *context)
335
377
GHashTable *cache;
337
379
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
339
gimp_container_freeze (factory->container);
380
g_return_if_fail (GIMP_IS_CONTEXT (context));
382
gimp_container_freeze (factory->priv->container);
384
/* First, save all dirty data objects */
385
gimp_data_factory_data_save (factory);
341
387
cache = g_hash_table_new (g_str_hash, g_str_equal);
343
gimp_data_factory_data_foreach (factory,
344
gimp_data_factory_data_move_to_cache, cache);
389
gimp_data_factory_data_foreach (factory, TRUE,
390
gimp_data_factory_refresh_cache_add, cache);
346
/* Now the cache contains a filename => list-of-objects mapping of
347
* the old objects. So we should now traverse the directory and for
348
* each file load it only if it's mtime is newer.
392
/* Now the cache contains a filename => list-of-objects mapping of
393
* the old objects. So we should now traverse the directory and for
394
* each file load it only if its mtime is newer.
350
* Once a file was added, it is removed from the cache, so the only
351
* objects remaining there will be those that are not present on the
352
* disk (that have to be destroyed)
396
* Once a file was added, it is removed from the cache, so the only
397
* objects remaining there will be those that are not present on
398
* the disk (that have to be destroyed)
354
gimp_data_factory_data_load (factory, cache);
400
gimp_data_factory_data_load (factory, context, cache);
356
/* Now all the data is loaded. Free what remains in the cache. */
402
/* Now all the data is loaded. Free what remains in the cache */
357
403
g_hash_table_foreach_remove (cache,
358
404
gimp_data_factory_refresh_cache_remove, NULL);
359
406
g_hash_table_destroy (cache);
361
gimp_container_thaw (factory->container);
365
gimp_data_factory_data_refresh (GimpDataFactory *factory)
367
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
369
gimp_container_freeze (factory->container);
371
gimp_data_factory_data_save (factory);
373
gimp_data_factory_data_reload (factory);
375
gimp_container_thaw (factory->container);
408
gimp_container_thaw (factory->priv->container);
379
412
gimp_data_factory_data_save (GimpDataFactory *factory)
416
GError *error = NULL;
384
418
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
386
if (gimp_container_is_empty (factory->container))
420
if (gimp_container_is_empty (factory->priv->container))
389
writable_dir = gimp_data_factory_get_save_dir (factory);
423
writable_dir = gimp_data_factory_get_save_dir (factory, &error);
391
425
if (! writable_dir)
394
for (list = GIMP_LIST (factory->container)->list;
427
gimp_message (factory->priv->gimp, NULL, GIMP_MESSAGE_ERROR,
428
_("Failed to save data:\n\n%s"),
430
g_clear_error (&error);
435
for (list = GIMP_LIST (factory->priv->container)->list;
396
437
list = g_list_next (list))
398
439
GimpData *data = list->data;
400
if (! data->filename)
441
if (! gimp_data_get_filename (data))
401
442
gimp_data_create_filename (data, writable_dir);
403
if (data->dirty && data->writable)
444
if (gimp_data_is_dirty (data) &&
445
gimp_data_is_writable (data))
405
447
GError *error = NULL;
437
479
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
439
gimp_container_freeze (factory->container);
441
gimp_data_factory_data_foreach (factory, gimp_data_factory_remove_cb, NULL);
443
gimp_container_thaw (factory->container);
481
gimp_container_freeze (factory->priv->container);
483
gimp_data_factory_data_foreach (factory, TRUE,
484
gimp_data_factory_remove_cb, NULL);
486
gimp_container_thaw (factory->priv->container);
447
490
gimp_data_factory_data_new (GimpDataFactory *factory,
491
GimpContext *context,
448
492
const gchar *name)
450
494
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
495
g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
451
496
g_return_val_if_fail (name != NULL, NULL);
452
497
g_return_val_if_fail (*name != '\0', NULL);
454
if (factory->data_new_func)
499
if (factory->priv->data_new_func)
456
GimpData *data = factory->data_new_func (name);
501
GimpData *data = factory->priv->data_new_func (context, name);
460
gimp_container_add (factory->container, GIMP_OBJECT (data));
505
gimp_container_add (factory->priv->container, GIMP_OBJECT (data));
461
506
g_object_unref (data);
466
g_warning ("%s: factory->data_new_func() returned NULL", G_STRFUNC);
511
g_warning ("%s: factory->priv->data_new_func() returned NULL", G_STRFUNC);
628
711
g_free (writable_path);
629
712
writable_path = tmp;
631
path_list = gimp_path_parse (path, 16, TRUE, NULL);
632
writable_list = gimp_path_parse (writable_path, 16, TRUE, NULL);
714
path_list = gimp_path_parse (path, 16, FALSE, NULL);
715
writable_list = gimp_path_parse (writable_path, 16, FALSE, NULL);
635
718
g_free (writable_path);
637
for (list = writable_list; list; list = g_list_next (list))
639
GList *found = g_list_find_custom (path_list,
640
list->data, (GCompareFunc) strcmp);
643
writable_dir = g_strdup (found->data);
722
gboolean found_any = FALSE;
724
for (list = writable_list; list; list = g_list_next (list))
726
GList *found = g_list_find_custom (path_list,
727
list->data, (GCompareFunc) strcmp);
730
const gchar *dir = found->data;
734
if (! g_file_test (dir, G_FILE_TEST_IS_DIR))
736
/* error out only if this is the last chance */
739
gchar *display_name = g_filename_display_name (dir);
741
g_set_error (error, GIMP_DATA_ERROR, 0,
742
_("You have a writable data folder "
743
"configured (%s), but this folder does "
744
"not exist. Please create the folder or "
745
"fix your configuation in the "
746
"Preferences dialog's 'Folders' section."),
749
g_free (display_name);
754
writable_dir = g_strdup (dir);
760
if (! writable_dir && ! found_any)
762
g_set_error (error, GIMP_DATA_ERROR, 0,
763
_("You have a writable data folder configured, but this "
764
"folder is not part of your data search path. You "
765
"probably edited the gimprc file manually, "
766
"please fix it in the Preferences dialog's 'Folders' "
772
g_set_error (error, GIMP_DATA_ERROR, 0,
773
_("You don't have any writable data folder configured."));
648
776
gimp_path_free (path_list);
651
779
return writable_dir;
783
gimp_data_factory_is_dir_writable (const gchar *dirname,
784
GList *writable_path)
788
for (list = writable_path; list; list = g_list_next (list))
790
if (g_str_has_prefix (dirname, list->data))
655
798
gimp_data_factory_load_data_recursive (const GimpDatafileData *file_data,
658
801
GimpDataLoadContext *context = data;
660
gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_EXISTS,
802
gboolean top_set = FALSE;
804
/* When processing subdirectories, set the top_directory if it's
805
* unset. This way me make sure gimp_data_set_folder_tags()'
806
* calling convention is honored: pass NULL when processing the
807
* toplevel directory itself, and pass the toplevel directory when
808
* processing any folder inside.
810
if (! context->top_directory)
812
context->top_directory = file_data->dirname;
816
gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_IS_REGULAR,
661
817
gimp_data_factory_load_data, context);
663
819
gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_IS_DIR,
664
820
gimp_data_factory_load_data_recursive,
823
/* Unset, the string is only valid within this function, and will
824
* be set again for the next subdirectory.
827
context->top_directory = NULL;
669
831
gimp_data_factory_load_data (const GimpDatafileData *file_data,
672
GimpDataLoadContext *context = data;
673
GimpDataFactory *factory = context->factory;
674
GHashTable *cache = context->cache;
834
GimpDataLoadContext *context = data;
835
GimpDataFactory *factory = context->factory;
836
GHashTable *cache = context->cache;
837
const GimpDataFactoryLoaderEntry *loader = NULL;
838
GError *error = NULL;
677
for (i = 0; i < factory->n_loader_entries; i++)
842
for (i = 0; i < factory->priv->n_loader_entries; i++)
679
if (! factory->loader_entries[i].extension ||
844
loader = &factory->priv->loader_entries[i];
846
/* a loder matches if its extension matches, or if it doesn't
847
* have an extension, which is the case for the fallback loader,
848
* which must be last in the loader array
850
if (! loader->extension ||
680
851
gimp_datafiles_check_extension (file_data->filename,
681
factory->loader_entries[i].extension))
690
gboolean load_from_disk = TRUE;
693
(cached_data = g_hash_table_lookup (cache, file_data->filename)))
695
GimpData *data = cached_data->data;
697
load_from_disk = (data->mtime == 0 || data->mtime != file_data->mtime);
699
if (! load_from_disk)
703
for (list = cached_data; list; list = g_list_next (list))
704
gimp_container_add (factory->container, GIMP_OBJECT (list->data));
711
GError *error = NULL;
713
data_list = factory->loader_entries[i].load_func (file_data->filename,
716
if (G_LIKELY (data_list))
718
GList *writable_list;
723
writable_list = g_object_get_data (G_OBJECT (factory),
726
deletable = (g_list_length (data_list) == 1 &&
727
g_list_find_custom (writable_list, file_data->dirname,
728
(GCompareFunc) strcmp) != NULL);
730
writable = (deletable && factory->loader_entries[i].writable);
732
for (list = data_list; list; list = g_list_next (list))
734
GimpData *data = list->data;
736
gimp_data_set_filename (data, file_data->filename,
737
writable, deletable);
738
data->mtime = file_data->mtime;
741
gimp_container_add (factory->container, GIMP_OBJECT (data));
742
g_object_unref (data);
745
g_list_free (data_list);
748
if (G_UNLIKELY (error))
750
gimp_message (factory->gimp, NULL, GIMP_MESSAGE_ERROR,
751
_("Failed to load data:\n\n%s"), error->message);
752
g_clear_error (&error);
863
cached_data = g_hash_table_lookup (cache, file_data->filename);
866
gimp_data_get_mtime (cached_data->data) != 0 &&
867
gimp_data_get_mtime (cached_data->data) == file_data->mtime)
871
for (list = cached_data; list; list = g_list_next (list))
872
gimp_container_add (factory->priv->container, list->data);
878
data_list = loader->load_func (context->context, file_data->filename, &error);
880
if (G_LIKELY (data_list))
884
gboolean writable = FALSE;
885
gboolean deletable = FALSE;
887
obsolete = (strstr (file_data->dirname,
888
GIMP_OBSOLETE_DATA_DIR_NAME) != 0);
890
/* obsolete files are immutable, don't check their writability */
893
GList *writable_list;
895
writable_list = g_object_get_data (G_OBJECT (factory),
898
deletable = (g_list_length (data_list) == 1 &&
899
gimp_data_factory_is_dir_writable (file_data->dirname,
902
writable = (deletable && loader->writable);
905
for (list = data_list; list; list = g_list_next (list))
907
GimpData *data = list->data;
909
gimp_data_set_filename (data, file_data->filename,
910
writable, deletable);
911
gimp_data_set_mtime (data, file_data->mtime);
913
gimp_data_clean (data);
917
gimp_container_add (factory->priv->container_obsolete,
922
gimp_data_set_folder_tags (data, context->top_directory);
924
gimp_container_add (factory->priv->container,
928
g_object_unref (data);
931
g_list_free (data_list);
934
if (G_UNLIKELY (error))
936
gimp_message (factory->priv->gimp, NULL, GIMP_MESSAGE_ERROR,
937
_("Failed to load data:\n\n%s"), error->message);
938
g_clear_error (&error);