Fix of bug #350246: Broken snap-to-grid inside objects.
[dia.git] / objects / custom / custom.c
blob9cc94da6a8c8618034aa9c49edf8ebaab24a80b4
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 #include <glib.h>
33 #include "sheet.h"
34 #include "shape_info.h"
35 #include "dia_dirs.h"
36 #include "intl.h"
37 #include "plug-ins.h"
39 void custom_object_new (ShapeInfo *info,
40 DiaObjectType **otype);
42 G_MODULE_EXPORT gboolean custom_object_load(gchar *filename,
43 DiaObjectType **otype);
45 /* Cannot be static, because we may use this fn later when loading
46 a new shape via the sheets dialog */
48 G_MODULE_EXPORT gboolean
49 custom_object_load(gchar *filename, DiaObjectType **otype)
51 ShapeInfo *info;
53 if (!filename)
54 return FALSE;
55 info = shape_info_load(filename);
56 /*g_assert(info);*/
57 if (!info) {
58 *otype = NULL;
59 return FALSE;
61 custom_object_new(info, otype);
62 return TRUE;
65 static void load_shapes_from_tree(const gchar *directory)
67 GDir *dp;
68 const char *dentry;
70 dp = g_dir_open(directory, 0, NULL);
71 if (dp == NULL) {
72 return;
74 while ( (dentry = g_dir_read_name(dp)) ) {
75 gchar *filename = g_strconcat(directory, G_DIR_SEPARATOR_S,
76 dentry, NULL);
77 const gchar *p;
79 if (g_file_test(filename, G_FILE_TEST_IS_DIR)) {
80 load_shapes_from_tree(filename);
81 g_free(filename);
82 continue;
84 /* if it's not a directory, then it must be a .shape file */
85 if ( !g_file_test(filename, G_FILE_TEST_IS_REGULAR)
86 || (strlen(dentry) < 6)) {
87 g_free(filename);
88 continue;
91 p = dentry + strlen(dentry) - 6;
92 if (0==strcmp(".shape",p)) {
93 DiaObjectType *ot;
95 if (!custom_object_load(filename, &ot)) {
96 g_warning("could not load shape file %s",filename);
97 } else {
98 g_assert(ot);
99 g_assert(ot->default_user_data);
100 object_register_type(ot);
103 g_free(filename);
105 g_dir_close(dp);
109 DIA_PLUGIN_CHECK_INIT
111 PluginInitResult
112 dia_plugin_init(PluginInfo *info)
114 char *shape_path;
115 const char *home_dir;
117 if (!dia_plugin_info_init(info, _("Custom"), _("Custom XML shapes loader"),
118 NULL, NULL))
119 return DIA_PLUGIN_INIT_ERROR;
121 home_dir = g_get_home_dir();
122 if (home_dir) {
123 home_dir = dia_config_filename("shapes");
124 load_shapes_from_tree(home_dir);
125 g_free((char *)home_dir);
128 shape_path = getenv("DIA_SHAPE_PATH");
129 if (shape_path) {
130 char **dirs = g_strsplit(shape_path, G_SEARCHPATH_SEPARATOR_S, 0);
131 int i;
133 for (i = 0; dirs[i] != NULL; i++)
134 load_shapes_from_tree(dirs[i]);
135 g_strfreev(dirs);
136 } else {
137 char *thedir = dia_get_data_directory("shapes");
138 load_shapes_from_tree(thedir);
139 g_free(thedir);
142 return DIA_PLUGIN_INIT_OK;