summaryrefslogtreecommitdiff
path: root/lib/object_defaults.c
blob: fd151a8367f6af5603bab5218fe9eea55da88125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * object_defaults.c : manage default properties of dia objects
 *	The serialization is done with standard object methods in 
 *	a diagram compatible format.
 *
 * Copyright (C) 2002 Hans Breuer
 *
 * 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 <stdlib.h> /* atoi() */
#include <string.h>

#include <glib.h>

#include "intl.h"

#include <libxml/tree.h>
#include "dia_xml_libxml.h"
#include "dia_xml.h"
#include "object.h"
#include "diacontext.h"
#include "dia_dirs.h"
#include "propinternals.h"
 
static GHashTable *defaults_hash = NULL;
static gboolean object_default_create_lazy = FALSE;

static void
_obj_create (gpointer key,
             gpointer value,
             gpointer user_data)
{
  gchar *name = (gchar *)key;
  DiaObjectType *type = (DiaObjectType *)value;
  GHashTable *ht = (GHashTable *) user_data;
  DiaObject *obj;
  Point startpoint = {0.0,0.0};
  Handle *handle1,*handle2;
  
  g_assert (g_hash_table_lookup (ht, name) == NULL);

  /* at least 'Group' has no ops */
  if (!type->ops)
    return;

  /* the custom objects needs extra_data */
  obj = type->ops->create(&startpoint, type->default_user_data, &handle1,&handle2);
  if (!obj)
    g_warning ("Failed to create default object for '%s'", name);
  else if (0 != strcmp (obj->type->name, name))
    object_destroy (obj); /* Skip over 'compatibility' names/objects */
  else
    g_hash_table_insert(ht, obj->type->name, obj);
}

static void
_obj_destroy (gpointer val)
{
  DiaObject *obj = (DiaObject *)val;

  obj->ops->destroy (obj);
  g_free (obj);
}

/**
 * @param filename the file to load from or NULL for default
 * @param create_lazy if FALSE creates default objects for
 *             every known type. Otherwise default objects
 *             are created on demand
 * @param ctx The context in which this function is called
 * 
 * Create all the default objects.
 */
gboolean
dia_object_defaults_load (const gchar *filename, gboolean create_lazy, DiaContext *ctx)
{
  xmlDocPtr doc;
  xmlNsPtr name_space;
  ObjectNode obj_node, layer_node;

  object_default_create_lazy = create_lazy;

  if (!defaults_hash)
    {
      defaults_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
                                            NULL, _obj_destroy);

      if (!create_lazy)
        object_registry_foreach (_obj_create, defaults_hash);
    }

    
  /* overload properties from file */
  if (!filename) 
    {
      gchar *default_filename = dia_config_filename("defaults.dia");

      dia_context_set_filename(ctx, default_filename);
      if (g_file_test(default_filename, G_FILE_TEST_EXISTS))
        doc = diaXmlParseFile (default_filename, ctx, FALSE);
      else
        doc = NULL;
      g_free (default_filename);
    } 
  else
    {
      dia_context_set_filename (ctx, filename);
      doc = diaXmlParseFile (filename, ctx, FALSE);
    }

  if (!doc)
      return FALSE;

  name_space = xmlSearchNs(doc, doc->xmlRootNode, (const xmlChar *)"dia");
  if (xmlStrcmp (doc->xmlRootNode->name, (const xmlChar *)"diagram") 
      || (name_space == NULL))
    {
      dia_context_add_message(ctx, _("Error loading defaults '%s'.\n"
				     "Not a Dia diagram file."), filename);
      xmlFreeDoc (doc);
      return FALSE;
    }

  layer_node = doc->xmlRootNode->xmlChildrenNode;
  while (layer_node)
    {
      if (   !xmlIsBlankNode(layer_node)
          && 0 == xmlStrcmp(layer_node->name, (const xmlChar *)"layer")) 
        {
	  obj_node = layer_node->xmlChildrenNode;
	  while (obj_node)
	    {
	      if (!xmlIsBlankNode(obj_node)
		  && 0 == xmlStrcmp(obj_node->name, (const xmlChar *)"object")) 
		{
		  char *typestr = (char *) xmlGetProp(obj_node, (const xmlChar *)"type");
		  char *version = (char *) xmlGetProp(obj_node, (const xmlChar *)"version");
		  if (typestr)
		    {
		      DiaObject *obj = g_hash_table_lookup (defaults_hash, typestr);
		      if (!obj)
		        {
			  if (!create_lazy)
			    g_warning ("Unknown object '%s' while reading '%s'",
				       typestr, filename);
			  else
			    {
			      DiaObjectType *type = object_get_type (typestr);
			      if (type)
			        obj = type->ops->load (
					obj_node,
					version ? atoi(version) : 0,
					ctx);
			      if (obj)
			        g_hash_table_insert (defaults_hash,
			                             obj->type->name, obj);
			    }
			}
		      else
		        {
#if 0 /* lots of complaining about missing attributes */
			  object_load_props(obj, obj_node, ctx); /* leaks ?? */
#else
			  DiaObject *def_obj;
			  def_obj = obj->type->ops->load (
					obj_node,
			                version ? atoi(version) : 0,
					ctx);
			  if (def_obj->ops->set_props)
			    { 
			      object_copy_props (obj, def_obj, TRUE);
			      def_obj->ops->destroy (def_obj);
			    }
			  else
			    {
			      /* can't copy props */
			      g_hash_table_replace (defaults_hash,
			                            def_obj->type->name, def_obj);
			    }
#endif
			}
		      if (version)
		          xmlFree (version);
		      xmlFree (typestr);
		    }
		}
	      obj_node = obj_node->next;
	    }
	}
      layer_node = layer_node->next;
    }
  xmlFreeDoc(doc);
  return TRUE;
}

