gimpdata.c 7.52 KB
Newer Older
1 2 3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
Michael Natterer's avatar
Michael Natterer committed
4 5 6
 * gimpdata.c
 * Copyright (C) 2001 Michael Natterer <mitch@gimp.org>
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 * 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"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

31
#include <glib-object.h>
32

33
#include "libgimpbase/gimpbase.h"
Michael Natterer's avatar
Michael Natterer committed
34 35

#include "core-types.h"
36 37

#include "gimpdata.h"
38
#include "gimpmarshal.h"
39 40 41 42 43 44


enum
{
  DIRTY,
  SAVE,
45
  GET_EXTENSION,
46
  DUPLICATE,
47 48 49 50
  LAST_SIGNAL
};


51 52
static void    gimp_data_class_init   (GimpDataClass *klass);
static void    gimp_data_init         (GimpData      *data);
53

54
static void    gimp_data_finalize     (GObject       *object);
55

56 57 58 59
static void    gimp_data_name_changed (GimpObject    *object);
static gsize   gimp_data_get_memsize  (GimpObject    *object);

static void    gimp_data_real_dirty   (GimpData      *data);
60 61 62 63 64 65 66


static guint data_signals[LAST_SIGNAL] = { 0 };

static GimpViewableClass *parent_class = NULL;


67
GType
68 69
gimp_data_get_type (void)
{
70
  static GType data_type = 0;
71 72 73

  if (! data_type)
    {
74
      static const GTypeInfo data_info =
75 76
      {
        sizeof (GimpDataClass),
77 78 79 80 81 82 83 84
	(GBaseInitFunc) NULL,
	(GBaseFinalizeFunc) NULL,
	(GClassInitFunc) gimp_data_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data     */
	sizeof (GimpData),
	0,              /* n_preallocs    */
	(GInstanceInitFunc) gimp_data_init,
85 86
      };

87 88 89
      data_type = g_type_register_static (GIMP_TYPE_VIEWABLE,
					  "GimpData",
					  &data_info, 0);
90 91 92 93 94 95 96 97
  }

  return data_type;
}

static void
gimp_data_class_init (GimpDataClass *klass)
{
98
  GObjectClass    *object_class;
99
  GimpObjectClass *gimp_object_class;
100

101 102
  object_class      = G_OBJECT_CLASS (klass);
  gimp_object_class = GIMP_OBJECT_CLASS (klass);
103

104
  parent_class = g_type_class_peek_parent (klass);
105 106

  data_signals[DIRTY] = 
107 108 109 110 111
    g_signal_new ("dirty",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (GimpDataClass, dirty),
		  NULL, NULL,
112
		  gimp_marshal_VOID__VOID,
113
		  G_TYPE_NONE, 0);
114 115

  data_signals[SAVE] = 
116 117 118 119 120
    g_signal_new ("save",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GimpDataClass, save),
		  NULL, NULL,
121
		  gimp_marshal_BOOLEAN__VOID,
122
		  G_TYPE_BOOLEAN, 0);
123

124
  data_signals[GET_EXTENSION] = 
125 126 127 128 129
    g_signal_new ("get_extension",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GimpDataClass, get_extension),
		  NULL, NULL,
130
		  gimp_marshal_POINTER__VOID,
131
		  G_TYPE_POINTER, 0);
132

133
  data_signals[DUPLICATE] = 
134 135 136 137 138
    g_signal_new ("duplicate",
		  G_TYPE_FROM_CLASS (klass),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GimpDataClass, duplicate),
		  NULL, NULL,
139
		  gimp_marshal_POINTER__VOID,
140
		  G_TYPE_POINTER, 0);
141

142
  object_class->finalize          = gimp_data_finalize;
143

144
  gimp_object_class->name_changed = gimp_data_name_changed;
145
  gimp_object_class->get_memsize  = gimp_data_get_memsize;
146

147 148 149 150
  klass->dirty                    = gimp_data_real_dirty;
  klass->save                     = NULL;
  klass->get_extension            = NULL;
  klass->duplicate                = NULL;
151 152 153 154 155 156 157 158 159 160
}

static void
gimp_data_init (GimpData *data)
{
  data->filename = NULL;
  data->dirty    = FALSE;
}

static void
161
gimp_data_finalize (GObject *object)
162 163 164 165 166
{
  GimpData *data;

  data = GIMP_DATA (object);

167 168 169 170 171
  if (data->filename)
    {
      g_free (data->filename);
      data->filename = NULL;
    }
172

173
  G_OBJECT_CLASS (parent_class)->finalize (object);
174 175
}

