2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007 The Geany contributors
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
36 #include "filetypesprivate.h"
37 #include "geanyobject.h"
40 #include "projectprivate.h"
53 ProjectPrefs project_prefs
= { NULL
, 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 /* simple struct to keep references to the elements of the properties dialog */
67 typedef struct _PropertyDialogElements
72 GtkWidget
*description
;
76 BuildTableData build_properties
;
78 gboolean entries_modified
;
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(void);
86 static void update_new_project_dlg(GtkEditable
*editable
, PropertyDialogElements
*e
,
88 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
89 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
);
90 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
);
91 static void run_new_dialog(PropertyDialogElements
*e
);
92 static void apply_editor_prefs(void);
93 static void init_stash_prefs(void);
94 static void destroy_project(gboolean open_default
);
97 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
98 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
99 #define MAX_NAME_LEN 50
100 /* "projects" is part of the default project base path so be careful when translating
101 * please avoid special characters and spaces, look at the source for details or ask Frank */
102 #define PROJECT_DIR _("projects")
105 // returns whether we have working documents open
106 static gboolean
have_session_docs(void)
108 gint npages
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
109 GeanyDocument
*doc
= document_get_current();
111 return npages
> 1 || (npages
== 1 && (doc
->file_name
|| doc
->changed
));
115 static gboolean
handle_current_session(void)
119 /* save session in case the dialog is cancelled */
120 configuration_save_default_session();
121 /* don't ask if the only doc is an unmodified new doc */
122 if (have_session_docs())
124 if (dialogs_show_question(
125 _("Move the current documents into the new project's session?")))
127 // don't reload session on closing project
128 configuration_clear_default_session();
132 if (!document_close_all())
138 return project_close(FALSE
);
143 /* TODO: this should be ported to Glade like the project preferences dialog,
144 * then we can get rid of the PropertyDialogElements struct altogether as
145 * widgets pointers can be accessed through ui_lookup_widget(). */
146 void project_new(gboolean from_folder
)
155 gchar
*base_path
= NULL
;
156 PropertyDialogElements e
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
};
160 base_path
= ui_get_project_directory(local_prefs
.project_file_path
);
165 e
.dialog
= gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets
.window
),
166 GTK_DIALOG_DESTROY_WITH_PARENT
,
167 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
169 gtk_widget_set_name(e
.dialog
, "GeanyDialogProject");
170 button
= ui_button_new_with_image(GTK_STOCK_NEW
, _("C_reate"));
171 gtk_widget_set_can_default(button
, TRUE
);
172 gtk_window_set_default(GTK_WINDOW(e
.dialog
), button
);
173 gtk_dialog_add_action_widget(GTK_DIALOG(e
.dialog
), button
, GTK_RESPONSE_OK
);
175 vbox
= ui_dialog_vbox_new(GTK_DIALOG(e
.dialog
));
177 table
= gtk_table_new(3, 2, FALSE
);
178 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
179 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
181 label
= gtk_label_new(_("Name:"));
182 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
184 e
.name
= gtk_entry_new();
185 gtk_entry_set_activates_default(GTK_ENTRY(e
.name
), TRUE
);
186 ui_entry_add_clear_icon(GTK_ENTRY(e
.name
));
187 gtk_entry_set_max_length(GTK_ENTRY(e
.name
), MAX_NAME_LEN
);
188 gtk_widget_set_tooltip_text(e
.name
, _("Project name"));
190 ui_table_add_row(GTK_TABLE(table
), 0, label
, e
.name
, NULL
);
192 label
= gtk_label_new(_("Filename:"));
193 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
195 e
.file_name
= gtk_entry_new();
196 gtk_entry_set_activates_default(GTK_ENTRY(e
.file_name
), TRUE
);
197 ui_entry_add_clear_icon(GTK_ENTRY(e
.file_name
));
198 gtk_entry_set_width_chars(GTK_ENTRY(e
.file_name
), 30);
199 tooltip
= g_strdup_printf(
200 _("Path of the file representing the project and storing its settings. "
201 "It should normally have the \"%s\" extension."), "."GEANY_PROJECT_EXT
);
202 gtk_widget_set_tooltip_text(e
.file_name
, tooltip
);
204 button
= gtk_button_new();
205 g_signal_connect(button
, "clicked", G_CALLBACK(on_file_save_button_clicked
), &e
);
206 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
207 gtk_container_add(GTK_CONTAINER(button
), image
);
208 bbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 6);
209 gtk_box_pack_start(GTK_BOX(bbox
), e
.file_name
, TRUE
, TRUE
, 0);
210 gtk_box_pack_start(GTK_BOX(bbox
), button
, FALSE
, FALSE
, 0);
212 ui_table_add_row(GTK_TABLE(table
), 1, label
, bbox
, NULL
);
214 label
= gtk_label_new(_("Base path:"));
215 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
217 e
.base_path
= gtk_entry_new();
218 gtk_entry_set_activates_default(GTK_ENTRY(e
.base_path
), TRUE
);
219 ui_entry_add_clear_icon(GTK_ENTRY(e
.base_path
));
220 gtk_widget_set_tooltip_text(e
.base_path
,
221 _("Base directory of all files that make up the project. "
222 "This can be a new path, or an existing directory tree. "
223 "You can use paths relative to the project filename."));
224 bbox
= ui_path_box_new(_("Choose Project Base Path"),
225 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
.base_path
));
227 ui_table_add_row(GTK_TABLE(table
), 2, label
, bbox
, NULL
);
229 gtk_box_pack_start(GTK_BOX(vbox
), table
, TRUE
, TRUE
, 0);
233 update_new_project_dlg(GTK_EDITABLE(e
.name
), &e
, base_path
);
239 g_signal_connect(e
.name
, "changed", G_CALLBACK(on_name_entry_changed
), &e
);
240 g_signal_connect(e
.file_name
, "changed", G_CALLBACK(on_entries_changed
), &e
);
241 g_signal_connect(e
.base_path
, "changed", G_CALLBACK(on_entries_changed
), &e
);
243 update_new_project_dlg(GTK_EDITABLE(e
.name
), &e
, NULL
);
246 gtk_widget_show_all(e
.dialog
);
248 gtk_widget_destroy(e
.dialog
);
249 document_new_file_if_non_open();
250 ui_focus_current_document();
254 static void run_new_dialog(PropertyDialogElements
*e
)
256 if (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) != GTK_RESPONSE_OK
||
257 !handle_current_session())
261 if (update_config(e
, TRUE
))
263 // app->project is now set
266 SHOW_ERR(_("Project file could not be written"));
267 destroy_project(FALSE
);
271 ui_set_statusbar(TRUE
, _("Project \"%s\" created."), app
->project
->name
);
272 ui_add_recent_project_file(app
->project
->file_name
);
277 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
);
278 // any open docs were meant to be moved into the project
279 // rewrite default session because it was cleared
280 if (have_session_docs())
281 configuration_save_default_session();
284 // reload any documents that were closed
285 configuration_load_default_session();
286 configuration_open_default_session();
291 gboolean
project_load_file_with_session(const gchar
*locale_file_name
)
293 if (project_load_file(locale_file_name
))
295 configuration_open_files(app
->project
->priv
->session_files
);
296 app
->project
->priv
->session_files
= NULL
;
297 document_new_file_if_non_open();
298 ui_focus_current_document();
305 static void run_open_dialog(GtkDialog
*dialog
)
307 while (gtk_dialog_run(dialog
) == GTK_RESPONSE_ACCEPT
)
309 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
311 if (app
->project
&& !project_close(FALSE
)) {}
312 /* try to load the config */
313 else if (! project_load_file_with_session(filename
))
315 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
317 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename
);
318 gtk_widget_grab_focus(GTK_WIDGET(dialog
));
319 g_free(utf8_filename
);
329 void project_open(void)
331 const gchar
*dir
= local_prefs
.project_file_path
;
334 if (interface_prefs
.use_native_windows_dialogs
)
336 gchar
*file
= win32_show_project_open_dialog(main_widgets
.window
, _("Open Project"), dir
, FALSE
, TRUE
);
339 if (app
->project
&& !project_close(FALSE
)) {}
340 /* try to load the config */
341 else if (! project_load_file_with_session(file
))
343 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file
);
352 GtkFileFilter
*filter
;
355 dialog
= gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets
.window
),
356 GTK_FILE_CHOOSER_ACTION_OPEN
,
357 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
358 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
359 gtk_widget_set_name(dialog
, "GeanyDialogProject");
361 /* set default Open, so pressing enter can open multiple files */
362 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
363 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
364 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
365 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
366 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(main_widgets
.window
));
367 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog
), TRUE
);
369 /* add FileFilters */
370 filter
= gtk_file_filter_new();
371 gtk_file_filter_set_name(filter
, _("All files"));
372 gtk_file_filter_add_pattern(filter
, "*");
373 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
374 filter
= gtk_file_filter_new();
375 gtk_file_filter_set_name(filter
, _("Project files"));
376 gtk_file_filter_add_pattern(filter
, "*." GEANY_PROJECT_EXT
);
377 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
378 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog
), filter
);
380 locale_path
= utils_get_locale_from_utf8(dir
);
381 if (g_file_test(locale_path
, G_FILE_TEST_EXISTS
) &&
382 g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
384 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
388 gtk_widget_show_all(dialog
);
389 run_open_dialog(GTK_DIALOG(dialog
));
390 gtk_widget_destroy(GTK_WIDGET(dialog
));
395 /* Called when creating, opening, closing and updating projects. */
396 static void update_ui(void)
398 if (main_status
.quitting
)
401 ui_set_window_title(NULL
);
402 build_menu_update(NULL
);
403 // update project name
404 sidebar_openfiles_update_all();
405 ui_update_recent_project_menu();
409 static void remove_foreach_project_filetype(gpointer data
, gpointer user_data
)
411 GeanyFiletype
*ft
= data
;
414 SETPTR(ft
->priv
->projfilecmds
, NULL
);
415 SETPTR(ft
->priv
->projexeccmds
, NULL
);
416 SETPTR(ft
->priv
->projerror_regex_string
, NULL
);
417 ft
->priv
->project_list_entry
= -1;
422 /* open_default will make function reload default session files on close */
423 gboolean
project_close(gboolean open_default
)
425 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
427 /* save project session files, etc */
429 g_warning("Project file \"%s\" could not be written", app
->project
->file_name
);
431 /* close all existing tabs first */
432 if (!document_close_all())
435 ui_set_statusbar(TRUE
, _("Project \"%s\" closed."), app
->project
->name
);
436 destroy_project(open_default
);
441 static void destroy_project(gboolean open_default
)
445 g_return_if_fail(app
->project
!= NULL
);
447 g_signal_emit_by_name(geany_object
, "project-before-close");
449 /* remove project filetypes build entries */
450 if (app
->project
->priv
->build_filetypes_list
!= NULL
)
452 g_ptr_array_foreach(app
->project
->priv
->build_filetypes_list
, remove_foreach_project_filetype
, NULL
);
453 g_ptr_array_free(app
->project
->priv
->build_filetypes_list
, FALSE
);
456 /* remove project non filetype build menu items */
457 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_NON_FT
, -1);
458 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_EXEC
, -1);
460 g_free(app
->project
->name
);
461 g_free(app
->project
->description
);
462 g_free(app
->project
->file_name
);
463 g_free(app
->project
->base_path
);
464 g_strfreev(app
->project
->file_patterns
);
466 g_free(app
->project
);
469 foreach_slist(node
, stash_groups
)
470 stash_group_free(node
->data
);
472 g_slist_free(stash_groups
);
475 apply_editor_prefs(); /* ensure that global settings are restored */
477 /* after closing all tabs let's open the tabs found in the default config */
478 if (open_default
&& cl_options
.load_session
)
480 configuration_load_default_session();
481 configuration_open_default_session();
482 document_new_file_if_non_open();
483 ui_focus_current_document();
485 g_signal_emit_by_name(geany_object
, "project-close");
491 /* Shows the file chooser dialog when base path button is clicked
492 * FIXME: this should be connected in Glade but 3.8.1 has a bug
493 * where it won't pass any objects as user data (#588824). */
494 static void on_project_properties_base_path_button_clicked(GtkWidget
*button
,
495 GtkWidget
*base_path_entry
)
499 g_return_if_fail(base_path_entry
!= NULL
);
500 g_return_if_fail(GTK_IS_WIDGET(base_path_entry
));
502 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
503 NULL
, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
504 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
505 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
508 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
510 gtk_entry_set_text(GTK_ENTRY(base_path_entry
),
511 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
)));
514 gtk_widget_destroy(dialog
);
518 static void insert_build_page(PropertyDialogElements
*e
)
520 GtkWidget
*build_table
, *label
;
521 GeanyDocument
*doc
= document_get_current();
522 GeanyFiletype
*ft
= NULL
;
527 build_table
= build_commands_table(doc
, GEANY_BCS_PROJ
, &(e
->build_properties
), ft
);
528 gtk_container_set_border_width(GTK_CONTAINER(build_table
), 6);
529 label
= gtk_label_new(_("Build"));
530 e
->build_page_num
= gtk_notebook_append_page(GTK_NOTEBOOK(e
->notebook
),
535 static void create_properties_dialog(PropertyDialogElements
*e
)
538 static guint base_path_button_handler_id
= 0;
539 static guint radio_long_line_handler_id
= 0;
541 e
->dialog
= create_project_dialog();
542 e
->notebook
= ui_lookup_widget(e
->dialog
, "project_notebook");
543 e
->file_name
= ui_lookup_widget(e
->dialog
, "label_project_dialog_filename");
544 e
->name
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_name");
545 e
->description
= ui_lookup_widget(e
->dialog
, "textview_project_dialog_description");
546 e
->base_path
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_base_path");
547 e
->patterns
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_file_patterns");
549 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
551 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
552 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
553 ui_entry_add_clear_icon(GTK_ENTRY(e
->patterns
));
555 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
556 if (base_path_button_handler_id
== 0)
558 wid
= ui_lookup_widget(e
->dialog
, "button_project_dialog_base_path");
559 base_path_button_handler_id
=
560 g_signal_connect(wid
, "clicked",
561 G_CALLBACK(on_project_properties_base_path_button_clicked
),
565 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
566 if (radio_long_line_handler_id
== 0)
568 wid
= ui_lookup_widget(e
->dialog
, "radio_long_line_custom_project");
569 radio_long_line_handler_id
=
570 g_signal_connect(wid
, "toggled",
571 G_CALLBACK(on_radio_long_line_custom_toggled
),
572 ui_lookup_widget(e
->dialog
, "spin_long_line_project"));
577 static void show_project_properties(gboolean show_build
)
579 GeanyProject
*p
= app
->project
;
580 GtkWidget
*widget
= NULL
;
581 GtkWidget
*radio_long_line_custom
;
582 static PropertyDialogElements e
;
585 GtkTextBuffer
*buffer
;
587 g_return_if_fail(app
->project
!= NULL
);
589 if (e
.dialog
== NULL
)
590 create_properties_dialog(&e
);
592 insert_build_page(&e
);
594 foreach_slist(node
, stash_groups
)
595 stash_group_display(node
->data
, e
.dialog
);
597 /* fill the elements with the appropriate data */
598 gtk_entry_set_text(GTK_ENTRY(e
.name
), p
->name
);
599 gtk_label_set_text(GTK_LABEL(e
.file_name
), p
->file_name
);
600 gtk_entry_set_text(GTK_ENTRY(e
.base_path
), p
->base_path
);
602 radio_long_line_custom
= ui_lookup_widget(e
.dialog
, "radio_long_line_custom_project");
603 switch (p
->priv
->long_line_behaviour
)
605 case 0: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_disabled_project"); break;
606 case 1: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_default_project"); break;
607 case 2: widget
= radio_long_line_custom
; break;
609 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
611 widget
= ui_lookup_widget(e
.dialog
, "spin_long_line_project");
612 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), (gdouble
)p
->priv
->long_line_column
);
613 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom
), widget
);
616 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
.description
));
617 gtk_text_buffer_set_text(buffer
, p
->description
? p
->description
: "", -1);
619 /* set the file patterns */
620 entry_text
= p
->file_patterns
? g_strjoinv(" ", p
->file_patterns
) : g_strdup("");
621 gtk_entry_set_text(GTK_ENTRY(e
.patterns
), entry_text
);
624 g_signal_emit_by_name(geany_object
, "project-dialog-open", e
.notebook
);
625 gtk_widget_show_all(e
.dialog
);
627 /* note: notebook page must be shown before setting current page */
629 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
631 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), 0);
633 while (gtk_dialog_run(GTK_DIALOG(e
.dialog
)) == GTK_RESPONSE_OK
)
635 if (update_config(&e
, FALSE
))
637 g_signal_emit_by_name(geany_object
, "project-dialog-confirmed", e
.notebook
);
639 SHOW_ERR(_("Project file could not be written"));
642 ui_set_statusbar(TRUE
, _("Project \"%s\" saved."), app
->project
->name
);
648 build_free_fields(e
.build_properties
);
649 g_signal_emit_by_name(geany_object
, "project-dialog-close", e
.notebook
);
650 gtk_notebook_remove_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
651 gtk_widget_hide(e
.dialog
);
655 void project_properties(void)
657 show_project_properties(FALSE
);
661 void project_build_properties(void)
663 show_project_properties(TRUE
);
667 /* checks whether there is an already open project and asks the user if he wants to close it or
668 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
669 * and TRUE if we can go ahead */
670 gboolean
project_ask_close(void)
672 if (app
->project
!= NULL
)
674 if (!interface_prefs
.warn_on_project_close
||
675 dialogs_show_question_full(NULL
, GTK_STOCK_CLOSE
, GTK_STOCK_CANCEL
,
676 _("Do you want to close it before proceeding?"),
677 _("The '%s' project is open."), app
->project
->name
))
679 return project_close(FALSE
);
689 static GeanyProject
*create_project(void)
691 GeanyProject
*project
= g_new0(GeanyProject
, 1);
693 memset(&priv
, 0, sizeof priv
);
694 priv
.indentation
= &indentation
;
695 project
->priv
= &priv
;
699 project
->file_patterns
= NULL
;
701 project
->priv
->long_line_behaviour
= 1 /* use global settings */;
702 project
->priv
->long_line_column
= editor_prefs
.long_line_column
;
704 app
->project
= project
;
709 /* Verifies data for New & Properties dialogs.
710 * Creates app->project if NULL.
711 * Returns: FALSE if the user needs to change any data. */
712 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
)
714 const gchar
*name
, *file_name
, *base_path
;
715 gchar
*locale_filename
;
720 g_return_val_if_fail(e
!= NULL
, TRUE
);
722 name
= gtk_entry_get_text(GTK_ENTRY(e
->name
));
723 name_len
= strlen(name
);
726 SHOW_ERR(_("The specified project name is too short."));
727 gtk_widget_grab_focus(e
->name
);
730 else if (name_len
> MAX_NAME_LEN
)
732 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN
);
733 gtk_widget_grab_focus(e
->name
);
738 file_name
= gtk_entry_get_text(GTK_ENTRY(e
->file_name
));
740 file_name
= gtk_label_get_text(GTK_LABEL(e
->file_name
));
742 if (G_UNLIKELY(EMPTY(file_name
)))
744 SHOW_ERR(_("You have specified an invalid project filename."));
745 gtk_widget_grab_focus(e
->file_name
);
749 locale_filename
= utils_get_locale_from_utf8(file_name
);
750 base_path
= gtk_entry_get_text(GTK_ENTRY(e
->base_path
));
751 if (!EMPTY(base_path
))
752 { /* check whether the given directory actually exists */
753 gchar
*locale_path
= utils_get_locale_from_utf8(base_path
);
755 if (! g_path_is_absolute(locale_path
))
756 { /* relative base path, so add base dir of project file name */
757 gchar
*dir
= g_path_get_dirname(locale_filename
);
758 SETPTR(locale_path
, g_build_filename(dir
, locale_path
, NULL
));
762 if (! g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
766 create_dir
= dialogs_show_question_full(NULL
, GTK_STOCK_OK
, GTK_STOCK_CANCEL
,
767 _("Create the project's base path directory?"),
768 _("The path \"%s\" does not exist."),
772 err_code
= utils_mkdir(locale_path
, TRUE
);
774 if (! create_dir
|| err_code
!= 0)
777 SHOW_ERR1(_("Project base directory could not be created (%s)."),
778 g_strerror(err_code
));
779 gtk_widget_grab_focus(e
->base_path
);
780 utils_free_pointers(2, locale_path
, locale_filename
, NULL
);
786 /* finally test whether the given project file can be written */
787 if ((err_code
= utils_is_file_writable(locale_filename
)) != 0 ||
788 (err_code
= g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
) ? EISDIR
: 0) != 0)
790 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code
));
791 gtk_widget_grab_focus(e
->file_name
);
792 g_free(locale_filename
);
795 else if (new_project
&& g_file_test(locale_filename
, G_FILE_TEST_EXISTS
) &&
796 ! dialogs_show_question_full(NULL
, _("_Replace"), GTK_STOCK_CANCEL
,
798 _("The file '%s' already exists. Do you want to overwrite it?"),
801 gtk_widget_grab_focus(e
->file_name
);
802 g_free(locale_filename
);
805 g_free(locale_filename
);
807 if (app
->project
== NULL
)
814 SETPTR(p
->name
, g_strdup(name
));
815 SETPTR(p
->file_name
, g_strdup(file_name
));
816 /* use "." if base_path is empty */
817 SETPTR(p
->base_path
, g_strdup(!EMPTY(base_path
) ? base_path
: "./"));
819 if (! new_project
) /* save properties specific fields */
821 GtkTextIter start
, end
;
822 GtkTextBuffer
*buffer
;
823 GeanyDocument
*doc
= document_get_current();
824 GeanyBuildCommand
*oldvalue
;
825 GeanyFiletype
*ft
= doc
? doc
->file_type
: NULL
;
831 /* get and set the project description */
832 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
833 gtk_text_buffer_get_start_iter(buffer
, &start
);
834 gtk_text_buffer_get_end_iter(buffer
, &end
);
835 SETPTR(p
->description
, gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
));
837 foreach_slist(node
, stash_groups
)
838 stash_group_update(node
->data
, e
->dialog
);
840 /* read the project build menu */
841 oldvalue
= ft
? ft
->priv
->projfilecmds
: NULL
;
842 build_read_project(ft
, e
->build_properties
);
844 if (ft
!= NULL
&& ft
->priv
->projfilecmds
!= oldvalue
&& ft
->priv
->project_list_entry
< 0)
846 if (p
->priv
->build_filetypes_list
== NULL
)
847 p
->priv
->build_filetypes_list
= g_ptr_array_new();
848 ft
->priv
->project_list_entry
= p
->priv
->build_filetypes_list
->len
;
849 g_ptr_array_add(p
->priv
->build_filetypes_list
, ft
);
851 build_menu_update(doc
);
853 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled_project");
854 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
855 p
->priv
->long_line_behaviour
= 0;
858 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default_project");
859 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
860 p
->priv
->long_line_behaviour
= 1;
862 /* "Custom" radio button must be checked */
863 p
->priv
->long_line_behaviour
= 2;
866 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line_project");
867 p
->priv
->long_line_column
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
868 apply_editor_prefs();
870 /* get and set the project file patterns */
871 tmp
= g_strdup(gtk_entry_get_text(GTK_ENTRY(e
->patterns
)));
872 g_strfreev(p
->file_patterns
);
874 str
= g_string_new(tmp
);
875 do {} while (utils_string_replace_all(str
, " ", " "));
876 p
->file_patterns
= g_strsplit(str
->str
, " ", -1);
877 g_string_free(str
, TRUE
);
888 static void run_dialog(GtkWidget
*dialog
, GtkWidget
*entry
)
890 /* set filename in the file chooser dialog */
891 const gchar
*utf8_filename
= gtk_entry_get_text(GTK_ENTRY(entry
));
892 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
894 if (g_path_is_absolute(locale_filename
))
896 if (g_file_test(locale_filename
, G_FILE_TEST_EXISTS
))
898 /* if the current filename is a directory, we must use
899 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
900 * we end up in the parent directory */
901 if (g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
902 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_filename
);
904 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
906 else /* if the file doesn't yet exist, use at least the current directory */
908 gchar
*locale_dir
= g_path_get_dirname(locale_filename
);
909 gchar
*name
= g_path_get_basename(utf8_filename
);
911 if (g_file_test(locale_dir
, G_FILE_TEST_EXISTS
))
912 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dir
);
913 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), name
);
919 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog
)) != GTK_FILE_CHOOSER_ACTION_OPEN
)
921 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
923 g_free(locale_filename
);
926 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
928 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
929 gchar
*tmp_utf8_filename
= utils_get_utf8_from_locale(filename
);
931 gtk_entry_set_text(GTK_ENTRY(entry
), tmp_utf8_filename
);
933 g_free(tmp_utf8_filename
);
936 gtk_widget_destroy(dialog
);
941 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
)
944 gchar
*path
= win32_show_project_open_dialog(e
->dialog
, _("Choose Project Filename"),
945 gtk_entry_get_text(GTK_ENTRY(e
->file_name
)), TRUE
, TRUE
);
948 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), path
);
954 /* initialise the dialog */
955 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL
,
956 GTK_FILE_CHOOSER_ACTION_SAVE
,
957 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
958 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
959 gtk_widget_set_name(dialog
, "GeanyDialogProject");
960 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
961 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
962 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
963 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
965 run_dialog(dialog
, e
->file_name
);
970 /* sets the New Project dialog entries according to the base path or project name */
971 static void update_new_project_dlg(GtkEditable
*editable
, PropertyDialogElements
*e
,
977 const gchar
*project_dir
= local_prefs
.project_file_path
;
979 if (e
->entries_modified
)
984 gchar
*name
= g_path_get_basename(base_p
);
986 base_path
= g_strdup(base_p
);
987 gtk_entry_set_text(GTK_ENTRY(e
->name
), name
);
988 if (project_prefs
.project_file_in_basedir
)
989 file_name
= g_strconcat(base_path
, G_DIR_SEPARATOR_S
,
990 name
, "." GEANY_PROJECT_EXT
, NULL
);
992 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
993 name
, "." GEANY_PROJECT_EXT
, NULL
);
998 gchar
*name
= gtk_editable_get_chars(editable
, 0, -1);
1001 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
1002 name
, G_DIR_SEPARATOR_S
, NULL
);
1003 if (project_prefs
.project_file_in_basedir
)
1004 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, name
, G_DIR_SEPARATOR_S
,
1005 name
, "." GEANY_PROJECT_EXT
, NULL
);
1007 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
1008 name
, "." GEANY_PROJECT_EXT
, NULL
);
1012 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
1013 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
1018 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), base_path
);
1019 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), file_name
);
1021 e
->entries_modified
= FALSE
;
1028 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
1030 update_new_project_dlg(editable
, e
, NULL
);
1034 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
1036 e
->entries_modified
= TRUE
;
1040 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
)
1042 gtk_widget_set_sensitive(spin_long_line
, gtk_toggle_button_get_active(radio
));
1046 gboolean
project_load_file(const gchar
*locale_file_name
)
1048 g_return_val_if_fail(locale_file_name
!= NULL
, FALSE
);
1050 if (load_config(locale_file_name
))
1052 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
1054 ui_set_statusbar(TRUE
, _("Project \"%s\" opened."), app
->project
->name
);
1056 ui_add_recent_project_file(utf8_filename
);
1057 g_free(utf8_filename
);
1062 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
1064 ui_set_statusbar(TRUE
, _("Project file \"%s\" could not be loaded."), utf8_filename
);
1065 g_free(utf8_filename
);
1071 /* Reads the given filename and creates a new project with the data found in the file.
1072 * At this point there should not be an already opened project in Geany otherwise it will just
1074 * The filename is expected in the locale encoding. */
1075 static gboolean
load_config(const gchar
*filename
)
1081 /* there should not be an open project */
1082 g_return_val_if_fail(app
->project
== NULL
&& filename
!= NULL
, FALSE
);
1084 config
= g_key_file_new();
1085 if (! g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
))
1087 g_key_file_free(config
);
1091 p
= create_project();
1093 foreach_slist(node
, stash_groups
)
1094 stash_group_load_from_key_file(node
->data
, config
);
1096 p
->name
= utils_get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
1097 p
->description
= utils_get_setting_string(config
, "project", "description", "");
1098 p
->file_name
= utils_get_utf8_from_locale(filename
);
1099 p
->base_path
= utils_get_setting_string(config
, "project", "base_path", "");
1100 p
->file_patterns
= g_key_file_get_string_list(config
, "project", "file_patterns", NULL
, NULL
);
1102 p
->priv
->long_line_behaviour
= utils_get_setting_integer(config
, "long line marker",
1103 "long_line_behaviour", 1 /* follow global */);
1104 p
->priv
->long_line_column
= utils_get_setting_integer(config
, "long line marker",
1105 "long_line_column", editor_prefs
.long_line_column
);
1106 apply_editor_prefs();
1108 build_load_menu(config
, GEANY_BCS_PROJ
, (gpointer
)p
);
1109 /* save current (non-project) session (it could have been changed since program startup) */
1110 if (!main_status
.opening_session_files
)
1112 configuration_save_default_session();
1113 /* now close all open files */
1114 document_close_all();
1116 /* read session files so they can be opened with configuration_open_files() */
1117 p
->priv
->session_files
= configuration_load_session_files(config
);
1118 g_signal_emit_by_name(geany_object
, "project-open", config
);
1119 g_key_file_free(config
);
1126 static void apply_editor_prefs(void)
1131 editor_apply_update_prefs(documents
[i
]->editor
);
1135 /* Write the project settings as well as the project session files into its configuration files.
1136 * Returns: TRUE if project file was written successfully. */
1137 static gboolean
write_config(void)
1143 gboolean ret
= FALSE
;
1146 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
1150 config
= g_key_file_new();
1151 /* try to load an existing config to keep manually added comments */
1152 filename
= utils_get_locale_from_utf8(p
->file_name
);
1153 g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
);
1155 foreach_slist(node
, stash_groups
)
1156 stash_group_save_to_key_file(node
->data
, config
);
1158 g_key_file_set_string(config
, "project", "name", p
->name
);
1159 g_key_file_set_string(config
, "project", "base_path", p
->base_path
);
1162 g_key_file_set_string(config
, "project", "description", p
->description
);
1163 if (p
->file_patterns
)
1164 g_key_file_set_string_list(config
, "project", "file_patterns",
1165 (const gchar
**) p
->file_patterns
, g_strv_length(p
->file_patterns
));
1168 g_key_file_set_integer(config
, "long line marker", "long_line_behaviour", p
->priv
->long_line_behaviour
);
1169 g_key_file_set_integer(config
, "long line marker", "long_line_column", p
->priv
->long_line_column
);
1171 /* store the session files into the project too */
1172 configuration_save_session_files(config
);
1173 build_save_menu(config
, (gpointer
)p
, GEANY_BCS_PROJ
);
1174 g_signal_emit_by_name(geany_object
, "project-save", config
);
1175 /* write the file */
1176 data
= g_key_file_to_data(config
, NULL
, NULL
);
1177 ret
= (utils_write_file(filename
, data
) == 0);
1181 g_key_file_free(config
);
1187 /** Forces the project file rewrite and emission of the project-save signal. Plugins
1188 * can use this function to save additional project data outside the project dialog.
1193 void project_write_config(void)
1195 if (!write_config())
1196 SHOW_ERR(_("Project file could not be written"));
1200 /* Constructs the project's base path which is used for "Make all" and "Execute".
1201 * The result is an absolute string in UTF-8 encoding which is either the same as
1202 * base path if it is absolute or it is built out of project file name's dir and base_path.
1203 * If there is no project or project's base_path is invalid, NULL will be returned.
1204 * The returned string should be freed when no longer needed. */
1205 gchar
*project_get_base_path(void)
1207 GeanyProject
*project
= app
->project
;
1209 if (project
&& !EMPTY(project
->base_path
))
1211 if (g_path_is_absolute(project
->base_path
))
1212 return g_strdup(project
->base_path
);
1214 { /* build base_path out of project file name's dir and base_path */
1216 gchar
*dir
= g_path_get_dirname(project
->file_name
);
1218 if (utils_str_equal(project
->base_path
, "./"))
1221 path
= g_build_filename(dir
, project
->base_path
, NULL
);
1230 /* This is to save project-related global settings, NOT project file settings. */
1231 void project_save_prefs(GKeyFile
*config
)
1233 GeanyProject
*project
= app
->project
;
1235 if (cl_options
.load_session
)
1237 const gchar
*utf8_filename
= (project
== NULL
) ? "" : project
->file_name
;
1239 g_key_file_set_string(config
, "project", "session_file", utf8_filename
);
1241 g_key_file_set_string(config
, "project", "project_file_path",
1242 FALLBACK(local_prefs
.project_file_path
, ""));
1246 void project_load_prefs(GKeyFile
*config
)
1248 if (cl_options
.load_session
)
1250 g_return_if_fail(project_prefs
.session_file
== NULL
);
1251 project_prefs
.session_file
= utils_get_setting_string(config
, "project",
1252 "session_file", "");
1254 local_prefs
.project_file_path
= utils_get_setting_string(config
, "project",
1255 "project_file_path", NULL
);
1256 if (local_prefs
.project_file_path
== NULL
)
1258 local_prefs
.project_file_path
= g_build_filename(g_get_home_dir(), PROJECT_DIR
, NULL
);
1263 /* Initialize project-related preferences in the Preferences dialog. */
1264 void project_setup_prefs(void)
1266 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1267 GtkWidget
*path_btn
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_button");
1268 static gboolean callback_setup
= FALSE
;
1270 g_return_if_fail(local_prefs
.project_file_path
!= NULL
);
1272 gtk_entry_set_text(GTK_ENTRY(path_entry
), local_prefs
.project_file_path
);
1273 if (! callback_setup
)
1274 { /* connect the callback only once */
1275 callback_setup
= TRUE
;
1276 ui_setup_open_button_callback(path_btn
, NULL
,
1277 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(path_entry
));
1282 /* Update project-related preferences after using the Preferences dialog. */
1283 void project_apply_prefs(void)
1285 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1288 str
= gtk_entry_get_text(GTK_ENTRY(path_entry
));
1289 SETPTR(local_prefs
.project_file_path
, g_strdup(str
));
1293 static void add_stash_group(StashGroup
*group
, gboolean apply_defaults
)
1297 stash_groups
= g_slist_prepend(stash_groups
, group
);
1298 if (!apply_defaults
)
1301 kf
= g_key_file_new();
1302 stash_group_load_from_key_file(group
, kf
);
1303 g_key_file_free(kf
);
1307 static void init_stash_prefs(void)
1311 group
= stash_group_new("indentation");
1312 /* copy global defaults */
1313 indentation
= *editor_get_indent_prefs(NULL
);
1314 stash_group_set_use_defaults(group
, FALSE
);
1315 add_stash_group(group
, FALSE
);
1317 stash_group_add_spin_button_integer(group
, &indentation
.width
,
1318 "indent_width", 4, "spin_indent_width_project");
1319 stash_group_add_radio_buttons(group
, (gint
*)(gpointer
)&indentation
.type
,
1320 "indent_type", GEANY_INDENT_TYPE_TABS
,
1321 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES
,
1322 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS
,
1323 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH
,
1325 /* This is a 'hidden' pref for backwards-compatibility */
1326 stash_group_add_integer(group
, &indentation
.hard_tab_width
,
1327 "indent_hard_tab_width", 8);
1328 stash_group_add_toggle_button(group
, &indentation
.detect_type
,
1329 "detect_indent", FALSE
, "check_detect_indent_type_project");
1330 stash_group_add_toggle_button(group
, &indentation
.detect_width
,
1331 "detect_indent_width", FALSE
, "check_detect_indent_width_project");
1332 stash_group_add_combo_box(group
, (gint
*)(gpointer
)&indentation
.auto_indent_mode
,
1333 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS
, "combo_auto_indent_mode_project");
1335 group
= stash_group_new("file_prefs");
1336 stash_group_add_toggle_button(group
, &priv
.final_new_line
,
1337 "final_new_line", file_prefs
.final_new_line
, "check_new_line1");
1338 stash_group_add_toggle_button(group
, &priv
.ensure_convert_new_lines
,
1339 "ensure_convert_new_lines", file_prefs
.ensure_convert_new_lines
, "check_ensure_convert_new_lines1");
1340 stash_group_add_toggle_button(group
, &priv
.strip_trailing_spaces
,
1341 "strip_trailing_spaces", file_prefs
.strip_trailing_spaces
, "check_trailing_spaces1");
1342 stash_group_add_toggle_button(group
, &priv
.replace_tabs
,
1343 "replace_tabs", file_prefs
.replace_tabs
, "check_replace_tabs1");
1344 add_stash_group(group
, TRUE
);
1346 group
= stash_group_new("editor");
1347 stash_group_add_toggle_button(group
, &priv
.line_wrapping
,
1348 "line_wrapping", editor_prefs
.line_wrapping
, "check_line_wrapping1");
1349 stash_group_add_spin_button_integer(group
, &priv
.line_break_column
,
1350 "line_break_column", editor_prefs
.line_break_column
, "spin_line_break1");
1351 stash_group_add_toggle_button(group
, &priv
.auto_continue_multiline
,
1352 "auto_continue_multiline", editor_prefs
.auto_continue_multiline
,
1353 "check_auto_multiline1");
1354 add_stash_group(group
, TRUE
);
1358 #define COPY_PREF(dest, prefname)\
1359 (dest.prefname = priv.prefname)
1361 const GeanyFilePrefs
*project_get_file_prefs(void)
1363 static GeanyFilePrefs fp
;
1369 COPY_PREF(fp
, final_new_line
);
1370 COPY_PREF(fp
, ensure_convert_new_lines
);
1371 COPY_PREF(fp
, strip_trailing_spaces
);
1372 COPY_PREF(fp
, replace_tabs
);
1377 void project_init(void)
1382 void project_finalize(void)