~ubuntu-branches/ubuntu/vivid/gimp/vivid

« back to all changes in this revision

Viewing changes to app/core/gimpdatafactory.c

  • Committer: Package Import Robot
  • Author(s): Jordi Mallach
  • Date: 2012-05-08 18:50:03 UTC
  • mto: (1.1.26) (0.5.1 experimental)
  • mto: This revision was merged to the branch mainline in revision 71.
  • Revision ID: package-import@ubuntu.com-20120508185003-tltkvbaysf8d2426
ImportĀ upstreamĀ versionĀ 2.8.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * gimpdatafactory.c
5
5
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
6
6
 *
7
 
 * This program is free software; you can redistribute it and/or modify
 
7
 * This program is free software: you can redistribute it and/or modify
8
8
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * the Free Software Foundation; either version 3 of the License, or
10
10
 * (at your option) any later version.
11
11
 *
12
12
 * This program is distributed in the hope that it will be useful,
15
15
 * GNU General Public License for more details.
16
16
 *
17
17
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
19
 */
21
20
 
22
21
#include "config.h"
43
42
 
44
43
#define WRITABLE_PATH_KEY "gimp-data-factory-writable-path"
45
44
 
 
45
/* Data files that have this string in their path are considered
 
46
 * obsolete and are only kept around for backwards compatibility
 
47
 */
 
48
#define GIMP_OBSOLETE_DATA_DIR_NAME "gimp-obsolete-files"
 
49
 
 
50
 
 
51
typedef void (* GimpDataForeachFunc) (GimpDataFactory *factory,
 
52
                                      GimpData        *data,
 
53
                                      gpointer         user_data);
 
54
 
 
55
 
 
56
struct _GimpDataFactoryPriv
 
57
{
 
58
  Gimp                             *gimp;
 
59
  GimpContainer                    *container;
 
60
 
 
61
  GimpContainer                    *container_obsolete;
 
62
 
 
63
  gchar                            *path_property_name;
 
64
  gchar                            *writable_property_name;
 
65
 
 
66
  const GimpDataFactoryLoaderEntry *loader_entries;
 
67
  gint                              n_loader_entries;
 
68
 
 
69
  GimpDataNewFunc                   data_new_func;
 
70
  GimpDataGetStandardFunc           data_get_standard_func;
 
71
};
 
72
 
46
73
 
47
74
static void    gimp_data_factory_finalize     (GObject              *object);
48
75
 
49
76
static void    gimp_data_factory_data_load    (GimpDataFactory      *factory,
 
77
                                               GimpContext          *context,
50
78
                                               GHashTable           *cache);
51
79
 
52
80
static gint64  gimp_data_factory_get_memsize  (GimpObject           *object,
53
81
                                               gint64               *gui_size);
54
82
 
55
 
static gchar * gimp_data_factory_get_save_dir (GimpDataFactory      *factory);
 
83
static gchar * gimp_data_factory_get_save_dir (GimpDataFactory      *factory,
 
84
                                               GError              **error);
56
85
 
57
86
static void    gimp_data_factory_load_data  (const GimpDatafileData *file_data,
58
87
                                             gpointer                data);
74
103
  object_class->finalize         = gimp_data_factory_finalize;
75
104
 
76
105
  gimp_object_class->get_memsize = gimp_data_factory_get_memsize;
 
106
 
 
107
  g_type_class_add_private (klass, sizeof (GimpDataFactoryPriv));
77
108
}
78
109
 
79
110
static void
80
111
gimp_data_factory_init (GimpDataFactory *factory)
81
112
{
82
 
  factory->gimp                   = NULL;
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);
 
116
 
 
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;
90
126
}
91
127
 
