2006-12-10 05:33:38 +08:00
|
|
|
/* GIMP - The GNU Image Manipulation Program
|
2001-02-14 03:53:07 +08:00
|
|
|
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
|
|
|
|
*
|
2001-04-22 08:38:56 +08:00
|
|
|
* gimpdatafactory.c
|
|
|
|
* Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
|
|
|
|
*
|
2001-02-14 03:53:07 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-08-14 22:53:55 +08:00
|
|
|
#include <glib-object.h>
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-10-23 22:55:07 +08:00
|
|
|
#include "libgimpbase/gimpbase.h"
|
2007-03-09 21:00:01 +08:00
|
|
|
#include "libgimpmath/gimpmath.h"
|
2005-01-26 03:11:26 +08:00
|
|
|
#include "libgimpconfig/gimpconfig.h"
|
2002-10-23 22:55:07 +08:00
|
|
|
|
2001-05-10 06:34:59 +08:00
|
|
|
#include "core-types.h"
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-12-01 00:31:39 +08:00
|
|
|
#include "gimp.h"
|
2001-07-10 03:48:30 +08:00
|
|
|
#include "gimpcontext.h"
|
2001-02-14 03:53:07 +08:00
|
|
|
#include "gimpdata.h"
|
|
|
|
#include "gimpdatafactory.h"
|
2004-05-24 18:49:34 +08:00
|
|
|
#include "gimplist.h"
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2003-03-26 00:38:19 +08:00
|
|
|
#include "gimp-intl.h"
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
#define WRITABLE_PATH_KEY "gimp-data-factory-writable-path"
|
2001-08-11 22:39:19 +08:00
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void gimp_data_factory_finalize (GObject *object);
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void gimp_data_factory_data_load (GimpDataFactory *factory,
|
|
|
|
GHashTable *cache);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static gint64 gimp_data_factory_get_memsize (GimpObject *object,
|
|
|
|
gint64 *gui_size);
|
2005-04-13 06:58:12 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static gchar * gimp_data_factory_get_save_dir (GimpDataFactory *factory);
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void gimp_data_factory_load_data (const GimpDatafileData *file_data,
|
|
|
|
gpointer data);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2008-02-06 03:15:13 +08:00
|
|
|
static void gimp_data_factory_load_data_recursive (const GimpDatafileData *file_data,
|
|
|
|
gpointer data);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2006-05-15 17:46:31 +08:00
|
|
|
G_DEFINE_TYPE (GimpDataFactory, gimp_data_factory, GIMP_TYPE_OBJECT)
|
2005-12-11 03:24:36 +08:00
|
|
|
|
|
|
|
#define parent_class gimp_data_factory_parent_class
|
2001-02-14 03:53:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_data_factory_class_init (GimpDataFactoryClass *klass)
|
|
|
|
{
|
2004-05-24 18:49:34 +08:00
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-01-31 00:14:26 +08:00
|
|
|
object_class->finalize = gimp_data_factory_finalize;
|
|
|
|
|
|
|
|
gimp_object_class->get_memsize = gimp_data_factory_get_memsize;
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_data_factory_init (GimpDataFactory *factory)
|
|
|
|
{
|
2002-12-01 00:31:39 +08:00
|
|
|
factory->gimp = NULL;
|
2001-02-14 03:53:07 +08:00
|
|
|
factory->container = NULL;
|
2002-12-02 21:39:09 +08:00
|
|
|
factory->path_property_name = NULL;
|
2004-01-29 05:53:50 +08:00
|
|
|
factory->writable_property_name = NULL;
|
2001-02-14 09:42:12 +08:00
|
|
|
factory->loader_entries = NULL;
|
|
|
|
factory->n_loader_entries = 0;
|
|
|
|
factory->data_new_func = NULL;
|
|
|
|
factory->data_get_standard_func = NULL;
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2001-08-11 22:39:19 +08:00
|
|
|
gimp_data_factory_finalize (GObject *object)
|
2001-02-14 03:53:07 +08:00
|
|
|
{
|
2004-05-24 18:49:34 +08:00
|
|
|
GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
|
|
|
if (factory->container)
|
2001-07-05 03:31:35 +08:00
|
|
|
{
|
2002-12-02 21:39:09 +08:00
|
|
|
g_object_unref (factory->container);
|
2001-07-05 03:31:35 +08:00
|
|
|
factory->container = NULL;
|
|
|
|
}
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
if (factory->path_property_name)
|
|
|
|
{
|
|
|
|
g_free (factory->path_property_name);
|
|
|
|
factory->path_property_name = NULL;
|
|
|
|
}
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (factory->writable_property_name)
|
|
|
|
{
|
|
|
|
g_free (factory->writable_property_name);
|
|
|
|
factory->writable_property_name = NULL;
|
|
|
|
}
|
|
|
|
|
2001-08-11 22:39:19 +08:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
2003-11-17 01:51:36 +08:00
|
|
|
static gint64
|
2003-08-25 18:49:33 +08:00
|
|
|
gimp_data_factory_get_memsize (GimpObject *object,
|
2003-11-17 01:51:36 +08:00
|
|
|
gint64 *gui_size)
|
2002-01-31 00:14:26 +08:00
|
|
|
{
|
2004-05-24 18:49:34 +08:00
|
|
|
GimpDataFactory *factory = GIMP_DATA_FACTORY (object);
|
2003-11-17 01:51:36 +08:00
|
|
|
gint64 memsize = 0;
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2003-08-25 18:49:33 +08:00
|
|
|
memsize += gimp_object_get_memsize (GIMP_OBJECT (factory->container),
|
|
|
|
gui_size);
|
2002-01-31 00:14:26 +08:00
|
|
|
|
2003-08-25 18:49:33 +08:00
|
|
|
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
|
|
|
|
gui_size);
|
2002-01-31 00:14:26 +08:00
|
|
|
}
|
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
GimpDataFactory *
|
2002-12-02 21:39:09 +08:00
|
|
|
gimp_data_factory_new (Gimp *gimp,
|
|
|
|
GType data_type,
|
2006-04-12 20:49:29 +08:00
|
|
|
const gchar *path_property_name,
|
2004-01-29 05:53:50 +08:00
|
|
|
const gchar *writable_property_name,
|
2006-04-12 20:49:29 +08:00
|
|
|
const GimpDataFactoryLoaderEntry *loader_entries,
|
|
|
|
gint n_loader_entries,
|
|
|
|
GimpDataNewFunc new_func,
|
|
|
|
GimpDataGetStandardFunc standard_func)
|
2001-02-14 03:53:07 +08:00
|
|
|
{
|
|
|
|
GimpDataFactory *factory;
|
|
|
|
|
2002-12-01 00:31:39 +08:00
|
|
|
g_return_val_if_fail (GIMP_IS_GIMP (gimp), NULL);
|
2001-08-11 22:39:19 +08:00
|
|
|
g_return_val_if_fail (g_type_is_a (data_type, GIMP_TYPE_DATA), NULL);
|
2002-12-02 21:39:09 +08:00
|
|
|
g_return_val_if_fail (path_property_name != NULL, NULL);
|
2004-01-29 05:53:50 +08:00
|
|
|
g_return_val_if_fail (writable_property_name != NULL, NULL);
|
2001-02-14 09:42:12 +08:00
|
|
|
g_return_val_if_fail (loader_entries != NULL, NULL);
|
|
|
|
g_return_val_if_fail (n_loader_entries > 0, NULL);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2001-08-10 22:41:39 +08:00
|
|
|
factory = g_object_new (GIMP_TYPE_DATA_FACTORY, NULL);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-12-01 00:31:39 +08:00
|
|
|
factory->gimp = gimp;
|
2004-05-24 18:49:34 +08:00
|
|
|
factory->container = gimp_list_new (data_type, TRUE);
|
|
|
|
gimp_list_set_sort_func (GIMP_LIST (factory->container),
|
|
|
|
(GCompareFunc) gimp_data_name_compare);
|
2001-02-14 03:53:07 +08:00
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
factory->path_property_name = g_strdup (path_property_name);
|
2004-01-29 05:53:50 +08:00
|
|
|
factory->writable_property_name = g_strdup (writable_property_name);
|
2001-02-14 09:42:12 +08:00
|
|
|
|
|
|
|
factory->loader_entries = loader_entries;
|
|
|
|
factory->n_loader_entries = n_loader_entries;
|
|
|
|
|
|
|
|
factory->data_new_func = new_func;
|
|
|
|
factory->data_get_standard_func = standard_func;
|
2001-02-14 03:53:07 +08:00
|
|
|
|
|
|
|
return factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2001-02-14 09:42:12 +08:00
|
|
|
gimp_data_factory_data_init (GimpDataFactory *factory,
|
2006-04-12 20:49:29 +08:00
|
|
|
gboolean no_data)
|
2001-02-14 09:42:12 +08:00
|
|
|
{
|
2005-04-13 06:58:12 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
|
|
|
|
|
2005-04-13 08:16:24 +08:00
|
|
|
/* Freeze and thaw the container even if no_data,
|
|
|
|
* this creates the standard data that serves as fallback.
|
|
|
|
*/
|
|
|
|
gimp_container_freeze (factory->container);
|
|
|
|
|
|
|
|
if (! no_data)
|
2005-04-13 06:58:12 +08:00
|
|
|
{
|
2005-04-13 08:16:24 +08:00
|
|
|
if (factory->gimp->be_verbose)
|
|
|
|
{
|
|
|
|
const gchar *name = gimp_object_get_name (GIMP_OBJECT (factory));
|
2005-04-13 06:58:12 +08:00
|
|
|
|
2005-11-07 06:01:25 +08:00
|
|
|
g_print ("Loading '%s' data\n", name ? name : "???");
|
2005-04-13 08:16:24 +08:00
|
|
|
}
|
2002-12-02 21:39:09 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
gimp_data_factory_data_load (factory, NULL);
|
2005-04-13 08:16:24 +08:00
|
|
|
}
|
2005-04-13 06:58:12 +08:00
|
|
|
|
|
|
|
gimp_container_thaw (factory->container);
|
|
|
|
}
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
typedef struct
|
2005-04-13 06:58:12 +08:00
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GimpDataFactory *factory;
|
|
|
|
GHashTable *cache;
|
|
|
|
} GimpDataLoadContext;
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gimp_data_factory_refresh_cache_remove (gpointer key,
|
|
|
|
gpointer value,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2007-06-14 19:30:12 +08:00
|
|
|
GList *list;
|
|
|
|
|
|
|
|
for (list = value; list; list = list->next)
|
|
|
|
g_object_unref (list->data);
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
g_list_free (value);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_data_factory_data_move_to_cache (GimpDataFactory *factory,
|
|
|
|
gpointer object,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GHashTable *cache = data;
|
|
|
|
GList *list;
|
|
|
|
const gchar *filename = GIMP_DATA (object)->filename;
|
|
|
|
|
|
|
|
g_object_ref (object);
|
|
|
|
|
|
|
|
gimp_container_remove (factory->container, GIMP_OBJECT (object));
|
|
|
|
|
|
|
|
list = g_hash_table_lookup (cache, filename);
|
|
|
|
list = g_list_prepend (list, object);
|
|
|
|
|
|
|
|
g_hash_table_insert (cache, (gpointer) filename, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gimp_data_factory_data_foreach (GimpDataFactory *factory,
|
|
|
|
void (*callback) (GimpDataFactory *factory,
|
|
|
|
gpointer expr,
|
|
|
|
gpointer context),
|
|
|
|
gpointer context)
|
|
|
|
{
|
|
|
|
GimpList *list;
|
|
|
|
|
|
|
|
if (gimp_container_is_empty (factory->container))
|
|
|
|
return;
|
|
|
|
|
|
|
|
list = GIMP_LIST (factory->container);
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
if (list->list)
|
|
|
|
{
|
|
|
|
if (GIMP_DATA (list->list->data)->internal)
|
|
|
|
{
|
|
|
|
/* if there are internal objects in the list, skip them */
|
|
|
|
GList *glist;
|
|
|
|
|
|
|
|
for (glist = list->list; glist; glist = g_list_next (glist))
|
|
|
|
{
|
|
|
|
if (glist->next && ! GIMP_DATA (glist->next->data)->internal)
|
|
|
|
{
|
|
|
|
while (glist->next)
|
|
|
|
callback (factory, glist->next->data, context);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (list->list)
|
|
|
|
callback (factory, list->list->data, context);
|
|
|
|
}
|
|
|
|
}
|
2005-04-13 06:58:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-10-31 19:29:01 +08:00
|
|
|
gimp_data_factory_data_load (GimpDataFactory *factory,
|
|
|
|
GHashTable *cache)
|
2005-04-13 06:58:12 +08:00
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
gchar *writable_path;
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2003-01-06 06:07:10 +08:00
|
|
|
g_object_get (factory->gimp->config,
|
2004-01-29 05:53:50 +08:00
|
|
|
factory->path_property_name, &path,
|
|
|
|
factory->writable_property_name, &writable_path,
|
2002-12-02 21:39:09 +08:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
if (path && strlen (path))
|
2001-02-14 09:42:12 +08:00
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GList *writable_list = NULL;
|
|
|
|
gchar *tmp;
|
|
|
|
GimpDataLoadContext context;
|
|
|
|
|
|
|
|
context.factory = factory;
|
|
|
|
context.cache = cache;
|
2002-12-30 02:58:24 +08:00
|
|
|
|
|
|
|
tmp = gimp_config_path_expand (path, TRUE, NULL);
|
|
|
|
g_free (path);
|
|
|
|
path = tmp;
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (writable_path)
|
|
|
|
{
|
|
|
|
tmp = gimp_config_path_expand (writable_path, TRUE, NULL);
|
|
|
|
g_free (writable_path);
|
|
|
|
writable_path = tmp;
|
|
|
|
|
|
|
|
writable_list = gimp_path_parse (writable_path, 16, TRUE, NULL);
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
g_object_set_data (G_OBJECT (factory),
|
|
|
|
WRITABLE_PATH_KEY, writable_list);
|
2004-01-29 05:53:50 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
gimp_datafiles_read_directories (path, G_FILE_TEST_EXISTS,
|
|
|
|
gimp_data_factory_load_data, &context);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2008-02-06 03:15:13 +08:00
|
|
|
gimp_datafiles_read_directories (path, G_FILE_TEST_IS_DIR,
|
|
|
|
gimp_data_factory_load_data_recursive,
|
|
|
|
&context);
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (writable_path)
|
|
|
|
{
|
|
|
|
gimp_path_free (writable_list);
|
|
|
|
g_object_set_data (G_OBJECT (factory), WRITABLE_PATH_KEY, NULL);
|
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
g_free (path);
|
2004-01-29 05:53:50 +08:00
|
|
|
g_free (writable_path);
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void
|
|
|
|
gimp_data_factory_data_reload (GimpDataFactory *factory)
|
|
|
|
{
|
|
|
|
|
|
|
|
GHashTable *cache;
|
|
|
|
|
|
|
|
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
|
|
|
|
|
2007-06-14 19:12:31 +08:00
|
|
|
gimp_container_freeze (factory->container);
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
cache = g_hash_table_new (g_str_hash, g_str_equal);
|
|
|
|
|
|
|
|
gimp_data_factory_data_foreach (factory,
|
|
|
|
gimp_data_factory_data_move_to_cache, cache);
|
|
|
|
|
|
|
|
/* Now the cache contains a filename => list-of-objects mapping of
|
|
|
|
* the old objects. So we should now traverse the directory and for
|
|
|
|
* each file load it only if it's mtime is newer.
|
|
|
|
*
|
|
|
|
* Once a file was added, it is removed from the cache, so the only
|
|
|
|
* objects remaining there will be those that are not present on the
|
|
|
|
* disk (that have to be destroyed)
|
|
|
|
*/
|
|
|
|
gimp_data_factory_data_load (factory, cache);
|
|
|
|
|
|
|
|
/* Now all the data is loaded. Free what remains in the cache. */
|
|
|
|
g_hash_table_foreach_remove (cache,
|
|
|
|
gimp_data_factory_refresh_cache_remove, NULL);
|
|
|
|
g_hash_table_destroy (cache);
|
2007-06-14 19:12:31 +08:00
|
|
|
|
|
|
|
gimp_container_thaw (factory->container);
|
2005-10-31 19:29:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gimp_data_factory_data_refresh (GimpDataFactory *factory)
|
|
|
|
{
|
|
|
|
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
|
|
|
|
|
|
|
|
gimp_container_freeze (factory->container);
|
|
|
|
|
|
|
|
gimp_data_factory_data_save (factory);
|
|
|
|
|
|
|
|
gimp_data_factory_data_reload (factory);
|
|
|
|
|
|
|
|
gimp_container_thaw (factory->container);
|
|
|
|
}
|
|
|
|
|
2001-02-14 09:42:12 +08:00
|
|
|
void
|
|
|
|
gimp_data_factory_data_save (GimpDataFactory *factory)
|
2001-02-14 03:53:07 +08:00
|
|
|
{
|
2004-01-29 05:53:50 +08:00
|
|
|
GList *list;
|
|
|
|
gchar *writable_dir;
|
2001-02-18 05:20:10 +08:00
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
|
2001-02-18 05:20:10 +08:00
|
|
|
|
2005-04-13 06:58:12 +08:00
|
|
|
if (gimp_container_is_empty (factory->container))
|
2001-02-18 05:20:10 +08:00
|
|
|
return;
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
writable_dir = gimp_data_factory_get_save_dir (factory);
|
2001-02-18 05:20:10 +08:00
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (! writable_dir)
|
|
|
|
return;
|
2002-12-30 02:58:24 +08:00
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
for (list = GIMP_LIST (factory->container)->list;
|
|
|
|
list;
|
|
|
|
list = g_list_next (list))
|
|
|
|
{
|
|
|
|
GimpData *data = list->data;
|
2001-02-18 05:20:10 +08:00
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (! data->filename)
|
2004-07-06 02:10:05 +08:00
|
|
|
gimp_data_create_filename (data, writable_dir);
|
2001-02-18 05:20:10 +08:00
|
|
|
|
2004-02-13 19:53:22 +08:00
|
|
|
if (data->dirty && data->writable)
|
2004-01-29 05:53:50 +08:00
|
|
|
{
|
|
|
|
GError *error = NULL;
|
2002-12-02 21:39:09 +08:00
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
if (! gimp_data_save (data, &error))
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
2004-01-29 05:53:50 +08:00
|
|
|
/* check if there actually was an error (no error
|
|
|
|
* means the data class does not implement save)
|
|
|
|
*/
|
|
|
|
if (error)
|
2002-12-02 21:39:09 +08:00
|
|
|
{
|
2006-10-10 02:49:15 +08:00
|
|
|
gimp_message (factory->gimp, NULL, GIMP_MESSAGE_ERROR,
|
|
|
|
_("Failed to save data:\n\n%s"),
|
|
|
|
error->message);
|
2004-01-29 05:53:50 +08:00
|
|
|
g_clear_error (&error);
|
2002-12-02 21:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-02-18 05:20:10 +08:00
|
|
|
}
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
g_free (writable_dir);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void
|
|
|
|
gimp_data_factory_remove_cb (GimpDataFactory *factory,
|
|
|
|
gpointer data,
|
|
|
|
gpointer context)
|
|
|
|
{
|
|
|
|
gimp_container_remove (factory->container, GIMP_OBJECT (data));
|
|
|
|
}
|
|
|
|
|
2001-02-14 03:53:07 +08:00
|
|
|
void
|
|
|
|
gimp_data_factory_data_free (GimpDataFactory *factory)
|
|
|
|
{
|
2005-11-14 01:10:22 +08:00
|
|
|
g_return_if_fail (GIMP_IS_DATA_FACTORY (factory));
|
|
|
|
|
2007-06-14 19:12:31 +08:00
|
|
|
gimp_container_freeze (factory->container);
|
|
|
|
|
2005-11-14 01:10:22 +08:00
|
|
|
gimp_data_factory_data_foreach (factory, gimp_data_factory_remove_cb, NULL);
|
2007-06-14 19:12:31 +08:00
|
|
|
|
|
|
|
gimp_container_thaw (factory->container);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
|
|
|
|
GimpData *
|
|
|
|
gimp_data_factory_data_new (GimpDataFactory *factory,
|
2006-04-12 20:49:29 +08:00
|
|
|
const gchar *name)
|
2001-02-14 09:42:12 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
|
|
|
|
g_return_val_if_fail (name != NULL, NULL);
|
2004-10-04 17:39:27 +08:00
|
|
|
g_return_val_if_fail (*name != '\0', NULL);
|
2001-02-14 09:42:12 +08:00
|
|
|
|
|
|
|
if (factory->data_new_func)
|
|
|
|
{
|
2005-06-08 19:27:31 +08:00
|
|
|
GimpData *data = factory->data_new_func (name);
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2004-10-04 17:39:27 +08:00
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
gimp_container_add (factory->container, GIMP_OBJECT (data));
|
|
|
|
g_object_unref (data);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
|
2004-10-04 17:39:27 +08:00
|
|
|
g_warning ("%s: factory->data_new_func() returned NULL", G_STRFUNC);
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2002-12-02 21:39:09 +08:00
|
|
|
GimpData *
|
|
|
|
gimp_data_factory_data_duplicate (GimpDataFactory *factory,
|
|
|
|
GimpData *data)
|
|
|
|
{
|
2005-06-08 19:27:31 +08:00
|
|
|
GimpData *new_data;
|
2002-12-02 21:39:09 +08:00
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
|
|
|
|
|
2005-06-08 19:27:31 +08:00
|
|
|
new_data = gimp_data_duplicate (data);
|
2002-12-02 21:39:09 +08:00
|
|
|
|
|
|
|
if (new_data)
|
|
|
|
{
|
|
|
|
const gchar *name;
|
|
|
|
gchar *ext;
|
|
|
|
gint copy_len;
|
|
|
|
gint number;
|
|
|
|
gchar *new_name;
|
|
|
|
|
|
|
|
name = gimp_object_get_name (GIMP_OBJECT (data));
|
|
|
|
|
|
|
|
ext = strrchr (name, '#');
|
|
|
|
copy_len = strlen (_("copy"));
|
|
|
|
|
|
|
|
if ((strlen (name) >= copy_len &&
|
|
|
|
strcmp (&name[strlen (name) - copy_len], _("copy")) == 0) ||
|
|
|
|
(ext && (number = atoi (ext + 1)) > 0 &&
|
|
|
|
((gint) (log10 (number) + 1)) == strlen (ext + 1)))
|
|
|
|
{
|
|
|
|
/* don't have redundant "copy"s */
|
|
|
|
new_name = g_strdup (name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
new_name = g_strdup_printf (_("%s copy"), name);
|
|
|
|
}
|
|
|
|
|
2006-04-07 18:51:22 +08:00
|
|
|
gimp_object_take_name (GIMP_OBJECT (new_data), new_name);
|
2002-12-02 21:39:09 +08:00
|
|
|
|
|
|
|
gimp_container_add (factory->container, GIMP_OBJECT (new_data));
|
|
|
|
g_object_unref (new_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_data;
|
|
|
|
}
|
|
|
|
|
2004-06-02 05:37:36 +08:00
|
|
|
gboolean
|
|
|
|
gimp_data_factory_data_delete (GimpDataFactory *factory,
|
|
|
|
GimpData *data,
|
|
|
|
gboolean delete_from_disk,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
gboolean retval = TRUE;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), FALSE);
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
|
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
|
|
|
|
|
|
|
if (gimp_container_have (factory->container, GIMP_OBJECT (data)))
|
|
|
|
{
|
|
|
|
g_object_ref (data);
|
|
|
|
|
|
|
|
gimp_container_remove (factory->container, GIMP_OBJECT (data));
|
|
|
|
|
|
|
|
if (delete_from_disk && data->filename)
|
|
|
|
retval = gimp_data_delete_from_disk (data, error);
|
|
|
|
|
|
|
|
g_object_unref (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2001-02-14 09:42:12 +08:00
|
|
|
GimpData *
|
|
|
|
gimp_data_factory_data_get_standard (GimpDataFactory *factory)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), NULL);
|
|
|
|
|
|
|
|
if (factory->data_get_standard_func)
|
|
|
|
return factory->data_get_standard_func ();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-01-29 05:53:50 +08:00
|
|
|
gboolean
|
2006-10-10 02:49:15 +08:00
|
|
|
gimp_data_factory_data_save_single (GimpDataFactory *factory,
|
|
|
|
GimpData *data,
|
|
|
|
GError **error)
|
2004-01-29 05:53:50 +08:00
|
|
|
{
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), FALSE);
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
|
2006-10-10 02:49:15 +08:00
|
|
|
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
|
|
|
if (! data->dirty)
|
|
|
|
return TRUE;
|
|
|
|
|
2004-10-04 17:39:27 +08:00
|
|
|
if (! data->filename)
|
|
|
|
{
|
2005-04-13 06:58:12 +08:00
|
|
|
gchar *writable_dir = gimp_data_factory_get_save_dir (factory);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2004-10-04 17:39:27 +08:00
|
|
|
if (! writable_dir)
|
2006-10-10 02:49:15 +08:00
|
|
|
{
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, 0,
|
|
|
|
_("Failed to save data:\n\n%s"),
|
|
|
|
_("You don't have a writable data folder configured."));
|
|
|
|
return FALSE;
|
|
|
|
}
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2004-10-04 17:39:27 +08:00
|
|
|
gimp_data_create_filename (data, writable_dir);
|
|
|
|
|
|
|
|
g_free (writable_dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! data->writable)
|
|
|
|
return FALSE;
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2006-10-10 02:49:15 +08:00
|
|
|
if (! gimp_data_save (data, error))
|
2004-01-29 05:53:50 +08:00
|
|
|
{
|
|
|
|
/* check if there actually was an error (no error
|
|
|
|
* means the data class does not implement save)
|
|
|
|
*/
|
2006-10-10 02:49:15 +08:00
|
|
|
if (! error)
|
|
|
|
g_set_error (error, GIMP_DATA_ERROR, 0,
|
|
|
|
_("Failed to save data:\n\n%s"),
|
|
|
|
"Data class does not implement saving");
|
2004-01-29 05:53:50 +08:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* private functions */
|
|
|
|
|
|
|
|
static gchar *
|
|
|
|
gimp_data_factory_get_save_dir (GimpDataFactory *factory)
|
|
|
|
{
|
|
|
|
gchar *path;
|
|
|
|
gchar *writable_path;
|
|
|
|
gchar *tmp;
|
|
|
|
GList *path_list;
|
|
|
|
GList *writable_list;
|
|
|
|
GList *list;
|
|
|
|
gchar *writable_dir = NULL;
|
|
|
|
|
|
|
|
g_object_get (factory->gimp->config,
|
|
|
|
factory->path_property_name, &path,
|
|
|
|
factory->writable_property_name, &writable_path,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
tmp = gimp_config_path_expand (path, TRUE, NULL);
|
|
|
|
g_free (path);
|
|
|
|
path = tmp;
|
|
|
|
|
|
|
|
tmp = gimp_config_path_expand (writable_path, TRUE, NULL);
|
|
|
|
g_free (writable_path);
|
|
|
|
writable_path = tmp;
|
|
|
|
|
|
|
|
path_list = gimp_path_parse (path, 16, TRUE, NULL);
|
|
|
|
writable_list = gimp_path_parse (writable_path, 16, TRUE, NULL);
|
|
|
|
|
|
|
|
g_free (path);
|
|
|
|
g_free (writable_path);
|
|
|
|
|
|
|
|
for (list = writable_list; list; list = g_list_next (list))
|
|
|
|
{
|
2005-04-13 06:58:12 +08:00
|
|
|
GList *found = g_list_find_custom (path_list,
|
|
|
|
list->data, (GCompareFunc) strcmp);
|
2004-01-29 05:53:50 +08:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
writable_dir = g_strdup (found->data);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_path_free (path_list);
|
|
|
|
gimp_path_free (writable_list);
|
|
|
|
|
|
|
|
return writable_dir;
|
|
|
|
}
|
|
|
|
|
2008-02-06 03:15:13 +08:00
|
|
|
static void
|
|
|
|
gimp_data_factory_load_data_recursive (const GimpDatafileData *file_data,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GimpDataLoadContext *context = data;
|
|
|
|
|
|
|
|
gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_EXISTS,
|
|
|
|
gimp_data_factory_load_data, context);
|
|
|
|
|
|
|
|
gimp_datafiles_read_directories (file_data->filename, G_FILE_TEST_IS_DIR,
|
|
|
|
gimp_data_factory_load_data_recursive,
|
|
|
|
context);
|
|
|
|
}
|
|
|
|
|
2001-02-14 09:42:12 +08:00
|
|
|
static void
|
2003-07-03 01:11:00 +08:00
|
|
|
gimp_data_factory_load_data (const GimpDatafileData *file_data,
|
2005-10-31 19:29:01 +08:00
|
|
|
gpointer data)
|
2001-02-14 09:42:12 +08:00
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GimpDataLoadContext *context = data;
|
|
|
|
GimpDataFactory *factory = context->factory;
|
|
|
|
GHashTable *cache = context->cache;
|
|
|
|
gint i;
|
2001-02-14 09:42:12 +08:00
|
|
|
|
|
|
|
for (i = 0; i < factory->n_loader_entries; i++)
|
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
if (! factory->loader_entries[i].extension ||
|
|
|
|
gimp_datafiles_check_extension (file_data->filename,
|
|
|
|
factory->loader_entries[i].extension))
|
|
|
|
goto insert;
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
insert:
|
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GList *cached_data;
|
|
|
|
gboolean load_from_disk = TRUE;
|
2005-06-08 19:27:31 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
if (cache &&
|
|
|
|
(cached_data = g_hash_table_lookup (cache, file_data->filename)))
|
2001-08-12 23:39:23 +08:00
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GimpData *data = cached_data->data;
|
|
|
|
|
|
|
|
load_from_disk = (data->mtime == 0 || data->mtime != file_data->mtime);
|
|
|
|
|
|
|
|
if (! load_from_disk)
|
|
|
|
{
|
|
|
|
GList *list;
|
|
|
|
|
|
|
|
for (list = cached_data; list; list = g_list_next (list))
|
|
|
|
gimp_container_add (factory->container, GIMP_OBJECT (list->data));
|
|
|
|
}
|
2001-08-12 23:39:23 +08:00
|
|
|
}
|
2005-10-31 19:29:01 +08:00
|
|
|
|
|
|
|
if (load_from_disk)
|
2001-08-12 23:39:23 +08:00
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
GList *data_list;
|
|
|
|
GError *error = NULL;
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
data_list = factory->loader_entries[i].load_func (file_data->filename,
|
|
|
|
&error);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
if (G_LIKELY (data_list))
|
|
|
|
{
|
|
|
|
GList *writable_list;
|
|
|
|
GList *list;
|
|
|
|
gboolean writable;
|
|
|
|
gboolean deletable;
|
2004-10-04 17:39:27 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
writable_list = g_object_get_data (G_OBJECT (factory),
|
|
|
|
WRITABLE_PATH_KEY);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
deletable = (g_list_length (data_list) == 1 &&
|
|
|
|
g_list_find_custom (writable_list, file_data->dirname,
|
|
|
|
(GCompareFunc) strcmp) != NULL);
|
2004-01-29 05:53:50 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
writable = (deletable && factory->loader_entries[i].writable);
|
2004-07-27 03:00:22 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
for (list = data_list; list; list = g_list_next (list))
|
|
|
|
{
|
|
|
|
GimpData *data = list->data;
|
|
|
|
|
|
|
|
gimp_data_set_filename (data, file_data->filename,
|
|
|
|
writable, deletable);
|
|
|
|
data->mtime = file_data->mtime;
|
|
|
|
data->dirty = FALSE;
|
2004-07-27 03:00:22 +08:00
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
gimp_container_add (factory->container, GIMP_OBJECT (data));
|
|
|
|
g_object_unref (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_list_free (data_list);
|
|
|
|
}
|
2007-06-04 05:46:04 +08:00
|
|
|
|
|
|
|
if (G_UNLIKELY (error))
|
2005-10-31 19:29:01 +08:00
|
|
|
{
|
2006-10-10 02:49:15 +08:00
|
|
|
gimp_message (factory->gimp, NULL, GIMP_MESSAGE_ERROR,
|
|
|
|
_("Failed to load data:\n\n%s"), error->message);
|
2005-10-31 19:29:01 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
2001-08-12 23:39:23 +08:00
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
}
|