176 177 178 179 180 181 182 183 184
static void
gimp_data_name_changed (GimpObject *object)
{
  if (GIMP_OBJECT_CLASS (parent_class)->name_changed)
    GIMP_OBJECT_CLASS (parent_class)->name_changed (object);

  gimp_data_dirty (GIMP_DATA (object));
}

185 186 187 188 189 190 191 192 193 194 195 196 197 198
static gsize
gimp_data_get_memsize (GimpObject *object)
{
  GimpData *data;
  gsize     memsize = 0;

  data = GIMP_DATA (object);

  if (data->filename)
    memsize += strlen (data->filename) + 1;

  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object);
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212
gboolean
gimp_data_save (GimpData *data)
{
  gboolean success = FALSE;

  g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);

  if (! data->filename)
    {
      g_warning ("%s(): can't save data with NULL filename",
		 G_GNUC_FUNCTION);
      return FALSE;
    }

213 214
  g_signal_emit (G_OBJECT (data), data_signals[SAVE], 0,
		 &success);
215 216 217 218 219 220 221 222 223 224 225 226

  if (success)
    data->dirty = FALSE;

  return success;
}

void
gimp_data_dirty (GimpData *data)
{
  g_return_if_fail (GIMP_IS_DATA (data));

227
  g_signal_emit (G_OBJECT (data), data_signals[DIRTY], 0);
228 229 230 231 232 233
}

static void
gimp_data_real_dirty (GimpData *data)
{
  data->dirty = TRUE;
234 235

  gimp_viewable_invalidate_preview (GIMP_VIEWABLE (data));
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

gboolean
gimp_data_delete_from_disk (GimpData *data)
{
  g_return_val_if_fail (GIMP_IS_DATA (data), FALSE);
  g_return_val_if_fail (data->filename != NULL, FALSE);

  if (unlink (data->filename) == -1)
    {
      g_message ("%s(): could not unlink() %s: %s",
		 G_GNUC_FUNCTION, data->filename, g_strerror (errno));
      return FALSE;
    }

  return TRUE;
}

254 255 256 257 258 259 260
const gchar *
gimp_data_get_extension (GimpData *data)
{
  const gchar *extension = NULL;

  g_return_val_if_fail (GIMP_IS_DATA (data), NULL);

261 262
  g_signal_emit (G_OBJECT (data), data_signals[GET_EXTENSION], 0,
		 &extension);
263 264 265 266

  return extension;
}

267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
void
gimp_data_set_filename (GimpData    *data,
			const gchar *filename)
{
  g_return_if_fail (GIMP_IS_DATA (data));

  g_free (data->filename);

  data->filename = g_strdup (filename);
}

void
gimp_data_create_filename (GimpData    *data,
			   const gchar *basename,
			   const gchar *data_path)
{
  GList *path;
  gchar *dir;
  gchar *filename;
286
  gchar *fullpath;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
  gchar *safe_name;
  gint   i;
  gint   unum = 1;
  FILE  *file;

  g_return_if_fail (GIMP_IS_DATA (data));
  g_return_if_fail (basename != NULL);
  g_return_if_fail (data_path != NULL);

  path = gimp_path_parse (data_path, 16, TRUE, NULL);
  dir  = gimp_path_get_user_writable_dir (path);
  gimp_path_free (path);

  if (! dir)
    return;

  safe_name = g_strdup (basename);
  if (safe_name[0] == '.')
    safe_name[0] = '_';
  for (i = 0; safe_name[i]; i++)
307
    if (safe_name[i] == G_DIR_SEPARATOR || g_ascii_isspace (safe_name[i]))
308 309
      safe_name[i] = '_';

310 311
  filename = g_strdup_printf ("%s%s",
			      safe_name,
312
			      gimp_data_get_extension (data));
313

314 315 316 317 318
  fullpath = g_build_filename (dir, filename, NULL);

  g_free (filename);

  while ((file = fopen (fullpath, "r")))
319 320 321
    {
      fclose (file);

322 323 324 325 326
      g_free (fullpath);

      filename = g_strdup_printf ("%s_%d%s",
				  safe_name,
                                  unum,
327
				  gimp_data_get_extension (data));
328 329 330 331 332

      fullpath = g_build_filename (dir, filename, NULL);

      g_free (filename);

333 334 335 336 337 338
      unum++;
    }

  g_free (dir);
  g_free (safe_name);

339
  gimp_data_set_filename (data, fullpath);
340

341
  g_free (fullpath);
342
}
343 344 345 346 347 348

GimpData *
gimp_data_duplicate (GimpData *data)
{
  GimpData *new_data = NULL;

349 350
  g_signal_emit (G_OBJECT (data), data_signals[DUPLICATE], 0,
		 &new_data);
351 352 353

  return new_data;
}