92
128
static void
94
130
{
95
131
  GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
96
132
 
97
 
  if (factory->container)
98
 
    {
99
 
      g_object_unref (factory->container);
100
 
      factory->container = NULL;
101
 
    }
102
 
 
103
 
  if (factory->path_property_name)
104
 
    {
105
 
      g_free (factory->path_property_name);
106
 
      factory->path_property_name = NULL;
107
 
    }
108
 
 
109
 
  if (factory->writable_property_name)
110
 
    {
111
 
      g_free (factory->writable_property_name);
112
 
      factory->writable_property_name = NULL;
 
133
  if (factory->priv->container)
 
134
    {
 
135
      g_object_unref (factory->priv->container);
 
136
      factory->priv->container = NULL;
 
137
    }
 
138
 
 
139
  if (factory->priv->container_obsolete)
 
140
    {
 
141
      g_object_unref (factory->priv->container_obsolete);
 
142
      factory->priv->container_obsolete = NULL;
 
143
    }
 
144
 
 
145
  if (factory->priv->path_property_name)
 
146
    {
 
147
      g_free (factory->priv->path_property_name);
 
148
      factory->priv->path_property_name = NULL;
 
149
    }
 
150
 
 
151
  if (factory->priv->writable_property_name)
 
152
    {
 
153
      g_free (factory->priv->writable_property_name);
 
154
      factory->priv->writable_property_name = NULL;
113
155
    }
114
156
 
115
157
  G_OBJECT_CLASS (parent_class)->finalize (object);
122
164
  GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
123
165
  gint64           memsize = 0;
124
166
 
125
 
  memsize += gimp_object_get_memsize (GIMP_OBJECT (factory->container),
 
167
  memsize += gimp_object_get_memsize (GIMP_OBJECT (factory->priv->container),
 
168
                                      gui_size);
 
169
  memsize += gimp_object_get_memsize (GIMP_OBJECT (factory->priv->container_obsolete),
126
170
                                      gui_size);
127
171
 
128
172
  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
150
194
 
151
195
  factory = g_object_new (GIMP_TYPE_DATA_FACTORY, NULL);
152
196
 
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);
157
 
 
158
 
  factory->path_property_name     = g_strdup (path_property_name);
159
 
  factory->writable_property_name = g_strdup (writable_property_name);
160
 
 
161
 
  factory->loader_entries         = loader_entries;
162
 
  factory->n_loader_entries       = n_loader_entries;
163
 
 
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);
 
204
 
 
205
  factory->priv->path_property_name     = g_strdup (path_property_name);
 
206
  factory->priv->writable_property_name = g_strdup (writable_property_name);
 
207
 
 
208
  factory->priv->loader_entries         = loader_entries;
 
209
  factory->priv->n_loader_entries       = n_loader_entries;
 
210
 
 
211
  factory->priv->data_new_func          = new_func;
 
212
  factory->priv->data_get_standard_func = get_standard_func;
166
213
 
167
214
  return factory;
168
215
}
169
216
 
170
217
void
171
218
gimp_data_factory_data_init (GimpDataFactory *factory,
 
219
                             GimpContext     *context,
172
220
                             gboolean         no_data)
173
221
{
174
222
  g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
 
223
  g_return_if_fail (GIMP_IS_CONTEXT (context));
175
224
 
176
225
  /*  Freeze and thaw the container even if no_data,
177
226
   *  this creates the standard data that serves as fallback.
178
227
   */
179
 
  gimp_container_freeze (factory->container);
 
228
  gimp_container_freeze (factory->priv->container);
180
229
 
181
230
  if (! no_data)
182
231
    {
183
 
      if (factory->gimp->be_verbose)
 
232
      if (factory->priv->gimp->be_verbose)
184
233
        {
185
 
          const gchar *name = gimp_object_get_name (GIMP_OBJECT (factory));
 
234
          const gchar *name = gimp_object_get_name (factory);
186
235
 
187
236
          g_print ("Loading '%s' data\n", name ? name : "???");
188
237
        }
189
238
 
190
 
      gimp_data_factory_data_load (factory, NULL);
 
239
      gimp_data_factory_data_load (factory, context, NULL);
191
240
    }
192
241
 
193
 
  gimp_container_thaw (factory->container);
 
242
  gimp_container_thaw (factory->priv->container);
194
243
}
195
244
 
196
 
typedef struct
 
245
static void
 
246
gimp_data_factory_refresh_cache_add (GimpDataFactory *factory,
 
247
                                     GimpData        *data,
 
248
                                     gpointer         user_data)
197
249
{
198
 
  GimpDataFactory *factory;
199
 
  GHashTable      *cache;
200
 
} GimpDataLoadContext;
 
250
  const gchar *filename = gimp_data_get_filename (data);
 
251
 
 
252
  if (filename)
 
253
    {
 
254
      GHashTable *cache = user_data;
 
255
      GList      *list;
 
256
 
 
257
      g_object_ref (data);
 
258
 
 
259
      gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
 
260
 
 
261
      list = g_hash_table_lookup (cache, filename);
 
262
      list = g_list_prepend (list, data);
 
263
 
 
264
      g_hash_table_insert (cache, (gpointer) filename, list);
 
265
    }
 
