1
/* The GIMP -- an image manipulation program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
5
* Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
7
* This program is free software; you can redistribute it and/or modify
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
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
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.
32
#include <glib-object.h>
34
#include "libgimpbase/gimpbase.h"
37
#include "libgimpbase/gimpwin32-io.h"
40
#include "core-types.h"
43
#include "gimpmarshal.h"
45
#include "gimp-intl.h"
47
/* Need this test somewhere, might as well do it here */
49
#if GLIB_CHECK_VERSION (2,6,0)
50
#error GIMP 2.2 for Win32 must be compiled against GLib 2.4
61
static void gimp_data_class_init (GimpDataClass *klass);
62
static void gimp_data_init (GimpData *data,
63
GimpDataClass *data_class);
65
static void gimp_data_finalize (GObject *object);
67
static void gimp_data_name_changed (GimpObject *object);
68
static gint64 gimp_data_get_memsize (GimpObject *object,
71
static void gimp_data_real_dirty (GimpData *data);
74
static guint data_signals[LAST_SIGNAL] = { 0 };
76
static GimpViewableClass *parent_class = NULL;
80
gimp_data_get_type (void)
82
static GType data_type = 0;
86
static const GTypeInfo data_info =
88
sizeof (GimpDataClass),
90
(GBaseFinalizeFunc) NULL,
91
(GClassInitFunc) gimp_data_class_init,
92
NULL, /* class_finalize */
93
NULL, /* class_data */
96
(GInstanceInitFunc) gimp_data_init,
99
data_type = g_type_register_static (GIMP_TYPE_VIEWABLE,
108
gimp_data_class_init (GimpDataClass *klass)
110
GObjectClass *object_class = G_OBJECT_CLASS (klass);
111
GimpObjectClass *gimp_object_class = GIMP_OBJECT_CLASS (klass);
113
parent_class = g_type_class_peek_parent (klass);
115
data_signals[DIRTY] =
116
g_signal_new ("dirty",
117
G_TYPE_FROM_CLASS (klass),
119
G_STRUCT_OFFSET (GimpDataClass, dirty),
121
gimp_marshal_VOID__VOID,
124
object_class->finalize = gimp_data_finalize;
126
gimp_object_class->name_changed = gimp_data_name_changed;
127
gimp_object_class->get_memsize = gimp_data_get_memsize;
129
klass->dirty = gimp_data_real_dirty;
131
klass->get_extension = NULL;
132
klass->duplicate = NULL;
136
gimp_data_init (GimpData *data,
137
GimpDataClass *data_class)
139
data->filename = NULL;
140
data->writable = TRUE;
141
data->deletable = TRUE;
143
data->internal = FALSE;
144
data->freeze_count = 0;
146
/* look at the passed class pointer, not at GIMP_DATA_GET_CLASS(data)
147
* here, because the latter is always GimpDataClass itself
149
if (! data_class->save)
150
data->writable = FALSE;
154
gimp_data_finalize (GObject *object)
156
GimpData *data = GIMP_DATA (object);
160
g_free (data->filename);
161
data->filename = NULL;
164
G_OBJECT_CLASS (parent_class)->finalize (object);
168
gimp_data_name_changed (GimpObject *object)
170
if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
171
GIMP_OBJECT_CLASS (parent_class)->name_changed (object);
173
gimp_data_dirty (GIMP_DATA (object));
177
gimp_data_get_memsize (GimpObject *object,
180
GimpData *data = GIMP_DATA (object);
184
memsize += strlen (data->filename) + 1;
186
return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
191
gimp_data_real_dirty (GimpData *data)
195
gimp_viewable_invalidate_preview (GIMP_VIEWABLE (data));
200
* @data: object whose contents are to be saved.
201
* @error: return location for errors or %NULL
203
* Save the object. If the object is marked as "internal", nothing happens.
204
* Otherwise, it is saved to disk, using the file name set by
205
* gimp_data_set_filename(). If the save is successful, the
206
* object is marked as not dirty. If not, an error message is returned
207
* using the @error argument.
209
* Returns: %TRUE if the object is internal or the save is successful.
212
gimp_data_save (GimpData *data,
215
gboolean success = FALSE;
217
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
218
g_return_val_if_fail (data->writable == TRUE, FALSE);
219
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
227
g_return_val_if_fail (data->filename != NULL, FALSE);
229
if (GIMP_DATA_GET_CLASS (data)->save)
230
success = GIMP_DATA_GET_CLASS (data)->save (data, error);
240
* @data: a #GimpData object.
242
* Marks @data as dirty. Unless the object is frozen, this causes
243
* its preview to be invalidated, and emits a "dirty" signal. If the
244
* object is frozen, the function has no effect.
247
gimp_data_dirty (GimpData *data)
249
g_return_if_fail (GIMP_IS_DATA (data));
251
if (data->freeze_count == 0)
252
g_signal_emit (data, data_signals[DIRTY], 0);
257
* @data: a #GimpData object.
259
* Increments the freeze count for the object. A positive freeze count
260
* prevents the object from being treated as dirty. Any call to this
261
* function must be followed eventually by a call to gimp_data_thaw().
264
gimp_data_freeze (GimpData *data)
266
g_return_if_fail (GIMP_IS_DATA (data));
268
data->freeze_count++;
273
* @data: a #GimpData object.
275
* Decrements the freeze count for the object. If the freeze count
276
* drops to zero, the object is marked as dirty, and the "dirty"
277
* signal is emitted. It is an error to call this function without
278
* having previously called gimp_data_freeze().
281
gimp_data_thaw (GimpData *data)
283
g_return_if_fail (GIMP_IS_DATA (data));
284
g_return_if_fail (data->freeze_count > 0);
286
data->freeze_count--;
288
if (data->freeze_count == 0)
289
gimp_data_dirty (data);
293
* gimp_data_delete_from_disk:
294
* @data: a #GimpData object.
295
* @error: return location for errors or %NULL
297
* Deletes the object from disk. If the object is marked as "internal",
298
* nothing happens. Otherwise, if the file exists whose name has been
299
* set by gimp_data_set_filename(), it is deleted. Obviously this is
300
* a potentially dangerous function, which should be used with care.
302
* Returns: %TRUE if the object is internal to Gimp, or the deletion is
306
gimp_data_delete_from_disk (GimpData *data,
309
g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
310
g_return_val_if_fail (data->filename != NULL, FALSE);
311
g_return_val_if_fail (data->deletable == TRUE, FALSE);
312
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
317
if (unlink (data->filename) == -1)
319
g_set_error (error, GIMP_DATA_ERROR, GIMP_DATA_ERROR_DELETE,
320
_("Could not delete '%s': %s"),
321
gimp_filename_to_utf8 (data->filename), g_strerror (errno));
329
gimp_data_get_extension (GimpData *data)
331
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
333
if (GIMP_DATA_GET_CLASS (data)->get_extension)
334
return GIMP_DATA_GET_CLASS (data)->get_extension (data);
340
* gimp_data_set_filename:
341
* @data: A #GimpData object
342
* @filename: File name to assign to @data.
343
* @writable: %TRUE if we want to be able to write to this file.
344
* @deletable: %TRUE if we want to be able to delete this file.
346
* This function assigns a file name to @data, and sets some flags
347
* according to the properties of the file. If @writable is %TRUE,
348
* and the user has permission to write or overwrite the requested file
349
* name, and a "save" method exists for @data's object type, then
350
* @data is marked as writable.
353
gimp_data_set_filename (GimpData *data,
354
const gchar *filename,
358
g_return_if_fail (GIMP_IS_DATA (data));
359
g_return_if_fail (filename != NULL);
360
g_return_if_fail (g_path_is_absolute (filename));
366
g_free (data->filename);
368
data->filename = g_strdup (filename);
369
data->writable = FALSE;
370
data->deletable = FALSE;
372
/* if the data is supposed to be writable or deletable,
373
* still check if it really is
375
if (writable || deletable)
377
gchar *dirname = g_path_get_dirname (filename);
379
if ((access (filename, F_OK) == 0 && /* check if the file exists */
380
access (filename, W_OK) == 0) || /* and is writable */
381
(access (filename, F_OK) != 0 && /* OR doesn't exist */
382
access (dirname, W_OK) == 0)) /* and we can write to its dir */
384
data->writable = writable ? TRUE : FALSE;
385
data->deletable = deletable ? TRUE : FALSE;
390
/* if we can't save, we are not writable */
391
if (! GIMP_DATA_GET_CLASS (data)->save)
392
data->writable = FALSE;
397
* gimp_data_create_filename:
398
* @data: a #Gimpdata object.
399
* @dest_dir: directory in which to create a file name.
401
* This function creates a unique file name to be used for saving
402
* a representation of @data in the directory @dest_dir. If the
403
* user does not have write permission in @dest_dir, then @data
404
* is marked as "not writable", so you should check on this before
405
* assuming that @data can be saved.
408
gimp_data_create_filename (GimpData *data,
409
const gchar *dest_dir)
416
GError *error = NULL;
418
g_return_if_fail (GIMP_IS_DATA (data));
419
g_return_if_fail (dest_dir != NULL);
420
g_return_if_fail (g_path_is_absolute (dest_dir));
427
const gchar *charset;
429
filename = g_strdup (gimp_object_get_name (GIMP_OBJECT (data)));
431
/* Map illegal characters to '-' while the name is still in UTF-8 */
432
for (i = 0; filename[i]; i++)
433
if (strchr ("<>:\"/\\|", filename[i]) ||
434
g_ascii_iscntrl (filename[i]) ||
435
g_ascii_isspace (filename[i]))
438
/* Map also trailing periods to '-' */
439
for (i = strlen (filename) - 1; i >= 0; i--)
440
if (filename[i] == '.')
445
/* Next convert to the filename charset. Note that this branch of
446
* GIMP must be compiled against GLib 2.4 and we thus use system
447
* codepage for file names in the GLib API.
449
g_get_charset (&charset);
450
safename = g_convert_with_fallback (filename,
451
-1, charset, "UTF-8",
452
"-", NULL, NULL, &error);
455
g_warning ("gimp_data_create_filename:\n"
456
"g_convert_with_fallback() failed for '%s': %s",
457
filename, error->message);
459
g_error_free (error);
465
safename = g_filename_from_utf8 (gimp_object_get_name (GIMP_OBJECT (data)),
466
-1, NULL, NULL, &error);
469
g_warning ("gimp_data_create_filename:\n"
470
"g_filename_from_utf8() failed for '%s': %s",
471
gimp_object_get_name (GIMP_OBJECT (data)), error->message);
472
g_error_free (error);
476
if (safename[0] == '.')
479
for (i = 0; safename[i]; i++)
480
if (safename[i] == G_DIR_SEPARATOR || g_ascii_isspace (safename[i]))
484
filename = g_strconcat (safename, gimp_data_get_extension (data), NULL);
486
fullpath = g_build_filename (dest_dir, filename, NULL);
490
while (g_file_test (fullpath, G_FILE_TEST_EXISTS))
494
filename = g_strdup_printf ("%s-%d%s",
497
gimp_data_get_extension (data));
499
fullpath = g_build_filename (dest_dir, filename, NULL);
506
gimp_data_set_filename (data, fullpath, TRUE, TRUE);
512
* gimp_data_duplicate:
513
* @data: a #GimpData object
514
* @stingy_memory_use: if %TRUE, use the disk rather than RAM
517
* Creates a copy of @data, if possible. Only the object data is
518
* copied: the newly created object is not automatically given an
519
* object name, file name, preview, etc.
521
* Returns: the newly created copy, or %NULL if @data cannot be copied.
524
gimp_data_duplicate (GimpData *data,
525
gboolean stingy_memory_use)
527
g_return_val_if_fail (GIMP_IS_DATA (data), NULL);
529
if (GIMP_DATA_GET_CLASS (data)->duplicate)
530
return GIMP_DATA_GET_CLASS (data)->duplicate (data, stingy_memory_use);
536
* gimp_data_make_internal:
537
* @data: a #GimpData object.
539
* Mark @data as "internal" to Gimp, which means that it will not be
540
* saved to disk. Note that if you do this, later calls to
541
* gimp_data_save() and gimp_data_delete_from_disk() will
542
* automatically return successfully without giving any warning.
545
gimp_data_make_internal (GimpData *data)
547
g_return_if_fail (GIMP_IS_DATA (data));
551
g_free (data->filename);
552
data->filename = NULL;
555
data->internal = TRUE;
556
data->writable = FALSE;
557
data->deletable = FALSE;
561
* gimp_data_name_compare:
562
* @data1: a #GimpData object.
563
* @data2: another #GimpData object.
565
* Compares the names of the two objects for use in sorting; see
566
* gimp_object_name_collate() for the method. Objects marked as
567
* "internal" are considered to come before any objects that are not.
569
* Return value: -1 if @data1 compares before @data2,
570
* 0 if they compare equal,
571
* 1 if @data1 compares after @data2.
574
gimp_data_name_compare (GimpData *data1,
577
/* move the internal objects (like the FG -> BG) gradient) to the top */
578
if (data1->internal != data2->internal)
579
return data1->internal ? -1 : 1;
581
return gimp_object_name_collate ((GimpObject *) data1,
582
(GimpObject *) data2);
586
* gimp_data_error_quark:
588
* This function is used to implement the GIMP_DATA_ERROR macro. It
589
* shouldn't be called directly.
591
* Return value: the #GQuark to identify error in the GimpData error domain.
594
gimp_data_error_quark (void)
596
static GQuark quark = 0;
599
quark = g_quark_from_static_string ("gimp-data-error-quark");