Updated Spanish translation
[anjuta-git-plugin.git] / plugins / project-wizard / druid.c
blob66080e9215c82490bea9c1b100aebd28da067846
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 druid.c
4 Copyright (C) 2004 Sebastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * All GUI functions
24 *---------------------------------------------------------------------------*/
26 #include <config.h>
28 #include "druid.h"
30 #include "plugin.h"
31 #include "header.h"
32 #include "property.h"
33 #include "parser.h"
34 #include "install.h"
35 #include "autogen.h"
37 #include <libanjuta/anjuta-debug.h>
38 #include <libanjuta/interfaces/ianjuta-wizard.h>
40 #include <gnome.h>
41 #include <libgnome/gnome-i18n.h>
43 #include <gtk/gtkactiongroup.h>
45 #include <string.h>
47 /*---------------------------------------------------------------------------*/
49 #define PROJECT_WIZARD_DIRECTORY PACKAGE_DATA_DIR"/project"
50 #define LOCAL_PROJECT_WIZARD_DIRECTORY "/.anjuta/project"
51 #define PIXMAP_APPWIZ_LOGO PACKAGE_DATA_DIR"/glade/applogo.png"
52 #define PIXMAP_APPWIZ_WATERMARK PACKAGE_DATA_DIR"/glade/appwizard.png"
54 /* Default property name useable in wizard file
55 *---------------------------------------------------------------------------*/
57 #define ANJUTA_PROJECT_DIRECTORY_PROPERTY "AnjutaProjectDirectory"
58 #define USER_NAME_PROPERTY "UserName"
59 #define EMAIL_ADDRESS_PROPERTY "EmailAddress"
61 /* Widget and signal name found in glade file
62 *---------------------------------------------------------------------------*/
64 #define NEW_PROJECT_DIALOG "druid_window"
65 #define PROJECT_SELECTION_LIST "project_list"
66 #define PROJECT_SELECTION_SCROLL_LIST "project_scroll_list"
67 #define PROJECT_SELECTION_FRAME "project_table"
68 #define PROJECT_SELECTION_BOOK "project_book"
69 #define PROJECT_SELECTION_BOOK_LABEL "project_book_label"
70 #define PROJECT_DESCRIPTION "project_description"
71 #define DRUID_WIDGET "druid"
72 #define DRUID_START_PAGE "start_page"
73 #define DRUID_SELECTION_PAGE "selection_page"
74 #define DRUID_PROPERTY_PAGE "property_page"
75 #define DRUID_PROPERTY_LABEL "property_label"
76 #define DRUID_PROPERTY_TABLE "property_table"
77 #define DRUID_FINISH_PAGE "finish_page"
79 #define DRUID_DELETE_SIGNAL "on_druid_delete"
80 #define DRUID_CANCEL_SIGNAL "on_druid_cancel"
81 #define DRUID_BACK_SIGNAL "on_druid_back"
82 #define DRUID_NEXT_SIGNAL "on_druid_next"
83 #define DRUID_FINISH_SIGNAL "on_druid_finish"
84 #define DRUID_PROJECT_SELECT_ICON_SIGNAL "on_druid_project_select_icon"
85 #define DRUID_PROJECT_SELECT_PAGE_SIGNAL "on_druid_project_map_page"
87 /*---------------------------------------------------------------------------*/
89 struct _NPWDruid
91 GtkWidget* dialog;
92 GtkNotebook* project_book;
93 GMemChunk* pool;
94 GnomeDruid* druid;
95 const gchar* project_file;
96 GnomeDruidPage* selection_page;
97 GnomeDruidPageStandard* property_page;
98 GtkLabel* property_label;
99 GtkTable* property_table;
100 GnomeDruidPage* finish_page;
101 GtkTooltips *tooltips;
102 NPWPlugin* plugin;
104 guint page;
105 GQueue* page_list;
106 NPWValueHeap* values;
107 NPWPageParser* parser;
108 NPWHeaderList* header_list;
109 NPWHeader* header;
110 NPWAutogen* gen;
111 gboolean busy;
114 typedef struct _NPWDruidAndTextBuffer
116 NPWDruid* druid;
117 GtkLabel* description;
118 NPWHeader* header;
119 } NPWDruidAndTextBuffer;
121 typedef union _NPWDruidStuff
123 NPWDruidAndTextBuffer a;
124 } NPWDruidStuff;
127 /* Create start and finish pages
128 *---------------------------------------------------------------------------*/
130 /* libGlade doesn't seem to set these parameters for the start and end page */
131 static void
132 npw_druid_fix_edge_pages (NPWDruid* this, GladeXML* xml)
134 GdkColor bg_color = {0, 15616, 33280, 46848};
135 GtkWidget* page;
136 GdkPixbuf* pixbuf;
138 /* Start page */
139 page = glade_xml_get_widget (xml, DRUID_START_PAGE);
140 gnome_druid_page_edge_set_bg_color (GNOME_DRUID_PAGE_EDGE (page), &bg_color);
141 gnome_druid_page_edge_set_logo_bg_color (GNOME_DRUID_PAGE_EDGE (page), &bg_color);
142 pixbuf = gdk_pixbuf_new_from_file (PIXMAP_APPWIZ_WATERMARK, NULL);
143 gnome_druid_page_edge_set_watermark (GNOME_DRUID_PAGE_EDGE (page), pixbuf);
144 g_object_unref (pixbuf);
145 pixbuf = gdk_pixbuf_new_from_file (PIXMAP_APPWIZ_LOGO, NULL);
146 gnome_druid_page_edge_set_logo (GNOME_DRUID_PAGE_EDGE (page), pixbuf);
148 /* Finish page */
149 page = glade_xml_get_widget (xml, DRUID_FINISH_PAGE);
150 gnome_druid_page_edge_set_bg_color (GNOME_DRUID_PAGE_EDGE (page), &bg_color);
151 gnome_druid_page_edge_set_logo_bg_color (GNOME_DRUID_PAGE_EDGE (page), &bg_color);
152 gnome_druid_page_edge_set_logo (GNOME_DRUID_PAGE_EDGE (page), pixbuf);
153 g_object_unref (pixbuf);
156 static void
157 cb_druid_add_summary_property (NPWProperty* property, gpointer user_data)
159 GString* text = (GString*)user_data;
161 if (npw_property_get_options (property) & NPW_SUMMARY_OPTION)
163 g_string_append (text, _(npw_property_get_label (property)));
164 g_string_append (text, npw_property_get_value (property));
165 g_string_append (text, "\n");
169 /* Fill last page (summary) */
170 static void
171 npw_druid_fill_summary (NPWDruid* this)
173 NPWPage* page;
174 guint i;
175 GString* text;
177 text = g_string_new (_("Confim the following information:\n\n"));
179 g_string_append (text,_("Project Type: "));
180 g_string_append (text, _(npw_header_get_name (this->header)));
181 g_string_append (text,"\n");
183 for (i = 0; (page = g_queue_peek_nth (this->page_list, i)) != NULL; ++i)
185 npw_page_foreach_property (page, cb_druid_add_summary_property, text);
188 gnome_druid_page_edge_set_text (GNOME_DRUID_PAGE_EDGE (this->finish_page), text->str);
189 g_string_free (text, TRUE);
193 /* Create project selection page
194 *---------------------------------------------------------------------------*/
196 /* Display project information */
197 static void
198 on_druid_project_select_icon (GnomeIconList* iconlist, gint idx, GdkEvent* event, gpointer user_data)
200 NPWHeader* header;
201 NPWDruidAndTextBuffer* data = (NPWDruidAndTextBuffer *)user_data;
203 header = (NPWHeader*)gnome_icon_list_get_icon_data (iconlist, idx);
204 data->header = header;
206 /* As the icon list is in Browse mode, this function is called after inserting the
207 * first item but before setting the associated data, so header could be NULL */
208 if (header != NULL)
210 gtk_label_set_text (data->description, _(npw_header_get_description (header)));
211 data->druid->header = header;
215 /* Reselect default icon in page */
216 static void
217 on_druid_project_select_page (GtkWidget* widget, gpointer user_data)
219 /* NPWHeader* header; */
220 NPWDruidAndTextBuffer* data = (NPWDruidAndTextBuffer *)user_data;
222 g_return_if_fail (data->header != NULL);
224 gtk_label_set_text (data->description, _(npw_header_get_description (data->header)));
225 data->druid->header = data->header;
228 /* Add a project in the icon list */
229 static void
230 cb_druid_insert_project_icon (NPWHeader* header, gpointer user_data)
232 gint idx;
233 GnomeIconList* list = GNOME_ICON_LIST (user_data);
235 idx = gnome_icon_list_append (list, npw_header_get_iconfile (header), _(npw_header_get_name (header)));
236 gnome_icon_list_set_icon_data (list, idx, header);
239 /* Add a page in the project notebook */
240 static void
241 cb_druid_insert_project_page (NPWHeader* header, gpointer user_data)
243 NPWDruid* this = (NPWDruid*)user_data;
244 GladeXML* xml;
245 GtkWidget* label;
246 GtkWidget* frame;
247 GnomeIconList* list;
248 GtkLabel* desc;
249 const gchar* category;
250 NPWDruidAndTextBuffer* data;
252 category = npw_header_get_category(header);
254 /* Create new frame according to glade file */
255 xml = glade_xml_new (GLADE_FILE, PROJECT_SELECTION_FRAME, NULL);
256 g_return_if_fail (xml != NULL);
258 frame = glade_xml_get_widget (xml, PROJECT_SELECTION_FRAME);
259 list = GNOME_ICON_LIST (glade_xml_get_widget (xml, PROJECT_SELECTION_LIST));
260 desc = GTK_LABEL (glade_xml_get_widget (xml, PROJECT_DESCRIPTION));
262 npw_header_list_foreach_project_in (this->header_list, category, cb_druid_insert_project_icon, list);
264 /* put druid address and corresponding text buffer in one structure
265 * to pass them to a call back function as an unique pointer */
266 data = g_chunk_new (NPWDruidAndTextBuffer, this->pool);
267 data->druid = this;
268 data->description = desc;
269 data->header = (NPWHeader*)gnome_icon_list_get_icon_data (list, 0);
270 glade_xml_signal_connect_data (xml, DRUID_PROJECT_SELECT_ICON_SIGNAL, GTK_SIGNAL_FUNC (on_druid_project_select_icon), data);
271 glade_xml_signal_connect_data (xml, DRUID_PROJECT_SELECT_PAGE_SIGNAL, GTK_SIGNAL_FUNC (on_druid_project_select_page), data);
273 g_object_unref (xml);
275 /* Create label */
276 xml = glade_xml_new (GLADE_FILE, PROJECT_SELECTION_BOOK_LABEL, NULL);
277 g_return_if_fail (xml != NULL);
278 label = glade_xml_get_widget (xml, PROJECT_SELECTION_BOOK_LABEL);
279 g_object_unref (xml);
280 gtk_label_set_text (GTK_LABEL(label), category);
282 gtk_notebook_append_page (this->project_book, frame, label);
285 /* Fill project selection page */
286 static gboolean
287 npw_druid_fill_selection_page (NPWDruid* this)
289 gboolean ok;
290 gchar* local_dir;
292 /* Create list of projects */
293 if (this->header_list != NULL) npw_header_list_free (this->header_list);
294 this->header_list = npw_header_list_new ();
296 /* Fill list with all project in directory */
297 ok = npw_header_list_readdir (this->header_list, PROJECT_WIZARD_DIRECTORY);
298 local_dir = g_build_filename (g_get_home_dir(), LOCAL_PROJECT_WIZARD_DIRECTORY, NULL);
299 ok = npw_header_list_readdir (this->header_list, local_dir) || ok;
300 g_free (local_dir);
301 if (!ok)
303 anjuta_util_dialog_error (GTK_WINDOW (ANJUTA_PLUGIN (this->plugin)->shell),_("Unable to find any project template in %s"), PROJECT_WIZARD_DIRECTORY);
304 return FALSE;
307 /* Add all notebook page */
308 gtk_notebook_remove_page(this->project_book, 0);
309 npw_header_list_foreach_category (this->header_list, cb_druid_insert_project_page, this);
311 return TRUE;
314 /* Create properties pages
315 *---------------------------------------------------------------------------*/
317 /* Group infomation need by the following call back function */
318 typedef struct _NPWDruidAddPropertyData
320 NPWDruid* druid;
321 guint row;
322 } NPWDruidAddPropertyData;
324 static void
325 cb_druid_destroy_widget (GtkWidget* widget, gpointer user_data)
327 gtk_widget_destroy (widget);
330 static void
331 cb_druid_add_property (NPWProperty* property, gpointer user_data)
333 GtkWidget* label;
334 GtkWidget* entry;
335 NPWDruidAddPropertyData* data = (NPWDruidAddPropertyData *)user_data;
336 const gchar* description;
339 entry = npw_property_create_widget (property);
340 if (entry != NULL)
342 /* Not hidden property */
343 description = npw_property_get_description (property);
345 /* Set description tooltip */
346 if (description && (*description != '\0'))
348 GtkTooltips *tooltips;
350 tooltips = data->druid->tooltips;
351 if (!tooltips)
353 tooltips = data->druid->tooltips = gtk_tooltips_new ();
354 data->druid->tooltips = tooltips;
355 g_object_ref (tooltips);
356 gtk_object_sink (GTK_OBJECT (tooltips));
358 gtk_tooltips_set_tip (tooltips, entry, description, NULL);
361 /* Add label and entry */
362 gtk_table_resize (data->druid->property_table, data->row + 1, 2);
363 label = gtk_label_new (_(npw_property_get_label (property)));
364 gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
365 gtk_misc_set_padding (GTK_MISC (label), 5, 5);
366 gtk_table_attach (data->druid->property_table, label, 0, 1, data->row, data->row + 1,
367 (GtkAttachOptions)(GTK_FILL), (GtkAttachOptions)(0), 0, 0);
368 gtk_table_attach (data->druid->property_table, entry, 1, 2, data->row, data->row + 1,
369 (GtkAttachOptions)(GTK_EXPAND|GTK_FILL), (GtkAttachOptions)(0), 0, 0);
371 data->row++;
375 static gboolean
376 npw_druid_fill_property_page (NPWDruid* this, NPWPage* page)
378 NPWDruidAddPropertyData data;
379 PangoAttribute* attr;
380 PangoAttrList* attr_list;
382 /* Remove previous widgets */
383 gtk_container_foreach (GTK_CONTAINER (this->property_table), cb_druid_destroy_widget, NULL);
385 /* Update title */
386 gnome_druid_page_standard_set_title (this->property_page, _(npw_page_get_label (page)));
387 gtk_label_set_text (this->property_label, _(npw_page_get_description (page)));
388 attr = pango_attr_weight_new (PANGO_WEIGHT_BOLD);
389 attr->start_index = 0;
390 attr->end_index = G_MAXINT;
391 attr_list = pango_attr_list_new ();
392 pango_attr_list_insert (attr_list, attr);
393 gtk_label_set_attributes (this->property_label, attr_list);
394 pango_attr_list_unref (attr_list);
396 /* Add new widget */
397 data.druid = this;
398 data.row = 0;
399 npw_page_foreach_property (page, cb_druid_add_property, &data);
401 /* Change page only if current is project selection page */
402 gnome_druid_set_page (this->druid, GNOME_DRUID_PAGE (this->property_page));
403 gtk_widget_show_all (this->dialog);
405 return TRUE;
408 /* Handle page cache
409 *---------------------------------------------------------------------------*/
411 static NPWPage*
412 npw_druid_add_new_page (NPWDruid* this)
414 NPWPage* page;
416 /* Get page in cache */
417 page = g_queue_peek_nth (this->page_list, this->page);
418 if (page == NULL)
420 /* Page not found in cache, create */
421 page = npw_page_new (this->values);
423 /* Add page in cache */
424 g_queue_push_tail (this->page_list, page);
427 return page;
430 /* Remove all following page */
432 static void
433 npw_druid_remove_following_page (NPWDruid* this)
435 NPWPage* page;
437 for(;;)
439 page = g_queue_pop_nth (this->page_list, this->page);
440 if (page == NULL) break;
441 npw_page_free (page);
445 /* Save properties
446 *---------------------------------------------------------------------------*/
448 typedef struct _NPWSaveValidPropertyData
450 GtkWindow *parent;
451 gboolean next;
452 gboolean modified;
453 } NPWSaveValidPropertyData;
455 static void
456 cb_save_valid_property (NPWProperty* property, gpointer user_data)
458 NPWSaveValidPropertyData* data = (NPWSaveValidPropertyData *)user_data;
459 const gchar* value;
460 gboolean modified;
462 /* Get value from widget */
463 modified = npw_property_update_value_from_widget (property);
464 if (modified) data->modified = modified;
465 value = npw_property_get_value (property);
467 /* Check mandatory property */
468 if (modified && (npw_property_get_options (property) & NPW_MANDATORY_OPTION))
470 if ((value == NULL) || (strlen (value) <= 0))
472 if (data->next == TRUE)
474 /* First error message */
475 data->next = FALSE;
477 /* Show error message. */
478 anjuta_util_dialog_error (data->parent,
479 _("Field \"%s\" is mandatory. Please enter it."),
480 _(npw_property_get_label (property)));
482 npw_property_remove_value (property);
486 /* Check exist property */
487 if (modified && (npw_property_get_exist_option (property) == NPW_FALSE))
489 gboolean is_directory = npw_property_get_type (property) == NPW_DIRECTORY_PROPERTY;
490 gboolean exist = (value != NULL) && g_file_test (value, G_FILE_TEST_EXISTS);
491 /* Allow empty directory */
492 if (exist && is_directory)
494 GDir* dir;
496 dir = g_dir_open (value, 0, NULL);
497 if (dir != NULL)
499 if (g_dir_read_name (dir) == NULL) exist = FALSE;
500 g_dir_close (dir);
504 if (exist)
506 if (data->next == TRUE)
508 /* First error message */
509 gboolean yes;
511 yes = anjuta_util_dialog_boolean_question (data->parent, is_directory ?
512 _("Directory \"%s\" is not empty. Project creation could fail if some files "
513 "cannot be written. Do you want to continue?") :
514 _("File \"%s\" already exists. Do you want to overwrite it ?"),
515 value);
517 if (!yes)
519 data->next = FALSE;
520 npw_property_remove_value (property);
523 else
525 /* value is not invalid */
526 npw_property_remove_value (property);
532 static void
533 cb_save_old_property (NPWProperty* property, gpointer user_data)
535 if (npw_property_save_value_from_widget (property))
537 *(gboolean *)user_data = TRUE;
541 /* Save values and check them, if it's possible to go to next page */
543 static gboolean
544 npw_druid_save_valid_values (NPWDruid* this)
546 NPWPage* page;
547 NPWSaveValidPropertyData data;
549 page = g_queue_peek_nth (this->page_list, this->page - 1);
550 data.modified = FALSE;
551 data.next = TRUE;
552 data.parent = GTK_WINDOW (this->dialog);
553 npw_page_foreach_property (page, cb_save_valid_property, &data);
555 if (data.modified) npw_druid_remove_following_page (this);
557 return data.next;
560 /* Save current value, but do not check anything */
562 static void
563 npw_druid_save_old_values (NPWDruid* this)
565 NPWPage* page;
566 gboolean modified;
568 page = g_queue_peek_nth (this->page_list, this->page - 1);
569 modified = FALSE;
570 npw_page_foreach_property (page, cb_save_old_property, &modified);
574 /* Druid GUI functions
575 *---------------------------------------------------------------------------*/
577 static void
578 npw_druid_set_busy (NPWDruid *this, gboolean busy_state)
580 if (this->busy == busy_state)
581 return;
583 /* Set busy state */
584 gnome_druid_set_buttons_sensitive (GNOME_DRUID (this->druid),
585 !busy_state, !busy_state,
586 !busy_state, TRUE);
587 if (busy_state)
588 anjuta_status_busy_push (anjuta_shell_get_status (ANJUTA_PLUGIN (this->plugin)->shell, NULL));
589 else
590 anjuta_status_busy_pop (anjuta_shell_get_status (ANJUTA_PLUGIN (this->plugin)->shell, NULL));
591 this->busy = busy_state;
594 /* Druid call backs
595 *---------------------------------------------------------------------------*/
597 static gboolean
598 on_druid_cancel (GtkWidget* window, NPWDruid* druid)
600 DEBUG_PRINT ("Project wizard canceled");
601 anjuta_plugin_deactivate (ANJUTA_PLUGIN (druid->plugin));
602 npw_druid_free (druid);
604 return TRUE;
607 static gboolean
608 on_druid_delete (GtkWidget* window, GdkEvent* event, NPWDruid* druid)
610 DEBUG_PRINT ("Project wizard canceled");
611 anjuta_plugin_deactivate (ANJUTA_PLUGIN (druid->plugin));
612 npw_druid_free (druid);
614 return TRUE;
617 static gboolean
618 on_project_wizard_key_press_event(GtkWidget *widget, GdkEventKey *event,
619 NPWDruid* druid)
621 if (event->keyval == GDK_Escape)
623 npw_druid_free (druid);
624 return TRUE;
626 return FALSE;
629 static void
630 on_druid_get_new_page (NPWAutogen* gen, gpointer data)
632 NPWDruid* this = (NPWDruid *)data;
633 NPWPage* page;
635 page = g_queue_peek_nth (this->page_list, this->page - 1);
637 if (npw_page_get_name (page) == NULL)
639 /* no page, display finish page */
640 npw_druid_fill_summary (this);
641 gnome_druid_set_page (this->druid, this->finish_page);
643 else
645 /* display property page */
646 npw_druid_fill_property_page (this, page);
648 npw_druid_set_busy (this, FALSE);
651 static void
652 on_druid_parse_page (const gchar* output, gpointer data)
654 NPWPageParser* parser = (NPWPageParser*)data;
656 npw_page_parser_parse (parser, output, strlen (output), NULL);
659 static gboolean
660 on_druid_next (GnomeDruidPage* page, GtkWidget* widget, NPWDruid* this)
662 /* Skip if busy */
663 if (this->busy) return TRUE;
665 /* Set busy */
666 npw_druid_set_busy (this, TRUE);
668 if (this->page == 0)
670 const gchar* new_project;
672 /* Current is Select project page */
673 new_project = npw_header_get_filename (this->header);
674 if (this->project_file != new_project)
676 /* Change project */
678 this->project_file = new_project;
679 npw_druid_remove_following_page (this);
680 npw_autogen_set_input_file (this->gen, this->project_file, "[+","+]");
683 else
685 /* Current is one of the property page */
686 if (!npw_druid_save_valid_values (this))
688 /* Error stay on this page */
689 npw_druid_set_busy (this, FALSE);
691 return TRUE;
694 this->page++;
696 if (g_queue_peek_nth (this->page_list, this->page - 1) == NULL)
698 /* Regenerate new page */
699 if (this->parser != NULL)
700 npw_page_parser_free (this->parser);
701 this->parser = npw_page_parser_new (npw_druid_add_new_page (this), this->project_file, this->page - 1);
702 npw_autogen_set_output_callback (this->gen, on_druid_parse_page, this->parser);
703 npw_autogen_write_definition_file (this->gen, this->values);
704 npw_autogen_execute (this->gen, on_druid_get_new_page, this, NULL);
706 else
708 /* Page is already in cache */
709 on_druid_get_new_page (NULL, (gpointer)this);
712 return TRUE;
715 static gboolean
716 on_druid_back (GnomeDruidPage* dpage, GtkWidget* widget, NPWDruid* druid)
718 /* Skip if busy */
719 if (druid->busy) return TRUE;
721 g_return_val_if_fail (druid->page > 0, TRUE);
723 npw_druid_save_old_values (druid);
725 druid->page--;
726 if (druid->page == 0)
728 /* Go back to project selection page */
729 gnome_druid_set_page (druid->druid, druid->selection_page);
731 else
733 NPWPage* page;
735 page = g_queue_peek_nth (druid->page_list, druid->page - 1);
736 /* Create property page */
737 npw_druid_fill_property_page (druid, page);
740 return TRUE;
743 static void
744 on_druid_finish (GnomeDruidPage* page, GtkWidget* widget, NPWDruid* druid)
746 NPWInstall* inst;
748 inst = npw_install_new (druid->plugin);
749 npw_install_set_property (inst, druid->values);
750 npw_install_set_wizard_file (inst, npw_header_get_filename (druid->header));
751 npw_install_launch (inst);
753 npw_druid_free (druid);
756 static void
757 npw_druid_connect_all_signal (NPWDruid* this, GladeXML* xml)
759 glade_xml_signal_connect_data (xml, DRUID_DELETE_SIGNAL, GTK_SIGNAL_FUNC (on_druid_delete), this);
760 glade_xml_signal_connect_data (xml, DRUID_CANCEL_SIGNAL, GTK_SIGNAL_FUNC (on_druid_cancel), this);
761 glade_xml_signal_connect_data (xml, DRUID_FINISH_SIGNAL, GTK_SIGNAL_FUNC (on_druid_finish), this);
762 glade_xml_signal_connect_data (xml, DRUID_NEXT_SIGNAL, GTK_SIGNAL_FUNC (on_druid_next), this);
763 glade_xml_signal_connect_data (xml, DRUID_BACK_SIGNAL, GTK_SIGNAL_FUNC (on_druid_back), this);
766 /* Add default property
767 *---------------------------------------------------------------------------*/
769 static void
770 npw_druid_add_default_property (NPWDruid* this)
772 NPWValue* value;
773 gchar* s;
774 /* gchar* email; */
775 AnjutaPreferences* pref;
777 pref = anjuta_shell_get_preferences (ANJUTA_PLUGIN (this->plugin)->shell, NULL);
779 /* Add default base project directory */
780 value = npw_value_heap_find_value (this->values, ANJUTA_PROJECT_DIRECTORY_PROPERTY);
781 s = anjuta_preferences_get (pref, "anjuta.project.directory");
782 npw_value_heap_set_value (this->values, value, s, NPW_VALID_VALUE);
783 g_free (s);
785 /* Add user name */
786 value = npw_value_heap_find_value (this->values, USER_NAME_PROPERTY);
787 s = anjuta_preferences_get (pref, "anjuta.user.name");
788 if (!s || strlen(s) == 0)
790 s = (gchar *)g_get_real_name();
791 npw_value_heap_set_value (this->values, value, s, NPW_VALID_VALUE);
793 else
795 npw_value_heap_set_value (this->values, value, s, NPW_VALID_VALUE);
796 g_free (s);
798 /* Add Email address */
799 value = npw_value_heap_find_value (this->values, EMAIL_ADDRESS_PROPERTY);
800 s = anjuta_preferences_get (pref, "anjuta.user.email");
801 /* If Email address not defined in Preferences */
802 if (!s || strlen(s) == 0)
804 if (!(s = getenv("USERNAME")) || strlen(s) == 0)
805 s = getenv("USER");
806 s = g_strconcat(s, "@", getenv("HOSTNAME"), NULL);
808 npw_value_heap_set_value (this->values, value, s, NPW_VALID_VALUE);
809 g_free (s);
812 /* Druid public functions
813 *---------------------------------------------------------------------------*/
815 NPWDruid*
816 npw_druid_new (NPWPlugin* plugin)
818 GladeXML* xml;
819 /* GtkContainer* project_scroll; */
820 NPWDruid* this;
822 /* Skip if already created */
823 if (plugin->druid != NULL) return plugin->druid;
825 /* Check if autogen is present */
826 if (!npw_check_autogen())
828 anjuta_util_dialog_error (NULL, _("Could not find autogen version 5, please install the autogen package. You can get it from http://autogen.sourceforge.net"));
829 return NULL;
832 this = g_new0(NPWDruid, 1);
833 xml = glade_xml_new (GLADE_FILE, NEW_PROJECT_DIALOG, NULL);
834 if ((this == NULL) || (xml == NULL))
836 anjuta_util_dialog_error (NULL, _("Unable to build project wizard user interface"));
837 g_free (this);
839 return NULL;
841 this->pool = g_mem_chunk_new ("druid pool", sizeof(NPWDruidStuff), sizeof(NPWDruidStuff) * 20, G_ALLOC_ONLY);
843 /* Get reference on all useful widget */
844 this->dialog = glade_xml_get_widget (xml, NEW_PROJECT_DIALOG);
845 gtk_window_set_transient_for (GTK_WINDOW (this->dialog),
846 GTK_WINDOW (ANJUTA_PLUGIN (plugin)->shell));
847 this->tooltips = NULL;
848 /* add GtkIconView in the program as it's not handled by libglade */
849 this->druid = GNOME_DRUID (glade_xml_get_widget (xml, DRUID_WIDGET));
850 this->selection_page = GNOME_DRUID_PAGE (glade_xml_get_widget (xml, DRUID_SELECTION_PAGE));
851 this->project_book = GTK_NOTEBOOK (glade_xml_get_widget (xml, PROJECT_SELECTION_BOOK));
852 this->property_page = GNOME_DRUID_PAGE_STANDARD (glade_xml_get_widget (xml, DRUID_PROPERTY_PAGE));
853 this->property_label = GTK_LABEL (glade_xml_get_widget (xml, DRUID_PROPERTY_LABEL));
854 this->property_table = GTK_TABLE (glade_xml_get_widget (xml, DRUID_PROPERTY_TABLE));
855 this->finish_page = GNOME_DRUID_PAGE (glade_xml_get_widget (xml, DRUID_FINISH_PAGE));
856 this->page = 0;
857 this->project_file = NULL;
858 this->busy = FALSE;
859 this->page_list = g_queue_new ();
860 this->values = npw_value_heap_new ();
861 this->gen = npw_autogen_new ();
862 this->plugin = plugin;
864 npw_druid_fix_edge_pages (this, xml);
865 npw_druid_connect_all_signal (this, xml);
866 g_object_unref (xml);
868 if (!npw_druid_fill_selection_page (this))
870 /* No project available */
871 npw_druid_free (this);
873 return NULL;
876 /* Add dialog widget to anjuta status. */
877 anjuta_status_add_widget (anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell, NULL), this->dialog);
879 /* Needed by GnomeDruid widget */
880 gtk_widget_show_all (this->dialog);
882 g_signal_connect(G_OBJECT(this->dialog), "key-press-event",
883 G_CALLBACK(on_project_wizard_key_press_event), this);
885 plugin->druid = this;
886 npw_druid_add_default_property (this);
888 return this;
891 void
892 npw_druid_free (NPWDruid* this)
894 /* NPWPage* page; */
896 g_return_if_fail (this != NULL);
898 if (this->tooltips)
900 g_object_unref (this->tooltips);
901 this->tooltips = NULL;
903 /* Delete page list */
904 this->page = 0;
905 npw_druid_remove_following_page (this);
906 g_queue_free (this->page_list);
907 npw_value_heap_free (this->values);
908 npw_autogen_free (this->gen);
909 if (this->parser != NULL) npw_page_parser_free (this->parser);
910 g_mem_chunk_destroy(this->pool);
911 npw_header_list_free (this->header_list);
912 gtk_widget_destroy (this->dialog);
913 this->plugin->druid = NULL;
914 g_free (this);
917 void
918 npw_druid_show (NPWDruid* this)
920 g_return_if_fail (this);
922 /* Display dialog box */
923 if (this->dialog) gtk_window_present (GTK_WINDOW (this->dialog));