266
}
201
267
 
202
268
static gboolean
203
269
gimp_data_factory_refresh_cache_remove (gpointer key,
215
281
}
216
282
 
217
283
static void
218
 
gimp_data_factory_data_move_to_cache (GimpDataFactory *factory,
219
 
                                      gpointer         object,
220
 
                                      gpointer         data)
221
 
{
222
 
  GHashTable  *cache    = data;
223
 
  GList       *list;
224
 
  const gchar *filename = GIMP_DATA (object)->filename;
225
 
 
226
 
  g_object_ref (object);
227
 
 
228
 
  gimp_container_remove (factory->container, GIMP_OBJECT (object));
229
 
 
230
 
  list = g_hash_table_lookup (cache, filename);
231
 
  list = g_list_prepend (list, object);
232
 
 
233
 
  g_hash_table_insert (cache, (gpointer) filename, list);
234
 
}
235
 
 
236
 
static void
237
 
gimp_data_factory_data_foreach (GimpDataFactory *factory,
238
 
                                void (*callback) (GimpDataFactory *factory,
239
 
                                                  gpointer         expr,
240
 
                                                  gpointer         context),
241
 
                                gpointer         context)
242
 
{
243
 
  GimpList *list;
244
 
 
245
 
  if (gimp_container_is_empty (factory->container))
246
 
    return;
247
 
 
248
 
  list = GIMP_LIST (factory->container);
249
 
 
250
 
  if (list->list)
251
 
    {
252
 
      if (GIMP_DATA (list->list->data)->internal)
253
 
        {
254
 
          /*  if there are internal objects in the list, skip them  */
255
 
          GList *glist;
256
 
 
257
 
          for (glist = list->list; glist; glist = g_list_next (glist))
258
 
            {
259
 
              if (glist->next && ! GIMP_DATA (glist->next->data)->internal)
260
 
                {
261
 
                  while (glist->next)
262
 
                    callback (factory, glist->next->data, context);
263
 
 
264
 
                  break;
265
 
                }
266
 
            }
267
 
        }
268
 
      else
269
 
        {
270
 
          while (list->list)
271
 
            callback (factory, list->list->data, context);
272
 
        }
273
 
    }
274
 
}
 
284
gimp_data_factory_data_foreach (GimpDataFactory     *factory,
 
285
                                gboolean             skip_internal,
 
286
                                GimpDataForeachFunc  callback,
 
287
                                gpointer             user_data)
 
288
{
 
289
  GList *list = GIMP_LIST (factory->priv->container)->list;
 
290
 
 
291
  if (skip_internal)
 
292
    {
 
293
      while (list && gimp_data_is_internal (GIMP_DATA (list->data)))
 
294
        list = g_list_next (list);
 
295
    }
 
296
 
 
297
  while (list)
 
298
    {
 
299
      GList *next = g_list_next (list);
 
300
 
 
301
      callback (factory, list->data, user_data);
 
302
 
 
303
      list = next;
 
304
    }
 
305
}
 
306
 
 
307
typedef struct
 
308
{
 
309
  GimpDataFactory *factory;
 
310
  GimpContext     *context;
 
311
  GHashTable      *cache;
 
312
  const gchar     *top_directory;
 
313
} GimpDataLoadContext;
275
314
 
276
315
static void
277
316
gimp_data_factory_data_load (GimpDataFactory *factory,
 
317
                             GimpContext     *context,
278
318
                             GHashTable      *cache)
279
319
{
280
320
  gchar *path;
281
321
  gchar *writable_path;
282
322
 
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,
286
326
                NULL);
287
327
 
288
328
  if (path && strlen (path))
289
329
    {
290
330
      GList               *writable_list = NULL;
291
331
      gchar               *tmp;
292
 
      GimpDataLoadContext  context;
 
332
      GimpDataLoadContext  load_context = { 0, };
293
333
 
294
 
      context.factory = factory;
295
 
      context.cache   = cache;
 
334
      load_context.factory = factory;
 
335
      load_context.context = context;
 
336
      load_context.cache   = cache;
296
337
 
297
338
      tmp = gimp_config_path_expand (path, TRUE, NULL);
298
339
      g_free (path);
310
351
                             WRITABLE_PATH_KEY, writable_list);
311
352
        }
312
353
 
