1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 /***************************************************************************
6 * Sun Nov 30 17:46:54 2003
7 * Copyright 2003 Jean-Noel Guiheneuf
8 * jnoel@lotuscompounds.com
9 ****************************************************************************/
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
34 #include <libanjuta/interfaces/ianjuta-file.h>
35 #include <libanjuta/interfaces/ianjuta-file-savable.h>
36 #include <libanjuta/interfaces/ianjuta-document-manager.h>
37 #include <libanjuta/interfaces/ianjuta-snippets-manager.h>
38 #include <libanjuta/interfaces/ianjuta-file.h>
39 #include <libanjuta/interfaces/ianjuta-project.h>
40 #include <libanjuta/interfaces/ianjuta-project-manager.h>
41 #include <libanjuta/interfaces/ianjuta-project-chooser.h>
42 #include <libanjuta/interfaces/ianjuta-vcs.h>
47 #define BUILDER_FILE_FILE PACKAGE_DATA_DIR "/glade/anjuta-file-wizard.ui"
48 #define NEW_FILE_DIALOG "dialog.new.file"
49 #define NEW_FILE_OK_BUTTON "okbutton"
50 #define NEW_FILE_ENTRY "new.file.entry"
51 #define NEW_FILE_TYPE "new.file.type"
52 #define NEW_FILE_TYPE_STORE "new.file.type.store"
53 #define NEW_FILE_TEMPLATE "new.file.template"
54 #define NEW_FILE_HEADER "new.file.header"
55 #define NEW_FILE_LICENSE "new.file.license"
56 #define NEW_FILE_MENU_LICENSE "new.file.menu.license"
57 #define NEW_FILE_MENU_LICENSE_STORE "new.file.menu.license.store"
58 #define NEW_FILE_ADD_TO_PROJECT "add_to_project"
59 #define NEW_FILE_ADD_TO_PROJECT_PARENT "add_to_project.combo.parent"
60 #define NEW_FILE_ADD_TO_REPOSITORY "add_to_repository"
62 typedef struct _NewFileGUI
66 GtkWidget
*add_to_project
;
67 GtkWidget
*add_to_repository
;
68 GtkWidget
*add_to_project_parent
;
71 AnjutaFileWizardPlugin
*plugin
;
75 typedef struct _NewfileType
88 NewfileType new_file_type
[] = {
89 {N_("C Source File"), ".c", LGE_HC
, TRUE
, TRUE
, CMT_C
, LGE_C
},
90 {N_("C/C++ Header File"), ".h", -1, TRUE
, TRUE
, CMT_C
, LGE_HC
},
91 {N_("C++ Source File"), ".cxx", LGE_HC
, TRUE
, TRUE
, CMT_CPP
, LGE_CPLUS
},
92 {N_("C# Source File"), ".c#", -1, FALSE
, FALSE
, CMT_CPP
, LGE_CSHARP
},
93 {N_("Java Source File"), ".java", -1, FALSE
, FALSE
, CMT_CPP
, LGE_JAVA
},
94 {N_("Perl Source File"), ".pl", -1, TRUE
, TRUE
, CMT_P
, LGE_PERL
},
95 {N_("Python Source File"), ".py", -1, TRUE
, FALSE
, CMT_P
, LGE_PYTHON
},
96 {N_("Shell Script File"), ".sh", -1, TRUE
, TRUE
, CMT_P
, LGE_SHELL
},
97 {N_("Vala Source File"), ".vala", -1, FALSE
, CMT_CPP
, LGE_VALA
},
98 {N_("Other"), NULL
, -1, FALSE
, FALSE
, CMT_C
, LGE_C
}
102 typedef struct _NewlicenseType
108 NewlicenseType new_license_type
[] = {
109 {N_("General Public License (GPL)"), "GPL"},
110 {N_("Lesser General Public License (LGPL)"), "LGPL"},
111 {N_("BSD Public License"), "BSD"}
114 static NewFileGUI
*nfg
= NULL
;
117 static gboolean
create_new_file_dialog(IAnjutaDocumentManager
*docman
);
118 static void insert_notice(IAnjutaSnippetsManager
* snippets_manager
, const gchar
* license_type
, gint comment_type
);
119 static void insert_header(IAnjutaSnippetsManager
* snippets_manager
, gint source_type
);
122 on_project_parent_changed (GtkWidget
*project_combo
, NewFileGUI
*gui
)
124 gboolean active
= TRUE
;
126 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gui
->add_to_project
)))
130 file
= ianjuta_project_chooser_get_selected (IANJUTA_PROJECT_CHOOSER (gui
->add_to_project_parent
), NULL
);
131 active
= file
!= NULL
;
133 gtk_widget_set_sensitive (gui
->ok_button
, active
);
137 on_add_to_project_toggled (GtkWidget
* toggle_button
, NewFileGUI
*gui
)
139 gboolean status
= gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(toggle_button
));
140 gtk_widget_set_sensitive (gui
->add_to_repository
, status
);
141 gtk_widget_set_sensitive (gui
->add_to_project_parent
, status
);
144 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(gui
->add_to_repository
),
147 on_project_parent_changed (toggle_button
, gui
);
151 display_new_file(AnjutaFileWizardPlugin
*plugin
,
152 IAnjutaDocumentManager
*docman
)
155 gboolean has_project
;
158 if (!create_new_file_dialog (docman
))
161 nfg
->plugin
= plugin
;
163 /* check whether we have a loaded project or not */
164 if (plugin
->top_dir
) {
165 IAnjutaProjectManager
*manager
=
166 anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin
)->shell
,
167 IAnjutaProjectManager
, NULL
);
170 caps
= ianjuta_project_manager_get_capabilities (manager
, NULL
);
171 /* Add combo node selection */
172 ianjuta_project_chooser_set_project_model (IANJUTA_PROJECT_CHOOSER (nfg
->add_to_project_parent
),
173 IANJUTA_PROJECT_MANAGER (manager
),
174 ANJUTA_PROJECT_SOURCE
,
176 on_project_parent_changed (nfg
->add_to_project_parent
, nfg
);
180 g_signal_connect (nfg
->add_to_project
, "toggled",
181 G_CALLBACK(on_add_to_project_toggled
),
183 g_signal_connect (nfg
->add_to_project_parent
, "changed",
184 G_CALLBACK(on_project_parent_changed
),
187 has_project
= (caps
& ANJUTA_PROJECT_CAN_ADD_SOURCE
) ? TRUE
: FALSE
;
188 gtk_widget_set_visible (nfg
->add_to_project
, has_project
);
189 gtk_widget_set_visible (nfg
->add_to_project_parent
, has_project
);
190 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (nfg
->add_to_project
),
192 gtk_widget_set_sensitive (nfg
->add_to_project
, has_project
);
193 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (nfg
->add_to_repository
),
196 if (nfg
&& !(nfg
->showing
))
198 gtk_window_present (GTK_WINDOW (nfg
->dialog
));
204 create_new_file_dialog(IAnjutaDocumentManager
*docman
)
206 GtkComboBox
*optionmenu
;
208 GError
* error
= NULL
;
212 nfg
= g_new0(NewFileGUI
, 1);
213 nfg
->bxml
= gtk_builder_new ();
214 if (!gtk_builder_add_from_file (nfg
->bxml
, BUILDER_FILE_FILE
, &error
))
216 g_warning ("Couldn't load builder file: %s", error
->message
);
217 g_error_free (error
);
222 nfg
->dialog
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_DIALOG
));
223 nfg
->ok_button
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_OK_BUTTON
));
224 nfg
->add_to_project
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_ADD_TO_PROJECT
));
225 nfg
->add_to_project_parent
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_ADD_TO_PROJECT_PARENT
));
226 nfg
->add_to_repository
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_ADD_TO_REPOSITORY
));
227 nfg
->showing
= FALSE
;
229 store
= GTK_LIST_STORE (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TYPE_STORE
));
230 for (i
=0; i
< (sizeof(new_file_type
) / sizeof(NewfileType
)); i
++)
234 gtk_list_store_append (store
, &iter
);
235 gtk_list_store_set (store
, &iter
, 0, new_file_type
[i
].name
, -1);
237 optionmenu
= GTK_COMBO_BOX (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TYPE
));
238 gtk_combo_box_set_active (optionmenu
, 0);
240 store
= GTK_LIST_STORE (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_MENU_LICENSE_STORE
));
241 for (i
=0; i
< (sizeof(new_license_type
) / sizeof(NewlicenseType
)); i
++)
245 gtk_list_store_append (store
, &iter
);
246 gtk_list_store_set (store
, &iter
, 0, new_license_type
[i
].name
, -1);
248 optionmenu
= GTK_COMBO_BOX (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_MENU_LICENSE
));
249 gtk_combo_box_set_active (optionmenu
, 0);
251 g_object_set_data (G_OBJECT (nfg
->dialog
), "IAnjutaDocumentManager", docman
);
252 gtk_builder_connect_signals (nfg
->bxml
, NULL
);
253 g_signal_emit_by_name(G_OBJECT (optionmenu
), "changed");
259 on_new_file_cancelbutton_clicked(GtkWidget
*window
, GdkEvent
*event
,
264 gtk_widget_hide(nfg
->dialog
);
265 nfg
->showing
= FALSE
;
271 on_new_file_okbutton_clicked(GtkWidget
*window
, GdkEvent
*event
,
275 GtkWidget
*checkbutton
;
276 GtkWidget
*optionmenu
;
278 gchar
*header_name
= NULL
;
280 const gchar
* license_type
;
283 IAnjutaDocumentManager
*docman
;
285 IAnjutaSnippetsManager
* snippets_manager
;
286 IAnjutaEditor
*te
= NULL
;
287 IAnjutaEditor
*teh
= NULL
;
290 toplevel
= gtk_widget_get_toplevel (window
);
291 docman
= IANJUTA_DOCUMENT_MANAGER (g_object_get_data (G_OBJECT(toplevel
),
292 "IAnjutaDocumentManager"));
293 snippets_manager
= anjuta_shell_get_interface (ANJUTA_PLUGIN(docman
)->shell
,
294 IAnjutaSnippetsManager
, NULL
);
295 entry
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_ENTRY
));
296 name
= gtk_entry_get_text(GTK_ENTRY(entry
));
298 /* Create main file */
299 if (name
&& strlen (name
) > 0)
300 te
= ianjuta_document_manager_add_buffer (docman
, name
, NULL
, NULL
);
302 te
= ianjuta_document_manager_add_buffer (docman
, "", NULL
, NULL
);
307 /* Create header file */
308 optionmenu
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TYPE
));
309 source_type
= gtk_combo_box_get_active(GTK_COMBO_BOX(optionmenu
));
311 checkbutton
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_HEADER
));
312 if (gtk_widget_get_sensitive (checkbutton
) &&
313 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton
)))
315 if (name
&& strlen (name
) > 0)
317 const gchar
*old_ext
= strrchr (name
,'.');
318 const gchar
*new_ext
= new_file_type
[new_file_type
[source_type
].header
].ext
;
322 header_name
= g_strconcat (name
, new_ext
, NULL
);
326 header_name
= g_strndup (name
, old_ext
- name
+ strlen(new_ext
));
327 strcpy(&header_name
[old_ext
- name
], new_ext
);
329 teh
= ianjuta_document_manager_add_buffer (docman
, header_name
, NULL
, NULL
);
333 teh
= ianjuta_document_manager_add_buffer (docman
, "", NULL
, NULL
);
335 ianjuta_document_manager_set_current_document (docman
, IANJUTA_DOCUMENT(te
), NULL
);
338 checkbutton
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TEMPLATE
));
339 if (gtk_widget_get_sensitive (checkbutton
) &&
340 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton
)))
342 insert_header(snippets_manager
, source_type
);
345 ianjuta_document_manager_set_current_document (docman
, IANJUTA_DOCUMENT(teh
), NULL
);
346 insert_header (snippets_manager
, new_file_type
[source_type
].header
);
347 ianjuta_document_manager_set_current_document (docman
, IANJUTA_DOCUMENT(te
), NULL
);
351 checkbutton
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_LICENSE
));
352 if (gtk_widget_get_sensitive (checkbutton
) &&
353 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton
)))
355 optionmenu
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_MENU_LICENSE
));
356 sel
= gtk_combo_box_get_active(GTK_COMBO_BOX(optionmenu
));
357 license_type
= new_license_type
[sel
].type
;
358 comment_type
= new_file_type
[source_type
].comment
;
360 insert_notice(snippets_manager
, license_type
, comment_type
);
363 comment_type
= new_file_type
[new_file_type
[source_type
].header
].comment
;
364 ianjuta_document_manager_set_current_document (docman
, IANJUTA_DOCUMENT(teh
), NULL
);
365 insert_notice(snippets_manager
, license_type
, comment_type
);
366 ianjuta_document_manager_set_current_document (docman
, IANJUTA_DOCUMENT(te
), NULL
);
370 /* Add file to project */
371 if (nfg
->plugin
->top_dir
&&
372 gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (nfg
->add_to_project
)))
374 IAnjutaProjectManager
*pm
;
377 GFile
*header
= NULL
;
379 pm
= anjuta_shell_get_interface (ANJUTA_PLUGIN(docman
)->shell
,
380 IAnjutaProjectManager
, NULL
);
381 g_return_val_if_fail (pm
!= NULL
, FALSE
);
383 parent
= ianjuta_project_chooser_get_selected (IANJUTA_PROJECT_CHOOSER (nfg
->add_to_project_parent
), NULL
);
386 source
= ianjuta_project_manager_add_source_quiet (pm
, name
, parent
, NULL
);
388 if (ok
) ianjuta_file_savable_save_as (IANJUTA_FILE_SAVABLE (te
), source
, NULL
);
392 /* Save header file */
393 header
= ianjuta_project_manager_add_source_quiet (pm
, header_name
, parent
, NULL
);
395 if (ok
) ianjuta_file_savable_save_as (IANJUTA_FILE_SAVABLE (teh
), header
, NULL
);
399 /* Add to repository */
400 if (ok
&& gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (nfg
->add_to_repository
)))
402 IAnjutaVcs
* ivcs
= anjuta_shell_get_interface (ANJUTA_PLUGIN(docman
)->shell
,
406 AnjutaAsyncNotify
* notify
= anjuta_async_notify_new();
407 GList
*file_list
= NULL
;
409 file_list
= g_list_prepend (file_list
, source
);
410 if (header
!= NULL
) file_list
= g_list_prepend (file_list
, header
);
411 ianjuta_vcs_add (ivcs
, file_list
, notify
, NULL
);
412 g_list_free (file_list
);
416 /* Re emit element_added for symbol-db */
417 if (source
) g_signal_emit_by_name (G_OBJECT (pm
), "element_added", source
);
418 if (header
) g_signal_emit_by_name (G_OBJECT (pm
), "element_added", header
);
420 if (source
) g_object_unref (source
);
421 if (header
) g_object_unref (header
);
423 g_free (header_name
);
425 gtk_widget_hide (nfg
->dialog
);
426 nfg
->showing
= FALSE
;
432 on_new_file_entry_changed (GtkEditable
*entry
, gpointer user_data
)
436 static gint last_length
= 0;
438 GtkWidget
*optionmenu
;
440 name
= g_strdup(gtk_entry_get_text(GTK_ENTRY(entry
)));
441 length
= strlen(name
);
443 if (last_length
!= 2 && length
== 1)
445 optionmenu
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TYPE
));
446 sel
= gtk_combo_box_get_active(GTK_COMBO_BOX(optionmenu
));
447 name
= g_strconcat (name
, new_file_type
[sel
].ext
, NULL
);
448 gtk_entry_set_text (GTK_ENTRY(entry
), name
);
450 last_length
= length
;
456 on_new_file_type_changed (GtkComboBox
*optionmenu
, gpointer user_data
)
463 sel
= gtk_combo_box_get_active(optionmenu
);
465 widget
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_HEADER
));
466 gtk_widget_set_sensitive(widget
, new_file_type
[sel
].header
>= 0);
467 widget
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_LICENSE
));
468 gtk_widget_set_sensitive(widget
, new_file_type
[sel
].gpl
);
469 widget
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_TEMPLATE
));
470 gtk_widget_set_sensitive(widget
, new_file_type
[sel
].template);
471 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), FALSE
);
473 entry
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_ENTRY
));
474 name
= g_strdup(gtk_entry_get_text(GTK_ENTRY(entry
)));
475 if (strlen(name
) > 0)
477 tmp
= strrchr(name
, '.');
479 name
= g_strndup(name
, tmp
- name
);
480 name
= g_strconcat (name
, new_file_type
[sel
].ext
, NULL
);
481 gtk_entry_set_text (GTK_ENTRY(entry
), name
);
487 on_new_file_license_toggled(GtkToggleButton
*button
, gpointer user_data
)
491 widget
= GTK_WIDGET (gtk_builder_get_object (nfg
->bxml
, NEW_FILE_MENU_LICENSE
));
492 gtk_widget_set_sensitive(widget
, gtk_toggle_button_get_active(button
));
496 insert_notice(IAnjutaSnippetsManager
* snippets_manager
, const gchar
* license_type
, gint comment_type
)
500 name
= g_utf8_strdown (license_type
, -1);
501 ianjuta_snippets_manager_insert(snippets_manager
, name
, FALSE
, NULL
);
506 insert_header(IAnjutaSnippetsManager
* snippets_manager
, gint source_type
)
508 ianjuta_snippets_manager_insert (snippets_manager
, "top_com", FALSE
, NULL
);