Patch to add Sheets & Objects Dialog from M. C. Nelson
[dia.git] / objects / custom / custom.c
blob4c9dc670558c8769c82fb5b1b4b0f212064ae76f
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998, 1999 Alexander Larsson
4 * Custom Objects -- objects defined in XML rather than C.
5 * Copyright (C) 1999 James Henstridge.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <config.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #ifdef HAVE_DIRENT_H
32 #include <dirent.h>
33 #endif
34 #include <glib.h>
36 #include "sheet.h"
37 #include "shape_info.h"
38 #include "dia_dirs.h"
39 #include "intl.h"
40 #include "plug-ins.h"
42 void custom_object_new (ShapeInfo *info,
43 ObjectType **otype);
45 gboolean custom_object_load(gchar *filename, ObjectType **otype);
47 /* Cannot be static, because we may use this fn later when loading
48 a new shape via the sheets dialog */
50 gboolean
51 custom_object_load(gchar *filename, ObjectType **otype)
53 ShapeInfo *info;
55 if (!filename)
56 return FALSE;
57 info = shape_info_load(filename);
58 /*g_assert(info);*/
59 if (!info) {
60 *otype = NULL;
61 return FALSE;
63 custom_object_new(info, otype);
64 return TRUE;
67 static void load_shapes_from_tree(const gchar *directory)
69 DIR *dp;
70 struct dirent *dirp;
71 struct stat statbuf;
73 dp = opendir(directory);
74 if (dp == NULL) {
75 return;
77 while ( (dirp = readdir(dp)) ) {
78 gchar *filename = g_strconcat(directory, G_DIR_SEPARATOR_S,
79 dirp->d_name, NULL);
80 gchar *p;
82 if (!strcmp(dirp->d_name, ".") || !strcmp(dirp->d_name, "..")) {
83 g_free(filename);
84 continue;
87 if (stat(filename, &statbuf) < 0) {
88 g_free(filename);
89 continue;
91 if (S_ISDIR(statbuf.st_mode)) {
92 load_shapes_from_tree(filename);
93 g_free(filename);
94 continue;
96 /* if it's not a directory, then it must be a .shape file */
97 if (!S_ISREG(statbuf.st_mode) || (strlen(dirp->d_name) < 6)) {
98 g_free(filename);
99 continue;
103 p = dirp->d_name + strlen(dirp->d_name) - 6;
104 if (0==strcmp(".shape",p)) {
105 ObjectType *ot;
107 if (!custom_object_load(filename, &ot)) {
108 g_warning("could not load shape file %s",filename);
109 } else {
110 g_assert(ot);
111 g_assert(ot->default_user_data);
112 object_register_type(ot);
115 g_free(filename);
117 closedir(dp);
121 DIA_PLUGIN_CHECK_INIT
123 PluginInitResult
124 dia_plugin_init(PluginInfo *info)
126 char *shape_path;
127 char *home_dir;
129 if (!dia_plugin_info_init(info, _("Custom"), _("Custom XML shapes loader"),
130 NULL, NULL))
131 return DIA_PLUGIN_INIT_ERROR;
133 home_dir = g_get_home_dir();
134 if (home_dir) {
135 home_dir = dia_config_filename("shapes");
136 load_shapes_from_tree(home_dir);
137 g_free(home_dir);
140 shape_path = getenv("DIA_SHAPE_PATH");
141 if (shape_path) {
142 char **dirs = g_strsplit(shape_path, G_SEARCHPATH_SEPARATOR_S, 0);
143 int i;
145 for (i = 0; dirs[i] != NULL; i++)
146 load_shapes_from_tree(dirs[i]);
147 g_strfreev(dirs);
148 } else {
149 char *thedir = dia_get_data_directory("shapes");
150 load_shapes_from_tree(thedir);
151 g_free(thedir);
154 return DIA_PLUGIN_INIT_OK;