1 /* GIMP - The GNU Image Manipulation Program
2 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3 *
4 * Test suite for GimpConfig.
5 * Copyright (C) 2001-2002 Sven Neumann <sven@gimp.org>
6 *
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 3 of the License, or
10 * (at your option) any later version.
11 *
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.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 #include "config.h"
22
23 #include "stdlib.h"
24 #include "string.h"
25
26 #include <gio/gio.h>
27
28 #include "libgimpbase/gimpbase.h"
29 #include "libgimpbase/gimpbase-private.h"
30 #include "libgimpconfig/gimpconfig.h"
31
32 #include "core/core-types.h"
33 #include "core/gimpgrid.h"
34
35 #include "gimprc-unknown.h"
36
37
38 static void notify_callback (GObject *object,
39 GParamSpec *pspec);
40 static void output_unknown_token (const gchar *key,
41 const gchar *value,
42 gpointer data);
43
44 static void units_init (void);
45
46
47 int
main(int argc,char * argv[])48 main (int argc,
49 char *argv[])
50 {
51 GimpConfig *grid;
52 GimpConfig *grid2;
53 const gchar *filename = "foorc";
54 gchar *header;
55 gchar *result;
56 GList *list;
57 gint i;
58 GError *error = NULL;
59
60 for (i = 1; i < argc; i++)
61 {
62 if (strcmp (argv[i], "--g-fatal-warnings") == 0)
63 {
64 GLogLevelFlags fatal_mask;
65
66 fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK);
67 fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL;
68 g_log_set_always_fatal (fatal_mask);
69 }
70 }
71
72 units_init ();
73
74 g_print ("\nTesting GimpConfig ...\n");
75
76 g_print (" Creating a new Grid object ...");
77 grid = g_object_new (GIMP_TYPE_GRID, NULL);
78 g_print (" done.\n");
79
80 g_print (" Adding the unknown token (foobar \"hadjaha\") ...");
81 gimp_rc_add_unknown_token (grid, "foobar", "hadjaha");
82 g_print (" done.\n");
83
84 g_print (" Serializing %s to '%s' ...",
85 g_type_name (G_TYPE_FROM_INSTANCE (grid)), filename);
86
87 if (! gimp_config_serialize_to_file (grid,
88 filename,
89 "foorc", "end of foorc",
90 NULL, &error))
91 {
92 g_print ("%s\n", error->message);
93 return EXIT_FAILURE;
94 }
95 g_print (" done.\n");
96
97 g_signal_connect (grid, "notify",
98 G_CALLBACK (notify_callback),
99 NULL);
100
101 g_print (" Deserializing from '%s' ...\n", filename);
102 if (! gimp_config_deserialize_file (grid, filename, NULL, &error))
103 {
104 g_print ("%s\n", error->message);
105 return EXIT_FAILURE;
106 }
107 header = " Unknown string tokens:\n";
108 gimp_rc_foreach_unknown_token (grid, output_unknown_token, &header);
109 g_print (" done.\n\n");
110
111 g_print (" Changing a property ...");
112 g_object_set (grid, "style", GIMP_GRID_DOTS, NULL);
113
114 g_print (" Testing gimp_config_duplicate() ...");
115 grid2 = gimp_config_duplicate (grid);
116 g_print (" done.\n");
117
118 g_signal_connect (grid2, "notify",
119 G_CALLBACK (notify_callback),
120 NULL);
121
122 g_print (" Changing a property in the duplicate ...");
123 g_object_set (grid2, "xspacing", 20.0, NULL);
124
125 g_print (" Creating a diff between the two ...");
126 for (list = gimp_config_diff (G_OBJECT (grid), G_OBJECT (grid2), 0);
127 list;
128 list = list->next)
129 {
130 GParamSpec *pspec = list->data;
131
132 g_print ("%c%s", (list->prev ? ',' : ' '), pspec->name);
133 }
134 g_print ("\n\n");
135
136 g_object_unref (grid2);
137
138 g_print (" Deserializing from gimpconfig.c (should fail) ...");
139 if (! gimp_config_deserialize_file (grid, "gimpconfig.c", NULL, &error))
140 {
141 g_print (" OK, failed. The error was:\n %s\n", error->message);
142 g_error_free (error);
143 error = NULL;
144 }
145 else
146 {
147 g_print ("This test should have failed :-(\n");
148 return EXIT_FAILURE;
149 }
150
151 g_print (" Serializing to a string and back ... ");
152
153 result = gimp_config_serialize_to_string (grid, NULL);
154
155 grid2 = g_object_new (GIMP_TYPE_GRID, NULL);
156
157 if (! gimp_config_deserialize_string (grid2, result, -1, NULL, &error))
158 {
159 g_print ("failed!\nThe error was:\n %s\n", error->message);
160 g_error_free (error);
161 return EXIT_FAILURE;
162 }
163 else
164 {
165 GList *diff = gimp_config_diff (G_OBJECT (grid), G_OBJECT (grid2), 0);
166
167 if (diff)
168 {
169 GList *list;
170
171 g_print ("succeeded but properties differ:\n");
172 for (list = diff; list; list = list->next)
173 {
174 GParamSpec *pspec = list->data;
175 g_print (" %s\n", pspec->name);
176 }
177 return EXIT_FAILURE;
178 }
179
180 g_print ("OK (%u bytes)\n", result ? (guint) strlen (result) : 0);
181 }
182
183 g_free (result);
184 g_object_unref (grid2);
185 g_object_unref (grid);
186
187 g_print ("\nFinished test of GimpConfig.\n\n");
188
189 return EXIT_SUCCESS;
190 }
191
192 static void
notify_callback(GObject * object,GParamSpec * pspec)193 notify_callback (GObject *object,
194 GParamSpec *pspec)
195 {
196 GString *str;
197 GValue value = G_VALUE_INIT;
198
199 g_return_if_fail (G_IS_OBJECT (object));
200 g_return_if_fail (G_IS_PARAM_SPEC (pspec));
201
202 g_value_init (&value, pspec->value_type);
203 g_object_get_property (object, pspec->name, &value);
204
205 str = g_string_new (NULL);
206
207 if (gimp_config_serialize_value (&value, str, TRUE))
208 {
209 g_print (" %s -> %s\n", pspec->name, str->str);
210 }
211 else
212 {
213 g_print (" %s changed but we failed to serialize its value!\n",
214 pspec->name);
215 }
216
217 g_string_free (str, TRUE);
218 g_value_unset (&value);
219 }
220
221 static void
output_unknown_token(const gchar * key,const gchar * value,gpointer data)222 output_unknown_token (const gchar *key,
223 const gchar *value,
224 gpointer data)
225 {
226 gchar **header = (gchar **) data;
227 gchar *escaped = g_strescape (value, NULL);
228
229 if (*header)
230 {
231 g_print ("%s", *header);
232 *header = NULL;
233 }
234
235 g_print (" %s \"%s\"\n", key, escaped);
236
237 g_free (escaped);
238 }
239
240
241 /* minimal dummy units implementation */
242
243 static const gchar *
unit_get_identifier(GimpUnit unit)244 unit_get_identifier (GimpUnit unit)
245 {
246 switch (unit)
247 {
248 case GIMP_UNIT_PIXEL:
249 return "pixels";
250 case GIMP_UNIT_INCH:
251 return "inches";
252 case GIMP_UNIT_MM:
253 return "millimeters";
254 case GIMP_UNIT_POINT:
255 return "points";
256 case GIMP_UNIT_PICA:
257 return "picas";
258 default:
259 return NULL;
260 }
261 }
262
263 static gint
unit_get_number_of_units(void)264 unit_get_number_of_units (void)
265 {
266 return GIMP_UNIT_END;
267 }
268
269 static void
units_init(void)270 units_init (void)
271 {
272 GimpUnitVtable vtable;
273
274 vtable.unit_get_number_of_units = unit_get_number_of_units;
275 vtable.unit_get_identifier = unit_get_identifier;
276
277 gimp_base_init (&vtable);
278 }
279