/**
 * dia_object_default_get :
 * @param type The type of the object for which you want the defaults object.
 * @param user_data unused
 *
 * Allows to edit one defaults object properties
 */
DiaObject *
dia_object_default_get (const DiaObjectType *type, gpointer user_data)
{
  DiaObject *obj;

  obj = g_hash_table_lookup (defaults_hash, type->name);
  if (!obj && object_default_create_lazy)
    {
      Point startpoint = {0.0,0.0};
      Handle *handle1,*handle2;
  
      /* at least 'Group' has no ops */
      if (!type->ops)
	return NULL;

      /* the custom objects needs extra_data */
      obj = type->ops->create(&startpoint, 
                              type->default_user_data, 
			      &handle1,&handle2);
      if (obj)
        g_hash_table_insert (defaults_hash, obj->type->name, obj);
    }

  return obj;
}

static gboolean 
pdtpp_standard_or_defaults (const PropDescription *pdesc)
{
  return (   (pdesc->flags & PROP_FLAG_NO_DEFAULTS) == 0
          && (pdesc->flags & PROP_FLAG_STANDARD) == 0);
}


/**
 * dia_object_default_create:
 * @param type The objects type
 * @param startpoint The left upper corner
 * @param user_data
 * @param handle1
 * @param handle2
 * @return A newly created object.
 *
 * Create an object respecting defaults if available
 */
DiaObject *
dia_object_default_create (const DiaObjectType *type,
                           Point *startpoint,
                           void *user_data,
                           Handle **handle1,
                           Handle **handle2)
{
  const DiaObject *def_obj;
  DiaObject *obj;

  g_return_val_if_fail (type != NULL, NULL);

  /* don't use dia_object_default_get() as it would insert the object into the hashtable (store defaults without being asked for it) */
  def_obj = g_hash_table_lookup (defaults_hash, type->name);
  if (def_obj && def_obj->ops->describe_props)
    {
      /* copy properties to new object, but keep position */
      obj = type->ops->create (startpoint, user_data, handle1, handle2);
      if (obj)
        {
	  GPtrArray *props = prop_list_from_descs (
	      object_get_prop_descriptions(def_obj), pdtpp_standard_or_defaults);
          def_obj->ops->get_props((DiaObject *)def_obj, props);
          obj->ops->set_props(obj, props);
	  obj->ops->move (obj, startpoint);
          prop_list_free(props);
	}
    }
  else
    {
      obj = type->ops->create (startpoint, user_data, handle1, handle2);
    }

  return obj;
}

typedef struct _MyLayerInfo MyLayerInfo;
struct _MyLayerInfo
{
  Point      pos;
  xmlNodePtr node;
};

