1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
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.
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>
35 /* #include "class_gen.h" */
39 #define ICON_FILE "class_logo.xpm"
41 static gpointer parent_class
;
44 project_root_added (AnjutaPlugin
*plugin
,
45 G_GNUC_UNUSED
const gchar
*name
,
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
);
57 gchar
*root_dir
= gnome_vfs_get_local_path_from_uri (root_uri
);
59 cg_plugin
->top_dir
= g_strdup(root_dir
);
61 cg_plugin
->top_dir
= NULL
;
65 cg_plugin
->top_dir
= NULL
;
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
;
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(
99 _("Could not find autogen version 5, please install the "
100 "autogen package. You can get it from "
101 "http://autogen.sourceforge.net"));
106 /* set up project directory watch */
107 cg_plugin
->root_watch_id
= anjuta_plugin_add_watch (plugin
,
110 project_root_removed
, NULL
);
116 deactivate_plugin (AnjutaPlugin
*plugin
)
118 AnjutaClassGenPlugin
*cg_plugin
;
119 cg_plugin
= ANJUTA_PLUGIN_CLASS_GEN (plugin
);
120 DEBUG_PRINT ("AnjutaClassGenPlugin: Deactivating ClassGen plugin ...");
123 anjuta_plugin_remove_watch (plugin
, cg_plugin
->root_watch_id
, TRUE
);
129 dispose (GObject
*obj
)
131 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
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
));
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
;
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
;
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
;
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 ();
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
,
206 added_files
= ianjuta_project_manager_add_sources (manager
, filenames
,
210 if (g_list_length (added_files
) != 2)
212 for (node
= added_files
; node
!= NULL
; node
= g_list_next (node
))
219 *new_header_file
= added_files
->data
;
220 *new_source_file
= g_list_next (added_files
)->data
;
227 g_list_free (added_files
);
228 g_list_free (filenames
);
234 cg_plugin_add_to_repository (AnjutaClassGenPlugin
*plugin
,
235 const gchar
*header_file
,
236 const gchar
*source_file
)
239 vcs
= anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
244 ianjuta_vcs_add (vcs
, header_file
, NULL
);
245 ianjuta_vcs_add (vcs
, source_file
, NULL
);
250 cg_plugin_generator_error_cb (G_GNUC_UNUSED CgGenerator
*generator
,
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
);
266 cg_plugin_load (AnjutaClassGenPlugin
*plugin
,
267 CgGenerator
*generator
,
271 IAnjutaDocumentManager
*docman
;
272 IAnjutaEditor
*editor
;
277 docman
= anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
278 IAnjutaDocumentManager
, NULL
);
280 if(g_file_get_contents(file
, &contents
, NULL
, error
) == FALSE
)
283 name
= g_path_get_basename (file
);
286 /* The content argument seems not to work */
287 editor
= ianjuta_document_manager_add_buffer (docman
, name
, "", error
);
291 ianjuta_editor_append(editor
, contents
, -1, error
);
292 if(!error
|| *error
== NULL
)
303 cg_plugin_generator_created_cb (CgGenerator
*generator
,
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
);
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
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
;
342 cg_plugin_window_response_cb (G_GNUC_UNUSED GtkDialog
*dialog
,
346 AnjutaClassGenPlugin
*plugin
;
347 IAnjutaProjectManager
*manager
;
348 NPWValueHeap
*values
;
357 plugin
= (AnjutaClassGenPlugin
*) user_data
;
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
);
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
);
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
);
396 name
= g_path_get_basename (cg_window_get_source_file(
398 value
= npw_value_heap_find_value (values
, "ProjectName");
399 npw_value_heap_set_value (values
, value
, name
, NPW_VALID_VALUE
);
403 plugin
->generator
= cg_generator_new (
404 cg_window_get_header_template(plugin
->window
),
405 cg_window_get_source_template(plugin
->window
),
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
);
420 g_signal_connect (G_OBJECT (plugin
->generator
), "error",
421 G_CALLBACK (cg_plugin_generator_error_cb
),
424 g_signal_connect (G_OBJECT (plugin
->generator
), "created",
425 G_CALLBACK (cg_plugin_generator_created_cb
),
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
);
439 g_object_unref (G_OBJECT (plugin
->window
));
440 plugin
->window
= NULL
;
445 iwizard_activate (IAnjutaWizard
*wiz
, G_GNUC_UNUSED GError
**err
)
447 /* IAnjutaProjectManager *pm; */
448 AnjutaClassGenPlugin
*cg_plugin
;
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
,
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
);
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
);
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
),
498 gtk_widget_show (GTK_WIDGET (cg_window_get_dialog (cg_plugin
->window
)));
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
);
511 ANJUTA_SIMPLE_PLUGIN (AnjutaClassGenPlugin
, class_gen_plugin
);