File: custom.c

package info (click to toggle)
dia 0.97.3%2Bgit20160930-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 52,952 kB
  • ctags: 17,146
  • sloc: ansic: 155,065; xml: 16,326; python: 6,641; cpp: 4,935; makefile: 3,826; sh: 540; perl: 137; sed: 19
file content (166 lines) | stat: -rw-r--r-- 4,523 bytes parent folder | download | duplicates (6)
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
/* Dia -- an diagram creation/manipulation program
 * Copyright (C) 1998, 1999 Alexander Larsson
 *
 * Custom Objects -- objects defined in XML rather than C.
 * Copyright (C) 1999 James Henstridge.
 *
 * 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>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <glib.h>

#include "sheet.h"
#include "shape_info.h"
#include "dia_dirs.h"
#include "intl.h"
#include "plug-ins.h"

void custom_object_new (ShapeInfo *info,
                        DiaObjectType **otype);

G_MODULE_EXPORT gboolean custom_object_load(gchar *filename, 
                                            DiaObjectType **otype);

/* Cannot be static, because we may use this fn later when loading
   a new shape via the sheets dialog */
   
G_MODULE_EXPORT gboolean
custom_object_load(gchar *filename, DiaObjectType **otype)
{
  ShapeInfo *info;

  if (!filename)
    return FALSE;
  info = shape_info_load(filename);
  /*g_assert(info);*/
  if (!info) {
    *otype = NULL; 
    return FALSE;
  }
  custom_object_new(info, otype);
  return TRUE;
}

static gboolean
custom_object_preload(gchar *filename, DiaObjectType **otype)
{
  ShapeInfo *info;
  
  info = g_new0 (ShapeInfo, 1);
  info->filename = g_strdup (filename);
  /* Just enough to register the type, not enough to create the object */
  if (!shape_typeinfo_load(info)) {
    /* there are currently 5 - out of ~700 shapes - which fail the size assumption 
     * (reading only the first 512 bytes of the shape).
     * Instead of not loading them at all, they are loaded  completely as a fallback.
     * Another way would be to increase the size to read for every shape, seems worse.
     */
     g_free (info->filename);
     g_free (info);
     if ((info = shape_info_load(filename)) == NULL)
       return FALSE;
  }
  shape_info_register (info);
  custom_object_new(info, otype);
  return TRUE;
}
static void 
load_shapes_from_tree(const gchar *directory)
{
  GDir *dp;
  const char *dentry;

  dp = g_dir_open(directory, 0, NULL);
  if (dp == NULL) {
    return;
  }
  while ( (dentry = g_dir_read_name(dp)) ) {
    gchar *filename = g_strconcat(directory, G_DIR_SEPARATOR_S,
				  dentry, NULL);
    const gchar *p;

    if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
      load_shapes_from_tree(filename);
      g_free(filename);
      continue;
    }
    /* if it's not a directory, then it must be a .shape file */
    if (   !g_file_test(filename, G_FILE_TEST_IS_REGULAR)
        || (strlen(dentry) < 6)) {
      g_free(filename);
      continue;
    }
    
    p = dentry + strlen(dentry) - 6;
    if (0==strcmp(".shape",p)) {
      DiaObjectType *ot;

      if (!custom_object_preload(filename, &ot)) {
        g_warning("could not load shape file %s",filename);
      } else {
        g_assert(ot); 
        g_assert(ot->default_user_data);
        object_register_type(ot);
      }
    }
    g_free(filename);
  }
  g_dir_close(dp);
}

DIA_PLUGIN_CHECK_INIT

PluginInitResult
dia_plugin_init(PluginInfo *info)
{
  char *shape_path;
  const char *home_dir;

  if (!dia_plugin_info_init(info, _("Custom"), _("Custom XML shapes loader"),
			    NULL, NULL))
    return DIA_PLUGIN_INIT_ERROR;

  home_dir = g_get_home_dir();
  if (home_dir) {
    home_dir = dia_config_filename("shapes");
    load_shapes_from_tree(home_dir);
    g_free((char *)home_dir);
  }

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

    for (i = 0; dirs[i] != NULL; i++)
      load_shapes_from_tree(dirs[i]);
    g_strfreev(dirs);
  } else {
    char *thedir = dia_get_data_directory("shapes");
    load_shapes_from_tree(thedir);
    g_free(thedir);
  }

  return DIA_PLUGIN_INIT_OK;
}