2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include "projectprivate.h"
40 #include "msgwindow.h"
48 #include "filetypes.h"
51 ProjectPrefs project_prefs
= { NULL
, FALSE
, FALSE
};
54 static GeanyProjectPrivate priv
;
55 static GeanyIndentPrefs indentation
;
57 static StashGroup
*indent_group
= NULL
;
61 gchar
*project_file_path
; /* in UTF-8 */
62 } local_prefs
= { NULL
};
65 static gboolean entries_modified
;
67 /* simple struct to keep references to the elements of the properties dialog */
68 typedef struct _PropertyDialogElements
73 GtkWidget
*description
;
77 BuildTableData build_properties
;
79 } PropertyDialogElements
;
82 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
);
83 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
);
84 static gboolean
load_config(const gchar
*filename
);
85 static gboolean
write_config(gboolean emit_signal
);
86 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
87 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
88 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
);
89 static void apply_editor_prefs(void);
92 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
93 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
94 #define MAX_NAME_LEN 50
95 /* "projects" is part of the default project base path so be careful when translating
96 * please avoid special characters and spaces, look at the source for details or ask Frank */
97 #define PROJECT_DIR _("projects")
100 /* TODO: this should be ported to Glade like the project preferences dialog,
101 * then we can get rid of the PropertyDialogElements struct altogether as
102 * widgets pointers can be accessed through ui_lookup_widget(). */
103 void project_new(void)
111 PropertyDialogElements
*e
;
113 if (! project_ask_close())
116 g_return_if_fail(app
->project
== NULL
);
118 e
= g_new0(PropertyDialogElements
, 1);
119 e
->dialog
= gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets
.window
),
120 GTK_DIALOG_DESTROY_WITH_PARENT
,
121 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
123 gtk_widget_set_name(e
->dialog
, "GeanyDialogProject");
124 bbox
= gtk_hbox_new(FALSE
, 0);
125 button
= gtk_button_new();
126 image
= gtk_image_new_from_stock(GTK_STOCK_NEW
, GTK_ICON_SIZE_BUTTON
);
127 label
= gtk_label_new_with_mnemonic(_("C_reate"));
128 gtk_box_pack_start(GTK_BOX(bbox
), image
, FALSE
, FALSE
, 3);
129 gtk_box_pack_start(GTK_BOX(bbox
), label
, FALSE
, FALSE
, 3);
130 gtk_container_add(GTK_CONTAINER(button
), bbox
);
131 gtk_dialog_add_action_widget(GTK_DIALOG(e
->dialog
), button
, GTK_RESPONSE_OK
);
133 vbox
= ui_dialog_vbox_new(GTK_DIALOG(e
->dialog
));
135 entries_modified
= FALSE
;
137 table
= gtk_table_new(3, 2, FALSE
);
138 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
139 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
141 label
= gtk_label_new(_("Name:"));
142 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
144 e
->name
= gtk_entry_new();
145 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
146 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
148 ui_table_add_row(GTK_TABLE(table
), 0, label
, e
->name
, NULL
);
150 label
= gtk_label_new(_("Filename:"));
151 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
153 e
->file_name
= gtk_entry_new();
154 ui_entry_add_clear_icon(GTK_ENTRY(e
->file_name
));
155 gtk_entry_set_width_chars(GTK_ENTRY(e
->file_name
), 30);
156 button
= gtk_button_new();
157 g_signal_connect(button
, "clicked", G_CALLBACK(on_file_save_button_clicked
), e
);
158 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
159 gtk_container_add(GTK_CONTAINER(button
), image
);
160 bbox
= gtk_hbox_new(FALSE
, 6);
161 gtk_box_pack_start_defaults(GTK_BOX(bbox
), e
->file_name
);
162 gtk_box_pack_start(GTK_BOX(bbox
), button
, FALSE
, FALSE
, 0);
164 ui_table_add_row(GTK_TABLE(table
), 1, label
, bbox
, NULL
);
166 label
= gtk_label_new(_("Base path:"));
167 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
169 e
->base_path
= gtk_entry_new();
170 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
171 gtk_widget_set_tooltip_text(e
->base_path
,
172 _("Base directory of all files that make up the project. "
173 "This can be a new path, or an existing directory tree. "
174 "You can use paths relative to the project filename."));
175 bbox
= ui_path_box_new(_("Choose Project Base Path"),
176 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
->base_path
));
178 ui_table_add_row(GTK_TABLE(table
), 2, label
, bbox
, NULL
);
180 gtk_container_add(GTK_CONTAINER(vbox
), table
);
183 g_signal_connect(e
->name
, "changed", G_CALLBACK(on_name_entry_changed
), e
);
184 /* run the callback manually to initialise the base_path and file_name fields */
185 on_name_entry_changed(GTK_EDITABLE(e
->name
), e
);
187 g_signal_connect(e
->file_name
, "changed", G_CALLBACK(on_entries_changed
), e
);
188 g_signal_connect(e
->base_path
, "changed", G_CALLBACK(on_entries_changed
), e
);
190 gtk_widget_show_all(e
->dialog
);
192 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
)
194 if (update_config(e
, TRUE
))
196 if (!write_config(TRUE
))
197 SHOW_ERR(_("Project file could not be written"));
200 ui_set_statusbar(TRUE
, _("Project \"%s\" created."), app
->project
->name
);
202 ui_add_recent_project_file(app
->project
->file_name
);
207 gtk_widget_destroy(e
->dialog
);
212 gboolean
project_load_file_with_session(const gchar
*locale_file_name
)
214 if (project_load_file(locale_file_name
))
216 if (project_prefs
.project_session
)
218 configuration_open_files();
219 /* open a new file if no other file was opened */
220 document_new_file_if_non_open();
221 ui_focus_current_document();
230 static void run_open_dialog(GtkDialog
*dialog
)
232 while (gtk_dialog_run(dialog
) == GTK_RESPONSE_ACCEPT
)
234 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
236 /* try to load the config */
237 if (! project_load_file_with_session(filename
))
239 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
241 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename
);
242 gtk_widget_grab_focus(GTK_WIDGET(dialog
));
243 g_free(utf8_filename
);
254 void project_open(void)
256 const gchar
*dir
= local_prefs
.project_file_path
;
261 GtkFileFilter
*filter
;
264 if (! project_ask_close()) return;
267 file
= win32_show_project_open_dialog(main_widgets
.window
, _("Open Project"), dir
, FALSE
, TRUE
);
270 /* try to load the config */
271 if (! project_load_file_with_session(file
))
273 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file
);
279 dialog
= gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets
.window
),
280 GTK_FILE_CHOOSER_ACTION_OPEN
,
281 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
282 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
283 gtk_widget_set_name(dialog
, "GeanyDialogProject");
285 /* set default Open, so pressing enter can open multiple files */
286 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
287 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
288 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
289 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
290 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(main_widgets
.window
));
291 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog
), TRUE
);
293 /* add FileFilters */
294 filter
= gtk_file_filter_new();
295 gtk_file_filter_set_name(filter
, _("All files"));
296 gtk_file_filter_add_pattern(filter
, "*");
297 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
298 filter
= gtk_file_filter_new();
299 gtk_file_filter_set_name(filter
, _("Project files"));
300 gtk_file_filter_add_pattern(filter
, "*." GEANY_PROJECT_EXT
);
301 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
302 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog
), filter
);
304 locale_path
= utils_get_locale_from_utf8(dir
);
305 if (g_file_test(locale_path
, G_FILE_TEST_EXISTS
) &&
306 g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
308 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
312 gtk_widget_show_all(dialog
);
313 run_open_dialog(GTK_DIALOG(dialog
));
314 gtk_widget_destroy(GTK_WIDGET(dialog
));
319 /* Called when creating, opening, closing and updating projects. */
320 static void update_ui(void)
322 if (main_status
.quitting
)
325 ui_set_window_title(NULL
);
326 build_menu_update(NULL
);
327 sidebar_openfiles_update_all();
331 static void remove_foreach_project_filetype(gpointer data
, gpointer user_data
)
333 GeanyFiletype
*ft
= data
;
336 setptr(ft
->projfilecmds
, NULL
);
337 setptr(ft
->projexeccmds
, NULL
);
338 setptr(ft
->projerror_regex_string
, NULL
);
339 ft
->project_list_entry
= -1;
344 /* open_default will make function reload default session files on close */
345 void project_close(gboolean open_default
)
347 g_return_if_fail(app
->project
!= NULL
);
349 ui_set_statusbar(TRUE
, _("Project \"%s\" closed."), app
->project
->name
);
351 /* use write_config() to save project session files */
352 if (!write_config(FALSE
))
353 g_warning("Project file \"%s\" could not be written", app
->project
->file_name
);
355 /* remove project filetypes build entries */
356 if (app
->project
->build_filetypes_list
!= NULL
)
358 g_ptr_array_foreach(app
->project
->build_filetypes_list
, remove_foreach_project_filetype
, NULL
);
359 g_ptr_array_free(app
->project
->build_filetypes_list
, FALSE
);
362 /* remove project non filetype build menu items */
363 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_NON_FT
, -1);
364 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_EXEC
, -1);
366 g_free(app
->project
->name
);
367 g_free(app
->project
->description
);
368 g_free(app
->project
->file_name
);
369 g_free(app
->project
->base_path
);
371 g_free(app
->project
);
374 apply_editor_prefs(); /* ensure that global settings are restored */
376 if (project_prefs
.project_session
)
378 /* close all existing tabs first */
379 document_close_all();
381 /* after closing all tabs let's open the tabs found in the default config */
382 if (open_default
&& cl_options
.load_session
)
384 configuration_reload_default_session();
385 configuration_open_files();
386 /* open a new file if no other file was opened */
387 document_new_file_if_non_open();
388 ui_focus_current_document();
391 g_signal_emit_by_name(geany_object
, "project-close");
397 /* Shows the file chooser dialog when base path button is clicked
398 * FIXME: this should be connected in Glade but 3.8.1 has a bug
399 * where it won't pass any objects as user data (#588824). */
401 on_project_properties_base_path_button_clicked(GtkWidget
*button
,
402 GtkWidget
*base_path_entry
)
406 g_return_if_fail(base_path_entry
!= NULL
);
407 g_return_if_fail(GTK_IS_WIDGET(base_path_entry
));
409 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
410 NULL
, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
411 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
412 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
415 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
417 gtk_entry_set_text(GTK_ENTRY(base_path_entry
),
418 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
)));
421 gtk_widget_destroy(dialog
);
425 static void insert_build_page(PropertyDialogElements
*e
)
427 GtkWidget
*build_table
, *label
, *editor_tab
;
428 GeanyDocument
*doc
= document_get_current();
429 GeanyFiletype
*ft
= NULL
;
432 /* lookup the "Editor" tab page so the "Build" tab can be inserted
434 editor_tab
= ui_lookup_widget(e
->dialog
, "vbox_project_dialog_editor");
435 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(e
->notebook
), editor_tab
);
440 build_table
= build_commands_table(doc
, GEANY_BCS_PROJ
, &(e
->build_properties
), ft
);
441 gtk_container_set_border_width(GTK_CONTAINER(build_table
), 6);
442 label
= gtk_label_new(_("Build"));
443 e
->build_page_num
= gtk_notebook_insert_page(GTK_NOTEBOOK(e
->notebook
),
444 build_table
, label
, ++page_num
);
448 static void create_properties_dialog(PropertyDialogElements
*e
)
450 GtkWidget
*base_path_button
;
451 static guint base_path_button_handler_id
= 0;
452 static guint radio_long_line_handler_id
= 0;
454 e
->dialog
= create_project_dialog();
455 e
->notebook
= ui_lookup_widget(e
->dialog
, "project_notebook");
456 e
->file_name
= ui_lookup_widget(e
->dialog
, "label_project_dialog_filename");
457 e
->name
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_name");
458 e
->description
= ui_lookup_widget(e
->dialog
, "textview_project_dialog_description");
459 e
->base_path
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_base_path");
460 e
->patterns
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_file_patterns");
462 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
464 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
465 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
466 ui_entry_add_clear_icon(GTK_ENTRY(e
->patterns
));
468 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
469 if (base_path_button_handler_id
== 0)
471 base_path_button
= ui_lookup_widget(e
->dialog
, "button_project_dialog_base_path");
472 base_path_button_handler_id
=
473 g_signal_connect(base_path_button
, "clicked",
474 G_CALLBACK(on_project_properties_base_path_button_clicked
),
478 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
479 if (radio_long_line_handler_id
== 0)
481 radio_long_line_handler_id
=
482 g_signal_connect(ui_lookup_widget(e
->dialog
,
483 "radio_long_line_custom_project"), "toggled",
484 G_CALLBACK(on_radio_long_line_custom_toggled
),
485 ui_lookup_widget(e
->dialog
, "spin_long_line_project"));
490 static void show_project_properties(gboolean show_build
)
492 GeanyProject
*p
= app
->project
;
493 GtkWidget
*widget
= NULL
;
494 GtkWidget
*radio_long_line_custom
;
495 static PropertyDialogElements e
= { 0 };
497 g_return_if_fail(app
->project
!= NULL
);
499 entries_modified
= FALSE
;
501 if (e
.dialog
== NULL
)
502 create_properties_dialog(&e
);
504 insert_build_page(&e
);
506 stash_group_display(indent_group
, e
.dialog
);
508 /* fill the elements with the appropriate data */
509 gtk_entry_set_text(GTK_ENTRY(e
.name
), p
->name
);
510 gtk_label_set_text(GTK_LABEL(e
.file_name
), p
->file_name
);
511 gtk_entry_set_text(GTK_ENTRY(e
.base_path
), p
->base_path
);
513 radio_long_line_custom
= ui_lookup_widget(e
.dialog
, "radio_long_line_custom_project");
514 switch (p
->long_line_behaviour
)
516 case 0: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_disabled_project"); break;
517 case 1: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_default_project"); break;
518 case 2: widget
= radio_long_line_custom
; break;
520 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
522 widget
= ui_lookup_widget(e
.dialog
, "spin_long_line_project");
523 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), (gdouble
)p
->long_line_column
);
524 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom
), widget
);
526 if (p
->description
!= NULL
)
528 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
.description
));
529 gtk_text_buffer_set_text(buffer
, p
->description
, -1);
532 if (p
->file_patterns
!= NULL
)
533 { /* set the file patterns */
536 str
= g_strjoinv(" ", p
->file_patterns
);
537 gtk_entry_set_text(GTK_ENTRY(e
.patterns
), str
);
541 g_signal_emit_by_name(geany_object
, "project-dialog-create", e
.notebook
);
542 gtk_widget_show_all(e
.dialog
);
544 /* note: notebook page must be shown before setting current page */
546 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
548 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), 0);
550 while (gtk_dialog_run(GTK_DIALOG(e
.dialog
)) == GTK_RESPONSE_OK
)
552 if (update_config(&e
, FALSE
))
554 g_signal_emit_by_name(geany_object
, "project-dialog-confirmed", e
.notebook
);
555 if (!write_config(TRUE
))
556 SHOW_ERR(_("Project file could not be written"));
559 ui_set_statusbar(TRUE
, _("Project \"%s\" saved."), app
->project
->name
);
565 build_free_fields(e
.build_properties
);
566 gtk_notebook_remove_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
567 gtk_widget_hide(e
.dialog
);
571 void project_properties(void)
573 show_project_properties(FALSE
);
577 void project_build_properties(void)
579 show_project_properties(TRUE
);
583 /* checks whether there is an already open project and asks the user if he wants to close it or
584 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
585 * and TRUE if we can go ahead */
586 gboolean
project_ask_close(void)
588 if (app
->project
!= NULL
)
590 if (dialogs_show_question_full(NULL
, GTK_STOCK_CLOSE
, GTK_STOCK_CANCEL
,
591 _("Do you want to close it before proceeding?"),
592 _("The '%s' project is already open."), app
->project
->name
))
594 project_close(FALSE
);
605 static GeanyProject
*create_project(void)
607 GeanyProject
*project
= g_new0(GeanyProject
, 1);
609 memset(&priv
, 0, sizeof priv
);
610 indentation
= *editor_get_indent_prefs(NULL
);
611 priv
.indentation
= &indentation
;
612 project
->priv
= &priv
;
614 project
->file_patterns
= NULL
;
616 project
->long_line_behaviour
= 1 /* use global settings */;
617 project
->long_line_column
= editor_prefs
.long_line_column
;
619 app
->project
= project
;
624 /* Verifies data for New & Properties dialogs.
625 * Returns: FALSE if the user needs to change any data. */
626 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
)
628 const gchar
*name
, *file_name
, *base_path
;
629 gchar
*locale_filename
;
634 g_return_val_if_fail(e
!= NULL
, TRUE
);
636 name
= gtk_entry_get_text(GTK_ENTRY(e
->name
));
637 name_len
= strlen(name
);
640 SHOW_ERR(_("The specified project name is too short."));
641 gtk_widget_grab_focus(e
->name
);
644 else if (name_len
> MAX_NAME_LEN
)
646 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN
);
647 gtk_widget_grab_focus(e
->name
);
652 file_name
= gtk_entry_get_text(GTK_ENTRY(e
->file_name
));
654 file_name
= gtk_label_get_text(GTK_LABEL(e
->file_name
));
656 if (G_UNLIKELY(! NZV(file_name
)))
658 SHOW_ERR(_("You have specified an invalid project filename."));
659 gtk_widget_grab_focus(e
->file_name
);
663 locale_filename
= utils_get_locale_from_utf8(file_name
);
664 base_path
= gtk_entry_get_text(GTK_ENTRY(e
->base_path
));
666 { /* check whether the given directory actually exists */
667 gchar
*locale_path
= utils_get_locale_from_utf8(base_path
);
669 if (! g_path_is_absolute(locale_path
))
670 { /* relative base path, so add base dir of project file name */
671 gchar
*dir
= g_path_get_dirname(locale_filename
);
672 setptr(locale_path
, g_strconcat(dir
, G_DIR_SEPARATOR_S
, locale_path
, NULL
));
676 if (! g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
680 create_dir
= dialogs_show_question_full(NULL
, GTK_STOCK_OK
, GTK_STOCK_CANCEL
,
681 _("Create the project's base path directory?"),
682 _("The path \"%s\" does not exist."),
686 err_code
= utils_mkdir(locale_path
, TRUE
);
688 if (! create_dir
|| err_code
!= 0)
691 SHOW_ERR1(_("Project base directory could not be created (%s)."),
692 g_strerror(err_code
));
693 gtk_widget_grab_focus(e
->base_path
);
694 utils_free_pointers(2, locale_path
, locale_filename
, NULL
);
700 /* finally test whether the given project file can be written */
701 if ((err_code
= utils_is_file_writable(locale_filename
)) != 0 ||
702 (err_code
= g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
) ? EISDIR
: 0) != 0)
704 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code
));
705 gtk_widget_grab_focus(e
->file_name
);
706 g_free(locale_filename
);
709 g_free(locale_filename
);
711 if (app
->project
== NULL
)
718 setptr(p
->name
, g_strdup(name
));
719 setptr(p
->file_name
, g_strdup(file_name
));
720 /* use "." if base_path is empty */
721 setptr(p
->base_path
, g_strdup(NZV(base_path
) ? base_path
: "./"));
723 if (! new_project
) /* save properties specific fields */
725 GtkTextIter start
, end
;
726 GtkTextBuffer
*buffer
;
727 GeanyDocument
*doc
= document_get_current();
728 GeanyBuildCommand
*oldvalue
;
729 GeanyFiletype
*ft
= doc
? doc
->file_type
: NULL
;
734 /* get and set the project description */
735 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
736 gtk_text_buffer_get_start_iter(buffer
, &start
);
737 gtk_text_buffer_get_end_iter(buffer
, &end
);
738 setptr(p
->description
, g_strdup(gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
)));
740 stash_group_update(indent_group
, e
->dialog
);
742 /* read the project build menu */
743 oldvalue
= ft
? ft
->projfilecmds
: NULL
;
744 build_read_project(ft
, e
->build_properties
);
746 if (ft
!= NULL
&& ft
->projfilecmds
!= oldvalue
&& ft
->project_list_entry
< 0)
748 if (p
->build_filetypes_list
== NULL
)
749 p
->build_filetypes_list
= g_ptr_array_new();
750 ft
->project_list_entry
= p
->build_filetypes_list
->len
;
751 g_ptr_array_add(p
->build_filetypes_list
, ft
);
753 build_menu_update(doc
);
755 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled_project");
756 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
757 p
->long_line_behaviour
= 0;
760 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default_project");
761 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
762 p
->long_line_behaviour
= 1;
764 /* "Custom" radio button must be checked */
765 p
->long_line_behaviour
= 2;
768 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line_project");
769 p
->long_line_column
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
770 apply_editor_prefs();
772 /* get and set the project file patterns */
773 tmp
= g_strdup(gtk_entry_get_text(GTK_ENTRY(e
->patterns
)));
774 g_strfreev(p
->file_patterns
);
776 str
= g_string_new(tmp
);
777 do {} while (utils_string_replace_all(str
, " ", " "));
778 p
->file_patterns
= g_strsplit(str
->str
, " ", -1);
779 g_string_free(str
, TRUE
);
790 static void run_dialog(GtkWidget
*dialog
, GtkWidget
*entry
)
792 /* set filename in the file chooser dialog */
793 const gchar
*utf8_filename
= gtk_entry_get_text(GTK_ENTRY(entry
));
794 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
796 if (g_path_is_absolute(locale_filename
))
798 if (g_file_test(locale_filename
, G_FILE_TEST_EXISTS
))
800 /* if the current filename is a directory, we must use
801 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
802 * we end up in the parent directory */
803 if (g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
804 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_filename
);
806 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
808 else /* if the file doesn't yet exist, use at least the current directory */
810 gchar
*locale_dir
= g_path_get_dirname(locale_filename
);
811 gchar
*name
= g_path_get_basename(utf8_filename
);
813 if (g_file_test(locale_dir
, G_FILE_TEST_EXISTS
))
814 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dir
);
815 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), name
);
821 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog
)) != GTK_FILE_CHOOSER_ACTION_OPEN
)
823 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
825 g_free(locale_filename
);
828 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
830 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
831 gchar
*tmp_utf8_filename
= utils_get_utf8_from_locale(filename
);
833 gtk_entry_set_text(GTK_ENTRY(entry
), tmp_utf8_filename
);
835 g_free(tmp_utf8_filename
);
838 gtk_widget_destroy(dialog
);
843 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
)
846 gchar
*path
= win32_show_project_open_dialog(e
->dialog
, _("Choose Project Filename"),
847 gtk_entry_get_text(GTK_ENTRY(e
->file_name
)), TRUE
, TRUE
);
850 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), path
);
856 /* initialise the dialog */
857 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL
,
858 GTK_FILE_CHOOSER_ACTION_SAVE
,
859 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
860 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
861 gtk_widget_set_name(dialog
, "GeanyDialogProject");
862 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
863 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
864 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
865 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
867 run_dialog(dialog
, e
->file_name
);
872 /* sets the project base path and the project file name according to the project name */
873 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
878 const gchar
*project_dir
= local_prefs
.project_file_path
;
880 if (entries_modified
)
883 name
= gtk_editable_get_chars(editable
, 0, -1);
886 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
887 name
, G_DIR_SEPARATOR_S
, NULL
);
888 if (project_prefs
.project_file_in_basedir
)
889 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, name
, G_DIR_SEPARATOR_S
,
890 name
, "." GEANY_PROJECT_EXT
, NULL
);
892 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
893 name
, "." GEANY_PROJECT_EXT
, NULL
);
897 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
898 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
902 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), base_path
);
903 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), file_name
);
905 entries_modified
= FALSE
;
912 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
914 entries_modified
= TRUE
;
918 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
)
920 gtk_widget_set_sensitive(spin_long_line
, gtk_toggle_button_get_active(radio
));
924 gboolean
project_load_file(const gchar
*locale_file_name
)
926 g_return_val_if_fail(locale_file_name
!= NULL
, FALSE
);
928 if (load_config(locale_file_name
))
930 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
932 ui_set_statusbar(TRUE
, _("Project \"%s\" opened."), app
->project
->name
);
934 ui_add_recent_project_file(utf8_filename
);
935 g_free(utf8_filename
);
940 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
942 ui_set_statusbar(TRUE
, _("Project file \"%s\" could not be loaded."), utf8_filename
);
943 g_free(utf8_filename
);
949 /* Reads the given filename and creates a new project with the data found in the file.
950 * At this point there should not be an already opened project in Geany otherwise it will just
952 * The filename is expected in the locale encoding. */
953 static gboolean
load_config(const gchar
*filename
)
958 /* there should not be an open project */
959 g_return_val_if_fail(app
->project
== NULL
&& filename
!= NULL
, FALSE
);
961 config
= g_key_file_new();
962 if (! g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
))
964 g_key_file_free(config
);
968 p
= create_project();
970 stash_group_load_from_key_file(indent_group
, config
);
972 p
->name
= utils_get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
973 p
->description
= utils_get_setting_string(config
, "project", "description", "");
974 p
->file_name
= utils_get_utf8_from_locale(filename
);
975 p
->base_path
= utils_get_setting_string(config
, "project", "base_path", "");
976 p
->file_patterns
= g_key_file_get_string_list(config
, "project", "file_patterns", NULL
, NULL
);
978 p
->long_line_behaviour
= utils_get_setting_integer(config
, "long line marker",
979 "long_line_behaviour", 1 /* follow global */);
980 p
->long_line_column
= utils_get_setting_integer(config
, "long line marker",
981 "long_line_column", editor_prefs
.long_line_column
);
982 apply_editor_prefs();
984 build_load_menu(config
, GEANY_BCS_PROJ
, (gpointer
)p
);
985 if (project_prefs
.project_session
)
987 /* save current (non-project) session (it could has been changed since program startup) */
988 configuration_save_default_session();
989 /* now close all open files */
990 document_close_all();
991 /* read session files so they can be opened with configuration_open_files() */
992 configuration_load_session_files(config
, FALSE
);
993 ui_focus_current_document();
995 g_signal_emit_by_name(geany_object
, "project-open", config
);
996 g_key_file_free(config
);
1003 static void apply_editor_prefs(void)
1008 editor_apply_update_prefs(documents
[i
]->editor
);
1012 /* Write the project settings as well as the project session files into its configuration files.
1013 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1014 * is called while closing a project, this is used to skip emitting the signal because
1015 * project-close will be emitted afterwards.
1016 * Returns: TRUE if project file was written successfully. */
1017 static gboolean
write_config(gboolean emit_signal
)
1023 gboolean ret
= FALSE
;
1025 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
1029 config
= g_key_file_new();
1030 /* try to load an existing config to keep manually added comments */
1031 filename
= utils_get_locale_from_utf8(p
->file_name
);
1032 g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
);
1034 stash_group_save_to_key_file(indent_group
, config
);
1036 g_key_file_set_string(config
, "project", "name", p
->name
);
1037 g_key_file_set_string(config
, "project", "base_path", p
->base_path
);
1040 g_key_file_set_string(config
, "project", "description", p
->description
);
1041 if (p
->file_patterns
)
1042 g_key_file_set_string_list(config
, "project", "file_patterns",
1043 (const gchar
**) p
->file_patterns
, g_strv_length(p
->file_patterns
));
1045 g_key_file_set_integer(config
, "long line marker", "long_line_behaviour", p
->long_line_behaviour
);
1046 g_key_file_set_integer(config
, "long line marker", "long_line_column", p
->long_line_column
);
1048 /* store the session files into the project too */
1049 if (project_prefs
.project_session
)
1050 configuration_save_session_files(config
);
1051 build_save_menu(config
, (gpointer
)p
, GEANY_BCS_PROJ
);
1054 g_signal_emit_by_name(geany_object
, "project-save", config
);
1056 /* write the file */
1057 data
= g_key_file_to_data(config
, NULL
, NULL
);
1058 ret
= (utils_write_file(filename
, data
) == 0);
1062 g_key_file_free(config
);
1068 /* Constructs the project's base path which is used for "Make all" and "Execute".
1069 * The result is an absolute string in UTF-8 encoding which is either the same as
1070 * base path if it is absolute or it is built out of project file name's dir and base_path.
1071 * If there is no project or project's base_path is invalid, NULL will be returned.
1072 * The returned string should be freed when no longer needed. */
1073 gchar
*project_get_base_path(void)
1075 GeanyProject
*project
= app
->project
;
1077 if (project
&& NZV(project
->base_path
))
1079 if (g_path_is_absolute(project
->base_path
))
1080 return g_strdup(project
->base_path
);
1082 { /* build base_path out of project file name's dir and base_path */
1084 gchar
*dir
= g_path_get_dirname(project
->file_name
);
1086 if (utils_str_equal(project
->base_path
, "./"))
1089 path
= g_strconcat(dir
, G_DIR_SEPARATOR_S
, project
->base_path
, NULL
);
1098 /* This is to save project-related global settings, NOT project file settings. */
1099 void project_save_prefs(GKeyFile
*config
)
1101 GeanyProject
*project
= app
->project
;
1103 if (cl_options
.load_session
)
1105 const gchar
*utf8_filename
= (project
== NULL
) ? "" : project
->file_name
;
1107 g_key_file_set_string(config
, "project", "session_file", utf8_filename
);
1109 g_key_file_set_string(config
, "project", "project_file_path",
1110 NVL(local_prefs
.project_file_path
, ""));
1114 void project_load_prefs(GKeyFile
*config
)
1116 if (cl_options
.load_session
)
1118 g_return_if_fail(project_prefs
.session_file
== NULL
);
1119 project_prefs
.session_file
= utils_get_setting_string(config
, "project",
1120 "session_file", "");
1122 local_prefs
.project_file_path
= utils_get_setting_string(config
, "project",
1123 "project_file_path", NULL
);
1124 if (local_prefs
.project_file_path
== NULL
)
1126 local_prefs
.project_file_path
= g_strconcat(g_get_home_dir(),
1127 G_DIR_SEPARATOR_S
, PROJECT_DIR
, NULL
);
1132 /* Initialize project-related preferences in the Preferences dialog. */
1133 void project_setup_prefs(void)
1135 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1136 GtkWidget
*path_btn
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_button");
1137 static gboolean callback_setup
= FALSE
;
1139 g_return_if_fail(local_prefs
.project_file_path
!= NULL
);
1141 gtk_entry_set_text(GTK_ENTRY(path_entry
), local_prefs
.project_file_path
);
1142 if (! callback_setup
)
1143 { /* connect the callback only once */
1144 callback_setup
= TRUE
;
1145 ui_setup_open_button_callback(path_btn
, NULL
,
1146 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(path_entry
));
1151 /* Update project-related preferences after using the Preferences dialog. */
1152 void project_apply_prefs(void)
1154 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1157 str
= gtk_entry_get_text(GTK_ENTRY(path_entry
));
1158 setptr(local_prefs
.project_file_path
, g_strdup(str
));
1162 void project_init(void)
1166 group
= stash_group_new("indentation");
1167 /* defaults are copied from editor indent prefs */
1168 stash_group_set_use_defaults(group
, FALSE
);
1169 indent_group
= group
;
1171 stash_group_add_spin_button_integer(group
, &indentation
.width
,
1172 "indent_width", 4, "spin_indent_width_project");
1173 stash_group_add_radio_buttons(group
, (gint
*)(gpointer
)&indentation
.type
,
1174 "indent_type", GEANY_INDENT_TYPE_TABS
,
1175 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES
,
1176 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS
,
1177 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH
,
1179 /* This is a 'hidden' pref for backwards-compatibility */
1180 stash_group_add_integer(group
, &indentation
.hard_tab_width
,
1181 "indent_hard_tab_width", 8);
1182 stash_group_add_toggle_button(group
, &indentation
.detect_type
,
1183 "detect_indent", FALSE
, "check_detect_indent_type_project");
1184 stash_group_add_toggle_button(group
, &indentation
.detect_width
,
1185 "detect_indent_width", FALSE
, "check_detect_indent_width_project");
1186 stash_group_add_combo_box(group
, (gint
*)(gpointer
)&indentation
.auto_indent_mode
,
1187 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS
, "combo_auto_indent_mode_project");
1191 void project_finalize(void)
1193 stash_group_free(indent_group
);