313
 
      gimp_datafiles_read_directories (path, G_FILE_TEST_EXISTS,
314
 
                                       gimp_data_factory_load_data, &context);
 
354
      gimp_datafiles_read_directories (path, G_FILE_TEST_IS_REGULAR,
 
355
                                       gimp_data_factory_load_data,
 
356
                                       &load_context);
315
357
 
316
358
      gimp_datafiles_read_directories (path, G_FILE_TEST_IS_DIR,
317
359
                                       gimp_data_factory_load_data_recursive,
318
 
                                       &context);
 
360
                                       &load_context);
319
361
 
320
362
      if (writable_path)
321
363
        {
328
370
  g_free (writable_path);
329
371
}
330
372
 
331
 
static void
332
 
gimp_data_factory_data_reload (GimpDataFactory *factory)
 
373
void
 
374
gimp_data_factory_data_refresh (GimpDataFactory *factory,
 
375
                                GimpContext     *context)
333
376
{
334
 
 
335
377
  GHashTable *cache;
336
378
 
337
379
  g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
338
 
 
339
 
  gimp_container_freeze (factory->container);
 
380
  g_return_if_fail (GIMP_IS_CONTEXT (context));
 
381
 
 
382
  gimp_container_freeze (factory->priv->container);
 
383
 
 
384
  /*  First, save all dirty data objects  */
 
385
  gimp_data_factory_data_save (factory);
340
386
 
341
387
  cache = g_hash_table_new (g_str_hash, g_str_equal);
342
388
 
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);
345
391
 
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.
349
395
   *
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)
353
399
   */
354
 
  gimp_data_factory_data_load (factory, cache);
 
400
  gimp_data_factory_data_load (factory, context, cache);
355
401
 
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);
 
405
 
359
406
  g_hash_table_destroy (cache);
360
407
 
361
 
  gimp_container_thaw (factory->container);
362
 
}
363
 
 
364
 
void
365
 
gimp_data_factory_data_refresh (GimpDataFactory *factory)
366
 
{
367
 
  g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
368
 
 
369
 
  gimp_container_freeze (factory->container);
370
 
 
371
 
  gimp_data_factory_data_save (factory);
372
 
 
373
 
  gimp_data_factory_data_reload (factory);
374
 
 
375
 
  gimp_container_thaw (factory->container);
 
408
  gimp_container_thaw (factory->priv->container);
376
409
}
377
410
 
