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"
52 ProjectPrefs project_prefs
= { NULL
, FALSE
};
55 static GeanyProjectPrivate priv
;
56 static GeanyIndentPrefs indentation
;
58 static GSList
*stash_groups
= NULL
;
62 gchar
*project_file_path
; /* in UTF-8 */
63 } local_prefs
= { NULL
};
65 /* simple struct to keep references to the elements of the properties dialog */
66 typedef struct _PropertyDialogElements
71 GtkWidget
*description
;
75 BuildTableData build_properties
;
77 gboolean entries_modified
;
78 } PropertyDialogElements
;
81 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
);
82 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
);
83 static gboolean
load_config(const gchar
*filename
);
84 static gboolean
write_config(void);
85 static void update_new_project_dlg(GtkEditable
*editable
, PropertyDialogElements
*e
,
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 run_new_dialog(PropertyDialogElements
*e
);
91 static void apply_editor_prefs(void);
92 static void init_stash_prefs(void);
93 static void destroy_project(gboolean open_default
);
96 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
97 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
98 #define MAX_NAME_LEN 50
99 /* "projects" is part of the default project base path so be careful when translating
100 * please avoid special characters and spaces, look at the source for details or ask Frank */
101 #define PROJECT_DIR _("projects")
104 // returns whether we have working documents open
105 static gboolean
have_session_docs(void)
107 gint npages
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
108 GeanyDocument
*doc
= document_get_current();
110 return npages
> 1 || (npages
== 1 && (doc
->file_name
|| doc
->changed
));
114 static gboolean
handle_current_session(void)
118 /* save session in case the dialog is cancelled */
119 configuration_save_default_session();
120 /* don't ask if the only doc is an unmodified new doc */
121 if (have_session_docs())
123 if (dialogs_show_question(
124 _("Move the current documents into the new project's session?")))
126 // don't reload session on closing project
127 configuration_clear_default_session();
131 if (!document_close_all())
137 return project_close(FALSE
);
142 /* TODO: this should be ported to Glade like the project preferences dialog,
143 * then we can get rid of the PropertyDialogElements struct altogether as
144 * widgets pointers can be accessed through ui_lookup_widget(). */
145 void project_new(gboolean from_folder
)
154 gchar
*base_path
= NULL
;
155 PropertyDialogElements e
= { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, FALSE
};
159 GeanyDocument
*doc
= document_get_current();
162 if (doc
&& doc
->file_name
)
163 start_path
= g_path_get_dirname(doc
->file_name
);
164 else if (!EMPTY(local_prefs
.project_file_path
))
165 start_path
= g_strdup(local_prefs
.project_file_path
);
167 start_path
= utils_get_utf8_from_locale(g_get_home_dir());
169 base_path
= ui_get_project_directory(start_path
);
176 e
.dialog
= gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets
.window
),
177 GTK_DIALOG_DESTROY_WITH_PARENT
,
178 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
, NULL
);
180 gtk_widget_set_name(e
.dialog
, "GeanyDialogProject");
181 button
= ui_button_new_with_image(GTK_STOCK_NEW
, _("C_reate"));
182 gtk_widget_set_can_default(button
, TRUE
);
183 gtk_window_set_default(GTK_WINDOW(e
.dialog
), button
);
184 gtk_dialog_add_action_widget(GTK_DIALOG(e
.dialog
), button
, GTK_RESPONSE_OK
);
186 vbox
= ui_dialog_vbox_new(GTK_DIALOG(e
.dialog
));
188 table
= gtk_table_new(3, 2, FALSE
);
189 gtk_table_set_row_spacings(GTK_TABLE(table
), 5);
190 gtk_table_set_col_spacings(GTK_TABLE(table
), 10);
192 label
= gtk_label_new(_("Name:"));
193 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
195 e
.name
= gtk_entry_new();
196 gtk_entry_set_activates_default(GTK_ENTRY(e
.name
), TRUE
);
197 ui_entry_add_clear_icon(GTK_ENTRY(e
.name
));
198 gtk_entry_set_max_length(GTK_ENTRY(e
.name
), MAX_NAME_LEN
);
199 gtk_widget_set_tooltip_text(e
.name
, _("Project name"));
201 ui_table_add_row(GTK_TABLE(table
), 0, label
, e
.name
, NULL
);
203 label
= gtk_label_new(_("Filename:"));
204 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
206 e
.file_name
= gtk_entry_new();
207 gtk_entry_set_activates_default(GTK_ENTRY(e
.file_name
), TRUE
);
208 ui_entry_add_clear_icon(GTK_ENTRY(e
.file_name
));
209 gtk_entry_set_width_chars(GTK_ENTRY(e
.file_name
), 40);
210 tooltip
= g_strdup_printf(
211 _("Path of the file representing the project and storing its settings. "
212 "It should normally have the \"%s\" extension."), "."GEANY_PROJECT_EXT
);
213 gtk_widget_set_tooltip_text(e
.file_name
, tooltip
);
215 button
= gtk_button_new();
216 g_signal_connect(button
, "clicked", G_CALLBACK(on_file_save_button_clicked
), &e
);
217 image
= gtk_image_new_from_stock(GTK_STOCK_OPEN
, GTK_ICON_SIZE_BUTTON
);
218 gtk_container_add(GTK_CONTAINER(button
), image
);
219 bbox
= gtk_box_new(GTK_ORIENTATION_HORIZONTAL
, 6);
220 gtk_box_pack_start(GTK_BOX(bbox
), e
.file_name
, TRUE
, TRUE
, 0);
221 gtk_box_pack_start(GTK_BOX(bbox
), button
, FALSE
, FALSE
, 0);
223 ui_table_add_row(GTK_TABLE(table
), 1, label
, bbox
, NULL
);
225 label
= gtk_label_new(_("Base path:"));
226 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
228 e
.base_path
= gtk_entry_new();
229 gtk_entry_set_activates_default(GTK_ENTRY(e
.base_path
), TRUE
);
230 ui_entry_add_clear_icon(GTK_ENTRY(e
.base_path
));
231 gtk_widget_set_tooltip_text(e
.base_path
,
232 _("Base directory of all files that make up the project. "
233 "This can be a new path, or an existing directory tree. "
234 "You can use paths relative to the project filename."));
235 bbox
= ui_path_box_new(_("Choose Project Base Path"),
236 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(e
.base_path
));
238 ui_table_add_row(GTK_TABLE(table
), 2, label
, bbox
, NULL
);
240 gtk_box_pack_start(GTK_BOX(vbox
), table
, TRUE
, TRUE
, 0);
244 update_new_project_dlg(GTK_EDITABLE(e
.name
), &e
, base_path
);
250 g_signal_connect(e
.name
, "changed", G_CALLBACK(on_name_entry_changed
), &e
);
251 g_signal_connect(e
.file_name
, "changed", G_CALLBACK(on_entries_changed
), &e
);
252 g_signal_connect(e
.base_path
, "changed", G_CALLBACK(on_entries_changed
), &e
);
254 update_new_project_dlg(GTK_EDITABLE(e
.name
), &e
, NULL
);
257 gtk_widget_show_all(e
.dialog
);
259 gtk_widget_destroy(e
.dialog
);
260 document_new_file_if_non_open();
261 ui_focus_current_document();
265 static void run_new_dialog(PropertyDialogElements
*e
)
267 if (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) != GTK_RESPONSE_OK
||
268 !handle_current_session())
272 if (update_config(e
, TRUE
))
274 // app->project is now set
277 SHOW_ERR(_("Project file could not be written"));
278 destroy_project(FALSE
);
282 ui_set_statusbar(TRUE
, _("Project \"%s\" created."), app
->project
->name
);
283 ui_add_recent_project_file(app
->project
->file_name
);
288 while (gtk_dialog_run(GTK_DIALOG(e
->dialog
)) == GTK_RESPONSE_OK
);
289 // any open docs were meant to be moved into the project
290 // rewrite default session because it was cleared
291 if (have_session_docs())
292 configuration_save_default_session();
295 // reload any documents that were closed
296 configuration_load_default_session();
297 configuration_open_default_session();
302 gboolean
project_load_file_with_session(const gchar
*locale_file_name
)
304 if (project_load_file(locale_file_name
))
306 configuration_open_files(app
->project
->priv
->session_files
);
307 app
->project
->priv
->session_files
= NULL
;
308 document_new_file_if_non_open();
309 ui_focus_current_document();
316 static void run_open_dialog(GtkDialog
*dialog
)
318 while (gtk_dialog_run(dialog
) == GTK_RESPONSE_ACCEPT
)
320 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
322 if (app
->project
&& !project_close(FALSE
)) {}
323 /* try to load the config */
324 else if (! project_load_file_with_session(filename
))
326 gchar
*utf8_filename
= utils_get_utf8_from_locale(filename
);
328 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename
);
329 gtk_widget_grab_focus(GTK_WIDGET(dialog
));
330 g_free(utf8_filename
);
340 void project_open(void)
342 const gchar
*dir
= local_prefs
.project_file_path
;
345 GtkFileFilter
*filter
;
347 dialog
= gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets
.window
),
348 GTK_FILE_CHOOSER_ACTION_OPEN
,
349 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
350 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
, NULL
);
351 gtk_widget_set_name(dialog
, "GeanyDialogProject");
353 /* set default Open, so pressing enter can open multiple files */
354 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
355 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
356 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
357 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
358 gtk_window_set_transient_for(GTK_WINDOW(dialog
), GTK_WINDOW(main_widgets
.window
));
359 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog
), TRUE
);
361 /* add FileFilters */
362 filter
= gtk_file_filter_new();
363 gtk_file_filter_set_name(filter
, _("All files"));
364 gtk_file_filter_add_pattern(filter
, "*");
365 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
366 filter
= gtk_file_filter_new();
367 gtk_file_filter_set_name(filter
, _("Project files"));
368 gtk_file_filter_add_pattern(filter
, "*." GEANY_PROJECT_EXT
);
369 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog
), filter
);
370 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog
), filter
);
372 locale_path
= utils_get_locale_from_utf8(dir
);
373 if (g_file_test(locale_path
, G_FILE_TEST_EXISTS
) &&
374 g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
376 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_path
);
380 gtk_widget_show_all(dialog
);
381 run_open_dialog(GTK_DIALOG(dialog
));
382 gtk_widget_destroy(GTK_WIDGET(dialog
));
386 /* Called when creating, opening, closing and updating projects. */
387 static void update_ui(void)
389 if (main_status
.quitting
)
392 ui_set_window_title(NULL
);
393 build_menu_update(NULL
);
394 // update project name
395 sidebar_openfiles_update_all();
396 ui_update_recent_project_menu();
400 static void remove_foreach_project_filetype(gpointer data
, gpointer user_data
)
402 GeanyFiletype
*ft
= data
;
405 SETPTR(ft
->priv
->projfilecmds
, NULL
);
406 SETPTR(ft
->priv
->projexeccmds
, NULL
);
407 SETPTR(ft
->priv
->projerror_regex_string
, NULL
);
408 ft
->priv
->project_list_entry
= -1;
413 /* open_default will make function reload default session files on close */
414 gboolean
project_close(gboolean open_default
)
416 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
418 /* save project session files, etc */
420 g_warning("Project file \"%s\" could not be written", app
->project
->file_name
);
422 /* close all existing tabs first */
423 if (!document_close_all())
426 ui_set_statusbar(TRUE
, _("Project \"%s\" closed."), app
->project
->name
);
427 destroy_project(open_default
);
432 static void destroy_project(gboolean open_default
)
436 g_return_if_fail(app
->project
!= NULL
);
438 g_signal_emit_by_name(geany_object
, "project-before-close");
440 /* remove project filetypes build entries */
441 if (app
->project
->priv
->build_filetypes_list
!= NULL
)
443 g_ptr_array_foreach(app
->project
->priv
->build_filetypes_list
, remove_foreach_project_filetype
, NULL
);
444 g_ptr_array_free(app
->project
->priv
->build_filetypes_list
, FALSE
);
447 /* remove project non filetype build menu items */
448 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_NON_FT
, -1);
449 build_remove_menu_item(GEANY_BCS_PROJ
, GEANY_GBG_EXEC
, -1);
451 g_free(app
->project
->name
);
452 g_free(app
->project
->description
);
453 g_free(app
->project
->file_name
);
454 g_free(app
->project
->base_path
);
455 g_strfreev(app
->project
->file_patterns
);
457 g_free(app
->project
);
460 foreach_slist(node
, stash_groups
)
461 stash_group_free(node
->data
);
463 g_slist_free(stash_groups
);
466 apply_editor_prefs(); /* ensure that global settings are restored */
468 /* after closing all tabs let's open the tabs found in the default config */
469 if (open_default
&& cl_options
.load_session
)
471 configuration_load_default_session();
472 configuration_open_default_session();
473 document_new_file_if_non_open();
474 ui_focus_current_document();
476 g_signal_emit_by_name(geany_object
, "project-close");
482 /* Shows the file chooser dialog when base path button is clicked
483 * FIXME: this should be connected in Glade but 3.8.1 has a bug
484 * where it won't pass any objects as user data (#588824). */
485 static void on_project_properties_base_path_button_clicked(GtkWidget
*button
,
486 GtkWidget
*base_path_entry
)
490 g_return_if_fail(base_path_entry
!= NULL
);
491 g_return_if_fail(GTK_IS_WIDGET(base_path_entry
));
493 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Base Path"),
494 NULL
, GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
,
495 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
496 GTK_STOCK_OPEN
, GTK_RESPONSE_ACCEPT
,
499 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
501 gtk_entry_set_text(GTK_ENTRY(base_path_entry
),
502 gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
)));
505 gtk_widget_destroy(dialog
);
509 static void insert_build_page(PropertyDialogElements
*e
)
511 GtkWidget
*build_table
, *label
;
512 GeanyDocument
*doc
= document_get_current();
513 GeanyFiletype
*ft
= NULL
;
518 build_table
= build_commands_table(doc
, GEANY_BCS_PROJ
, &(e
->build_properties
), ft
);
519 gtk_container_set_border_width(GTK_CONTAINER(build_table
), 6);
520 label
= gtk_label_new(_("Build"));
521 e
->build_page_num
= gtk_notebook_append_page(GTK_NOTEBOOK(e
->notebook
),
526 static void create_properties_dialog(PropertyDialogElements
*e
)
529 static guint base_path_button_handler_id
= 0;
530 static guint radio_long_line_handler_id
= 0;
532 e
->dialog
= create_project_dialog();
533 e
->notebook
= ui_lookup_widget(e
->dialog
, "project_notebook");
534 e
->file_name
= ui_lookup_widget(e
->dialog
, "label_project_dialog_filename");
535 e
->name
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_name");
536 e
->description
= ui_lookup_widget(e
->dialog
, "textview_project_dialog_description");
537 e
->base_path
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_base_path");
538 e
->patterns
= ui_lookup_widget(e
->dialog
, "entry_project_dialog_file_patterns");
540 gtk_entry_set_max_length(GTK_ENTRY(e
->name
), MAX_NAME_LEN
);
542 ui_entry_add_clear_icon(GTK_ENTRY(e
->name
));
543 ui_entry_add_clear_icon(GTK_ENTRY(e
->base_path
));
544 ui_entry_add_clear_icon(GTK_ENTRY(e
->patterns
));
546 /* Workaround for bug in Glade 3.8.1, see comment above signal handler */
547 if (base_path_button_handler_id
== 0)
549 wid
= ui_lookup_widget(e
->dialog
, "button_project_dialog_base_path");
550 base_path_button_handler_id
=
551 g_signal_connect(wid
, "clicked",
552 G_CALLBACK(on_project_properties_base_path_button_clicked
),
556 /* Same as above, should be in Glade but can't due to bug in 3.8.1 */
557 if (radio_long_line_handler_id
== 0)
559 wid
= ui_lookup_widget(e
->dialog
, "radio_long_line_custom_project");
560 radio_long_line_handler_id
=
561 g_signal_connect(wid
, "toggled",
562 G_CALLBACK(on_radio_long_line_custom_toggled
),
563 ui_lookup_widget(e
->dialog
, "spin_long_line_project"));
568 static void show_project_properties(gboolean show_build
)
570 GeanyProject
*p
= app
->project
;
571 GtkWidget
*widget
= NULL
;
572 GtkWidget
*radio_long_line_custom
;
573 static PropertyDialogElements e
;
576 GtkTextBuffer
*buffer
;
578 g_return_if_fail(app
->project
!= NULL
);
580 if (e
.dialog
== NULL
)
581 create_properties_dialog(&e
);
583 insert_build_page(&e
);
585 foreach_slist(node
, stash_groups
)
586 stash_group_display(node
->data
, e
.dialog
);
588 /* fill the elements with the appropriate data */
589 gtk_entry_set_text(GTK_ENTRY(e
.name
), p
->name
);
590 gtk_label_set_text(GTK_LABEL(e
.file_name
), p
->file_name
);
591 gtk_entry_set_text(GTK_ENTRY(e
.base_path
), p
->base_path
);
593 radio_long_line_custom
= ui_lookup_widget(e
.dialog
, "radio_long_line_custom_project");
594 switch (p
->priv
->long_line_behaviour
)
596 case 0: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_disabled_project"); break;
597 case 1: widget
= ui_lookup_widget(e
.dialog
, "radio_long_line_default_project"); break;
598 case 2: widget
= radio_long_line_custom
; break;
600 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget
), TRUE
);
602 widget
= ui_lookup_widget(e
.dialog
, "spin_long_line_project");
603 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget
), (gdouble
)p
->priv
->long_line_column
);
604 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom
), widget
);
607 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
.description
));
608 gtk_text_buffer_set_text(buffer
, p
->description
? p
->description
: "", -1);
610 /* set the file patterns */
611 entry_text
= p
->file_patterns
? g_strjoinv(" ", p
->file_patterns
) : g_strdup("");
612 gtk_entry_set_text(GTK_ENTRY(e
.patterns
), entry_text
);
615 g_signal_emit_by_name(geany_object
, "project-dialog-open", e
.notebook
);
616 gtk_widget_show_all(e
.dialog
);
618 /* note: notebook page must be shown before setting current page */
620 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
622 gtk_notebook_set_current_page(GTK_NOTEBOOK(e
.notebook
), 0);
624 while (gtk_dialog_run(GTK_DIALOG(e
.dialog
)) == GTK_RESPONSE_OK
)
626 if (update_config(&e
, FALSE
))
628 g_signal_emit_by_name(geany_object
, "project-dialog-confirmed", e
.notebook
);
630 SHOW_ERR(_("Project file could not be written"));
633 ui_set_statusbar(TRUE
, _("Project \"%s\" saved."), app
->project
->name
);
639 build_free_fields(e
.build_properties
);
640 g_signal_emit_by_name(geany_object
, "project-dialog-close", e
.notebook
);
641 gtk_notebook_remove_page(GTK_NOTEBOOK(e
.notebook
), e
.build_page_num
);
642 gtk_widget_hide(e
.dialog
);
646 void project_properties(void)
648 show_project_properties(FALSE
);
652 void project_build_properties(void)
654 show_project_properties(TRUE
);
658 /* checks whether there is an already open project and asks the user if he wants to close it or
659 * abort the current action. Returns FALSE when the current action(the caller) should be cancelled
660 * and TRUE if we can go ahead */
661 gboolean
project_ask_close(void)
663 if (app
->project
!= NULL
)
665 if (!interface_prefs
.warn_on_project_close
||
666 dialogs_show_question_full(NULL
, GTK_STOCK_CLOSE
, GTK_STOCK_CANCEL
,
667 _("Do you want to close it before proceeding?"),
668 _("The '%s' project is open."), app
->project
->name
))
670 return project_close(FALSE
);
680 static GeanyProject
*create_project(void)
682 GeanyProject
*project
= g_new0(GeanyProject
, 1);
684 memset(&priv
, 0, sizeof priv
);
685 priv
.indentation
= &indentation
;
686 project
->priv
= &priv
;
690 project
->file_patterns
= NULL
;
692 project
->priv
->long_line_behaviour
= 1 /* use global settings */;
693 project
->priv
->long_line_column
= editor_prefs
.long_line_column
;
695 app
->project
= project
;
700 /* Verifies data for New & Properties dialogs.
701 * Creates app->project if NULL.
702 * Returns: FALSE if the user needs to change any data. */
703 static gboolean
update_config(const PropertyDialogElements
*e
, gboolean new_project
)
705 const gchar
*name
, *file_name
, *base_path
;
706 gchar
*locale_filename
;
711 g_return_val_if_fail(e
!= NULL
, TRUE
);
713 name
= gtk_entry_get_text(GTK_ENTRY(e
->name
));
714 name_len
= strlen(name
);
717 SHOW_ERR(_("The specified project name is too short."));
718 gtk_widget_grab_focus(e
->name
);
721 else if (name_len
> MAX_NAME_LEN
)
723 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN
);
724 gtk_widget_grab_focus(e
->name
);
729 file_name
= gtk_entry_get_text(GTK_ENTRY(e
->file_name
));
731 file_name
= gtk_label_get_text(GTK_LABEL(e
->file_name
));
733 if (G_UNLIKELY(EMPTY(file_name
)))
735 SHOW_ERR(_("You have specified an invalid project filename."));
736 gtk_widget_grab_focus(e
->file_name
);
740 locale_filename
= utils_get_locale_from_utf8(file_name
);
741 base_path
= gtk_entry_get_text(GTK_ENTRY(e
->base_path
));
742 if (!EMPTY(base_path
))
743 { /* check whether the given directory actually exists */
744 gchar
*locale_path
= utils_get_locale_from_utf8(base_path
);
746 if (! g_path_is_absolute(locale_path
))
747 { /* relative base path, so add base dir of project file name */
748 gchar
*dir
= g_path_get_dirname(locale_filename
);
749 SETPTR(locale_path
, g_build_filename(dir
, locale_path
, NULL
));
753 if (! g_file_test(locale_path
, G_FILE_TEST_IS_DIR
))
757 create_dir
= dialogs_show_question_full(NULL
, GTK_STOCK_OK
, GTK_STOCK_CANCEL
,
758 _("Create the project's base path directory?"),
759 _("The path \"%s\" does not exist."),
763 err_code
= utils_mkdir(locale_path
, TRUE
);
765 if (! create_dir
|| err_code
!= 0)
768 SHOW_ERR1(_("Project base directory could not be created (%s)."),
769 g_strerror(err_code
));
770 gtk_widget_grab_focus(e
->base_path
);
771 utils_free_pointers(2, locale_path
, locale_filename
, NULL
);
777 /* finally test whether the given project file can be written */
778 if ((err_code
= utils_is_file_writable(locale_filename
)) != 0 ||
779 (err_code
= g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
) ? EISDIR
: 0) != 0)
781 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code
));
782 gtk_widget_grab_focus(e
->file_name
);
783 g_free(locale_filename
);
786 else if (new_project
&& g_file_test(locale_filename
, G_FILE_TEST_EXISTS
) &&
787 ! dialogs_show_question_full(NULL
, _("_Replace"), GTK_STOCK_CANCEL
,
789 _("The file '%s' already exists. Do you want to overwrite it?"),
792 gtk_widget_grab_focus(e
->file_name
);
793 g_free(locale_filename
);
796 g_free(locale_filename
);
798 if (app
->project
== NULL
)
805 SETPTR(p
->name
, g_strdup(name
));
806 SETPTR(p
->file_name
, g_strdup(file_name
));
807 /* use "." if base_path is empty */
808 SETPTR(p
->base_path
, g_strdup(!EMPTY(base_path
) ? base_path
: "./"));
810 if (! new_project
) /* save properties specific fields */
812 GtkTextIter start
, end
;
813 GtkTextBuffer
*buffer
;
814 GeanyDocument
*doc
= document_get_current();
815 GeanyBuildCommand
*oldvalue
;
816 GeanyFiletype
*ft
= doc
? doc
->file_type
: NULL
;
822 /* get and set the project description */
823 buffer
= gtk_text_view_get_buffer(GTK_TEXT_VIEW(e
->description
));
824 gtk_text_buffer_get_start_iter(buffer
, &start
);
825 gtk_text_buffer_get_end_iter(buffer
, &end
);
826 SETPTR(p
->description
, gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
));
828 foreach_slist(node
, stash_groups
)
829 stash_group_update(node
->data
, e
->dialog
);
831 /* read the project build menu */
832 oldvalue
= ft
? ft
->priv
->projfilecmds
: NULL
;
833 build_read_project(ft
, e
->build_properties
);
835 if (ft
!= NULL
&& ft
->priv
->projfilecmds
!= oldvalue
&& ft
->priv
->project_list_entry
< 0)
837 if (p
->priv
->build_filetypes_list
== NULL
)
838 p
->priv
->build_filetypes_list
= g_ptr_array_new();
839 ft
->priv
->project_list_entry
= p
->priv
->build_filetypes_list
->len
;
840 g_ptr_array_add(p
->priv
->build_filetypes_list
, ft
);
842 build_menu_update(doc
);
844 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_disabled_project");
845 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
846 p
->priv
->long_line_behaviour
= 0;
849 widget
= ui_lookup_widget(e
->dialog
, "radio_long_line_default_project");
850 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget
)))
851 p
->priv
->long_line_behaviour
= 1;
853 /* "Custom" radio button must be checked */
854 p
->priv
->long_line_behaviour
= 2;
857 widget
= ui_lookup_widget(e
->dialog
, "spin_long_line_project");
858 p
->priv
->long_line_column
= gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget
));
859 apply_editor_prefs();
861 /* get and set the project file patterns */
862 tmp
= g_strdup(gtk_entry_get_text(GTK_ENTRY(e
->patterns
)));
863 g_strfreev(p
->file_patterns
);
865 str
= g_string_new(tmp
);
866 do {} while (utils_string_replace_all(str
, " ", " "));
867 p
->file_patterns
= g_strsplit(str
->str
, " ", -1);
868 g_string_free(str
, TRUE
);
878 static void run_dialog(GtkWidget
*dialog
, GtkWidget
*entry
)
880 /* set filename in the file chooser dialog */
881 const gchar
*utf8_filename
= gtk_entry_get_text(GTK_ENTRY(entry
));
882 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
884 if (g_path_is_absolute(locale_filename
))
886 if (g_file_test(locale_filename
, G_FILE_TEST_EXISTS
))
888 /* if the current filename is a directory, we must use
889 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
890 * we end up in the parent directory */
891 if (g_file_test(locale_filename
, G_FILE_TEST_IS_DIR
))
892 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_filename
);
894 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
896 else /* if the file doesn't yet exist, use at least the current directory */
898 gchar
*locale_dir
= g_path_get_dirname(locale_filename
);
899 gchar
*name
= g_path_get_basename(utf8_filename
);
901 if (g_file_test(locale_dir
, G_FILE_TEST_EXISTS
))
902 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog
), locale_dir
);
903 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), name
);
909 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog
)) != GTK_FILE_CHOOSER_ACTION_OPEN
)
911 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog
), utf8_filename
);
913 g_free(locale_filename
);
916 if (gtk_dialog_run(GTK_DIALOG(dialog
)) == GTK_RESPONSE_ACCEPT
)
918 gchar
*filename
= gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog
));
919 gchar
*tmp_utf8_filename
= utils_get_utf8_from_locale(filename
);
921 gtk_entry_set_text(GTK_ENTRY(entry
), tmp_utf8_filename
);
923 g_free(tmp_utf8_filename
);
926 gtk_widget_destroy(dialog
);
930 static void on_file_save_button_clicked(GtkButton
*button
, PropertyDialogElements
*e
)
934 /* initialise the dialog */
935 dialog
= gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL
,
936 GTK_FILE_CHOOSER_ACTION_SAVE
,
937 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
938 GTK_STOCK_SAVE
, GTK_RESPONSE_ACCEPT
, NULL
);
939 gtk_widget_set_name(dialog
, "GeanyDialogProject");
940 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog
), TRUE
);
941 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog
), TRUE
);
942 gtk_window_set_type_hint(GTK_WINDOW(dialog
), GDK_WINDOW_TYPE_HINT_DIALOG
);
943 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_ACCEPT
);
945 run_dialog(dialog
, e
->file_name
);
949 /* sets the New Project dialog entries according to the base path or project name */
950 static void update_new_project_dlg(GtkEditable
*editable
, PropertyDialogElements
*e
,
955 gchar
*project_dir
= NULL
;
957 if (e
->entries_modified
)
960 if (!EMPTY(local_prefs
.project_file_path
))
961 project_dir
= g_strdup(local_prefs
.project_file_path
);
964 GeanyDocument
*doc
= document_get_current();
966 if (doc
&& doc
->file_name
)
967 project_dir
= g_path_get_dirname(doc
->file_name
);
969 project_dir
= utils_get_utf8_from_locale(g_get_home_dir());
974 gchar
*name
= g_path_get_basename(base_p
);
976 base_path
= g_strdup(base_p
);
977 gtk_entry_set_text(GTK_ENTRY(e
->name
), name
);
978 if (project_prefs
.project_file_in_basedir
)
979 file_name
= g_strconcat(base_path
, G_DIR_SEPARATOR_S
,
980 name
, "." GEANY_PROJECT_EXT
, NULL
);
982 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
983 name
, "." GEANY_PROJECT_EXT
, NULL
);
988 gchar
*name
= gtk_editable_get_chars(editable
, 0, -1);
991 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
992 name
, G_DIR_SEPARATOR_S
, NULL
);
993 if (project_prefs
.project_file_in_basedir
)
994 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, name
, G_DIR_SEPARATOR_S
,
995 name
, "." GEANY_PROJECT_EXT
, NULL
);
997 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
,
998 name
, "." GEANY_PROJECT_EXT
, NULL
);
1002 base_path
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
1003 file_name
= g_strconcat(project_dir
, G_DIR_SEPARATOR_S
, NULL
);
1008 gtk_entry_set_text(GTK_ENTRY(e
->base_path
), base_path
);
1009 gtk_entry_set_text(GTK_ENTRY(e
->file_name
), file_name
);
1011 e
->entries_modified
= FALSE
;
1015 g_free(project_dir
);
1019 static void on_name_entry_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
1021 update_new_project_dlg(editable
, e
, NULL
);
1025 static void on_entries_changed(GtkEditable
*editable
, PropertyDialogElements
*e
)
1027 e
->entries_modified
= TRUE
;
1031 static void on_radio_long_line_custom_toggled(GtkToggleButton
*radio
, GtkWidget
*spin_long_line
)
1033 gtk_widget_set_sensitive(spin_long_line
, gtk_toggle_button_get_active(radio
));
1037 gboolean
project_load_file(const gchar
*locale_file_name
)
1039 g_return_val_if_fail(locale_file_name
!= NULL
, FALSE
);
1041 if (load_config(locale_file_name
))
1043 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
1045 ui_set_statusbar(TRUE
, _("Project \"%s\" opened."), app
->project
->name
);
1047 ui_add_recent_project_file(utf8_filename
);
1048 g_free(utf8_filename
);
1053 gchar
*utf8_filename
= utils_get_utf8_from_locale(locale_file_name
);
1055 ui_set_statusbar(TRUE
, _("Project file \"%s\" could not be loaded."), utf8_filename
);
1056 g_free(utf8_filename
);
1062 /* Reads the given filename and creates a new project with the data found in the file.
1063 * At this point there should not be an already opened project in Geany otherwise it will just
1065 * The filename is expected in the locale encoding. */
1066 static gboolean
load_config(const gchar
*filename
)
1072 /* there should not be an open project */
1073 g_return_val_if_fail(app
->project
== NULL
&& filename
!= NULL
, FALSE
);
1075 config
= g_key_file_new();
1076 if (! g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
))
1078 g_key_file_free(config
);
1082 p
= create_project();
1084 foreach_slist(node
, stash_groups
)
1085 stash_group_load_from_key_file(node
->data
, config
);
1087 p
->name
= utils_get_setting_string(config
, "project", "name", GEANY_STRING_UNTITLED
);
1088 p
->description
= utils_get_setting_string(config
, "project", "description", "");
1089 p
->file_name
= utils_get_utf8_from_locale(filename
);
1090 p
->base_path
= utils_get_setting_string(config
, "project", "base_path", "");
1091 p
->file_patterns
= g_key_file_get_string_list(config
, "project", "file_patterns", NULL
, NULL
);
1093 p
->priv
->long_line_behaviour
= utils_get_setting_integer(config
, "long line marker",
1094 "long_line_behaviour", 1 /* follow global */);
1095 p
->priv
->long_line_column
= utils_get_setting_integer(config
, "long line marker",
1096 "long_line_column", editor_prefs
.long_line_column
);
1097 apply_editor_prefs();
1099 build_load_menu(config
, GEANY_BCS_PROJ
, (gpointer
)p
);
1100 /* save current (non-project) session (it could have been changed since program startup) */
1101 if (!main_status
.opening_session_files
)
1103 configuration_save_default_session();
1104 /* now close all open files */
1105 document_close_all();
1107 /* read session files so they can be opened with configuration_open_files() */
1108 p
->priv
->session_files
= configuration_load_session_files(config
);
1109 g_signal_emit_by_name(geany_object
, "project-open", config
);
1110 g_key_file_free(config
);
1117 static void apply_editor_prefs(void)
1122 editor_apply_update_prefs(documents
[i
]->editor
);
1126 /* Write the project settings as well as the project session files into its configuration files.
1127 * Returns: TRUE if project file was written successfully. */
1128 static gboolean
write_config(void)
1134 gboolean ret
= FALSE
;
1137 g_return_val_if_fail(app
->project
!= NULL
, FALSE
);
1141 config
= g_key_file_new();
1142 /* try to load an existing config to keep manually added comments */
1143 filename
= utils_get_locale_from_utf8(p
->file_name
);
1144 g_key_file_load_from_file(config
, filename
, G_KEY_FILE_NONE
, NULL
);
1146 foreach_slist(node
, stash_groups
)
1147 stash_group_save_to_key_file(node
->data
, config
);
1149 g_key_file_set_string(config
, "project", "name", p
->name
);
1150 g_key_file_set_string(config
, "project", "base_path", p
->base_path
);
1153 g_key_file_set_string(config
, "project", "description", p
->description
);
1154 if (p
->file_patterns
)
1155 g_key_file_set_string_list(config
, "project", "file_patterns",
1156 (const gchar
**) p
->file_patterns
, g_strv_length(p
->file_patterns
));
1159 g_key_file_set_integer(config
, "long line marker", "long_line_behaviour", p
->priv
->long_line_behaviour
);
1160 g_key_file_set_integer(config
, "long line marker", "long_line_column", p
->priv
->long_line_column
);
1162 /* store the session files into the project too */
1163 configuration_save_session_files(config
);
1164 build_save_menu(config
, (gpointer
)p
, GEANY_BCS_PROJ
);
1165 g_signal_emit_by_name(geany_object
, "project-save", config
);
1166 /* write the file */
1167 data
= g_key_file_to_data(config
, NULL
, NULL
);
1168 ret
= (utils_write_file(filename
, data
) == 0);
1172 g_key_file_free(config
);
1178 /** Forces the project file rewrite and emission of the project-save signal. Plugins
1179 * can use this function to save additional project data outside the project dialog.
1184 void project_write_config(void)
1186 if (!write_config())
1187 SHOW_ERR(_("Project file could not be written"));
1191 /* Constructs the project's base path which is used for "Make all" and "Execute".
1192 * The result is an absolute string in UTF-8 encoding which is either the same as
1193 * base path if it is absolute or it is built out of project file name's dir and base_path.
1194 * If there is no project or project's base_path is invalid, NULL will be returned.
1195 * The returned string should be freed when no longer needed. */
1196 gchar
*project_get_base_path(void)
1198 GeanyProject
*project
= app
->project
;
1200 if (project
&& !EMPTY(project
->base_path
))
1202 if (g_path_is_absolute(project
->base_path
))
1203 return g_strdup(project
->base_path
);
1205 { /* build base_path out of project file name's dir and base_path */
1207 gchar
*dir
= g_path_get_dirname(project
->file_name
);
1209 if (utils_str_equal(project
->base_path
, "./"))
1212 path
= g_build_filename(dir
, project
->base_path
, NULL
);
1221 /* This is to save project-related global settings, NOT project file settings. */
1222 void project_save_prefs(GKeyFile
*config
)
1224 GeanyProject
*project
= app
->project
;
1226 if (cl_options
.load_session
)
1228 const gchar
*utf8_filename
= (project
== NULL
) ? "" : project
->file_name
;
1230 g_key_file_set_string(config
, "project", "session_file", utf8_filename
);
1232 g_key_file_set_string(config
, "project", "project_file_path",
1233 FALLBACK(local_prefs
.project_file_path
, ""));
1237 void project_load_prefs(GKeyFile
*config
)
1239 if (cl_options
.load_session
)
1241 g_return_if_fail(project_prefs
.session_file
== NULL
);
1242 project_prefs
.session_file
= utils_get_setting_string(config
, "project",
1243 "session_file", "");
1245 local_prefs
.project_file_path
= utils_get_setting_string(config
, "project",
1246 "project_file_path", NULL
);
1247 if (local_prefs
.project_file_path
== NULL
)
1249 local_prefs
.project_file_path
= g_build_filename(g_get_home_dir(), PROJECT_DIR
, NULL
);
1254 /* Initialize project-related preferences in the Preferences dialog. */
1255 void project_setup_prefs(void)
1257 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1258 GtkWidget
*path_btn
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_button");
1259 static gboolean callback_setup
= FALSE
;
1261 g_return_if_fail(local_prefs
.project_file_path
!= NULL
);
1263 gtk_entry_set_text(GTK_ENTRY(path_entry
), local_prefs
.project_file_path
);
1264 if (! callback_setup
)
1265 { /* connect the callback only once */
1266 callback_setup
= TRUE
;
1267 ui_setup_open_button_callback(path_btn
, NULL
,
1268 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
, GTK_ENTRY(path_entry
));
1273 /* Update project-related preferences after using the Preferences dialog. */
1274 void project_apply_prefs(void)
1276 GtkWidget
*path_entry
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "project_file_path_entry");
1279 str
= gtk_entry_get_text(GTK_ENTRY(path_entry
));
1280 SETPTR(local_prefs
.project_file_path
, g_strdup(str
));
1284 static void add_stash_group(StashGroup
*group
, gboolean apply_defaults
)
1288 stash_groups
= g_slist_prepend(stash_groups
, group
);
1289 if (!apply_defaults
)
1292 kf
= g_key_file_new();
1293 stash_group_load_from_key_file(group
, kf
);
1294 g_key_file_free(kf
);
1298 static void init_stash_prefs(void)
1302 group
= stash_group_new("indentation");
1303 /* copy global defaults */
1304 indentation
= *editor_get_indent_prefs(NULL
);
1305 stash_group_set_use_defaults(group
, FALSE
);
1306 add_stash_group(group
, FALSE
);
1308 stash_group_add_spin_button_integer(group
, &indentation
.width
,
1309 "indent_width", 4, "spin_indent_width_project");
1310 stash_group_add_radio_buttons(group
, (gint
*)(gpointer
)&indentation
.type
,
1311 "indent_type", GEANY_INDENT_TYPE_TABS
,
1312 "radio_indent_spaces_project", GEANY_INDENT_TYPE_SPACES
,
1313 "radio_indent_tabs_project", GEANY_INDENT_TYPE_TABS
,
1314 "radio_indent_both_project", GEANY_INDENT_TYPE_BOTH
,
1316 /* This is a 'hidden' pref for backwards-compatibility */
1317 stash_group_add_integer(group
, &indentation
.hard_tab_width
,
1318 "indent_hard_tab_width", 8);
1319 stash_group_add_toggle_button(group
, &indentation
.detect_type
,
1320 "detect_indent", FALSE
, "check_detect_indent_type_project");
1321 stash_group_add_toggle_button(group
, &indentation
.detect_width
,
1322 "detect_indent_width", FALSE
, "check_detect_indent_width_project");
1323 stash_group_add_combo_box(group
, (gint
*)(gpointer
)&indentation
.auto_indent_mode
,
1324 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS
, "combo_auto_indent_mode_project");
1326 group
= stash_group_new("file_prefs");
1327 stash_group_add_toggle_button(group
, &priv
.final_new_line
,
1328 "final_new_line", file_prefs
.final_new_line
, "check_new_line1");
1329 stash_group_add_toggle_button(group
, &priv
.ensure_convert_new_lines
,
1330 "ensure_convert_new_lines", file_prefs
.ensure_convert_new_lines
, "check_ensure_convert_new_lines1");
1331 stash_group_add_toggle_button(group
, &priv
.strip_trailing_spaces
,
1332 "strip_trailing_spaces", file_prefs
.strip_trailing_spaces
, "check_trailing_spaces1");
1333 stash_group_add_toggle_button(group
, &priv
.replace_tabs
,
1334 "replace_tabs", file_prefs
.replace_tabs
, "check_replace_tabs1");
1335 add_stash_group(group
, TRUE
);
1337 group
= stash_group_new("editor");
1338 stash_group_add_toggle_button(group
, &priv
.line_wrapping
,
1339 "line_wrapping", editor_prefs
.line_wrapping
, "check_line_wrapping1");
1340 stash_group_add_spin_button_integer(group
, &priv
.line_break_column
,
1341 "line_break_column", editor_prefs
.line_break_column
, "spin_line_break1");
1342 stash_group_add_toggle_button(group
, &priv
.auto_continue_multiline
,
1343 "auto_continue_multiline", editor_prefs
.auto_continue_multiline
,
1344 "check_auto_multiline1");
1345 add_stash_group(group
, TRUE
);
1349 #define COPY_PREF(dest, prefname)\
1350 (dest.prefname = priv.prefname)
1352 const GeanyFilePrefs
*project_get_file_prefs(void)
1354 static GeanyFilePrefs fp
;
1360 COPY_PREF(fp
, final_new_line
);
1361 COPY_PREF(fp
, ensure_convert_new_lines
);
1362 COPY_PREF(fp
, strip_trailing_spaces
);
1363 COPY_PREF(fp
, replace_tabs
);
1368 void project_init(void)
1373 void project_finalize(void)