2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
37 #include "filetypesprivate.h"
38 #include "geanyobject.h"
41 #include "projectprivate.h"
53 ProjectPrefs project_prefs
= { NULL
, FALSE
, FALSE
};
56 static GeanyProjectPrivate priv
;
57 static GeanyIndentPrefs indentation
;
59 static GSList
*stash_groups
= NULL
;
63 gchar
*project_file_path
; /* in UTF-8 */
64 } local_prefs
= { NULL
};
66 static gboolean entries_modified
;
68 /* simple struct to keep references to the elements of the properties dialog */
69 typedef struct _PropertyDialogElements
74 GtkWidget
*description
;
78 BuildTableData build_properties
;
80 } PropertyDialogElements
;
83 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
);
84 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
);
85 static gboolean
load_config(const gchar
*filename
);
86 static gboolean
write_config(gboolean emit_signal
);
87 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
88 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
89 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
);
90 static void apply_editor_prefs(void);
91 static void init_stash_prefs(void);
94 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
95 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
96 #define MAX_NAME_LEN 50
97 /* "projects" is part of the default project base path so be careful when translating
98 * please avoid special characters and spaces, look at the source for details or ask Frank */
99 #define PROJECT_DIR _("projects")
102 /* TODO: this should be ported to Glade like the project preferences dialog,
103 * then we can get rid of the PropertyDialogElements struct altogether as
104 * widgets pointers can be accessed through ui_lookup_widget(). */
105 void project_new(void)
113 PropertyDialogElements
*e
;
115 if (! project_ask_close())
118 g_return_if_fail(app
->project
== NULL
);
120 e
= g_new0(PropertyDialogElements
, 1);
121 e
->dialog
= gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets
.window
),
122 GTK_DIALOG_DESTROY_WITH_PARENT
,
123 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
125 gtk_widget_set_name(e
->dialog
, "GeanyDialogProject");
126 bbox
= gtk_hbox_new(FALSE
, 0);
127 button
= gtk_button_new();
128 gtk_widget_set_can_default(button
, TRUE
);
129 gtk_window_set_default(GTK_WINDOW(e
->dialog
), button
);
130 image
= gtk_image_new_from_stock(GTK_STOCK_NEW
, GTK_ICON_SIZE_BUTTON
);
131 label
= gtk_label_new_with_mnemonic(_("C_reate"));
132 gtk_box_pack_start(GTK_BOX(bbox
), image
, FALSE
, FALSE
, 3);
133 gtk_box_pack_start(GTK_BOX(bbox
), label
, FALSE
, FALSE
, 3);
134 gtk_container_add(GTK_CONTAINER(button
), bbox
);
135 gtk_dialog_add_action_widget(GTK_DIALOG(e
->dialog
), button
, GTK_RESPONSE_OK
);
137 vbox
= ui_dialog_vbox_new(GTK_DIALOG(e
->dialog
));
139 entries_modified
= FALSE
;
141 table
= gtk_table_new(3, 2, FALSE
);
142 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
143 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
145 label
= gtk_label_new(_("Name:"));
146 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
148 e
->name
= gtk_entry_new();
149 gtk_entry_set_activates_default(GTK_ENTRY(e
->name
), TRUE
);
150 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
151 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
153 ui_table_add_row(GTK_TABLE(table
), 0, label
, e
->name
, NULL
);
155 label
= gtk_label_new(_("Filename:"));
156 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
158 e
->file_name
= gtk_entry_new();
159 gtk_entry_set_activates_default(GTK_ENTRY(e
->file_name
), TRUE
);
160 ui_entry_add_clear_icon(GTK_ENTRY(e
->file_name
));
161 gtk_entry_set_width_chars(GTK_ENTRY(e
->file_name
), 30);
162 button
= gtk_button_new();
163 g_signal_connect(button
, "clicked", G_CALLBACK(on_file_save_button_clicked
), e
);
164 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
165 gtk_container_add(GTK_CONTAINER(button
), image
);
166 bbox
= gtk_hbox_new(FALSE
, 6);
167 gtk_box_pack_start(GTK_BOX(bbox
), e
->file_name
, TRUE
, TRUE
, 0);
168 gtk_box_pack_start(GTK_BOX(bbox
), button
, FALSE
, FALSE
, 0);
170 ui_table_add_row(GTK_TABLE(table
), 1, label
, bbox
, NULL
);
172 label
= gtk_label_new(_("Base path:"));
173 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
175 e
->base_path
= gtk_entry_new();
176 gtk_entry_set_activates_default(GTK_ENTRY(e
->base_path
), TRUE
);
177 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
178 gtk_widget_set_tooltip_text(e
->base_path
,
179 _("Base directory of all files that make up the project. "
180 "This can be a new path, or an existing directory tree. "
181 "You can use paths relative to the project filename."));
182 bbox
= ui_path_box_new(_("Choose Project Base Path"),
183 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
->base_path
));
185 ui_table_add_row(GTK_TABLE(table
), 2, label
, bbox
, NULL
);
187 gtk_box_pack_start(GTK_BOX(vbox
), table
, TRUE
, TRUE
, 0);
190 g_signal_connect(e
->name
, "changed", G_CALLBACK(on_name_entry_changed
), e
);
191 /* run the callback manually to initialise the base_path and file_name fields */
192 on_name_entry_changed(GTK_EDITABLE(e
->name
), e
);
194 g_signal_connect(e
->file_name
, "changed", G_CALLBACK(on_entries_changed
), e
);
195 g_signal_connect(e
->base_path
, "changed", G_CALLBACK(on_entries_changed
), e
);
197 gtk_widget_show_all(e
->dialog
);
199 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
)
201 if (update_config(e
, TRUE
))
203 if (!write_config(TRUE
))
204 SHOW_ERR(_("Project file could not be written"));
207 ui_set_statusbar(TRUE
, _("Project \"%s\" created."), app
->project
->name
);
209 ui_add_recent_project_file(app
->project
->file_name
);
214 gtk_widget_destroy(e
->dialog
);
219 gboolean
project_load_file_with_session(const gchar
*locale_file_name
)
221 if (project_load_file(locale_file_name
))
223 if (project_prefs
.project_session
)
225 configuration_open_files();
226 /* open a new file if no other file was opened */
227 document_new_file_if_non_open();
228 ui_focus_current_document();
237 static void run_open_dialog(GtkDialog
*dialog
)
239 while (gtk_dialog_run(dialog
) == GTK_RESPONSE_ACCEPT
)
241 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
243 /* try to load the config */
244 if (! project_load_file_with_session(filename
))
246 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
248 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename
);
249 gtk_widget_grab_focus(GTK_WIDGET(dialog
));
250 g_free(utf8_filename
);
261 void project_open(void)
263 const gchar
*dir
= local_prefs
.project_file_path
;
268 GtkFileFilter
*filter
;
271 if (! project_ask_close()) return;
274 file
= win32_show_project_open_dialog(main_widgets
.window
, _("Open Project"), dir
, FALSE
, TRUE
);
277 /* try to load the config */
278 if (! project_load_file_with_session(file
))
280 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file
);
286 dialog
= gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets
.window
),
287 GTK_FILE_CHOOSER_ACTION_OPEN
,
288 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
289 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
290 gtk_widget_set_name(dialog
, "GeanyDialogProject");
292 /* set default Open, so pressing enter can open multiple files */
293 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
294 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
295 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
296 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
297 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(main_widgets
.window
));
298 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog
), TRUE
);
300 /* add FileFilters */
301 filter
= gtk_file_filter_new();
302 gtk_file_filter_set_name(filter
, _("All files"));
303 gtk_file_filter_add_pattern(filter
, "*");
304 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
305 filter
= gtk_file_filter_new();
306 gtk_file_filter_set_name(filter
, _("Project files"));
307 gtk_file_filter_add_pattern(filter
, "*." GEANY_PROJECT_EXT
);
308 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
309 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog
), filter
);
311 locale_path
= utils_get_locale_from_utf8(dir
);
312 if (g_file_test(locale_path
, G_FILE_TEST_EXISTS
) &&
313 g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
315 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
319 gtk_widget_show_all(dialog
);
320 run_open_dialog(GTK_DIALOG(dialog
));
321 gtk_widget_destroy(GTK_WIDGET(dialog
));
326 /* Called when creating, opening, closing and updating projects. */
327 static void update_ui(void)
329 if (main_status
.quitting
)
332 ui_set_window_title(NULL
);
333 build_menu_update(NULL
);
334 sidebar_openfiles_update_all();
338 static void remove_foreach_project_filetype(gpointer data
, gpointer user_data
)
340 GeanyFiletype
*ft
= data
;
343 SETPTR(ft
->priv
->projfilecmds
, NULL
);
344 SETPTR(ft
->priv
->projexeccmds
, NULL
);
345 SETPTR(ft
->priv
->projerror_regex_string
, NULL
);
346 ft
->priv
->project_list_entry
= -1;
351 /* open_default will make function reload default session files on close */
352 void project_close(gboolean open_default
)
356 g_return_if_fail(app
->project
!= NULL
);
358 /* save project session files, etc */
359 if (!write_config(FALSE
))
360 g_warning("Project file \"%s\" could not be written", app
->project
->file_name
);
362 if (project_prefs
.project_session
)
364 /* close all existing tabs first */
365 if (!document_close_all())
368 ui_set_statusbar(TRUE
, _("Project \"%s\" closed."), app
->project
->name
);
370 /* remove project filetypes build entries */
371 if (app
->project
->priv
->build_filetypes_list
!= NULL
)
373 g_ptr_array_foreach(app
->project
->priv
->build_filetypes_list
, remove_foreach_project_filetype
, NULL
);
374 g_ptr_array_free(app
->project
->priv
->build_filetypes_list
, FALSE
);
377 /* remove project non filetype build menu items */
378 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_NON_FT
, -1);
379 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_EXEC
, -1);
381 g_free(app
->project
->name
);
382 g_free(app
->project
->description
);
383 g_free(app
->project
->file_name
);
384 g_free(app
->project
->base_path
);
386 g_free(app
->project
);
389 foreach_slist(node
, stash_groups
)
390 stash_group_free(node
->data
);
392 g_slist_free(stash_groups
);
395 apply_editor_prefs(); /* ensure that global settings are restored */
397 if (project_prefs
.project_session
)
399 /* after closing all tabs let's open the tabs found in the default config */
400 if (open_default
&& cl_options
.load_session
)
402 configuration_reload_default_session();
403 configuration_open_files();
404 /* open a new file if no other file was opened */
405 document_new_file_if_non_open();
406 ui_focus_current_document();
409 g_signal_emit_by_name(geany_object
, "project-close");
415 /* Shows the file chooser dialog when base path button is clicked
416 * FIXME: this should be connected in Glade but 3.8.1 has a bug
417 * where it won't pass any objects as user data (#588824). */
419 on_project_properties_base_path_button_clicked(GtkWidget
*button
,
420 GtkWidget
*base_path_entry
)
424 g_return_if_fail(base_path_entry
!= NULL
);
425 g_return_if_fail(GTK_IS_WIDGET(base_path_entry
));
427 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
428 NULL
, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
429 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
430 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
433 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
435 gtk_entry_set_text(GTK_ENTRY(base_path_entry
),
436 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
)));
439 gtk_widget_destroy(dialog
);
443 static void insert_build_page(PropertyDialogElements
*e
)
445 GtkWidget
*build_table
, *label
;
446 GeanyDocument
*doc
= document_get_current();
447 GeanyFiletype
*ft
= NULL
;
452 build_table
= build_commands_table(doc
, GEANY_BCS_PROJ
, &(e
->build_properties
), ft
);
453 gtk_container_set_border_width(GTK_CONTAINER(build_table
), 6);
454 label
= gtk_label_new(_("Build"));
455 e
->build_page_num
= gtk_notebook_append_page(GTK_NOTEBOOK(e
->notebook
),
460 static void create_properties_dialog(PropertyDialogElements
*e
)
462 GtkWidget
*base_path_button
;
463 static guint base_path_button_handler_id
= 0;
464 static guint radio_long_line_handler_id
= 0;
466 e
->dialog
= create_project_dialog();
467 e
->notebook
= ui_lookup_widget(e
->dialog
, "project_notebook");
468 e
->file_name
= ui_lookup_widget(e
->dialog
, "label_project_dialog_filename");
469 e
->name
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_name");
470 e
->description
= ui_lookup_widget(e
->dialog
, "textview_project_dialog_description");
471 e
->base_path
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_base_path");
472 e
->patterns
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_file_patterns");
474 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
476 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
477 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
478 ui_entry_add_clear_icon(GTK_ENTRY(e
->patterns
));
480 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
481 if (base_path_button_handler_id
== 0)
483 base_path_button
= ui_lookup_widget(e
->dialog
, "button_project_dialog_base_path");
484 base_path_button_handler_id
=
485 g_signal_connect(base_path_button
, "clicked",
486 G_CALLBACK(on_project_properties_base_path_button_clicked
),
490 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
491 if (radio_long_line_handler_id
== 0)
493 radio_long_line_handler_id
=
494 g_signal_connect(ui_lookup_widget(e
->dialog
,
495 "radio_long_line_custom_project"), "toggled",
496 G_CALLBACK(on_radio_long_line_custom_toggled
),
497 ui_lookup_widget(e
->dialog
, "spin_long_line_project"));
502 static void show_project_properties(gboolean show_build
)
504 GeanyProject
*p
= app
->project
;
505 GtkWidget
*widget
= NULL
;
506 GtkWidget
*radio_long_line_custom
;
507 static PropertyDialogElements e
;
510 g_return_if_fail(app
->project
!= NULL
);
512 entries_modified
= FALSE
;
514 if (e
.dialog
== NULL
)
515 create_properties_dialog(&e
);
517 insert_build_page(&e
);
519 foreach_slist(node
, stash_groups
)
520 stash_group_display(node
->data
, e
.dialog
);
522 /* fill the elements with the appropriate data */
523 gtk_entry_set_text(GTK_ENTRY(e
.name
), p
->name
);
524 gtk_label_set_text(GTK_LABEL(e
.file_name
), p
->file_name
);
525 gtk_entry_set_text(GTK_ENTRY(e
.base_path
), p
->base_path
);
527 radio_long_line_custom
= ui_lookup_widget(e
.dialog
, "radio_long_line_custom_project");
528 switch (p
->priv
->long_line_behaviour
)
530 case 0: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_disabled_project"); break;
531 case 1: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_default_project"); break;
532 case 2: widget
= radio_long_line_custom
; break;
534 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
536 widget
= ui_lookup_widget(e
.dialog
, "spin_long_line_project");
537 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), (gdouble
)p
->priv
->long_line_column
);
538 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom
), widget
);
540 if (p
->description
!= NULL
)
542 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
.description
));
543 gtk_text_buffer_set_text(buffer
, p
->description
, -1);
546 if (p
->file_patterns
!= NULL
)
547 { /* set the file patterns */
550 str
= g_strjoinv(" ", p
->file_patterns
);
551 gtk_entry_set_text(GTK_ENTRY(e
.patterns
), str
);
555 g_signal_emit_by_name(geany_object
, "project-dialog-open", e
.notebook
);
556 gtk_widget_show_all(e
.dialog
);
558 /* note: notebook page must be shown before setting current page */
560 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
562 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), 0);
564 while (gtk_dialog_run(GTK_DIALOG(e
.dialog
)) == GTK_RESPONSE_OK
)
566 if (update_config(&e
, FALSE
))
568 g_signal_emit_by_name(geany_object
, "project-dialog-confirmed", e
.notebook
);
569 if (!write_config(TRUE
))
570 SHOW_ERR(_("Project file could not be written"));
573 ui_set_statusbar(TRUE
, _("Project \"%s\" saved."), app
->project
->name
);
579 build_free_fields(e
.build_properties
);
580 g_signal_emit_by_name(geany_object
, "project-dialog-close", e
.notebook
);
581 gtk_notebook_remove_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
582 gtk_widget_hide(e
.dialog
);
586 void project_properties(void)
588 show_project_properties(FALSE
);
592 void project_build_properties(void)
594 show_project_properties(TRUE
);
598 /* checks whether there is an already open project and asks the user if he wants to close it or
599 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
600 * and TRUE if we can go ahead */
601 gboolean
project_ask_close(void)
603 if (app
->project
!= NULL
)
605 if (dialogs_show_question_full(NULL
, GTK_STOCK_CLOSE
, GTK_STOCK_CANCEL
,
606 _("Do you want to close it before proceeding?"),
607 _("The '%s' project is open."), app
->project
->name
))
609 project_close(FALSE
);
620 static GeanyProject
*create_project(void)
622 GeanyProject
*project
= g_new0(GeanyProject
, 1);
624 memset(&priv
, 0, sizeof priv
);
625 priv
.indentation
= &indentation
;
626 project
->priv
= &priv
;
630 project
->file_patterns
= NULL
;
632 project
->priv
->long_line_behaviour
= 1 /* use global settings */;
633 project
->priv
->long_line_column
= editor_prefs
.long_line_column
;
635 app
->project
= project
;
640 /* Verifies data for New & Properties dialogs.
641 * Returns: FALSE if the user needs to change any data. */
642 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
)
644 const gchar
*name
, *file_name
, *base_path
;
645 gchar
*locale_filename
;
650 g_return_val_if_fail(e
!= NULL
, TRUE
);
652 name
= gtk_entry_get_text(GTK_ENTRY(e
->name
));
653 name_len
= strlen(name
);
656 SHOW_ERR(_("The specified project name is too short."));
657 gtk_widget_grab_focus(e
->name
);
660 else if (name_len
> MAX_NAME_LEN
)
662 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN
);
663 gtk_widget_grab_focus(e
->name
);
668 file_name
= gtk_entry_get_text(GTK_ENTRY(e
->file_name
));
670 file_name
= gtk_label_get_text(GTK_LABEL(e
->file_name
));
672 if (G_UNLIKELY(EMPTY(file_name
)))
674 SHOW_ERR(_("You have specified an invalid project filename."));
675 gtk_widget_grab_focus(e
->file_name
);
679 locale_filename
= utils_get_locale_from_utf8(file_name
);
680 base_path
= gtk_entry_get_text(GTK_ENTRY(e
->base_path
));
681 if (!EMPTY(base_path
))
682 { /* check whether the given directory actually exists */
683 gchar
*locale_path
= utils_get_locale_from_utf8(base_path
);
685 if (! g_path_is_absolute(locale_path
))
686 { /* relative base path, so add base dir of project file name */
687 gchar
*dir
= g_path_get_dirname(locale_filename
);
688 SETPTR(locale_path
, g_strconcat(dir
, locale_path
, NULL
));
692 if (! g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
696 create_dir
= dialogs_show_question_full(NULL
, GTK_STOCK_OK
, GTK_STOCK_CANCEL
,
697 _("Create the project's base path directory?"),
698 _("The path \"%s\" does not exist."),
702 err_code
= utils_mkdir(locale_path
, TRUE
);
704 if (! create_dir
|| err_code
!= 0)
707 SHOW_ERR1(_("Project base directory could not be created (%s)."),
708 g_strerror(err_code
));
709 gtk_widget_grab_focus(e
->base_path
);
710 utils_free_pointers(2, locale_path
, locale_filename
, NULL
);
716 /* finally test whether the given project file can be written */
717 if ((err_code
= utils_is_file_writable(locale_filename
)) != 0 ||
718 (err_code
= g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
) ? EISDIR
: 0) != 0)
720 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code
));
721 gtk_widget_grab_focus(e
->file_name
);
722 g_free(locale_filename
);
725 g_free(locale_filename
);
727 if (app
->project
== NULL
)
734 SETPTR(p
->name
, g_strdup(name
));
735 SETPTR(p
->file_name
, g_strdup(file_name
));
736 /* use "." if base_path is empty */
737 SETPTR(p
->base_path
, g_strdup(!EMPTY(base_path
) ? base_path
: "./"));
739 if (! new_project
) /* save properties specific fields */
741 GtkTextIter start
, end
;
742 GtkTextBuffer
*buffer
;
743 GeanyDocument
*doc
= document_get_current();
744 GeanyBuildCommand
*oldvalue
;
745 GeanyFiletype
*ft
= doc
? doc
->file_type
: NULL
;
751 /* get and set the project description */
752 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
753 gtk_text_buffer_get_start_iter(buffer
, &start
);
754 gtk_text_buffer_get_end_iter(buffer
, &end
);
755 SETPTR(p
->description
, g_strdup(gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
)));
757 foreach_slist(node
, stash_groups
)
758 stash_group_update(node
->data
, e
->dialog
);
760 /* read the project build menu */
761 oldvalue
= ft
? ft
->priv
->projfilecmds
: NULL
;
762 build_read_project(ft
, e
->build_properties
);
764 if (ft
!= NULL
&& ft
->priv
->projfilecmds
!= oldvalue
&& ft
->priv
->project_list_entry
< 0)
766 if (p
->priv
->build_filetypes_list
== NULL
)
767 p
->priv
->build_filetypes_list
= g_ptr_array_new();
768 ft
->priv
->project_list_entry
= p
->priv
->build_filetypes_list
->len
;
769 g_ptr_array_add(p
->priv
->build_filetypes_list
, ft
);
771 build_menu_update(doc
);
773 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled_project");
774 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
775 p
->priv
->long_line_behaviour
= 0;
778 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default_project");
779 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
780 p
->priv
->long_line_behaviour
= 1;
782 /* "Custom" radio button must be checked */
783 p
->priv
->long_line_behaviour
= 2;
786 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line_project");
787 p
->priv
->long_line_column
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
788 apply_editor_prefs();
790 /* get and set the project file patterns */
791 tmp
= g_strdup(gtk_entry_get_text(GTK_ENTRY(e
->patterns
)));
792 g_strfreev(p
->file_patterns
);
794 str
= g_string_new(tmp
);
795 do {} while (utils_string_replace_all(str
, " ", " "));
796 p
->file_patterns
= g_strsplit(str
->str
, " ", -1);
797 g_string_free(str
, TRUE
);
808 static void run_dialog(GtkWidget
*dialog
, GtkWidget
*entry
)
810 /* set filename in the file chooser dialog */
811 const gchar
*utf8_filename
= gtk_entry_get_text(GTK_ENTRY(entry
));
812 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
814 if (g_path_is_absolute(locale_filename
))
816 if (g_file_test(locale_filename
, G_FILE_TEST_EXISTS
))
818 /* if the current filename is a directory, we must use
819 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
820 * we end up in the parent directory */
821 if (g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
822 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_filename
);
824 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
826 else /* if the file doesn't yet exist, use at least the current directory */
828 gchar
*locale_dir
= g_path_get_dirname(locale_filename
);
829 gchar
*name
= g_path_get_basename(utf8_filename
);
831 if (g_file_test(locale_dir
, G_FILE_TEST_EXISTS
))
832 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dir
);
833 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), name
);
839 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog
)) != GTK_FILE_CHOOSER_ACTION_OPEN
)
841 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
843 g_free(locale_filename
);
846 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
848 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
849 gchar
*tmp_utf8_filename
= utils_get_utf8_from_locale(filename
);
851 gtk_entry_set_text(GTK_ENTRY(entry
), tmp_utf8_filename
);
853 g_free(tmp_utf8_filename
);
856 gtk_widget_destroy(dialog
);
861 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
)
864 gchar
*path
= win32_show_project_open_dialog(e
->dialog
, _("Choose Project Filename"),
865 gtk_entry_get_text(GTK_ENTRY(e
->file_name
)), TRUE
, TRUE
);
868 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), path
);
874 /* initialise the dialog */
875 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL
,
876 GTK_FILE_CHOOSER_ACTION_SAVE
,
877 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
878 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
879 gtk_widget_set_name(dialog
, "GeanyDialogProject");
880 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
881 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
882 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
883 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
885 run_dialog(dialog
, e
->file_name
);
890 /* sets the project base path and the project file name according to the project name */
891 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
896 const gchar
*project_dir
= local_prefs
.project_file_path
;
898 if (entries_modified
)
901 name
= gtk_editable_get_chars(editable
, 0, -1);
904 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
905 name
, G_DIR_SEPARATOR_S
, NULL
);
906 if (project_prefs
.project_file_in_basedir
)
907 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, name
, G_DIR_SEPARATOR_S
,
908 name
, "." GEANY_PROJECT_EXT
, NULL
);
910 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
911 name
, "." GEANY_PROJECT_EXT
, NULL
);
915 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
916 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
920 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), base_path
);
921 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), file_name
);
923 entries_modified
= FALSE
;
930 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
932 entries_modified
= TRUE
;
936 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
)
938 gtk_widget_set_sensitive(spin_long_line
, gtk_toggle_button_get_active(radio
));
942 gboolean
project_load_file(const gchar
*locale_file_name
)
944 g_return_val_if_fail(locale_file_name
!= NULL
, FALSE
);
946 if (load_config(locale_file_name
))
948 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
950 ui_set_statusbar(TRUE
, _("Project \"%s\" opened."), app
->project
->name
);
952 ui_add_recent_project_file(utf8_filename
);
953 g_free(utf8_filename
);
958 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
960 ui_set_statusbar(TRUE
, _("Project file \"%s\" could not be loaded."), utf8_filename
);
961 g_free(utf8_filename
);
967 /* Reads the given filename and creates a new project with the data found in the file.
968 * At this point there should not be an already opened project in Geany otherwise it will just
970 * The filename is expected in the locale encoding. */
971 static gboolean
load_config(const gchar
*filename
)
977 /* there should not be an open project */
978 g_return_val_if_fail(app
->project
== NULL
&& filename
!= NULL
, FALSE
);
980 config
= g_key_file_new();
981 if (! g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
))
983 g_key_file_free(config
);
987 p
= create_project();
989 foreach_slist(node
, stash_groups
)
990 stash_group_load_from_key_file(node
->data
, config
);
992 p
->name
= utils_get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
993 p
->description
= utils_get_setting_string(config
, "project", "description", "");
994 p
->file_name
= utils_get_utf8_from_locale(filename
);
995 p
->base_path
= utils_get_setting_string(config
, "project", "base_path", "");
996 p
->file_patterns
= g_key_file_get_string_list(config
, "project", "file_patterns", NULL
, NULL
);
998 p
->priv
->long_line_behaviour
= utils_get_setting_integer(config
, "long line marker",
999 "long_line_behaviour", 1 /* follow global */);
1000 p
->priv
->long_line_column
= utils_get_setting_integer(config
, "long line marker",
1001 "long_line_column", editor_prefs
.long_line_column
);
1002 apply_editor_prefs();
1004 build_load_menu(config
, GEANY_BCS_PROJ
, (gpointer
)p
);
1005 if (project_prefs
.project_session
)
1007 /* save current (non-project) session (it could has been changed since program startup) */
1008 configuration_save_default_session();
1009 /* now close all open files */
1010 document_close_all();
1011 /* read session files so they can be opened with configuration_open_files() */
1012 configuration_load_session_files(config
, FALSE
);
1013 ui_focus_current_document();
1015 g_signal_emit_by_name(geany_object
, "project-open", config
);
1016 g_key_file_free(config
);
1023 static void apply_editor_prefs(void)
1028 editor_apply_update_prefs(documents
[i
]->editor
);
1032 /* Write the project settings as well as the project session files into its configuration files.
1033 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1034 * is called while closing a project, this is used to skip emitting the signal because
1035 * project-close will be emitted afterwards.
1036 * Returns: TRUE if project file was written successfully. */
1037 static gboolean
write_config(gboolean emit_signal
)
1043 gboolean ret
= FALSE
;
1046 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
1050 config
= g_key_file_new();
1051 /* try to load an existing config to keep manually added comments */
1052 filename
= utils_get_locale_from_utf8(p
->file_name
);
1053 g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
);
1055 foreach_slist(node
, stash_groups
)
1056 stash_group_save_to_key_file(node
->data
, config
);
1058 g_key_file_set_string(config
, "project", "name", p
->name
);
1059 g_key_file_set_string(config
, "project", "base_path", p
->base_path
);
1062 g_key_file_set_string(config
, "project", "description", p
->description
);
1063 if (p
->file_patterns
)
1064 g_key_file_set_string_list(config
, "project", "file_patterns",
1065 (const gchar
**) p
->file_patterns
, g_strv_length(p
->file_patterns
));
1067 g_key_file_set_integer(config
, "long line marker", "long_line_behaviour", p
->priv
->long_line_behaviour
);
1068 g_key_file_set_integer(config
, "long line marker", "long_line_column", p
->priv
->long_line_column
);
1070 /* store the session files into the project too */
1071 if (project_prefs
.project_session
)
1072 configuration_save_session_files(config
);
1073 build_save_menu(config
, (gpointer
)p
, GEANY_BCS_PROJ
);
1076 g_signal_emit_by_name(geany_object
, "project-save", config
);
1078 /* write the file */
1079 data
= g_key_file_to_data(config
, NULL
, NULL
);
1080 ret
= (utils_write_file(filename
, data
) == 0);
1084 g_key_file_free(config
);
1090 /* Constructs the project's base path which is used for "Make all" and "Execute".
1091 * The result is an absolute string in UTF-8 encoding which is either the same as
1092 * base path if it is absolute or it is built out of project file name's dir and base_path.
1093 * If there is no project or project's base_path is invalid, NULL will be returned.
1094 * The returned string should be freed when no longer needed. */
1095 gchar
*project_get_base_path(void)
1097 GeanyProject
*project
= app
->project
;
1099 if (project
&& !EMPTY(project
->base_path
))
1101 if (g_path_is_absolute(project
->base_path
))
1102 return g_strdup(project
->base_path
);
1104 { /* build base_path out of project file name's dir and base_path */
1106 gchar
*dir
= g_path_get_dirname(project
->file_name
);
1108 if (utils_str_equal(project
->base_path
, "./"))
1111 path
= g_build_filename(dir
, project
->base_path
, NULL
);
1120 /* This is to save project-related global settings, NOT project file settings. */
1121 void project_save_prefs(GKeyFile
*config
)
1123 GeanyProject
*project
= app
->project
;
1125 if (cl_options
.load_session
)
1127 const gchar
*utf8_filename
= (project
== NULL
) ? "" : project
->file_name
;
1129 g_key_file_set_string(config
, "project", "session_file", utf8_filename
);
1131 g_key_file_set_string(config
, "project", "project_file_path",
1132 FALLBACK(local_prefs
.project_file_path
, ""));
1136 void project_load_prefs(GKeyFile
*config
)
1138 if (cl_options
.load_session
)
1140 g_return_if_fail(project_prefs
.session_file
== NULL
);
1141 project_prefs
.session_file
= utils_get_setting_string(config
, "project",
1142 "session_file", "");
1144 local_prefs
.project_file_path
= utils_get_setting_string(config
, "project",
1145 "project_file_path", NULL
);
1146 if (local_prefs
.project_file_path
== NULL
)
1148 local_prefs
.project_file_path
= g_build_filename(g_get_home_dir(), PROJECT_DIR
, NULL
);
1153 /* Initialize project-related preferences in the Preferences dialog. */
1154 void project_setup_prefs(void)
1156 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1157 GtkWidget
*path_btn
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_button");
1158 static gboolean callback_setup
= FALSE
;
1160 g_return_if_fail(local_prefs
.project_file_path
!= NULL
);
1162 gtk_entry_set_text(GTK_ENTRY(path_entry
), local_prefs
.project_file_path
);
1163 if (! callback_setup
)
1164 { /* connect the callback only once */
1165 callback_setup
= TRUE
;
1166 ui_setup_open_button_callback(path_btn
, NULL
,
1167 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(path_entry
));
1172 /* Update project-related preferences after using the Preferences dialog. */
1173 void project_apply_prefs(void)
1175 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1178 str
= gtk_entry_get_text(GTK_ENTRY(path_entry
));
1179 SETPTR(local_prefs
.project_file_path
, g_strdup(str
));
1183 static void add_stash_group(StashGroup
*group
)
1185 stash_groups
= g_slist_prepend(stash_groups
, group
);
1189 static void init_stash_prefs(void)
1194 group
= stash_group_new("indentation");
1195 /* copy global defaults */
1196 indentation
= *editor_get_indent_prefs(NULL
);
1197 stash_group_set_use_defaults(group
, FALSE
);
1198 add_stash_group(group
);
1200 stash_group_add_spin_button_integer(group
, &indentation
.width
,
1201 "indent_width", 4, "spin_indent_width_project");
1202 stash_group_add_radio_buttons(group
, (gint
*)(gpointer
)&indentation
.type
,
1203 "indent_type", GEANY_INDENT_TYPE_TABS
,
1204 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES
,
1205 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS
,
1206 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH
,
1208 /* This is a 'hidden' pref for backwards-compatibility */
1209 stash_group_add_integer(group
, &indentation
.hard_tab_width
,
1210 "indent_hard_tab_width", 8);
1211 stash_group_add_toggle_button(group
, &indentation
.detect_type
,
1212 "detect_indent", FALSE
, "check_detect_indent_type_project");
1213 stash_group_add_toggle_button(group
, &indentation
.detect_width
,
1214 "detect_indent_width", FALSE
, "check_detect_indent_width_project");
1215 stash_group_add_combo_box(group
, (gint
*)(gpointer
)&indentation
.auto_indent_mode
,
1216 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS
, "combo_auto_indent_mode_project");
1218 group
= stash_group_new("file_prefs");
1219 stash_group_add_toggle_button(group
, &priv
.final_new_line
,
1220 "final_new_line", file_prefs
.final_new_line
, "check_new_line1");
1221 stash_group_add_toggle_button(group
, &priv
.ensure_convert_new_lines
,
1222 "ensure_convert_new_lines", file_prefs
.ensure_convert_new_lines
, "check_ensure_convert_new_lines1");
1223 stash_group_add_toggle_button(group
, &priv
.strip_trailing_spaces
,
1224 "strip_trailing_spaces", file_prefs
.strip_trailing_spaces
, "check_trailing_spaces1");
1225 stash_group_add_toggle_button(group
, &priv
.replace_tabs
,
1226 "replace_tabs", file_prefs
.replace_tabs
, "check_replace_tabs1");
1227 add_stash_group(group
);
1228 /* apply defaults */
1229 kf
= g_key_file_new();
1230 stash_group_load_from_key_file(group
, kf
);
1231 g_key_file_free(kf
);
1235 #define COPY_PREF(dest, prefname)\
1236 (dest.prefname = priv.prefname)
1238 const GeanyFilePrefs
*project_get_file_prefs(void)
1240 static GeanyFilePrefs fp
;
1246 COPY_PREF(fp
, final_new_line
);
1247 COPY_PREF(fp
, ensure_convert_new_lines
);
1248 COPY_PREF(fp
, strip_trailing_spaces
);
1249 COPY_PREF(fp
, replace_tabs
);
1254 void project_init(void)
1259 void project_finalize(void)