378
411
void
379
412
gimp_data_factory_data_save (GimpDataFactory *factory)
380
413
{
381
 
  GList *list;
382
 
  gchar *writable_dir;
 
414
  GList  *list;
 
415
  gchar  *writable_dir;
 
416
  GError *error = NULL;
383
417
 
384
418
  g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
385
419
 
386
 
  if (gimp_container_is_empty (factory->container))
 
420
  if (gimp_container_is_empty (factory->priv->container))
387
421
    return;
388
422
 
389
 
  writable_dir = gimp_data_factory_get_save_dir (factory);
 
423
  writable_dir = gimp_data_factory_get_save_dir (factory, &error);
390
424
 
391
425
  if (! writable_dir)
392
 
    return;
393
 
 
394
 
  for (list = GIMP_LIST (factory->container)->list;
 
426
    {
 
427
      gimp_message (factory->priv->gimp, NULL, GIMP_MESSAGE_ERROR,
 
428
                    _("Failed to save data:\n\n%s"),
 
429
                    error->message);
 
430
      g_clear_error (&error);
 
431
 
 
432
      return;
 
433
    }
 
434
 
 
435
  for (list = GIMP_LIST (factory->priv->container)->list;
395
436
       list;
396
437
       list = g_list_next (list))
397
438
    {
398
439
      GimpData *data = list->data;
399
440
 
400
 
      if (! data->filename)
 
441
      if (! gimp_data_get_filename (data))
401
442
        gimp_data_create_filename (data, writable_dir);
402
443
 
403
 
      if (data->dirty && data->writable)
 
444
      if (gimp_data_is_dirty (data) &&
 
445
          gimp_data_is_writable (data))
404
446
        {
405
447
          GError *error = NULL;
406
448
 
411
453
               */
412
454
              if (error)
413
455
                {
414
 
                  gimp_message (factory->gimp, NULL, GIMP_MESSAGE_ERROR,
 
456
                  gimp_message (factory->priv->gimp, NULL, GIMP_MESSAGE_ERROR,
415
457
                                _("Failed to save data:\n\n%s"),
416
458
                                error->message);
417
459
                  g_clear_error (&error);
425
467
 
426
468
static void
427
469
gimp_data_factory_remove_cb (GimpDataFactory *factory,
428
 
                             gpointer         data,
429
 
                             gpointer         context)
 
470
                             GimpData        *data,
 
471
                             gpointer         user_data)
430
472
{
431
 
  gimp_container_remove (factory->container, GIMP_OBJECT (data));
 
473
  gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
432
474
}
433
475
 
434
476
void
436
478
{
437
479
  g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
438
480
 
439
 
  gimp_container_freeze (factory->container);
440
 
 
441
 
  gimp_data_factory_data_foreach (factory, gimp_data_factory_remove_cb, NULL);
442
 
 
443
 
  gimp_container_thaw (factory->container);
 
481
  gimp_container_freeze (factory->priv->container);
 
482
 
 
483
  gimp_data_factory_data_foreach (factory, TRUE,
 
484
                                  gimp_data_factory_remove_cb, NULL);
 
485
 
 
486
  gimp_container_thaw (factory->priv->container);
444
487
}
445
488
 
446
489
GimpData *
447
490
gimp_data_factory_data_new (GimpDataFactory *factory,
 
491
                            GimpContext     *context,
448
492
                            const gchar     *name)
449
493
{
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);
453
498
 
454
 
  if (factory->data_new_func)
 
499
  if (factory->priv->data_new_func)
455
500
    {
456
 
      GimpData *data = factory->data_new_func (name);
 
501
      GimpData *data = factory->priv->data_new_func (context, name);
457
502
 
458
503
      if (data)
459
504
        {
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);
462
507
 
463
508
          return data;
464
509
        }
465
510
 
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);
467
512
    }
468
513
 
469
514
  return NULL;
482
527
 
483
528
  if (new_data)
484
529
    {
485
 
      const gchar *name;
 
530
      const gchar *name = gimp_object_get_name (data);
486
531
      gchar       *ext;
487
532
      gint         copy_len;
488
533
      gint         number;
489
534
      gchar       *new_name;
490
535
 
491
 
      name = gimp_object_get_name (GIMP_OBJECT (data));
492
 
 
493
536
      ext      = strrchr (name, '#');
494
537
      copy_len = strlen (_("copy"));
495
538
 
508
551
 
509
552
      gimp_object_take_name (GIMP_OBJECT (new_data), new_name);
510
553
 
511
 
      gimp_container_add (factory->container, GIMP_OBJECT (new_data));
 
554
      gimp_container_add (factory->priv->container, GIMP_OBJECT (new_data));
512
555
      g_object_unref (new_data);
513
556
    }
514
557
 
527
570
  g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
528
571
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
529
572
 
530
 
  if (gimp_container_have (factory->container, GIMP_OBJECT (data)))
 
573
  if (gimp_container_have (factory->priv->container, GIMP_OBJECT (data)))
531
574
    {
532
575
      g_object_ref (data);
533
576
 
534
 
      gimp_container_remove (factory->container, GIMP_OBJECT (data));
 
577
      gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
535
578
 
536
 
      if (delete_from_disk && data->filename)
 
579
      if (delete_from_disk && gimp_data_get_filename (data))
537
580
        retval = gimp_data_delete_from_disk (data, error);
538
581
 
539
582
      g_object_unref (data);
543
586
}
544
587
 
545
588
GimpData *
546
 
gimp_data_factory_data_get_standard (GimpDataFactory *factory)
 
589
gimp_data_factory_data_get_standard (GimpDataFactory *factory,
 
590
                                     GimpContext     *context)
547
591
{
548
592
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
 
593
  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
549
594
 
550
 
  if (factory->data_get_standard_func)
551
 
    return factory->data_get_standard_func ();
 
595
  if (factory->priv->data_get_standard_func)
 
596
    return factory->priv->data_get_standard_func (context);
552
597
 
553
598
  return NULL;
554
599
}
562
607
  g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
563
608
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
564
609
 
565
 
  if (! data->dirty)
 
610
  if (! gimp_data_is_dirty (data))
566
611
    return TRUE;
567
612
 
568
 
  if (! data->filename)
 
613
  if (! gimp_data_get_filename (data))
569
614
    {
570
 
      gchar *writable_dir = gimp_data_factory_get_save_dir (factory);
 
615
      gchar  *writable_dir;
 
616
      GError *my_error = NULL;
 
617
 
 
618
      writable_dir = gimp_data_factory_get_save_dir (factory, &my_error);
571
619
 
572
620
      if (! writable_dir)
573
621
        {
574
622
          g_set_error (error, GIMP_DATA_ERROR, 0,
575
623
                       _("Failed to save data:\n\n%s"),
576
 
                       _("You don't have a writable data folder configured."));
 
624
                       my_error->message);
 
625
          g_clear_error (&my_error);
 
626
 
577
627
          return FALSE;
578
628
        }
579
629
 
582
632
      g_free (writable_dir);
583
633
    }
584
634
 
585
 
  if (! data->writable)
 
635
  if (! gimp_data_is_writable (data))
586
636
    return FALSE;
587
637
 
588
638
  if (! gimp_data_save (data, error))
601
651
  return TRUE;
602
652
}
603
653
 
 
654
GimpContainer *
 
655
gimp_data_factory_get_container (GimpDataFactory *factory)
 
656
{
 
657
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
 
658
 
 
659
  return factory->priv->container;
 
660
}
 
661
 
 
662
GimpContainer *
 
663
gimp_data_factory_get_container_obsolete (GimpDataFactory *factory)
 
664
{
 
665
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
 
666
 
 
667
  return factory->priv->container_obsolete;
 
668
}
 
669
 
 
670
Gimp *
 
671
gimp_data_factory_get_gimp (GimpDataFactory *factory)
 
672
{
 
673
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
 
674
 
 
675
  return factory->priv->gimp;
 
676
}
 
677
 
 
678
gboolean
 
679
gimp_data_factory_has_data_new_func (GimpDataFactory *factory)
 
680
{
 
681
  g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), FALSE);
 
