Updated Spanish translation
[anjuta-git-plugin.git] / plugins / class-gen / plugin.c
blob01637cb91bf5813f29cc2e5137e2fcaaf3f27955
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /* plugin.c
3 * Copyright (C) 2005 Massimo Cora'
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 <config.h>
21 #include <gtk/gtkactiongroup.h>
22 #include <libgnomevfs/gnome-vfs-utils.h>
23 #include <libgnome/gnome-i18n.h>
24 #include <libanjuta/anjuta-preferences.h>
25 #include <libanjuta/anjuta-debug.h>
26 #include <libanjuta/interfaces/ianjuta-wizard.h>
27 #include <libanjuta/interfaces/ianjuta-project-manager.h>
28 #include <libanjuta/interfaces/ianjuta-file-loader.h>
29 #include <libanjuta/interfaces/ianjuta-vcs.h>
30 #include <libanjuta/interfaces/ianjuta-document-manager.h>
32 #include <plugins/project-wizard/autogen.h>
34 #include "plugin.h"
35 /* #include "class_gen.h" */
37 #include "window.h"
39 #define ICON_FILE "class_logo.xpm"
41 static gpointer parent_class;
43 static void
44 project_root_added (AnjutaPlugin *plugin,
45 G_GNUC_UNUSED const gchar *name,
46 const GValue *value,
47 G_GNUC_UNUSED gpointer user_data)
49 AnjutaClassGenPlugin *cg_plugin;
50 const gchar *root_uri;
52 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (plugin);
53 root_uri = g_value_get_string (value);
55 if (root_uri)
57 gchar *root_dir = gnome_vfs_get_local_path_from_uri (root_uri);
58 if (root_dir)
59 cg_plugin->top_dir = g_strdup(root_dir);
60 else
61 cg_plugin->top_dir = NULL;
62 g_free (root_dir);
64 else
65 cg_plugin->top_dir = NULL;
68 static void
69 project_root_removed (AnjutaPlugin *plugin,
70 G_GNUC_UNUSED const gchar *name,
71 G_GNUC_UNUSED gpointer user_data)
73 AnjutaClassGenPlugin *cg_plugin;
74 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (plugin);
76 if (cg_plugin->top_dir)
77 g_free(cg_plugin->top_dir);
78 cg_plugin->top_dir = NULL;
81 static gboolean
82 activate_plugin (AnjutaPlugin *plugin)
84 AnjutaClassGenPlugin *cg_plugin;
86 DEBUG_PRINT ("AnjutaClassGenPlugin: Activating ClassGen plugin...");
87 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (plugin);
88 cg_plugin->prefs = anjuta_shell_get_preferences (plugin->shell, NULL);
90 g_return_val_if_fail (cg_plugin->prefs != NULL, FALSE);
92 cg_plugin->top_dir = NULL;
94 /* Check if autogen is present */
95 if(!npw_check_autogen())
97 anjuta_util_dialog_error(
98 NULL,
99 _("Could not find autogen version 5, please install the "
100 "autogen package. You can get it from "
101 "http://autogen.sourceforge.net"));
103 return FALSE;
106 /* set up project directory watch */
107 cg_plugin->root_watch_id = anjuta_plugin_add_watch (plugin,
108 "project_root_uri",
109 project_root_added,
110 project_root_removed, NULL);
112 return TRUE;
115 static gboolean
116 deactivate_plugin (AnjutaPlugin *plugin)
118 AnjutaClassGenPlugin *cg_plugin;
119 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (plugin);
120 DEBUG_PRINT ("AnjutaClassGenPlugin: Deactivating ClassGen plugin ...");
122 /* Remove watches */
123 anjuta_plugin_remove_watch (plugin, cg_plugin->root_watch_id, TRUE);
125 return TRUE;
128 static void
129 dispose (GObject *obj)
131 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
134 static void
135 finalize (GObject *obj)
137 AnjutaClassGenPlugin *cg_plugin;
138 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (obj);
139 g_free (cg_plugin->top_dir);
141 if(cg_plugin->window != NULL)
142 g_object_unref(G_OBJECT(cg_plugin->window));
143 if(cg_plugin->generator != NULL)
144 g_object_unref(G_OBJECT(cg_plugin->generator));
146 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
149 static void
150 class_gen_plugin_class_init (GObjectClass *klass)
152 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
154 parent_class = g_type_class_peek_parent (klass);
156 plugin_class->activate = activate_plugin;
157 plugin_class->deactivate = deactivate_plugin;
158 klass->dispose = dispose;
159 klass->finalize = finalize;
162 static void
163 class_gen_plugin_instance_init (GObject *obj)
165 AnjutaClassGenPlugin *plugin = ANJUTA_PLUGIN_CLASS_GEN (obj);
166 plugin->root_watch_id = 0;
167 plugin->top_dir = NULL;
168 plugin->window = NULL;
169 plugin->generator = NULL;
172 static gboolean
173 cg_plugin_add_to_project (AnjutaClassGenPlugin *plugin,
174 const gchar *header_file,
175 const gchar *source_file,
176 gchar **new_header_file,
177 gchar **new_source_file)
179 IAnjutaProjectManager *manager;
180 GList *filenames;
181 GList *added_files;
182 GList *node;
183 gchar *dirname;
184 gchar *curdir;
185 gboolean result;
187 manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
188 IAnjutaProjectManager, NULL);
190 if (manager == NULL) return FALSE;
192 curdir = g_get_current_dir ();
194 filenames = NULL;
195 filenames = g_list_append (filenames, g_path_get_basename (header_file));
196 filenames = g_list_append (filenames, g_path_get_basename (source_file));
197 dirname = g_path_get_dirname (source_file);
199 if (dirname != NULL && strcmp (dirname, ".") != 0)
201 added_files = ianjuta_project_manager_add_sources (manager, filenames,
202 dirname, NULL);
204 else
206 added_files = ianjuta_project_manager_add_sources (manager, filenames,
207 curdir, NULL);
210 if (g_list_length (added_files) != 2)
212 for (node = added_files; node != NULL; node = g_list_next (node))
213 g_free (node->data);
215 result = FALSE;
217 else
219 *new_header_file = added_files->data;
220 *new_source_file = g_list_next (added_files)->data;
222 result = TRUE;
225 g_free (curdir);
226 g_free (dirname);
227 g_list_free (added_files);
228 g_list_free (filenames);
230 return result;
233 static void
234 cg_plugin_add_to_repository (AnjutaClassGenPlugin *plugin,
235 const gchar *header_file,
236 const gchar *source_file)
238 IAnjutaVcs *vcs;
239 vcs = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
240 IAnjutaVcs, NULL);
242 if(vcs != NULL)
244 ianjuta_vcs_add (vcs, header_file, NULL);
245 ianjuta_vcs_add (vcs, source_file, NULL);
249 static void
250 cg_plugin_generator_error_cb (G_GNUC_UNUSED CgGenerator *generator,
251 GError *error,
252 gpointer user_data)
254 AnjutaClassGenPlugin *plugin;
255 plugin = (AnjutaClassGenPlugin *) user_data;
257 anjuta_util_dialog_error (
258 GTK_WINDOW (cg_window_get_dialog (plugin->window)),
259 _("Failed to execute autogen: %s"), error->message);
261 gtk_widget_set_sensitive (
262 GTK_WIDGET (cg_window_get_dialog (plugin->window)), TRUE);
265 static gboolean
266 cg_plugin_load (AnjutaClassGenPlugin *plugin,
267 CgGenerator *generator,
268 const gchar *file,
269 GError **error)
271 IAnjutaDocumentManager *docman;
272 IAnjutaEditor *editor;
273 gchar *name;
274 gchar *contents;
275 gboolean result;
277 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
278 IAnjutaDocumentManager, NULL);
280 if(g_file_get_contents(file, &contents, NULL, error) == FALSE)
281 return FALSE;
283 name = g_path_get_basename (file);
285 result = FALSE;
286 /* The content argument seems not to work */
287 editor = ianjuta_document_manager_add_buffer (docman, name, "", error);
289 if(editor != NULL)
291 ianjuta_editor_append(editor, contents, -1, error);
292 if(!error || *error == NULL)
293 result = TRUE;
296 g_free(contents);
297 g_free(name);
299 return result;
302 static void
303 cg_plugin_generator_created_cb (CgGenerator *generator,
304 gpointer user_data)
306 AnjutaClassGenPlugin *plugin;
307 const gchar *header_file;
308 const gchar *source_file;
309 IAnjutaFileLoader *loader;
311 plugin = (AnjutaClassGenPlugin *) user_data;
312 header_file = cg_generator_get_header_destination (generator);
313 source_file = cg_generator_get_source_destination (generator);
315 loader = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
316 IAnjutaFileLoader, NULL);
318 if (cg_window_get_add_to_repository (plugin->window))
320 cg_plugin_add_to_repository (plugin, header_file, source_file);
323 if (cg_window_get_add_to_project (plugin->window))
325 ianjuta_file_loader_load (loader, header_file, FALSE, NULL);
326 ianjuta_file_loader_load (loader, source_file, FALSE, NULL);
328 else
330 /* We do not just use ianjuta_file_leader_load here to ensure that
331 * the new documents are flagged as changed and no path is
332 * already set. */
333 cg_plugin_load (plugin, generator, header_file, NULL);
334 cg_plugin_load (plugin, generator, source_file, NULL);
337 g_object_unref (G_OBJECT (plugin->window));
338 plugin->window = NULL;
341 static void
342 cg_plugin_window_response_cb (G_GNUC_UNUSED GtkDialog *dialog,
343 gint response_id,
344 gpointer user_data)
346 AnjutaClassGenPlugin *plugin;
347 IAnjutaProjectManager *manager;
348 NPWValueHeap *values;
349 NPWValue *value;
350 GError *error;
351 gchar *name;
353 gchar *header_file;
354 gchar *source_file;
355 gboolean result;
357 plugin = (AnjutaClassGenPlugin *) user_data;
358 error = NULL;
360 if (response_id == GTK_RESPONSE_ACCEPT)
362 if (cg_window_get_add_to_project (plugin->window))
364 result = cg_plugin_add_to_project (
365 plugin, cg_window_get_header_file (plugin->window),
366 cg_window_get_source_file (plugin->window),
367 &header_file, &source_file);
369 else
371 header_file = g_build_filename (g_get_tmp_dir (),
372 cg_window_get_header_file (plugin->window), NULL);
373 source_file = g_build_filename (g_get_tmp_dir (),
374 cg_window_get_source_file (plugin->window), NULL);
376 result = TRUE;
379 if(result == TRUE)
381 values = cg_window_create_value_heap (plugin->window);
383 manager = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
384 IAnjutaProjectManager, NULL);
386 if (manager != NULL && plugin->top_dir != NULL)
388 /* Use basename of the project's root URI as project name. */
389 name = g_path_get_basename (plugin->top_dir);
390 value = npw_value_heap_find_value (values, "ProjectName");
391 npw_value_heap_set_value (values, value, name, NPW_VALID_VALUE);
392 g_free (name);
394 else
396 name = g_path_get_basename (cg_window_get_source_file(
397 plugin->window));
398 value = npw_value_heap_find_value (values, "ProjectName");
399 npw_value_heap_set_value (values, value, name, NPW_VALID_VALUE);
400 g_free (name);
403 plugin->generator = cg_generator_new (
404 cg_window_get_header_template(plugin->window),
405 cg_window_get_source_template(plugin->window),
406 header_file,
407 source_file);
409 if (cg_generator_run (plugin->generator, values, &error) == FALSE)
411 anjuta_util_dialog_error (
412 GTK_WINDOW (cg_window_get_dialog (plugin->window)),
413 _("Failed to execute autogen: %s"), error->message);
415 g_object_unref (G_OBJECT (plugin->generator));
416 g_error_free (error);
418 else
420 g_signal_connect (G_OBJECT (plugin->generator), "error",
421 G_CALLBACK (cg_plugin_generator_error_cb),
422 plugin);
424 g_signal_connect (G_OBJECT (plugin->generator), "created",
425 G_CALLBACK (cg_plugin_generator_created_cb),
426 plugin);
428 gtk_widget_set_sensitive (
429 GTK_WIDGET (cg_window_get_dialog (plugin->window)), FALSE);
432 npw_value_heap_free (values);
433 g_free (header_file);
434 g_free (source_file);
437 else
439 g_object_unref (G_OBJECT (plugin->window));
440 plugin->window = NULL;
444 static void
445 iwizard_activate (IAnjutaWizard *wiz, G_GNUC_UNUSED GError **err)
447 /* IAnjutaProjectManager *pm; */
448 AnjutaClassGenPlugin *cg_plugin;
449 gchar *user_name;
450 gchar *user_email;
451 IAnjutaProjectManagerCapabilities caps =
452 IANJUTA_PROJECT_MANAGER_CAN_ADD_NONE;
454 cg_plugin = ANJUTA_PLUGIN_CLASS_GEN (wiz);
456 if (cg_plugin->window != NULL)
457 g_object_unref (G_OBJECT (cg_plugin->window));
459 cg_plugin->window = cg_window_new ();
461 user_name = anjuta_preferences_get (cg_plugin->prefs,
462 "anjuta.user.name");
463 user_email = anjuta_preferences_get (cg_plugin->prefs,
464 "anjuta.user.email");
466 if (user_name != NULL)
467 cg_window_set_author (cg_plugin->window, user_name);
469 if (user_email != NULL)
470 cg_window_set_email (cg_plugin->window, user_email);
472 g_free(user_name);
473 g_free(user_email);
475 /* Check whether we have a loaded project and it can add sources */
476 if (cg_plugin->top_dir)
478 IAnjutaProjectManager *manager =
479 anjuta_shell_get_interface (ANJUTA_PLUGIN (wiz)->shell,
480 IAnjutaProjectManager, NULL);
481 if (manager)
482 caps = ianjuta_project_manager_get_capabilities (manager, NULL);
485 if((caps & IANJUTA_PROJECT_MANAGER_CAN_ADD_SOURCE) == FALSE)
487 cg_window_set_add_to_project (cg_plugin->window, FALSE);
488 cg_window_enable_add_to_project (cg_plugin->window, FALSE);
491 /* TODO: Check whether the project is in version control, and enable
492 * "add to repository" button respectively. */
494 g_signal_connect (G_OBJECT (cg_window_get_dialog(cg_plugin->window)),
495 "response", G_CALLBACK (cg_plugin_window_response_cb),
496 cg_plugin);
498 gtk_widget_show (GTK_WIDGET (cg_window_get_dialog (cg_plugin->window)));
501 static void
502 iwizard_iface_init (IAnjutaWizardIface *iface)
504 iface->activate = iwizard_activate;
507 ANJUTA_PLUGIN_BEGIN (AnjutaClassGenPlugin, class_gen_plugin);
508 ANJUTA_PLUGIN_ADD_INTERFACE(iwizard, IANJUTA_TYPE_WIZARD);
509 ANJUTA_PLUGIN_END;
511 ANJUTA_SIMPLE_PLUGIN (AnjutaClassGenPlugin, class_gen_plugin);