1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * Copyright (C) 2006 Armin Burgmeier
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "generator.h"
22 #include <plugins/project-wizard/autogen.h>
24 #include <libanjuta/anjuta-marshal.h>
26 #include <glib/gstdio.h>
28 typedef struct _CgGeneratorPrivate CgGeneratorPrivate
;
29 struct _CgGeneratorPrivate
33 gchar
*header_template
;
34 gchar
*source_template
;
35 gchar
*header_destination
;
36 gchar
*source_destination
;
39 #define CG_GENERATOR_PRIVATE(o) \
40 (G_TYPE_INSTANCE_GET_PRIVATE( \
52 PROP_HEADER_DESTINATION
,
53 PROP_SOURCE_DESTINATION
64 static GObjectClass
*parent_class
= NULL
;
65 static guint generator_signals
[LAST_SIGNAL
] = { 0 };
68 cg_generator_make_absolute (const gchar
*path
)
73 /* TODO: Use gnome-vfs stuff here? */
74 if (g_path_is_absolute (path
))
76 abs_path
= g_strdup (path
);
80 current_dir
= g_get_current_dir ();
81 abs_path
= g_build_filename (current_dir
, path
, NULL
);
89 cg_generator_autogen_source_func (NPWAutogen
*autogen
,
92 CgGenerator
*generator
;
93 CgGeneratorPrivate
*priv
;
97 generator
= CG_GENERATOR (user_data
);
98 priv
= CG_GENERATOR_PRIVATE (generator
);
101 if (g_file_test (priv
->header_destination
,
102 G_FILE_TEST_IS_REGULAR
) == FALSE
)
104 if (g_file_test (priv
->source_destination
,
105 G_FILE_TEST_IS_REGULAR
) == TRUE
)
107 g_unlink (priv
->source_destination
);
112 else if (g_file_test (priv
->source_destination
,
113 G_FILE_TEST_IS_REGULAR
) == FALSE
)
115 /* It is guaranteed that header file is a regular file, otherwise
116 * the else branch would not have been executed */
117 g_unlink (priv
->source_destination
);
123 /* TODO: Check contents of destination file for autogen error */
124 g_signal_emit(G_OBJECT(generator
), generator_signals
[CREATED
], 0);
130 g_set_error(&error
, g_quark_from_static_string("CG_GENERATOR_ERROR"),
131 CG_GENERATOR_ERROR_NOT_GENERATED
,
132 _("Header or source file has not been created"));
134 g_signal_emit (G_OBJECT (generator
), generator_signals
[ERROR
],
136 g_error_free (error
);
141 cg_generator_autogen_header_func (NPWAutogen
*autogen
,
144 CgGenerator
*generator
;
145 CgGeneratorPrivate
*priv
;
149 generator
= CG_GENERATOR (user_data
);
150 priv
= CG_GENERATOR_PRIVATE (generator
);
154 npw_autogen_set_input_file (priv
->autogen
, priv
->source_template
, NULL
, NULL
); /*"[+", "+]");*/
155 npw_autogen_set_output_file (priv
->autogen
, priv
->source_destination
);
157 result
= npw_autogen_execute (priv
->autogen
,
158 cg_generator_autogen_source_func
,
163 g_signal_emit (G_OBJECT(generator
), generator_signals
[ERROR
],
165 g_error_free (error
);
170 cg_generator_init (CgGenerator
*generator
)
172 CgGeneratorPrivate
*priv
;
173 priv
= CG_GENERATOR_PRIVATE (generator
);
175 priv
->autogen
= npw_autogen_new ();
177 priv
->header_template
= NULL
;
178 priv
->source_template
= NULL
;
179 priv
->header_destination
= NULL
;
180 priv
->source_destination
= NULL
;
184 cg_generator_finalize (GObject
*object
)
186 CgGenerator
*generator
;
187 CgGeneratorPrivate
*priv
;
189 generator
= CG_GENERATOR (object
);
190 priv
= CG_GENERATOR_PRIVATE (generator
);
192 npw_autogen_free (priv
->autogen
);
194 g_free (priv
->header_template
);
195 g_free (priv
->source_template
);
196 g_free (priv
->header_destination
);
197 g_free (priv
->source_destination
);
199 G_OBJECT_CLASS (parent_class
)->finalize (object
);
203 cg_generator_set_property (GObject
*object
,
208 CgGenerator
*generator
;
209 CgGeneratorPrivate
*priv
;
211 g_return_if_fail (CG_IS_GENERATOR (object
));
213 generator
= CG_GENERATOR (object
);
214 priv
= CG_GENERATOR_PRIVATE (generator
);
218 case PROP_HEADER_TEMPLATE
:
219 g_free (priv
->header_template
);
220 priv
->header_template
=
221 cg_generator_make_absolute (g_value_get_string (value
));
223 case PROP_SOURCE_TEMPLATE
:
224 g_free (priv
->source_template
);
225 priv
->source_template
=
226 cg_generator_make_absolute (g_value_get_string (value
));
228 case PROP_HEADER_DESTINATION
:
229 g_free (priv
->header_destination
);
230 priv
->header_destination
=
231 cg_generator_make_absolute (g_value_get_string (value
));
233 case PROP_SOURCE_DESTINATION
:
234 g_free (priv
->source_destination
);
235 priv
->source_destination
=
236 cg_generator_make_absolute (g_value_get_string (value
));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
245 cg_generator_get_property (GObject
*object
,
250 CgGenerator
*generator
;
251 CgGeneratorPrivate
*priv
;
253 g_return_if_fail (CG_IS_GENERATOR (object
));
255 generator
= CG_GENERATOR (object
);
256 priv
= CG_GENERATOR_PRIVATE (generator
);
260 case PROP_HEADER_TEMPLATE
:
261 g_value_set_string (value
, priv
->header_template
);
263 case PROP_SOURCE_TEMPLATE
:
264 g_value_set_string (value
, priv
->source_template
);
266 case PROP_HEADER_DESTINATION
:
267 g_value_set_string (value
, priv
->header_destination
);
269 case PROP_SOURCE_DESTINATION
:
270 g_value_set_string (value
, priv
->source_destination
);
273 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
279 cg_generator_class_init (CgGeneratorClass
*klass
)
281 GObjectClass
*object_class
= G_OBJECT_CLASS (klass
);
282 parent_class
= g_type_class_peek_parent (klass
);
284 g_type_class_add_private(klass
, sizeof(CgGeneratorPrivate
));
286 object_class
->finalize
= cg_generator_finalize
;
287 object_class
->set_property
= cg_generator_set_property
;
288 object_class
->get_property
= cg_generator_get_property
;
290 g_object_class_install_property (object_class
,
291 PROP_HEADER_TEMPLATE
,
292 g_param_spec_string ("header-template",
293 "Header Template File",
294 _("Autogen template used for the header file"),
296 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
298 g_object_class_install_property (object_class
,
299 PROP_SOURCE_TEMPLATE
,
300 g_param_spec_string ("source-template",
301 "Source Template File",
302 _("Autogen template used for the implementation file"),
304 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
306 g_object_class_install_property (object_class
,
307 PROP_HEADER_DESTINATION
,
308 g_param_spec_string ("header-destination",
309 "Header Target File",
310 _("File to which the processed template will be written"),
312 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
314 g_object_class_install_property (object_class
,
315 PROP_SOURCE_DESTINATION
,
316 g_param_spec_string ("source-destination",
317 "Source Target File",
318 _("File to which the processed template will be written"),
320 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
322 generator_signals
[CREATED
] =
323 g_signal_new ("created",
324 G_OBJECT_CLASS_TYPE(object_class
),
326 0, /* no default handler */
328 anjuta_cclosure_marshal_VOID__VOID
,
332 generator_signals
[ERROR
] =
333 g_signal_new ("error",
334 G_OBJECT_CLASS_TYPE(object_class
),
336 0, /* no default handler */
338 anjuta_cclosure_marshal_VOID__POINTER
,
341 G_TYPE_POINTER
); /* GError seems not to have its own GType */
345 cg_generator_get_type (void)
347 static GType our_type
= 0;
351 static const GTypeInfo our_info
=
353 sizeof (CgGeneratorClass
),
354 (GBaseInitFunc
) NULL
,
355 (GBaseFinalizeFunc
) NULL
,
356 (GClassInitFunc
) cg_generator_class_init
,
359 sizeof (CgGenerator
),
361 (GInstanceInitFunc
) cg_generator_init
,
365 our_type
= g_type_register_static (G_TYPE_OBJECT
, "CgGenerator",
373 cg_generator_new (const gchar
*header_template
,
374 const gchar
*source_template
,
375 const gchar
*header_destination
,
376 const gchar
*source_destination
)
380 object
= g_object_new (CG_TYPE_GENERATOR
,
381 "header-template", header_template
,
382 "source-template", source_template
,
383 "header-destination", header_destination
,
384 "source-destination", source_destination
, NULL
);
386 return CG_GENERATOR (object
);
390 cg_generator_run (CgGenerator
*generator
,
391 NPWValueHeap
*values
,
394 CgGeneratorPrivate
*priv
;
395 priv
= CG_GENERATOR_PRIVATE (generator
);
397 /* TODO: npw_autogen_write_definiton_file should take a GError... */
398 if (npw_autogen_write_definition_file (priv
->autogen
, values
) == FALSE
)
400 g_set_error (error
, g_quark_from_static_string("CG_GENERATOR_ERROR"),
401 CG_GENERATOR_ERROR_DEFFILE
,
402 _("Failed to write autogen definition file"));
408 npw_autogen_set_input_file (priv
->autogen
, priv
->header_template
,
409 NULL
, NULL
); /*"[+", "+]");*/
410 npw_autogen_set_output_file (priv
->autogen
, priv
->header_destination
);
412 return npw_autogen_execute (priv
->autogen
,
413 cg_generator_autogen_header_func
,
419 cg_generator_get_header_template (CgGenerator
*generator
)
421 return CG_GENERATOR_PRIVATE (generator
)->header_template
;
425 cg_generator_get_source_template (CgGenerator
*generator
)
427 return CG_GENERATOR_PRIVATE (generator
)->source_template
;
431 cg_generator_get_header_destination (CgGenerator
*generator
)
433 return CG_GENERATOR_PRIVATE (generator
)->header_destination
;
437 cg_generator_get_source_destination(CgGenerator
*generator
)
439 return CG_GENERATOR_PRIVATE (generator
)->source_destination
;