682
 
 
683
  return factory->priv->data_new_func != NULL;
 
684
}
 
685
 
604
686
 
605
687
/*  private functions  */
606
688
 
607
689
static gchar *
608
 
gimp_data_factory_get_save_dir (GimpDataFactory *factory)
 
690
gimp_data_factory_get_save_dir (GimpDataFactory  *factory,
 
691
                                GError          **error)
609
692
{
610
693
  gchar *path;
611
694
  gchar *writable_path;
615
698
  GList *list;
616
699
  gchar *writable_dir = NULL;
617
700
 
618
 
  g_object_get (factory->gimp->config,
619
 
                factory->path_property_name,     &path,
620
 
                factory->writable_property_name, &writable_path,
 
701
  g_object_get (factory->priv->gimp->config,
 
702
                factory->priv->path_property_name,     &path,
 
703
                factory->priv->writable_property_name, &writable_path,
621
704
                NULL);
622
705
 
623
706
  tmp = gimp_config_path_expand (path, TRUE, NULL);
628
711
  g_free (writable_path);
629
712
  writable_path = tmp;
630
713
 
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);
633
716
 
634
717
  g_free (path);
635
718
  g_free (writable_path);
636
719
 
637
 
  for (list = writable_list; list; list = g_list_next (list))
638
 
    {
639
 
      GList *found = g_list_find_custom (path_list,
640
 
                                         list->data, (GCompareFunc) strcmp);
641
 
      if (found)
642
 
        {
643
 
          writable_dir = g_strdup (found->data);
644
 
          break;
645
 
        }
 
720
  if (writable_path)
 
721
    {
 
722
      gboolean found_any = FALSE;
 
723
 
 
724
      for (list = writable_list; list; list = g_list_next (list))
 
725
        {
 
726
          GList *found = g_list_find_custom (path_list,
 
727
                                             list->data, (GCompareFunc) strcmp);
 
728
          if (found)
 
729
            {
 
730
              const gchar *dir = found->data;
 
731
 
 
732
              found_any = TRUE;
 
733
 
 
734
              if (! g_file_test (dir, G_FILE_TEST_IS_DIR))
 
735
                {
 
736
                  /*  error out only if this is the last chance  */
 
737
                  if (! list->next)
 
738
                    {
 
739
                      gchar *display_name = g_filename_display_name (dir);
 
740
 
 
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."),
 
747
                                   display_name);
 
748
 
 
749
                      g_free (display_name);
 
750
                    }
 
751
                }
 
752
              else
 
753
                {
 
754
                  writable_dir = g_strdup (dir);
 
755
                  break;
 
756
                }
 
757
            }
 
758
        }
 
759
 
 
760
      if (! writable_dir && ! found_any)
 
761
        {
 
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' "
 
767
                         "section."));
 
768
        }
 
