Commit 7abcfbf2 authored by Michael Natterer's avatar Michael Natterer 😴

Add private struct to GimpData

And start sealing by moving "filename" there. Add
gimp_data_get_filename() and use it everywhere.
parent cfa76869
......@@ -106,25 +106,30 @@ void
brushes_actions_update (GimpActionGroup *group,
gpointer user_data)
{
GimpContext *context = action_data_get_context (user_data);
GimpBrush *brush = NULL;
GimpData *data = NULL;
GimpContext *context = action_data_get_context (user_data);
GimpBrush *brush = NULL;
GimpData *data = NULL;
const gchar *filename = NULL;
if (context)
{
brush = gimp_context_get_brush (context);
if (brush)
data = GIMP_DATA (brush);
{
data = GIMP_DATA (brush);
filename = gimp_data_get_filename (data);
}
}
#define SET_SENSITIVE(action,condition) \
gimp_action_group_set_action_sensitive (group, action, (condition) != 0)
SET_SENSITIVE ("brushes-edit", brush);
SET_SENSITIVE ("brushes-open-as-image", brush && data->filename && ! GIMP_IS_BRUSH_GENERATED (brush));
SET_SENSITIVE ("brushes-open-as-image", brush && filename && ! GIMP_IS_BRUSH_GENERATED (brush));
SET_SENSITIVE ("brushes-duplicate", brush && GIMP_DATA_GET_CLASS (data)->duplicate);
SET_SENSITIVE ("brushes-copy-location", brush && data->filename);
SET_SENSITIVE ("brushes-copy-location", brush && filename);
SET_SENSITIVE ("brushes-delete", brush && data->deletable);
#undef SET_SENSITIVE
......
......@@ -83,9 +83,9 @@ data_open_as_image_cmd_callback (GtkAction *action,
gimp_context_get_by_type (context,
gimp_data_factory_view_get_children_type (view));
if (data && data->filename)
if (data && gimp_data_get_filename (data))
{
gchar *uri = g_filename_to_uri (data->filename, NULL, NULL);
gchar *uri = g_filename_to_uri (gimp_data_get_filename (data), NULL, NULL);
if (uri)
{
......@@ -188,14 +188,19 @@ data_copy_location_cmd_callback (GtkAction *action,
gimp_context_get_by_type (context,
gimp_data_factory_view_get_children_type (view));
if (data && data->filename && *data->filename)
if (data)
{
gchar *uri = g_filename_to_uri (data->filename, NULL, NULL);
const gchar *filename = gimp_data_get_filename (data);
if (uri)
if (filename && *filename)
{
gimp_clipboard_set_text (context->gimp, uri);
g_free (uri);
gchar *uri = g_filename_to_uri (filename, NULL, NULL);
if (uri)
{
gimp_clipboard_set_text (context->gimp, uri);
g_free (uri);
}
}
}
}
......
......@@ -100,16 +100,21 @@ void
dynamics_actions_update (GimpActionGroup *group,
gpointer user_data)
{
GimpContext *context = action_data_get_context (user_data);
GimpContext *context = action_data_get_context (user_data);
GimpDynamics *dynamics = NULL;
GimpData *data = NULL;
GimpData *data = NULL;
const gchar *filename = NULL;
if (context)
{
dynamics = gimp_context_get_dynamics (context);
if (dynamics)
data = GIMP_DATA (dynamics);
{
data = GIMP_DATA (dynamics);
filename = gimp_data_get_filename (data);
}
}
#define SET_SENSITIVE(action,condition) \
......@@ -117,7 +122,7 @@ dynamics_actions_update (GimpActionGroup *group,
SET_SENSITIVE ("dynamics-edit", dynamics);
SET_SENSITIVE ("dynamics-duplicate", dynamics && GIMP_DATA_GET_CLASS (data)->duplicate);
SET_SENSITIVE ("dynamics-copy-location", dynamics && data->filename);
SET_SENSITIVE ("dynamics-copy-location", dynamics && filename);
SET_SENSITIVE ("dynamics-delete", dynamics && data->deletable);
#undef SET_SENSITIVE
......
......@@ -110,13 +110,18 @@ gradients_actions_update (GimpActionGroup *group,
GimpContext *context = action_data_get_context (user_data);
GimpGradient *gradient = NULL;
GimpData *data = NULL;
const gchar *filename = NULL;
if (context)
{
gradient = gimp_context_get_gradient (context);
if (gradient)
data = GIMP_DATA (gradient);
{
data = GIMP_DATA (gradient);
filename = gimp_data_get_filename (data);
}
}
#define SET_SENSITIVE(action,condition) \
......@@ -125,7 +130,7 @@ gradients_actions_update (GimpActionGroup *group,
SET_SENSITIVE ("gradients-edit", gradient);
SET_SENSITIVE ("gradients-duplicate", gradient);
SET_SENSITIVE ("gradients-save-as-pov", gradient);
SET_SENSITIVE ("gradients-copy-location", gradient && data->filename);
SET_SENSITIVE ("gradients-copy-location", gradient && filename);
SET_SENSITIVE ("gradients-delete", gradient && data->deletable);
#undef SET_SENSITIVE
......
......@@ -113,16 +113,21 @@ void
palettes_actions_update (GimpActionGroup *group,
gpointer user_data)
{
GimpContext *context = action_data_get_context (user_data);
GimpPalette *palette = NULL;
GimpData *data = NULL;
GimpContext *context = action_data_get_context (user_data);
GimpPalette *palette = NULL;
GimpData *data = NULL;
const gchar *filename = NULL;
if (context)
{
palette = gimp_context_get_palette (context);
if (palette)
data = GIMP_DATA (palette);
{
data = GIMP_DATA (palette);
filename = gimp_data_get_filename (data);
}
}
#define SET_SENSITIVE(action,condition) \
......@@ -131,7 +136,7 @@ palettes_actions_update (GimpActionGroup *group,
SET_SENSITIVE ("palettes-edit", palette);
SET_SENSITIVE ("palettes-duplicate", palette && GIMP_DATA_GET_CLASS (data)->duplicate);
SET_SENSITIVE ("palettes-merge", FALSE); /* FIXME palette && GIMP_IS_CONTAINER_LIST_VIEW (editor->view)); */
SET_SENSITIVE ("palettes-copy-location", palette && data->filename);
SET_SENSITIVE ("palettes-copy-location", palette && filename);
SET_SENSITIVE ("palettes-delete", palette && data->deletable);
#undef SET_SENSITIVE
......
......@@ -106,25 +106,30 @@ void
patterns_actions_update (GimpActionGroup *group,
gpointer user_data)
{
GimpContext *context = action_data_get_context (user_data);
GimpPattern *pattern = NULL;
GimpData *data = NULL;
GimpContext *context = action_data_get_context (user_data);
GimpPattern *pattern = NULL;
GimpData *data = NULL;
const gchar *filename = NULL;
if (context)
{
pattern = gimp_context_get_pattern (context);
if (pattern)
data = GIMP_DATA (pattern);
{
data = GIMP_DATA (pattern);
filename = gimp_data_get_filename (data);
}
}
#define SET_SENSITIVE(action,condition) \
gimp_action_group_set_action_sensitive (group, action, (condition) != 0)
SET_SENSITIVE ("patterns-edit", pattern && FALSE);
SET_SENSITIVE ("patterns-open-as-image", pattern && data->filename);
SET_SENSITIVE ("patterns-open-as-image", pattern && filename);
SET_SENSITIVE ("patterns-duplicate", pattern && GIMP_DATA_GET_CLASS (data)->duplicate);
SET_SENSITIVE ("patterns-copy-location", pattern && data->filename);
SET_SENSITIVE ("patterns-copy-location", pattern && filename);
SET_SENSITIVE ("patterns-delete", pattern && data->deletable);
#undef SET_SENSITIVE
......
......@@ -54,13 +54,13 @@ gimp_brush_generated_save (GimpData *data,
g_return_val_if_fail (name != NULL && *name != '\0', FALSE);
file = g_fopen (data->filename, "wb");
file = g_fopen (gimp_data_get_filename (data), "wb");
if (! file)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (data->filename),
gimp_filename_to_utf8 (gimp_data_get_filename (data)),
g_strerror (errno));
return FALSE;
}
......
......@@ -44,13 +44,13 @@ gimp_curve_save (GimpData *data,
curve = GIMP_CURVE (data);
file = g_fopen (data->filename, "wb");
file = g_fopen (gimp_data_get_filename (data), "wb");
if (! file)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (data->filename),
gimp_filename_to_utf8 (gimp_data_get_filename (data)),
g_strerror (errno));
return FALSE;
}
......
......@@ -63,6 +63,17 @@ enum
};
typedef struct _GimpDataPrivate GimpDataPrivate;
struct _GimpDataPrivate
{
gchar *filename;
};
#define GIMP_DATA_GET_PRIVATE(data) \
G_TYPE_INSTANCE_GET_PRIVATE (data, GIMP_TYPE_DATA, GimpDataPrivate)
static void gimp_data_class_init (GimpDataClass *klass);
static void gimp_data_tagged_iface_init (GimpTaggedInterface *iface);
......@@ -188,6 +199,8 @@ gimp_data_class_init (GimpDataClass *klass)
NULL,
GIMP_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY));
g_type_class_add_private (klass, sizeof (GimpDataPrivate));
}
static void
......@@ -204,7 +217,6 @@ static void
gimp_data_init (GimpData *data,
GimpDataClass *data_class)
{
data->filename = NULL;
data->mime_type = 0;
data->writable = TRUE;
data->deletable = TRUE;
......@@ -242,12 +254,13 @@ gimp_data_constructor (GType type,
static void
gimp_data_finalize (GObject *object)
{
GimpData *data = GIMP_DATA (object);
GimpData *data = GIMP_DATA (object);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
if (data->filename)
if (private->filename)
{
g_free (data->filename);
data->filename = NULL;
g_free (private->filename);
private->filename = NULL;
}
if (data->tags)
......@@ -309,12 +322,13 @@ gimp_data_get_property (GObject *object,
GValue *value,
GParamSpec *pspec)
{
GimpData *data = GIMP_DATA (object);
GimpData *data = GIMP_DATA (object);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
switch (property_id)
{
case PROP_FILENAME:
g_value_set_string (value, data->filename);
g_value_set_string (value, private->filename);
break;
case PROP_WRITABLE:
......@@ -339,10 +353,11 @@ static gint64
gimp_data_get_memsize (GimpObject *object,
gint64 *gui_size)
{
GimpData *data = GIMP_DATA (object);
gint64 memsize = 0;
GimpData *data = GIMP_DATA (object);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
gint64 memsize = 0;
memsize += gimp_string_get_memsize (data->filename);
memsize += gimp_string_get_memsize (private->filename);
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
gui_size);
......@@ -408,20 +423,21 @@ gimp_data_get_tags (GimpTagged *tagged)
return GIMP_DATA (tagged)->tags;
}
static gchar*
static gchar *
gimp_data_get_identifier (GimpTagged *tagged)
{
GimpData *data = GIMP_DATA (tagged);
gchar *identifier = NULL;
GimpData *data = GIMP_DATA (tagged);
GimpDataPrivate *private = GIMP_DATA_GET_PRIVATE (data);
gchar *identifier = NULL;
if (data->filename)
if (private->filename)
{
identifier = g_filename_to_utf8 (data->filename, -1, NULL, NULL, NULL);
identifier = g_filename_to_utf8 (private->filename, -1, NULL, NULL, NULL);
if (! identifier)
{
g_warning ("Failed to convert '%s' to utf8.\n", data->filename);
identifier = g_strdup (data->filename);
g_warning ("Failed to convert '%s' to utf8.\n", private->filename);
identifier = g_strdup (private->filename);
}
}
else if (data->internal)
......@@ -455,19 +471,22 @@ gboolean
gimp_data_save (GimpData *data,
GError **error)
{
gboolean success = FALSE;
GimpDataPrivate *private;
gboolean success = FALSE;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
g_return_val_if_fail (data->writable == TRUE, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
if (data->internal)
{
data->dirty = FALSE;
return TRUE;
}
g_return_val_if_fail (data->filename != NULL, FALSE);
g_return_val_if_fail (private->filename != NULL, FALSE);
if (GIMP_DATA_GET_CLASS (data)->save)
success = GIMP_DATA_GET_CLASS (data)->save (data, error);
......@@ -476,7 +495,7 @@ gimp_data_save (GimpData *data,
{
struct stat filestat;
g_stat (data->filename, &filestat);
g_stat (private->filename, &filestat);
data->mtime = filestat.st_mtime;
data->dirty = FALSE;
......@@ -556,19 +575,25 @@ gboolean
gimp_data_delete_from_disk (GimpData *data,
GError **error)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
g_return_val_if_fail (data->filename != NULL, FALSE);
g_return_val_if_fail (data->deletable == TRUE, FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
private = GIMP_DATA_GET_PRIVATE (data);
g_return_val_if_fail (private->filename != NULL, FALSE);
if (data->internal)
return TRUE;
if (g_unlink (data->filename) == -1)
if (g_unlink (private->filename) == -1)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_DELETE,
_("Could not delete '%s': %s"),
gimp_filename_to_utf8 (data->filename), g_strerror (errno));
gimp_filename_to_utf8 (private->filename),
g_strerror (errno));
return FALSE;
}
......@@ -605,19 +630,23 @@ gimp_data_set_filename (GimpData *data,
gboolean writable,
gboolean deletable)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
g_return_if_fail (filename != NULL);
g_return_if_fail (g_path_is_absolute (filename));
private = GIMP_DATA_GET_PRIVATE (data);
if (data->internal)
return;
if (data->filename)
g_free (data->filename);
if (private->filename)
g_free (private->filename);
data->filename = g_strdup (filename);
data->writable = FALSE;
data->deletable = FALSE;
private->filename = g_strdup (filename);
data->writable = FALSE;
data->deletable = FALSE;
/* if the data is supposed to be writable or deletable,
* still check if it really is
......@@ -717,6 +746,18 @@ gimp_data_create_filename (GimpData *data,
g_free (fullpath);
}
const gchar *
gimp_data_get_filename (GimpData *data)
{
GimpDataPrivate *private;
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
private = GIMP_DATA_GET_PRIVATE (data);
return private->filename;
}
const gchar *
gimp_data_get_mime_type (GimpData *data)
{
......@@ -762,12 +803,16 @@ void
gimp_data_make_internal (GimpData *data,
const gchar *identifier)
{
GimpDataPrivate *private;
g_return_if_fail (GIMP_IS_DATA (data));
if (data->filename)
private = GIMP_DATA_GET_PRIVATE (data);
if (private->filename)
{
g_free (data->filename);
data->filename = NULL;
g_free (private->filename);
private->filename = NULL;
}
data->identifier = g_strdup (identifier);
......
......@@ -49,7 +49,6 @@ struct _GimpData
{
GimpViewable parent_instance;
gchar *filename;
GQuark mime_type;
guint writable : 1;
guint deletable : 1;
......@@ -101,6 +100,7 @@ void gimp_data_set_filename (GimpData *data,
gboolean deletable);
void gimp_data_create_filename (GimpData *data,
const gchar *dest_dir);
const gchar * gimp_data_get_filename (GimpData *data);
const gchar * gimp_data_get_mime_type (GimpData *data);
......
......@@ -244,7 +244,9 @@ gimp_data_factory_refresh_cache_add (GimpDataFactory *factory,
GimpData *data,
gpointer user_data)
{
if (data->filename)
const gchar *filename = gimp_data_get_filename (data);
if (filename)
{
GHashTable *cache = user_data;
GList *list;
......@@ -253,10 +255,10 @@ gimp_data_factory_refresh_cache_add (GimpDataFactory *factory,
gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
list = g_hash_table_lookup (cache, data->filename);
list = g_hash_table_lookup (cache, filename);
list = g_list_prepend (list, data);
g_hash_table_insert (cache, (gpointer) data->filename, list);
g_hash_table_insert (cache, (gpointer) filename, list);
}
}
......@@ -426,7 +428,7 @@ gimp_data_factory_data_save (GimpDataFactory *factory)
{
GimpData *data = list->data;
if (! data->filename)
if (! gimp_data_get_filename (data))
gimp_data_create_filename (data, writable_dir);
if (data->dirty && data->writable)
......@@ -561,7 +563,7 @@ gimp_data_factory_data_delete (GimpDataFactory *factory,
gimp_container_remove (factory->priv->container, GIMP_OBJECT (data));
if (delete_from_disk && data->filename)
if (delete_from_disk && gimp_data_get_filename (data))
retval = gimp_data_delete_from_disk (data, error);
g_object_unref (data);
......@@ -593,7 +595,7 @@ gimp_data_factory_data_save_single (GimpDataFactory *factory,
if (! data->dirty)
return TRUE;
if (! data->filename)
if (! gimp_data_get_filename (data))
{
gchar *writable_dir;
GError *my_error = NULL;
......
......@@ -35,7 +35,7 @@ gimp_dynamics_save (GimpData *data,
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
return gimp_config_serialize_to_file (GIMP_CONFIG (data),
data->filename,
gimp_data_get_filename (data),
"GIMP dynamics file",
"end of GIMP dynamics file",
NULL, error);
......
......@@ -42,13 +42,13 @@ gimp_gradient_save (GimpData *data,
gint num_segments;
FILE *file;
file = g_fopen (data->filename, "wb");
file = g_fopen (gimp_data_get_filename (data), "wb");
if (! file)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (data->filename),
gimp_filename_to_utf8 (gimp_data_get_filename (data)),
g_strerror (errno));
return FALSE;
}
......
......@@ -47,13 +47,13 @@ gimp_palette_save (GimpData *data,
GList *list;
FILE *file;
file = g_fopen (data->filename, "wb");
file = g_fopen (gimp_data_get_filename (data), "wb");
if (! file)
{
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_OPEN,
_("Could not open '%s' for writing: %s"),
gimp_filename_to_utf8 (data->filename),
gimp_filename_to_utf8 (gimp_data_get_filename (data)),
g_strerror (errno));
return FALSE;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment