File: sheet.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-9
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 54,372 kB
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,833; sh: 540; perl: 137; sed: 19
file content (494 lines) | stat: -rw-r--r-- 14,918 bytes parent folder | download | duplicates (3)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998 Alexander Larsson
 *
 * 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>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <glib.h>
#include <glib/gstdio.h> /* g_stat() */
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include "dia_xml_libxml.h"
#include <string.h>

#include "intl.h"

#include "sheet.h"
#include "message.h"
#include "object.h"
#include "object-alias.h"
#include "dia_dirs.h"

static GSList *sheets = NULL;

Sheet *
new_sheet(char *name, gchar *description, char *filename, SheetScope scope,
          Sheet *shadowing)
{
  Sheet *sheet;

  sheet = g_new(Sheet, 1);

  sheet->name = g_strdup(name);
  sheet->description = g_strdup(description);

  sheet->filename = filename;
  sheet->scope = scope;
  sheet->shadowing = shadowing;
  sheet->objects = NULL;
  return sheet;
}

void
sheet_prepend_sheet_obj(Sheet *sheet, SheetObject *obj)
{
  DiaObjectType *type;

  type = object_get_type(obj->object_type);
  if (type == NULL) {
    message_warning(_("DiaObject '%s' needed in sheet '%s' was not found.\n"
		      "It will not be available for use."),
		    obj->object_type, sheet->name);
  } else {
    sheet->objects = g_slist_prepend( sheet->objects, (gpointer) obj);
  }
}

void
sheet_append_sheet_obj(Sheet *sheet, SheetObject *obj)
{
  DiaObjectType *type;

  type = object_get_type(obj->object_type);
  if (type == NULL) {
    message_warning(_("DiaObject '%s' needed in sheet '%s' was not found.\n"
		      "It will not be available for use."),
		      obj->object_type, sheet->name);
  } else {
    sheet->objects = g_slist_append( sheet->objects, (gpointer) obj);
  }
}

void
register_sheet(Sheet *sheet)
{
  sheets = g_slist_append(sheets, (gpointer) sheet);
}

GSList *
get_sheets_list(void)
{
  return sheets;
}

/* Sheet file management */

static void load_sheets_from_dir(const gchar *directory, SheetScope scope);
static void load_register_sheet(const gchar *directory,const gchar *filename,
                                SheetScope scope);

/** Sort the list of sheets by *locale*.
 */
static gint
dia_sheet_sort_callback(gconstpointer a, gconstpointer b)
{
  return g_utf8_collate(gettext( ((Sheet *)(a))->name ),
			gettext( ((Sheet *)(b))->name ));
}

void
dia_sort_sheets(void)
{
  sheets = g_slist_sort(sheets, dia_sheet_sort_callback);
}

void 
load_all_sheets(void) 
{
  char *sheet_path;
  char *home_dir;

  home_dir = dia_config_filename("sheets");
  if (home_dir) {
    dia_log_message ("sheets from '%s'", home_dir);
    load_sheets_from_dir(home_dir, SHEET_SCOPE_USER);
    g_free(home_dir);
  }

  sheet_path = getenv("DIA_SHEET_PATH");
  if (sheet_path) {
    char **dirs = g_strsplit(sheet_path,G_SEARCHPATH_SEPARATOR_S,0);
    int i;

    for (i=0; dirs[i] != NULL; i++) {
      dia_log_message ("sheets from '%s'", dirs[i]);
      load_sheets_from_dir(dirs[i], SHEET_SCOPE_SYSTEM);
    }
    g_strfreev(dirs);
  } else {
    char *thedir = dia_get_data_directory("sheets");
    dia_log_message ("sheets from '%s'", thedir);
    load_sheets_from_dir(thedir, SHEET_SCOPE_SYSTEM);
    g_free(thedir);
  }

  /* Sorting their sheets alphabetically makes user merging easier */

  dia_sort_sheets();
}

static void 
load_sheets_from_dir(const gchar *directory, SheetScope scope)
{
  GDir *dp;
  const char *dentry;
  gchar *p;

  dp = g_dir_open(directory, 0, NULL);
  if (!dp) return;

  while ( (dentry = g_dir_read_name(dp)) ) {
    gchar *filename = g_strconcat(directory,G_DIR_SEPARATOR_S,
				  dentry,NULL);

    if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
      g_free(filename);
      continue;
    }

    /* take only .sheet files */
    p = filename + strlen(filename) - 6 /* strlen(".sheet") */;
    if (0!=strncmp(p,".sheet",6)) {
      g_free(filename);
      continue;
    }

    load_register_sheet(directory, filename, scope);
    g_free(filename);
				
  }

  g_dir_close(dp);
}

static void 
load_register_sheet(const gchar *dirname, const gchar *filename,
                    SheetScope scope)
{
  xmlErrorPtr error_xml = NULL;
  xmlDocPtr doc;
  xmlNsPtr ns;
  xmlNodePtr node, contents,subnode,root;
  xmlChar *tmp;
  gchar *name = NULL, *description = NULL;
  int name_score = -1;
  int descr_score = -1;
  Sheet *sheet = NULL;
  GSList *sheetp;
  gboolean set_line_break = FALSE;
  gboolean name_is_gmalloced = FALSE;
  Sheet *shadowing = NULL;
  Sheet *shadowing_sheet = NULL;

  /* the XML fun begins here. */

  doc = xmlDoParseFile(filename, &error_xml);
  if (error_xml)
    g_warning ("Sheet parser error %s", error_xml->message);
  if (!doc) return;
  root = doc->xmlRootNode;
  while (root && (root->type != XML_ELEMENT_NODE)) root=root->next;
  if (!root) return;
  if (xmlIsBlankNode(root)) return;

  if (!(ns = xmlSearchNsByHref(doc,root, (const xmlChar *)
	   DIA_XML_NAME_SPACE_BASE "dia-sheet-ns"))) {
    g_warning("could not find sheet namespace");
    xmlFreeDoc(doc); 
    return;
  }
  if ((root->ns != ns) || (xmlStrcmp(root->name, (const xmlChar *)"sheet"))) {
    g_warning("root element was %s -- expecting sheet", 
              doc->xmlRootNode->name);
    xmlFreeDoc(doc);
    return;
  }

  contents = NULL;
  for (node = root->xmlChildrenNode; node != NULL; node = node->next) {
    if (xmlIsBlankNode(node)) continue;
    if (node->type != XML_ELEMENT_NODE)
      continue;

    if (node->ns == ns && !xmlStrcmp(node->name, (const xmlChar *)"name")) {
      gint score;
      
      /* compare the xml:lang property on this element to see if we get a
       * better language match.  LibXML seems to throw away attribute
       * namespaces, so we use "lang" instead of "xml:lang" */
      /* Now using the C locale for internal sheet names, instead gettexting
       * when the name is used in the menus.  Not going to figure out the
       * XML lang system more than absolutely necessary now.   --LC
       */
      /*
      tmp = xmlGetProp(node, "xml:lang");
      if (!tmp) tmp = xmlGetProp(node, "lang");
      */
      score = intl_score_locale("C");
      /*
      if (tmp) xmlFree(tmp);
      */

      if (name_score < 0 || score < name_score) {
        name_score = score;
        if (name) xmlFree(name);
        name = (char *) xmlNodeGetContent(node);
      }      
    } else if (node->ns == ns && !xmlStrcmp(node->name, (const xmlChar *)"description")) {
      gint score;

      /* compare the xml:lang property on this element to see if we get a
       * better language match.  LibXML seems to throw away attribute
       * namespaces, so we use "lang" instead of "xml:lang" */
      tmp = xmlGetProp(node, (const xmlChar *)"xml:lang");
      if (!tmp) tmp = xmlGetProp(node, (const xmlChar *)"lang");
      score = intl_score_locale((char *) tmp);
      if (tmp) xmlFree(tmp);

      if (descr_score < 0 || score < descr_score) {
        descr_score = score;
        if (description) xmlFree(description);
        description = (char *) xmlNodeGetContent(node);
      }
      
    } else if (node->ns == ns && !xmlStrcmp(node->name, (const xmlChar *)"contents")) {
      contents = node;
    }
  }

  if (!name || !contents) {
    g_warning("No <name> and/or <contents> in sheet %s--skipping", filename);
    xmlFreeDoc(doc);
    if (name) xmlFree(name);
    if (description) xmlFree(description);
    return;
  }

  /* Notify the user when we load a sheet that appears to be an updated
     version of a sheet loaded previously (i.e. from ~/.dia/sheets). */

  sheetp = get_sheets_list();
  while (sheetp)
  {
    if (sheetp->data && !strcmp(((Sheet *)(sheetp->data))->name, name)) 
    {
      struct stat first_file, this_file;
      int stat_ret;
      
      stat_ret = g_stat(((Sheet *)(sheetp->data))->filename, &first_file);
      g_assert(!stat_ret);

      stat_ret = g_stat(filename, &this_file);
      g_assert(!stat_ret);

      if (this_file.st_mtime > first_file.st_mtime)
      {
        gchar *tmp = g_strdup_printf("%s [Copy of system]", name);
        message_notice(_("The system sheet '%s' appears to be more recent"
                         " than your custom\n"
                         "version and has been loaded as '%s' for this session."
                         "\n\n"
                         "Move new objects (if any) from '%s' into your custom"
                         " sheet\n"
                         "or remove '%s', using the 'Sheets and Objects' dialog."),
                         name, tmp, tmp, tmp);
        xmlFree(name);
        name = tmp;
        name_is_gmalloced = TRUE;
        shadowing = sheetp->data;  /* This copy-of-system sheet shadows
                                      a user sheet */
      }
      else
      {
        /* The already-created user sheet shadows this sheet (which will be
           invisible), but we don't know this sheet's address yet */
        shadowing_sheet = sheetp->data;
      }
    }
    sheetp = g_slist_next(sheetp);
  }

  sheet = new_sheet(name, description, g_strdup(filename), scope, shadowing);

  if (shadowing_sheet)
    shadowing_sheet->shadowing = sheet;                   /* Hilarious :-) */

  if (name_is_gmalloced == TRUE)
    g_free(name);
  else
    xmlFree(name);
  xmlFree(description);

  for (node = contents->xmlChildrenNode ; node != NULL; node = node->next) {
    SheetObject *sheet_obj;
    DiaObjectType *otype;
    gchar *iconname = NULL;

    int subdesc_score = -1;
    xmlChar *objdesc = NULL;

    gint intdata = 0;
    gchar *chardata = NULL;

    gboolean has_intdata = FALSE;
    gboolean has_icon_on_sheet = FALSE;
    
    xmlChar *ot_name = NULL;

    if (xmlIsBlankNode(node)) continue;

    if (node->type != XML_ELEMENT_NODE) 
      continue;
    if (node->ns != ns) continue;
    if (!xmlStrcmp(node->name, (const xmlChar *)"object")) {
      /* nothing */
    } else if (!xmlStrcmp(node->name, (const xmlChar *)"shape")) {
      g_message(_("%s: you should use object tags rather than shape tags now"),
                filename);
    } else if (!xmlStrcmp(node->name, (const xmlChar *)"br")) {
      /* Line break tag. */
      set_line_break = TRUE;
      continue;
    } else
      continue; /* unknown tag */
    
    tmp = xmlGetProp(node, (const xmlChar *)"intdata");
    if (tmp) { 
      char *p;
      intdata = (gint)strtol((char *) tmp,&p,0);
      if (*p != 0) intdata = 0;
      xmlFree(tmp);
      has_intdata = TRUE;
    }
    chardata = (gchar *) xmlGetProp(node, (const xmlChar *)"chardata");
    /* TODO.... */
    if (chardata) xmlFree(chardata);
   
    ot_name = xmlGetProp(node, (xmlChar *)"name");
    
    for (subnode = node->xmlChildrenNode; 
         subnode != NULL ; 
         subnode = subnode->next) {
      if (xmlIsBlankNode(subnode)) continue;

      if (subnode->ns == ns && !xmlStrcmp(subnode->name, (const xmlChar *)"description")) {
	gint score;

	/* compare the xml:lang property on this element to see if we get a
	 * better language match.  LibXML seems to throw away attribute
	 * namespaces, so we use "lang" instead of "xml:lang" */
	  
	tmp = xmlGetProp(subnode, (xmlChar *)"xml:lang");
	if (!tmp) tmp = xmlGetProp(subnode, (xmlChar *)"lang");
	score = intl_score_locale((char *) tmp);
	if (tmp) xmlFree(tmp);

	if (subdesc_score < 0 || score < subdesc_score) {
	  subdesc_score = score;
	  if (objdesc) xmlFree(objdesc);
	  objdesc = xmlNodeGetContent(subnode);
	}
	  
      } else if (subnode->ns == ns && !xmlStrcmp(subnode->name, (const xmlChar *)"icon")) {
          tmp = xmlNodeGetContent(subnode);
          iconname = g_strconcat(dirname,G_DIR_SEPARATOR_S, (char *) tmp,NULL);
	  if(!shadowing_sheet && !g_file_test (iconname, G_FILE_TEST_EXISTS))
          {
	   /* Fall back to system directory if there is no user icon */
            gchar *sheetdir = dia_get_data_directory("sheets");
            iconname = g_strconcat(sheetdir,G_DIR_SEPARATOR_S, (char *) tmp,NULL);
	    g_free(sheetdir);
          }
          has_icon_on_sheet = TRUE;
          if (tmp) xmlFree(tmp);
      } else if (subnode->ns == ns && !xmlStrcmp(subnode->name, (const xmlChar *)"alias")) {
        if (ot_name)
          object_register_alias_type (object_get_type ((char *)ot_name), subnode); 
      }
    }

    sheet_obj = g_new(SheetObject,1);
    sheet_obj->object_type = g_strdup((char *) ot_name);
    sheet_obj->description = g_strdup((gchar *)objdesc);
    xmlFree(objdesc); objdesc = NULL;

    sheet_obj->pixmap = NULL;
    sheet_obj->user_data = GINT_TO_POINTER(intdata); /* XXX modify user_data type ? */
    sheet_obj->user_data_type = has_intdata ? USER_DATA_IS_INTDATA /* sure,   */
                                            : USER_DATA_IS_OTHER;  /* why not */
    sheet_obj->pixmap_file = iconname; 
    sheet_obj->has_icon_on_sheet = has_icon_on_sheet;
    sheet_obj->line_break = set_line_break;
    set_line_break = FALSE;

    if ((otype = object_get_type((char *) ot_name)) == NULL) {
      /* Don't complain. This does happen when disabling plug-ins too.
      g_warning("object_get_type(%s) returned NULL", tmp); */
      if (sheet_obj->description) g_free(sheet_obj->description);
      g_free(sheet_obj->pixmap_file);
      g_free(sheet_obj->object_type);
      g_free(sheet_obj);
      if (tmp) xmlFree(ot_name);
      continue; 
    }	  
    
    /* set defaults */
    if (sheet_obj->pixmap_file == NULL) {
      g_assert(otype->pixmap || otype->pixmap_file);
      sheet_obj->pixmap = otype->pixmap;
      sheet_obj->pixmap_file = otype->pixmap_file;
      sheet_obj->has_icon_on_sheet = has_icon_on_sheet;
    }
    if (sheet_obj->user_data == NULL
        && sheet_obj->user_data_type != USER_DATA_IS_INTDATA)
      sheet_obj->user_data = otype->default_user_data;
    else
      sheet_obj->user_data_type = USER_DATA_IS_INTDATA;

    if (ot_name)
      xmlFree(ot_name);
      
    /* we don't need to fix up the icon and descriptions for simple objects,
       since they don't have their own description, and their icon is 
       already automatically handled. */
    sheet_append_sheet_obj(sheet,sheet_obj);
  }

  if (!shadowing_sheet)
    register_sheet(sheet); 

  xmlFreeDoc(doc);
}