2001-02-14 03:53:07 +08:00
|
|
|
/* The GIMP -- an image manipulation program
|
|
|
|
* 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"
|
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
|
|
|
|
2005-10-31 19:29:01 +08:00
|
|
|
static void gimp_data_factory_class_init (GimpDataFactoryClass *klass);
|
|
|
|
static void gimp_data_factory_init (GimpDataFactory *factory);
|
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
|
|
|
|
|
|
|
static GimpObjectClass *parent_class = NULL;
|
|
|
|
|
|
|
|
|
2001-08-11 22:39:19 +08:00
|
|
|
GType
|
2001-02-14 03:53:07 +08:00
|
|
|
gimp_data_factory_get_type (void)
|
|
|
|
{
|
2001-08-11 22:39:19 +08:00
|
|
|
static GType factory_type = 0;
|
2001-02-14 03:53:07 +08:00
|
|
|
|
|
|
|
if (! factory_type)
|
|
|
|
{
|
2001-08-11 22:39:19 +08:00
|
|
|
static const GTypeInfo factory_info =
|
2001-02-14 03:53:07 +08:00
|
|
|
{
|
2001-08-11 22:39:19 +08:00
|
|
|
sizeof (GimpDataFactoryClass),
|
|
|
|
(GBaseInitFunc) NULL,
|
|
|
|
(GBaseFinalizeFunc) NULL,
|
|
|
|
(GClassInitFunc) gimp_data_factory_class_init,
|
|
|
|
NULL, /* class_finalize */
|
|
|
|
NULL, /* class_data */
|
2001-02-14 03:53:07 +08:00
|
|
|
sizeof (GimpDataFactory),
|
2001-08-11 22:39:19 +08:00
|
|
|
0, /* n_preallocs */
|
|
|
|
(GInstanceInitFunc) gimp_data_factory_init,
|
2001-02-14 03:53:07 +08:00
|
|
|
};
|
|
|
|
|
2001-08-11 22:39:19 +08:00
|
|
|
factory_type = g_type_register_static (GIMP_TYPE_OBJECT,
|
|
|
|
"GimpDataFactory",
|
|
|
|
&factory_info, 0);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return factory_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
2001-07-24 Michael Natterer <mitch@gimp.org>
Port to glib/gtk+ 2.0 episode I (every segfault has it's beginning)
* configure.in: require glib/gtk+ >= 1.3.7, commented out the
gtkxmhtml stuff.
From now on, you will need glib, pango, atk and gtk+ HEAD from CVS
to hack or use GIMP HEAD.
Beware, it crashes randomly :)
* app/core/Makefile.am
* app/core/gimpmarshal.list: new file plus rules to generate
gimpmarshal.[ch] from it.
* app/core/*
* app/tools/*
* app/widgets/*
* libgimpwidgets/*: started to use the glib object system. All
core/ objects are still gtk objects however. All signals are
created using g_signal_new(). There are many gtk+ artefacts left.
Finally, we will _not_ use the gtk_signal_foo() wrappers and
friends any more.
* app/colormaps.c
* app/devices.[ch]
* app/disp_callbacks.c
* app/errorconsole.c
* app/file-save.[ch]
* app/interface.c
* app/module_db.c
* app/nav_window.c
* app/ops_buttons.c
* app/scroll.c
* app/user_install.c
* app/gui/about-dialog.c
* app/gui/brush-editor.c
* app/gui/brushes-commands.c
* app/gui/color-notebook.c
* app/gui/colormap-dialog.c
* app/gui/dialogs-commands.c
* app/gui/dialogs-constructors.c
* app/gui/file-commands.c
* app/gui/file-dialog-utils.c
* app/gui/file-new-dialog.c
* app/gui/file-open-dialog.[ch]
* app/gui/file-save-dialog.c
* app/gui/gradient-editor.c
* app/gui/gradients-commands.c
* app/gui/image-commands.c
* app/gui/info-dialog.[ch]
* app/gui/layer-select.c
* app/gui/layers-commands.c
* app/gui/menus.c
* app/gui/offset-dialog.c
* app/gui/palette-editor.c
* app/gui/palettes-commands.c
* app/gui/patterns-commands.c
* app/gui/preferences-dialog.c
* app/gui/resize-dialog.[ch]
* app/gui/splash.c
* app/gui/tips-dialog.c
* app/gui/tool-options-dialog.c
* app/gui/toolbox.c
* app/gui/tools-commands.c
* libgimp/gimpbrushmenu.c
* libgimp/gimpmenu.c
* libgimp/gimppatternmenu.c
* libgimp/gimpui.c
* libgimpbase/gimpenv.c: tons and tons of changes like "const
gchar*", switch from GdkDeviceInfo to GdkDevice (very incomplete
and currently disables), lots of s/gtk_signal/g_signal/,
removal/replacement of deprecated stuff,
s/GtkSignalFunc/GCallback/ and lots of small changes and fixes
while I was on it, zillions of warnings left...
* modules/Makefile.am: disabled the water color selector
temporarily (XInput issues).
* plug-ins/Makefile.am
* plug-ins/common/.cvsignore
* plug-ins/common/Makefile.am
* plug-ins/common/plugin-defs.pl: simply excluded all plug-ins
which did not build (including Script-Fu). They are trivial to
fix.
2001-07-25 05:27:11 +08:00
|
|
|
parent_class = g_type_class_peek_parent (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,
|
|
|
|
const gchar *path_property_name,
|
2004-01-29 05:53:50 +08:00
|
|
|
const gchar *writable_property_name,
|
2002-12-02 21:39:09 +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,
|
2005-04-13 08:16:24 +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)
|
|
|
|
{
|
2005-11-07 06:01:25 +08:00
|
|
|
g_list_foreach (value, (GFunc) g_object_unref, NULL);
|
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
|
|
|
|
|
|
|
gimp_container_freeze (factory->container);
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
/* otherwise delete everything */
|
|
|
|
while (list->list)
|
|
|
|
callback (factory, list->list->data, context);
|
|
|
|
}
|
|
|
|
}
|
2005-04-13 06:58:12 +08:00
|
|
|
|
|
|
|
gimp_container_thaw (factory->container);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
gimp_container_freeze (factory->container);
|
2001-02-18 05:20:10 +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
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
g_message (_("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
|
|
|
gimp_container_thaw (factory->container);
|
|
|
|
|
|
|
|
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-10-31 19:29:01 +08:00
|
|
|
return gimp_data_factory_data_foreach (factory,
|
|
|
|
gimp_data_factory_remove_cb, NULL);
|
2001-02-14 03:53:07 +08:00
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
|
|
|
|
GimpData *
|
|
|
|
gimp_data_factory_data_new (GimpDataFactory *factory,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
gimp_object_set_name (GIMP_OBJECT (new_data), new_name);
|
|
|
|
|
|
|
|
g_free (new_name);
|
|
|
|
|
|
|
|
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
|
|
|
|
gimp_data_factory_data_save_single (GimpDataFactory *factory,
|
|
|
|
GimpData *data)
|
|
|
|
{
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA_FACTORY (factory), FALSE);
|
|
|
|
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
|
|
|
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
|
|
|
if (! gimp_data_save (data, &error))
|
|
|
|
{
|
|
|
|
/* check if there actually was an error (no error
|
|
|
|
* means the data class does not implement save)
|
|
|
|
*/
|
|
|
|
if (error)
|
|
|
|
{
|
2005-10-31 19:29:01 +08:00
|
|
|
g_message (_("Failed to save data:\n\n%s"), error->message);
|
2004-01-29 05:53:50 +08:00
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_message (_("Failed to load data:\n\n%s"), error->message);
|
|
|
|
g_clear_error (&error);
|
|
|
|
}
|
2001-08-12 23:39:23 +08:00
|
|
|
}
|
2001-02-14 09:42:12 +08:00
|
|
|
}
|
|
|
|
}
|