gimpobject.c 13.9 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7 8 9 10 11 12 13 14
 * (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
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17
 */

Sven Neumann's avatar
Sven Neumann committed
18 19
#include "config.h"

20 21
#include <string.h>

22
#include <gio/gio.h>
23
#include <gegl.h>
Sven Neumann's avatar
Sven Neumann committed
24

25
#include "libgimpbase/gimpbase.h"
26

27
#include "core-types.h"
Sven Neumann's avatar
Sven Neumann committed
28

29
#include "gimp-memsize.h"
30
#include "gimpmarshal.h"
Sven Neumann's avatar
Sven Neumann committed
31
#include "gimpobject.h"
32

33 34
#include "gimp-debug.h"

Sven Neumann's avatar
Sven Neumann committed
35

36
enum
37
{
38
  DISCONNECT,
39 40 41
  NAME_CHANGED,
  LAST_SIGNAL
};
42

43
enum
44
{
45 46
  PROP_0,
  PROP_NAME
47 48 49
};


50 51 52 53 54 55 56 57 58
struct _GimpObjectPrivate
{
  gchar *name;
  gchar *normalized;
  guint  static_name  : 1;
  guint  disconnected : 1;
};


59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static void    gimp_object_class_init       (GimpObjectClass *klass);
static void    gimp_object_init             (GimpObject      *object,
                                             GimpObjectClass *klass);
static void    gimp_object_dispose          (GObject         *object);
static void    gimp_object_finalize         (GObject         *object);
static void    gimp_object_set_property     (GObject         *object,
                                             guint            property_id,
                                             const GValue    *value,
                                             GParamSpec      *pspec);
static void    gimp_object_get_property     (GObject         *object,
                                             guint            property_id,
                                             GValue          *value,
                                             GParamSpec      *pspec);
static gint64  gimp_object_real_get_memsize (GimpObject      *object,
                                             gint64          *gui_size);
static void    gimp_object_name_normalize   (GimpObject      *object);


static GObjectClass *parent_class = NULL;
78

79
static guint object_signals[LAST_SIGNAL] = { 0 };
80 81


82 83 84 85
GType
gimp_object_get_type (void)
{
  static GType object_type = 0;
86

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
  if (! object_type)
    {
      const GTypeInfo object_info =
      {
        sizeof (GimpObjectClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_object_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpObject),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_object_init,
      };

      object_type = g_type_register_static (G_TYPE_OBJECT,
                                            "GimpObject",
                                            &object_info, 0);
    }

  return object_type;
}
109

110 111 112
static void
gimp_object_class_init (GimpObjectClass *klass)
{
113
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
114

115 116
  parent_class = g_type_class_peek_parent (klass);

117 118
  object_signals[DISCONNECT] =
    g_signal_new ("disconnect",
119 120 121 122 123 124
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpObjectClass, disconnect),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
125

126
  object_signals[NAME_CHANGED] =
127
    g_signal_new ("name-changed",
128 129 130 131 132 133
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  G_STRUCT_OFFSET (GimpObjectClass, name_changed),
                  NULL, NULL,
                  gimp_marshal_VOID__VOID,
                  G_TYPE_NONE, 0);
134

135
  object_class->dispose      = gimp_object_dispose;
136 137 138 139
  object_class->finalize     = gimp_object_finalize;
  object_class->set_property = gimp_object_set_property;
  object_class->get_property = gimp_object_get_property;

140
  klass->disconnect          = NULL;
141
  klass->name_changed        = NULL;
142
  klass->get_memsize         = gimp_object_real_get_memsize;
143

144
  g_object_class_install_property (object_class, PROP_NAME,
145 146 147 148
                                   g_param_spec_string ("name",
                                                        NULL, NULL,
                                                        NULL,
                                                        GIMP_PARAM_READWRITE |
Sven Neumann's avatar
Sven Neumann committed
149
                                                        G_PARAM_CONSTRUCT));
150 151
  g_type_class_add_private (klass,
                            sizeof (GimpObjectPrivate));
152 153 154
}

static void
155 156
gimp_object_init (GimpObject      *object,
                  GimpObjectClass *klass)
157
{
158 159 160 161 162
  object->p = G_TYPE_INSTANCE_GET_PRIVATE (object,
                                           GIMP_TYPE_OBJECT,
                                           GimpObjectPrivate);
  object->p->name       = NULL;
  object->p->normalized = NULL;
163

164
  gimp_debug_add_instance (G_OBJECT (object), G_OBJECT_CLASS (klass));
165 166
}

167 168 169
static void
gimp_object_dispose (GObject *object)
{
170
  GimpObject *gimp_object = GIMP_OBJECT (object);
171

172
  if (! gimp_object->p->disconnected)
173
    {
174
      g_signal_emit (object, object_signals[DISCONNECT], 0);
175

176
      gimp_object->p->disconnected = TRUE;
177
    }
178 179 180 181

  G_OBJECT_CLASS (parent_class)->dispose (object);
}

182
static void
183
gimp_object_finalize (GObject *object)
184
{
185
  gimp_object_name_free (GIMP_OBJECT (object));
186

187
  gimp_debug_remove_instance (object);
188

189
  G_OBJECT_CLASS (parent_class)->finalize (object);
190 191
}

192
static void
193
gimp_object_set_property (GObject      *object,
194 195 196
                          guint         property_id,
                          const GValue *value,
                          GParamSpec   *pspec)
197
{
198
  GimpObject *gimp_object = GIMP_OBJECT (object);
199

200
  switch (property_id)
201
    {
202 203
    case PROP_NAME:
      gimp_object_set_name (gimp_object, g_value_get_string (value));
204 205
      break;
    default:
206
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
207 208 209 210 211
      break;
    }
}

static void
212
gimp_object_get_property (GObject    *object,
213 214 215
                          guint       property_id,
                          GValue     *value,
                          GParamSpec *pspec)
216
{
217
  GimpObject *gimp_object = GIMP_OBJECT (object);
218

219
  switch (property_id)
220
    {
221
    case PROP_NAME:
222 223
      if (gimp_object->p->static_name)
        g_value_set_static_string (value, gimp_object->p->name);
224
      else
225
        g_value_set_string (value, gimp_object->p->name);
226 227
      break;
    default:
228
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
229 230 231 232
      break;
    }
}

233 234 235 236 237 238
/**
 * gimp_object_set_name:
 * @object: a #GimpObject
 * @name: the @object's new name
 *
 * Sets the @object's name. Takes care of freeing the old name and
239
 * emitting the ::name_changed signal if the old and new name differ.
240
 **/
241 242
void
gimp_object_set_name (GimpObject  *object,
243
                      const gchar *name)
244 245 246
{
  g_return_if_fail (GIMP_IS_OBJECT (object));

247
  if (! g_strcmp0 (object->p->name, name))
248 249
    return;

250
  gimp_object_name_free (object);
251

252 253
  object->p->name = g_strdup (name);
  object->p->static_name = FALSE;
254 255

  gimp_object_name_changed (object);
256
  g_object_notify (G_OBJECT (object), "name");
257 258
}

259 260 261 262 263 264 265 266 267
/**
 * gimp_object_set_name_safe:
 * @object: a #GimpObject
 * @name: the @object's new name
 *
 * A safe version of gimp_object_set_name() that takes care of
 * handling newlines and overly long names. The actual name set
 * may be different to the @name you pass.
 **/
268 269 270 271 272
void
gimp_object_set_name_safe (GimpObject  *object,
                           const gchar *name)
{
  g_return_if_fail (GIMP_IS_OBJECT (object));
273

274
  if (! g_strcmp0 (object->p->name, name))
275 276
    return;

277
  gimp_object_name_free (object);
278

279 280
  object->p->name = gimp_utf8_strtrim (name, 30);
  object->p->static_name = FALSE;
281 282 283 284 285 286 287 288 289 290 291

  gimp_object_name_changed (object);
  g_object_notify (G_OBJECT (object), "name");
}

void
gimp_object_set_static_name (GimpObject  *object,
                             const gchar *name)
{
  g_return_if_fail (GIMP_IS_OBJECT (object));

292 293 294
  if (! g_strcmp0 (object->p->name, name))
    return;

295 296
  gimp_object_name_free (object);

297 298
  object->p->name = (gchar *) name;
  object->p->static_name = TRUE;
299 300 301 302 303 304 305 306 307 308 309

  gimp_object_name_changed (object);
  g_object_notify (G_OBJECT (object), "name");
}

void
gimp_object_take_name (GimpObject *object,
                       gchar      *name)
{
  g_return_if_fail (GIMP_IS_OBJECT (object));

310 311 312 313 314 315
  if (! g_strcmp0 (object->p->name, name))
    {
      g_free (name);
      return;
    }

316 317
  gimp_object_name_free (object);

318 319
  object->p->name = name;
  object->p->static_name = FALSE;
320 321

  gimp_object_name_changed (object);
322
  g_object_notify (G_OBJECT (object), "name");
323 324
}

325 326 327 328 329 330 331 332 333
/**
 * gimp_object_get_name:
 * @object: a #GimpObject
 *
 * This function gives access to the name of a GimpObject. The
 * returned name belongs to the object and must not be freed.
 *
 * Return value: a pointer to the @object's name
 **/
334
const gchar *
335
gimp_object_get_name (gconstpointer object)
336
{
337 338
  const GimpObject *object_typed = object;
  g_return_val_if_fail (GIMP_IS_OBJECT (object_typed), NULL);
339

340
  return object_typed->p->name;
341 342
}

343 344 345 346
/**
 * gimp_object_name_changed:
 * @object: a #GimpObject
 *
347
 * Causes the ::name-changed signal to be emitted.
348
 **/
349
void
350
gimp_object_name_changed (GimpObject *object)
351 352 353
{
  g_return_if_fail (GIMP_IS_OBJECT (object));

354
  g_signal_emit (object, object_signals[NAME_CHANGED], 0);
355
}
356

357 358 359 360 361 362 363 364 365
/**
 * gimp_object_name_free:
 * @object: a #GimpObject
 *
 * Frees the name of @object and sets the name pointer to %NULL. Also
 * takes care of the normalized name that the object might be caching.
 *
 * In general you should be using gimp_object_set_name() instead. But
 * if you ever need to free the object name but don't want the
366
 * ::name-changed signal to be emitted, then use this function. Never
367 368 369 370 371
 * ever free the object name directly!
 **/
void
gimp_object_name_free (GimpObject *object)
{
372
  if (object->p->normalized)
373
    {
374 375
      if (object->p->normalized != object->p->name)
        g_free (object->p->normalized);
376

377
      object->p->normalized = NULL;
378 379
    }

380
  if (object->p->name)
381
    {
382 383
      if (! object->p->static_name)
        g_free (object->p->name);
384

385 386
      object->p->name = NULL;
      object->p->static_name = FALSE;
387 388 389
    }
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
/**
 * gimp_object_name_collate:
 * @object1: a #GimpObject
 * @object2: another #GimpObject
 *
 * Compares two object names for ordering using the linguistically
 * correct rules for the current locale. It caches the normalized
 * version of the object name to speed up subsequent calls.
 *
 * Return value: -1 if object1 compares before object2,
 *                0 if they compare equal,
 *                1 if object1 compares after object2.
 **/
gint
gimp_object_name_collate (GimpObject *object1,
                          GimpObject *object2)
{
407
  if (! object1->p->normalized)
408 409
    gimp_object_name_normalize (object1);

410
  if (! object2->p->normalized)
411 412
    gimp_object_name_normalize (object2);

413
  return strcmp (object1->p->normalized, object2->p->normalized);
414 415 416 417 418
}

static void
gimp_object_name_normalize (GimpObject *object)
{
419
  g_return_if_fail (object->p->normalized == NULL);
420

421
  if (object->p->name)
422
    {
423
      gchar *key = g_utf8_collate_key (object->p->name, -1);
424

425
      if (strcmp (key, object->p->name))
426
        {
427
          object->p->normalized = key;
428 429 430 431
        }
      else
        {
          g_free (key);
432
          object->p->normalized = object->p->name;
433 434 435 436
        }
    }
}

437 438 439 440 441 442 443

#define DEBUG_MEMSIZE 1

#ifdef DEBUG_MEMSIZE
gboolean gimp_debug_memsize = FALSE;
#endif

444
gint64
445
gimp_object_get_memsize (GimpObject *object,
446
                         gint64     *gui_size)
447
{
448 449
  gint64 my_size     = 0;
  gint64 my_gui_size = 0;
450

451 452 453 454 455 456 457 458 459
  g_return_val_if_fail (object == NULL || GIMP_IS_OBJECT (object), 0);

  if (! object)
    {
      if (gui_size)
        *gui_size = 0;

      return 0;
    }
460 461

#ifdef DEBUG_MEMSIZE
462 463 464 465 466
  if (gimp_debug_memsize)
    {
      static gint   indent_level     = 0;
      static GList *aggregation_tree = NULL;
      static gchar  indent_buf[256];
467

468 469 470 471 472
      gint64  memsize;
      gint64  gui_memsize = 0;
      gint    i;
      gint    my_indent_level;
      gchar  *object_size;
473

474
      indent_level++;
475

476
      my_indent_level = indent_level;
477

478 479
      memsize = GIMP_OBJECT_GET_CLASS (object)->get_memsize (object,
                                                             &gui_memsize);
480

481
      indent_level--;
482

483 484
      for (i = 0; i < MIN (my_indent_level * 2, sizeof (indent_buf) - 1); i++)
        indent_buf[i] = ' ';
485

486
      indent_buf[i] = '\0';
487

488 489 490
      object_size = g_strdup_printf ("%s%s \"%s\": "
                                     "%" G_GINT64_FORMAT
                                     "(%" G_GINT64_FORMAT ")\n",
491 492
                                     indent_buf,
                                     g_type_name (G_TYPE_FROM_INSTANCE (object)),
493
                                     object->p->name ? object->p->name : "anonymous",
494 495
                                     memsize,
                                     gui_memsize);
496

497
      aggregation_tree = g_list_prepend (aggregation_tree, object_size);
498

499 500
      if (indent_level == 0)
        {
501 502 503 504 505 506 507
          GList *list;

          for (list = aggregation_tree; list; list = g_list_next (list))
            {
              g_print ("%s", (gchar *) list->data);
              g_free (list->data);
            }
508

509
          g_list_free (aggregation_tree);
510 511
          aggregation_tree = NULL;
        }
512

513 514
      return memsize;
    }
515 516
#endif /* DEBUG_MEMSIZE */

517 518 519 520 521 522 523
  my_size = GIMP_OBJECT_GET_CLASS (object)->get_memsize (object,
                                                         &my_gui_size);

  if (gui_size)
    *gui_size = my_gui_size;

  return my_size;
524 525
}

526
static gint64
527
gimp_object_real_get_memsize (GimpObject *object,
528
                              gint64     *gui_size)
529
{
530
  gint64 memsize = 0;
531

532 533
  if (! object->p->static_name)
    memsize += gimp_string_get_memsize (object->p->name);
534

535
  return memsize + gimp_g_object_get_memsize ((GObject *) object);
536
}