typedef struct _MyRootInfo MyRootInfo;
struct _MyRootInfo
{
  xmlNodePtr  node;
  gchar      *filename;
  GHashTable *layer_hash;
  xmlNs      *name_space;
  gint        obj_nr;
  DiaContext *ctx;
};

static void
_obj_store (gpointer key,
            gpointer value,
            gpointer user_data)
{
  gchar *name = (gchar *)key;
  DiaObject *obj = (DiaObject *)value;
  MyRootInfo *ri = (MyRootInfo *)user_data;
  ObjectNode obj_node;
  gchar *layer_name;
  gchar buffer[31];
  gchar *p;
  MyLayerInfo *li;

  /* fires if you have messed up the hash keys, 
   * e.g. by using non permanent memory */
  g_assert (0 == strcmp (obj->type->name, name));

  p = strstr (name, " - ");
  if (p) {
    if (p > name)
      layer_name = g_strndup (name, p - name);
    else
      layer_name = g_strdup("NULL");
  }
  else
    layer_name = g_strdup ("default");

  li = g_hash_table_lookup (ri->layer_hash, layer_name);
  if (!li)
    {
      li = g_new (MyLayerInfo, 1);
      li->node = xmlNewChild(ri->node, ri->name_space, (const xmlChar *)"layer", NULL);
      xmlSetProp(li->node, (const xmlChar *)"name", (xmlChar *)layer_name);
      xmlSetProp(li->node, (const xmlChar *)"visible", (const xmlChar *)"false");
      li->pos.x = li->pos.y = 0.0;
      g_hash_table_insert (ri->layer_hash, layer_name, li);
    }
  else
    g_free (layer_name);

  obj_node = xmlNewChild(li->node, NULL, (const xmlChar *)"object", NULL);
  xmlSetProp(obj_node, (const xmlChar *)"type", (xmlChar *) obj->type->name);

  g_snprintf(buffer, 30, "%d", obj->type->version);
  xmlSetProp(obj_node, (const xmlChar *)"version", (xmlChar *)buffer);

  g_snprintf(buffer, 30, "O%d", ri->obj_nr++);
  xmlSetProp(obj_node, (const xmlChar *)"id", (xmlChar *)buffer);

  /* if it looks like intdata store it as well */
  if (   GPOINTER_TO_INT(obj->type->default_user_data) > 0 
      && GPOINTER_TO_INT(obj->type->default_user_data) < 0xFF) {
    g_snprintf(buffer, 30, "%d", GPOINTER_TO_INT(obj->type->default_user_data));
    xmlSetProp(obj_node, (const xmlChar *)"intdata", (xmlChar *)buffer);
  }

  obj->ops->move (obj,&(li->pos));
  /* saving every property of the object */
  obj->type->ops->save (obj, obj_node, ri->ctx);

  /* arrange following objects below */
  li->pos.y += (obj->bounding_box.bottom - obj->bounding_box.top + 1.0); 
}

/**
 * dia_object_defaults_save:
 *
 * Saves all the currently created default objects into a
 * valid diagram file. All the objects are placed into
 * separate invisible layers.
 */
gboolean
dia_object_defaults_save (const gchar *filename, DiaContext *ctx)
{
  MyRootInfo ni;
  xmlDocPtr doc;
  gboolean ret;
  gchar *real_filename;

  if (!filename)
    real_filename = dia_config_filename("defaults.dia");
  else
    real_filename = g_strdup (filename);

  doc = xmlNewDoc((const xmlChar *)"1.0");
  doc->encoding = xmlStrdup((const xmlChar *)"UTF-8");
  doc->xmlRootNode = xmlNewDocNode(doc, NULL, (const xmlChar *)"diagram", NULL);

  ni.name_space = xmlNewNs(doc->xmlRootNode, 
                           (const xmlChar *)DIA_XML_NAME_SPACE_BASE,
			   (const xmlChar *)"dia");
  xmlSetNs(doc->xmlRootNode, ni.name_space);

  ni.obj_nr = 0;
  ni.node = doc->xmlRootNode;
  ni.filename = real_filename;
  ni.ctx = ctx;
  ni.layer_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
                                         g_free, g_free);

  g_hash_table_foreach (defaults_hash, _obj_store, &ni);

  ret = xmlDiaSaveFile (real_filename, doc);
  g_free (real_filename);
  xmlFreeDoc(doc);

  g_hash_table_destroy (ni.layer_hash);

  return ret;
}