769
    }
 
770
  else
 
771
    {
 
772
      g_set_error (error, GIMP_DATA_ERROR, 0,
 
773
                   _("You don't have any writable data folder configured."));
646
774
    }
647
775
 
648
776
  gimp_path_free (path_list);
651
779
  return writable_dir;
652
780
}
653
781
 
 
782
static gboolean
 
783
gimp_data_factory_is_dir_writable (const gchar *dirname,
 
784
                                   GList       *writable_path)
 
785
{
 
786
  GList *list;
 
787
 
 
788
  for (list = writable_path; list; list = g_list_next (list))
 
789
    {
 
790
      if (g_str_has_prefix (dirname, list->data))
 
791
        return TRUE;
 
792
    }
 
793
 
 
794
  return FALSE;
 
795
}
 
796
 
654
797
static void
655
798
gimp_data_factory_load_data_recursive (const GimpDatafileData *file_data,
656
799
                                       gpointer                data)
657
800
{
658
801
  GimpDataLoadContext *context = data;
659
 
 
660
 
  gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_EXISTS,
 
802
  gboolean             top_set = FALSE;
 
803
 
 
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.
 
809
   */
 
810
  if (! context->top_directory)
 
811
    {
 
812
      context->top_directory = file_data->dirname;
 
813
      top_set = TRUE;
 
814
    }
 
815
 
 
816
  gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_IS_REGULAR,
661
817
                                   gimp_data_factory_load_data, context);
662
818
 
663
819
  gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_IS_DIR,
664
820
                                   gimp_data_factory_load_data_recursive,
665
821
                                   context);
 
822
 
 
823
  /*  Unset, the string is only valid within this function, and will
 
824
   *  be set again for the next subdirectory.
 
825
   */
 
826
  if (top_set)
 
827
    context->top_directory = NULL;
666
828
}
667
829
 
668
830
static void
669
831
gimp_data_factory_load_data (const GimpDatafileData *file_data,
670
832
                             gpointer                data)
671
833
{
672
 
  GimpDataLoadContext *context = data;
673
 
  GimpDataFactory     *factory = context->factory;
674
 
  GHashTable          *cache   = context->cache;
675
 
  gint                 i;
 
834
  GimpDataLoadContext              *context = data;
 
835
  GimpDataFactory                  *factory = context->factory;
 
836
  GHashTable                       *cache   = context->cache;
 
837
  const GimpDataFactoryLoaderEntry *loader  = NULL;
 
838
  GError                           *error   = NULL;
 
839
  GList                            *data_list;
 
840
  gint                              i;
676
841
 
677
 
  for (i = 0; i < factory->n_loader_entries; i++)
 
842
  for (i = 0; i < factory->priv->n_loader_entries; i++)
678
843
    {
679
 
      if (! factory->loader_entries[i].extension ||
 
844
      loader = &factory->priv->loader_entries[i];
 
845
 
 
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
 
849
       */
 
850
      if (! loader->extension ||
680
851
          gimp_datafiles_check_extension (file_data->filename,
681
 
                                          factory->loader_entries[i].extension))
 
852
                                          loader->extension))
682
853
        goto insert;
683
854
    }
684
855
 
685
856
  return;
686
857
 
687
858
 insert:
688
 
  {
689
 
    GList    *cached_data;
690
 
    gboolean  load_from_disk = TRUE;
691
 
 
692
 
    if (cache &&
693
 
        (cached_data = g_hash_table_lookup (cache, file_data->filename)))
694
 
      {
695
 
        GimpData *data = cached_data->data;
696
 
 
697
 
        load_from_disk = (data->mtime == 0 || data->mtime != file_data->mtime);
698
 
 
699
 
        if (! load_from_disk)
700
 
          {
701
 
            GList *list;
702
 
 
703
 
            for (list = cached_data; list; list = g_list_next (list))
704
 
              gimp_container_add (factory->container, GIMP_OBJECT (list->data));
705
 
          }
706
 
      }
707
 
 
708
 
    if (load_from_disk)
709
 
      {
710
 
        GList  *data_list;
711
 
        GError *error = NULL;
712
 
 
713
 
        data_list = factory->loader_entries[i].load_func (file_data->filename,
714
 
                                                          &error);
715
 
 
716
 
        if (G_LIKELY (data_list))
717
 
          {
718
 
            GList    *writable_list;
719
 
            GList    *list;
720
 
            gboolean  writable;
721
 
            gboolean  deletable;
722
 
 
723
 
            writable_list = g_object_get_data (G_OBJECT (factory),
724
 
                                               WRITABLE_PATH_KEY);
725
 
 
726
 
            deletable = (g_list_length (data_list) == 1 &&
727
 
                         g_list_find_custom (writable_list, file_data->dirname,
728
 
                                             (GCompareFunc) strcmp) != NULL);
729
 
 
730
 
            writable = (deletable && factory->loader_entries[i].writable);
731
 
 
732
 
            for (list = data_list; list; list = g_list_next (list))
733
 
              {
734
 
                GimpData *data = list->data;
735
 
 
736
 
                gimp_data_set_filename (data, file_data->filename,
737
 
                                        writable, deletable);
738
 
                data->mtime = file_data->mtime;
739
 
                data->dirty = FALSE;
740
 
 
741
 
                gimp_container_add (factory->container, GIMP_OBJECT (data));
742
 
                g_object_unref (data);
743
 
              }
744
 
 
745
 
            g_list_free (data_list);
746
 
          }
747
 
 
748
 
        if (G_UNLIKELY (error))
749
 
          {
750
 
            gimp_message (factory->gimp, NULL, GIMP_MESSAGE_ERROR,
751
 
                          _("Failed to load data:\n\n%s"), error->message);
752
 
            g_clear_error (&error);
753
 
          }
754
 
      }
755
 
  }
 
859
  if (cache)
 
860
    {
 
861
      GList *cached_data;
 
862
 
 
863
      cached_data = g_hash_table_lookup (cache, file_data->filename);
 
864
 
 
865
      if (cached_data &&
 
866
          gimp_data_get_mtime (cached_data->data) != 0 &&
 
867
          gimp_data_get_mtime (cached_data->data) == file_data->mtime)
 
868
        {
 
869
          GList *list;
 
870
 
 
871
          for (list = cached_data; list; list = g_list_next (list))
 
872
            gimp_container_add (factory->priv->container, list->data);
 
873
 
 
874
          return;
 
875
        }
 
876
    }
 
877
 
 
878
  data_list = loader->load_func (context->context, file_data->filename, &error);
 
879
 
 
880
  if (G_LIKELY (data_list))
 
881
    {
 
882
      GList    *list;
 
883
      gboolean  obsolete;
 
884
      gboolean  writable  = FALSE;
 
885
      gboolean  deletable = FALSE;
 
886
 
 
887
      obsolete = (strstr (file_data->dirname,
 
888
                          GIMP_OBSOLETE_DATA_DIR_NAME) != 0);
 
889
 
 
890
      /* obsolete files are immutable, don't check their writability */
 
891
      if (! obsolete)
 
892
        {
 
893
          GList *writable_list;
 
894
 
 
895
          writable_list = g_object_get_data (G_OBJECT (factory),
 
896
                                             WRITABLE_PATH_KEY);
 
897
 
 
898
          deletable = (g_list_length (data_list) == 1 &&
 
899
                       gimp_data_factory_is_dir_writable (file_data->dirname,
 
900
                                                          writable_list));
 
901
 
 
902
          writable = (deletable && loader->writable);
 
903
        }
 
904
 
 
905
      for (list = data_list; list; list = g_list_next (list))
 
906
        {
 
907
          GimpData *data = list->data;
 
908
 
 
909
          gimp_data_set_filename (data, file_data->filename,
 
910
                                  writable, deletable);
 
911
          gimp_data_set_mtime (data, file_data->mtime);
 
912
 
 
913
          gimp_data_clean (data);
 
914
 
 
915
          if (obsolete)
 
916
            {
 
917
              gimp_container_add (factory->priv->container_obsolete,
 
918
                                  GIMP_OBJECT (data));
 
919
            }
 
920
          else
 
921
            {
 
922
              gimp_data_set_folder_tags (data, context->top_directory);
 
923
 
 
924
              gimp_container_add (factory->priv->container,
 
925
                                  GIMP_OBJECT (data));
 
926
            }
 
927
 
 
928
          g_object_unref (data);
 
929
        }
 
930
 
 
931
      g_list_free (data_list);
 
932
    }
 
933
 
 
934
  if (G_UNLIKELY (error))
 
935
    {
 
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);
 
939
    }
756
940
}