Add note to ui_hookup_widget() doc comments.
[geany-mirror.git] / src / project.c
blob26ce0fd6a3fb5e3bcc5156840fcacfbe5326b941
1 /*
2 * project.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2007-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2007-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /** @file project.h
23 * Project Management.
26 #include "geany.h"
28 #include <string.h>
29 #include <unistd.h>
30 #include <errno.h>
32 #include "project.h"
33 #include "projectprivate.h"
35 #include "dialogs.h"
36 #include "support.h"
37 #include "utils.h"
38 #include "ui_utils.h"
39 #include "document.h"
40 #include "msgwindow.h"
41 #include "main.h"
42 #include "keyfile.h"
43 #include "win32.h"
44 #include "build.h"
45 #include "editor.h"
46 #include "stash.h"
47 #include "sidebar.h"
48 #include "filetypes.h"
51 ProjectPrefs project_prefs = { NULL, FALSE, FALSE };
54 static GeanyProjectPrivate priv;
55 static GeanyIndentPrefs indentation;
57 static StashGroup *indent_group = NULL;
59 static struct
61 gchar *project_file_path; /* in UTF-8 */
62 } local_prefs = { NULL };
65 static gboolean entries_modified;
67 /* simple struct to keep references to the elements of the properties dialog */
68 typedef struct _PropertyDialogElements
70 GtkWidget *dialog;
71 GtkWidget *notebook;
72 GtkWidget *name;
73 GtkWidget *description;
74 GtkWidget *file_name;
75 GtkWidget *base_path;
76 GtkWidget *patterns;
77 BuildTableData build_properties;
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(gboolean emit_signal);
85 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e);
86 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e);
87 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line);
88 static void apply_editor_prefs(void);
91 #define SHOW_ERR(args) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args)
92 #define SHOW_ERR1(args, more) dialogs_show_msgbox(GTK_MESSAGE_ERROR, args, more)
93 #define MAX_NAME_LEN 50
94 /* "projects" is part of the default project base path so be careful when translating
95 * please avoid special characters and spaces, look at the source for details or ask Frank */
96 #define PROJECT_DIR _("projects")
99 void project_new(void)
101 GtkWidget *vbox;
102 GtkWidget *table;
103 GtkWidget *image;
104 GtkWidget *button;
105 GtkWidget *bbox;
106 GtkWidget *label;
107 PropertyDialogElements *e;
109 if (! project_ask_close())
110 return;
112 g_return_if_fail(app->project == NULL);
114 e = g_new0(PropertyDialogElements, 1);
115 e->dialog = gtk_dialog_new_with_buttons(_("New Project"), GTK_WINDOW(main_widgets.window),
116 GTK_DIALOG_DESTROY_WITH_PARENT,
117 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, NULL);
119 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
120 bbox = gtk_hbox_new(FALSE, 0);
121 button = gtk_button_new();
122 image = gtk_image_new_from_stock("gtk-new", GTK_ICON_SIZE_BUTTON);
123 label = gtk_label_new_with_mnemonic(_("C_reate"));
124 gtk_box_pack_start(GTK_BOX(bbox), image, FALSE, FALSE, 3);
125 gtk_box_pack_start(GTK_BOX(bbox), label, FALSE, FALSE, 3);
126 gtk_container_add(GTK_CONTAINER(button), bbox);
127 gtk_dialog_add_action_widget(GTK_DIALOG(e->dialog), button, GTK_RESPONSE_OK);
129 vbox = ui_dialog_vbox_new(GTK_DIALOG(e->dialog));
131 entries_modified = FALSE;
133 table = gtk_table_new(3, 2, FALSE);
134 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
135 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
137 label = gtk_label_new(_("Name:"));
138 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
140 e->name = gtk_entry_new();
141 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
142 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
144 ui_table_add_row(GTK_TABLE(table), 0, label, e->name, NULL);
146 label = gtk_label_new(_("Filename:"));
147 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
149 e->file_name = gtk_entry_new();
150 ui_entry_add_clear_icon(GTK_ENTRY(e->file_name));
151 gtk_entry_set_width_chars(GTK_ENTRY(e->file_name), 30);
152 button = gtk_button_new();
153 g_signal_connect(button, "clicked", G_CALLBACK(on_file_save_button_clicked), e);
154 image = gtk_image_new_from_stock("gtk-open", GTK_ICON_SIZE_BUTTON);
155 gtk_container_add(GTK_CONTAINER(button), image);
156 bbox = gtk_hbox_new(FALSE, 6);
157 gtk_box_pack_start_defaults(GTK_BOX(bbox), e->file_name);
158 gtk_box_pack_start(GTK_BOX(bbox), button, FALSE, FALSE, 0);
160 ui_table_add_row(GTK_TABLE(table), 1, label, bbox, NULL);
162 label = gtk_label_new(_("Base path:"));
163 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
165 e->base_path = gtk_entry_new();
166 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
167 gtk_widget_set_tooltip_text(e->base_path,
168 _("Base directory of all files that make up the project. "
169 "This can be a new path, or an existing directory tree. "
170 "You can use paths relative to the project filename."));
171 bbox = ui_path_box_new(_("Choose Project Base Path"),
172 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
174 ui_table_add_row(GTK_TABLE(table), 2, label, bbox, NULL);
176 gtk_container_add(GTK_CONTAINER(vbox), table);
178 /* signals */
179 g_signal_connect(e->name, "changed", G_CALLBACK(on_name_entry_changed), e);
180 /* run the callback manually to initialise the base_path and file_name fields */
181 on_name_entry_changed(GTK_EDITABLE(e->name), e);
183 g_signal_connect(e->file_name, "changed", G_CALLBACK(on_entries_changed), e);
184 g_signal_connect(e->base_path, "changed", G_CALLBACK(on_entries_changed), e);
186 gtk_widget_show_all(e->dialog);
188 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
190 if (update_config(e, TRUE))
192 if (!write_config(TRUE))
193 SHOW_ERR(_("Project file could not be written"));
194 else
196 ui_set_statusbar(TRUE, _("Project \"%s\" created."), app->project->name);
198 ui_add_recent_project_file(app->project->file_name);
199 break;
203 gtk_widget_destroy(e->dialog);
204 g_free(e);
208 gboolean project_load_file_with_session(const gchar *locale_file_name)
210 if (project_load_file(locale_file_name))
212 if (project_prefs.project_session)
214 configuration_open_files();
215 /* open a new file if no other file was opened */
216 document_new_file_if_non_open();
217 ui_focus_current_document();
219 return TRUE;
221 return FALSE;
225 #ifndef G_OS_WIN32
226 static void run_open_dialog(GtkDialog *dialog)
228 while (gtk_dialog_run(dialog) == GTK_RESPONSE_ACCEPT)
230 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
232 /* try to load the config */
233 if (! project_load_file_with_session(filename))
235 gchar *utf8_filename = utils_get_utf8_from_locale(filename);
237 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), utf8_filename);
238 gtk_widget_grab_focus(GTK_WIDGET(dialog));
239 g_free(utf8_filename);
240 g_free(filename);
241 continue;
243 g_free(filename);
244 break;
247 #endif
250 void project_open(void)
252 const gchar *dir = local_prefs.project_file_path;
253 #ifdef G_OS_WIN32
254 gchar *file;
255 #else
256 GtkWidget *dialog;
257 GtkFileFilter *filter;
258 gchar *locale_path;
259 #endif
260 if (! project_ask_close()) return;
262 #ifdef G_OS_WIN32
263 file = win32_show_project_open_dialog(main_widgets.window, _("Open Project"), dir, FALSE, TRUE);
264 if (file != NULL)
266 /* try to load the config */
267 if (! project_load_file_with_session(file))
269 SHOW_ERR1(_("Project file \"%s\" could not be loaded."), file);
271 g_free(file);
273 #else
275 dialog = gtk_file_chooser_dialog_new(_("Open Project"), GTK_WINDOW(main_widgets.window),
276 GTK_FILE_CHOOSER_ACTION_OPEN,
277 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
278 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
279 gtk_widget_set_name(dialog, "GeanyDialogProject");
281 /* set default Open, so pressing enter can open multiple files */
282 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
283 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
284 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
285 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
286 gtk_window_set_transient_for(GTK_WINDOW(dialog), GTK_WINDOW(main_widgets.window));
287 gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), TRUE);
289 /* add FileFilters */
290 filter = gtk_file_filter_new();
291 gtk_file_filter_set_name(filter, _("All files"));
292 gtk_file_filter_add_pattern(filter, "*");
293 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
294 filter = gtk_file_filter_new();
295 gtk_file_filter_set_name(filter, _("Project files"));
296 gtk_file_filter_add_pattern(filter, "*." GEANY_PROJECT_EXT);
297 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
298 gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(dialog), filter);
300 locale_path = utils_get_locale_from_utf8(dir);
301 if (g_file_test(locale_path, G_FILE_TEST_EXISTS) &&
302 g_file_test(locale_path, G_FILE_TEST_IS_DIR))
304 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_path);
306 g_free(locale_path);
308 gtk_widget_show_all(dialog);
309 run_open_dialog(GTK_DIALOG(dialog));
310 gtk_widget_destroy(GTK_WIDGET(dialog));
311 #endif
315 /* Called when creating, opening, closing and updating projects. */
316 static void update_ui(void)
318 if (main_status.quitting)
319 return;
321 ui_set_window_title(NULL);
322 build_menu_update(NULL);
323 sidebar_openfiles_update_all();
327 static void remove_foreach_project_filetype(gpointer data, gpointer user_data)
329 GeanyFiletype *ft = data;
330 if (ft != NULL)
332 setptr(ft->projfilecmds, NULL);
333 setptr(ft->projexeccmds, NULL);
334 setptr(ft->projerror_regex_string, NULL);
335 ft->project_list_entry = -1;
340 /* open_default will make function reload default session files on close */
341 void project_close(gboolean open_default)
343 g_return_if_fail(app->project != NULL);
345 ui_set_statusbar(TRUE, _("Project \"%s\" closed."), app->project->name);
347 /* use write_config() to save project session files */
348 if (!write_config(FALSE))
349 g_warning("Project file \"%s\" could not be written", app->project->file_name);
351 /* remove project filetypes build entries */
352 if (app->project->build_filetypes_list != NULL)
354 g_ptr_array_foreach(app->project->build_filetypes_list, remove_foreach_project_filetype, NULL);
355 g_ptr_array_free(app->project->build_filetypes_list, FALSE);
358 /* remove project non filetype build menu items */
359 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_NON_FT, -1);
360 build_remove_menu_item(GEANY_BCS_PROJ, GEANY_GBG_EXEC, -1);
362 g_free(app->project->name);
363 g_free(app->project->description);
364 g_free(app->project->file_name);
365 g_free(app->project->base_path);
367 g_free(app->project);
368 app->project = NULL;
370 apply_editor_prefs(); /* ensure that global settings are restored */
372 if (project_prefs.project_session)
374 /* close all existing tabs first */
375 document_close_all();
377 /* after closing all tabs let's open the tabs found in the default config */
378 if (open_default && cl_options.load_session)
380 configuration_reload_default_session();
381 configuration_open_files();
382 /* open a new file if no other file was opened */
383 document_new_file_if_non_open();
384 ui_focus_current_document();
387 g_signal_emit_by_name(geany_object, "project-close");
389 update_ui();
393 static gint build_page_num = 0;
396 static void create_properties_dialog(PropertyDialogElements *e)
398 GtkWidget *table, *notebook, *build_table;
399 GtkWidget *bbox;
400 GtkWidget *label;
401 GtkWidget *swin;
402 GeanyDocument *doc = document_get_current();
403 GeanyFiletype *ft = NULL;
405 e->dialog = ui_lookup_widget(NULL, "project_dialog");
406 gtk_window_set_transient_for(GTK_WINDOW(e->dialog), GTK_WINDOW(main_widgets.window));
407 gtk_window_set_destroy_with_parent(GTK_WINDOW(e->dialog), TRUE);
408 gtk_widget_set_name(e->dialog, "GeanyDialogProject");
410 ui_entry_add_clear_icon(GTK_ENTRY(ui_lookup_widget(e->dialog, "spin_indent_width")));
412 table = gtk_table_new(5, 2, FALSE);
413 gtk_container_set_border_width(GTK_CONTAINER(table), 6);
414 gtk_table_set_row_spacings(GTK_TABLE(table), 5);
415 gtk_table_set_col_spacings(GTK_TABLE(table), 10);
417 label = gtk_label_new(_("Filename:"));
418 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 0, 1,
419 (GtkAttachOptions) (GTK_FILL),
420 (GtkAttachOptions) (0), 0, 0);
421 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
423 e->file_name = gtk_label_new("");
424 gtk_label_set_selectable(GTK_LABEL(e->file_name), TRUE);
425 gtk_table_attach(GTK_TABLE(table), e->file_name, 1, 2, 0, 1,
426 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
427 (GtkAttachOptions) (0), 0, 0);
428 gtk_misc_set_alignment(GTK_MISC(e->file_name), 0, 0);
430 label = gtk_label_new(_("Name:"));
431 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 1, 2,
432 (GtkAttachOptions) (GTK_FILL),
433 (GtkAttachOptions) (0), 0, 0);
434 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
436 e->name = gtk_entry_new();
437 ui_entry_add_clear_icon(GTK_ENTRY(e->name));
438 gtk_entry_set_max_length(GTK_ENTRY(e->name), MAX_NAME_LEN);
439 gtk_table_attach(GTK_TABLE(table), e->name, 1, 2, 1, 2,
440 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
441 (GtkAttachOptions) (0), 0, 0);
443 label = gtk_label_new(_("Description:"));
444 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 2, 3,
445 (GtkAttachOptions) (GTK_FILL),
446 (GtkAttachOptions) (GTK_FILL), 0, 0);
447 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
449 e->description = gtk_text_view_new();
450 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(e->description), GTK_WRAP_WORD);
451 swin = gtk_scrolled_window_new(NULL, NULL);
452 gtk_widget_set_size_request(swin, 250, 80);
453 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin),
454 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
455 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(swin), GTK_WIDGET(e->description));
456 gtk_table_attach(GTK_TABLE(table), swin, 1, 2, 2, 3,
457 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
458 (GtkAttachOptions) (0), 0, 0);
460 label = gtk_label_new(_("Base path:"));
461 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 3, 4,
462 (GtkAttachOptions) (GTK_FILL),
463 (GtkAttachOptions) (0), 0, 0);
464 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
466 e->base_path = gtk_entry_new();
467 ui_entry_add_clear_icon(GTK_ENTRY(e->base_path));
468 gtk_widget_set_tooltip_text(e->base_path,
469 _("Base directory of all files that make up the project. "
470 "This can be a new path, or an existing directory tree. "
471 "You can use paths relative to the project filename."));
472 bbox = ui_path_box_new(_("Choose Project Base Path"),
473 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(e->base_path));
474 gtk_table_attach(GTK_TABLE(table), bbox, 1, 2, 3, 4,
475 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
476 (GtkAttachOptions) (0), 0, 0);
478 if (doc != NULL) ft = doc->file_type;
479 build_table = build_commands_table(doc, GEANY_BCS_PROJ, &(e->build_properties), ft);
480 gtk_container_set_border_width(GTK_CONTAINER(build_table), 6);
481 label = gtk_label_new(_("Build"));
482 notebook = ui_lookup_widget(e->dialog, "project_notebook");
483 build_page_num = gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), build_table, label, 2);
484 e->notebook = notebook;
486 g_signal_connect(ui_lookup_widget(e->dialog, "radio_long_line_custom"), "toggled",
487 G_CALLBACK(on_radio_long_line_custom_toggled), ui_lookup_widget(e->dialog, "spin_long_line"));
489 label = gtk_label_new(_("File patterns:"));
490 gtk_table_attach(GTK_TABLE(table), label, 0, 1, 4, 5,
491 (GtkAttachOptions) (GTK_FILL),
492 (GtkAttachOptions) (0), 0, 0);
493 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
495 e->patterns = gtk_entry_new();
496 gtk_widget_set_tooltip_text(e->patterns,
497 _("Space separated list of file patterns used for the find in files dialog "
498 "(e.g. *.c *.h)"));
499 gtk_table_attach(GTK_TABLE(table), e->patterns, 1, 2, 4, 5,
500 (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
501 (GtkAttachOptions) (0), 0, 0);
503 label = gtk_label_new(_("Project"));
504 gtk_notebook_insert_page(GTK_NOTEBOOK(notebook), table, label, 0);
505 build_page_num++;
509 static void show_project_properties(gboolean show_build)
511 PropertyDialogElements *e = g_new(PropertyDialogElements, 1);
512 GeanyProject *p = app->project;
513 GtkWidget *widget = NULL;
514 GtkWidget *radio_long_line_custom;
516 g_return_if_fail(app->project != NULL);
518 entries_modified = FALSE;
520 create_properties_dialog(e);
522 stash_group_display(indent_group, e->dialog);
524 /* fill the elements with the appropriate data */
525 gtk_entry_set_text(GTK_ENTRY(e->name), p->name);
526 gtk_label_set_text(GTK_LABEL(e->file_name), p->file_name);
527 gtk_entry_set_text(GTK_ENTRY(e->base_path), p->base_path);
529 radio_long_line_custom = ui_lookup_widget(e->dialog, "radio_long_line_custom");
530 switch (p->long_line_behaviour)
532 case 0: widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled"); break;
533 case 1: widget = ui_lookup_widget(e->dialog, "radio_long_line_default"); break;
534 case 2: widget = radio_long_line_custom; break;
536 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
538 widget = ui_lookup_widget(e->dialog, "spin_long_line");
539 gtk_spin_button_set_value(GTK_SPIN_BUTTON(widget), (gdouble)p->long_line_column);
540 on_radio_long_line_custom_toggled(GTK_TOGGLE_BUTTON(radio_long_line_custom), widget);
542 if (p->description != NULL)
543 { /* set text */
544 GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
545 gtk_text_buffer_set_text(buffer, p->description, -1);
548 if (p->file_patterns != NULL)
549 { /* set the file patterns */
550 gchar *str;
552 str = g_strjoinv(" ", p->file_patterns);
553 gtk_entry_set_text(GTK_ENTRY(e->patterns), str);
554 g_free(str);
557 g_signal_emit_by_name(geany_object, "project-dialog-create", e->notebook);
558 gtk_widget_show_all(e->dialog);
560 /* note: notebook page must be shown before setting current page */
561 if (show_build)
562 gtk_notebook_set_current_page(GTK_NOTEBOOK(e->notebook), build_page_num);
563 else
564 gtk_notebook_set_current_page(GTK_NOTEBOOK(e->notebook), 0);
566 while (gtk_dialog_run(GTK_DIALOG(e->dialog)) == GTK_RESPONSE_OK)
568 if (update_config(e, FALSE))
570 g_signal_emit_by_name(geany_object, "project-dialog-confirmed", e->notebook);
571 if (!write_config(TRUE))
572 SHOW_ERR(_("Project file could not be written"));
573 else
575 ui_set_statusbar(TRUE, _("Project \"%s\" saved."), app->project->name);
576 break;
580 build_free_fields(e->build_properties);
581 gtk_widget_destroy(e->dialog);
582 g_free(e);
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 already open."), app->project->name))
609 project_close(FALSE);
610 return TRUE;
612 else
613 return FALSE;
615 else
616 return TRUE;
620 static GeanyProject *create_project(void)
622 GeanyProject *project = g_new0(GeanyProject, 1);
624 memset(&priv, 0, sizeof priv);
625 indentation = *editor_get_indent_prefs(NULL);
626 priv.indentation = &indentation;
627 project->priv = &priv;
629 project->file_patterns = NULL;
631 project->long_line_behaviour = 1 /* use global settings */;
632 project->long_line_column = editor_prefs.long_line_column;
634 app->project = project;
635 return project;
639 /* Verifies data for New & Properties dialogs.
640 * Returns: FALSE if the user needs to change any data. */
641 static gboolean update_config(const PropertyDialogElements *e, gboolean new_project)
643 const gchar *name, *file_name, *base_path;
644 gchar *locale_filename;
645 gint name_len;
646 gint err_code = 0;
647 GeanyProject *p;
649 g_return_val_if_fail(e != NULL, TRUE);
651 name = gtk_entry_get_text(GTK_ENTRY(e->name));
652 name_len = strlen(name);
653 if (name_len == 0)
655 SHOW_ERR(_("The specified project name is too short."));
656 gtk_widget_grab_focus(e->name);
657 return FALSE;
659 else if (name_len > MAX_NAME_LEN)
661 SHOW_ERR1(_("The specified project name is too long (max. %d characters)."), MAX_NAME_LEN);
662 gtk_widget_grab_focus(e->name);
663 return FALSE;
666 if (new_project)
667 file_name = gtk_entry_get_text(GTK_ENTRY(e->file_name));
668 else
669 file_name = gtk_label_get_text(GTK_LABEL(e->file_name));
671 if (G_UNLIKELY(! NZV(file_name)))
673 SHOW_ERR(_("You have specified an invalid project filename."));
674 gtk_widget_grab_focus(e->file_name);
675 return FALSE;
678 locale_filename = utils_get_locale_from_utf8(file_name);
679 base_path = gtk_entry_get_text(GTK_ENTRY(e->base_path));
680 if (NZV(base_path))
681 { /* check whether the given directory actually exists */
682 gchar *locale_path = utils_get_locale_from_utf8(base_path);
684 if (! g_path_is_absolute(locale_path))
685 { /* relative base path, so add base dir of project file name */
686 gchar *dir = g_path_get_dirname(locale_filename);
687 setptr(locale_path, g_strconcat(dir, G_DIR_SEPARATOR_S, locale_path, NULL));
688 g_free(dir);
691 if (! g_file_test(locale_path, G_FILE_TEST_IS_DIR))
693 gboolean create_dir;
695 create_dir = dialogs_show_question_full(NULL, GTK_STOCK_OK, GTK_STOCK_CANCEL,
696 _("Create the project's base path directory?"),
697 _("The path \"%s\" does not exist."),
698 base_path);
700 if (create_dir)
701 err_code = utils_mkdir(locale_path, TRUE);
703 if (! create_dir || err_code != 0)
705 if (err_code != 0)
706 SHOW_ERR1(_("Project base directory could not be created (%s)."),
707 g_strerror(err_code));
708 gtk_widget_grab_focus(e->base_path);
709 utils_free_pointers(2, locale_path, locale_filename, NULL);
710 return FALSE;
713 g_free(locale_path);
715 /* finally test whether the given project file can be written */
716 if ((err_code = utils_is_file_writable(locale_filename)) != 0 ||
717 (err_code = g_file_test(locale_filename, G_FILE_TEST_IS_DIR) ? EISDIR : 0) != 0)
719 SHOW_ERR1(_("Project file could not be written (%s)."), g_strerror(err_code));
720 gtk_widget_grab_focus(e->file_name);
721 g_free(locale_filename);
722 return FALSE;
724 g_free(locale_filename);
726 if (app->project == NULL)
728 create_project();
729 new_project = TRUE;
731 p = app->project;
733 setptr(p->name, g_strdup(name));
734 setptr(p->file_name, g_strdup(file_name));
735 /* use "." if base_path is empty */
736 setptr(p->base_path, g_strdup(NZV(base_path) ? base_path : "./"));
738 if (! new_project) /* save properties specific fields */
740 GtkTextIter start, end;
741 GtkTextBuffer *buffer;
742 GeanyDocument *doc = document_get_current();
743 GeanyBuildCommand *oldvalue;
744 GeanyFiletype *ft = doc ? doc->file_type : NULL;
745 GtkWidget *widget;
746 gchar *tmp;
747 GString *str;
749 /* get and set the project description */
750 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(e->description));
751 gtk_text_buffer_get_start_iter(buffer, &start);
752 gtk_text_buffer_get_end_iter(buffer, &end);
753 setptr(p->description, g_strdup(gtk_text_buffer_get_text(buffer, &start, &end, FALSE)));
755 stash_group_update(indent_group, e->dialog);
757 /* read the project build menu */
758 oldvalue = ft ? ft->projfilecmds : NULL;
759 build_read_project(ft, e->build_properties);
761 if (ft != NULL && ft->projfilecmds != oldvalue && ft->project_list_entry < 0)
763 if (p->build_filetypes_list == NULL)
764 p->build_filetypes_list = g_ptr_array_new();
765 ft->project_list_entry = p->build_filetypes_list->len;
766 g_ptr_array_add(p->build_filetypes_list, ft);
768 build_menu_update(doc);
770 widget = ui_lookup_widget(e->dialog, "radio_long_line_disabled");
771 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
772 p->long_line_behaviour = 0;
773 else
775 widget = ui_lookup_widget(e->dialog, "radio_long_line_default");
776 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
777 p->long_line_behaviour = 1;
778 else
779 /* "Custom" radio button must be checked */
780 p->long_line_behaviour = 2;
783 widget = ui_lookup_widget(e->dialog, "spin_long_line");
784 p->long_line_column = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
785 apply_editor_prefs();
787 /* get and set the project file patterns */
788 tmp = g_strdup(gtk_entry_get_text(GTK_ENTRY(e->patterns)));
789 g_strfreev(p->file_patterns);
790 g_strstrip(tmp);
791 str = g_string_new(tmp);
792 do {} while (utils_string_replace_all(str, " ", " "));
793 p->file_patterns = g_strsplit(str->str, " ", -1);
794 g_string_free(str, TRUE);
795 g_free(tmp);
798 update_ui();
800 return TRUE;
804 #ifndef G_OS_WIN32
805 static void run_dialog(GtkWidget *dialog, GtkWidget *entry)
807 /* set filename in the file chooser dialog */
808 const gchar *utf8_filename = gtk_entry_get_text(GTK_ENTRY(entry));
809 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
811 if (g_path_is_absolute(locale_filename))
813 if (g_file_test(locale_filename, G_FILE_TEST_EXISTS))
815 /* if the current filename is a directory, we must use
816 * gtk_file_chooser_set_current_folder(which expects a locale filename) otherwise
817 * we end up in the parent directory */
818 if (g_file_test(locale_filename, G_FILE_TEST_IS_DIR))
819 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_filename);
820 else
821 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), utf8_filename);
823 else /* if the file doesn't yet exist, use at least the current directory */
825 gchar *locale_dir = g_path_get_dirname(locale_filename);
826 gchar *name = g_path_get_basename(utf8_filename);
828 if (g_file_test(locale_dir, G_FILE_TEST_EXISTS))
829 gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog), locale_dir);
830 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), name);
832 g_free(name);
833 g_free(locale_dir);
836 else if (gtk_file_chooser_get_action(GTK_FILE_CHOOSER(dialog)) != GTK_FILE_CHOOSER_ACTION_OPEN)
838 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), utf8_filename);
840 g_free(locale_filename);
842 /* run it */
843 if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
845 gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
846 gchar *tmp_utf8_filename = utils_get_utf8_from_locale(filename);
848 gtk_entry_set_text(GTK_ENTRY(entry), tmp_utf8_filename);
850 g_free(tmp_utf8_filename);
851 g_free(filename);
853 gtk_widget_destroy(dialog);
855 #endif
858 static void on_file_save_button_clicked(GtkButton *button, PropertyDialogElements *e)
860 #ifdef G_OS_WIN32
861 gchar *path = win32_show_project_open_dialog(e->dialog, _("Choose Project Filename"),
862 gtk_entry_get_text(GTK_ENTRY(e->file_name)), TRUE, TRUE);
863 if (path != NULL)
865 gtk_entry_set_text(GTK_ENTRY(e->file_name), path);
866 g_free(path);
868 #else
869 GtkWidget *dialog;
871 /* initialise the dialog */
872 dialog = gtk_file_chooser_dialog_new(_("Choose Project Filename"), NULL,
873 GTK_FILE_CHOOSER_ACTION_SAVE,
874 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
875 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL);
876 gtk_widget_set_name(dialog, "GeanyDialogProject");
877 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
878 gtk_window_set_skip_taskbar_hint(GTK_WINDOW(dialog), TRUE);
879 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
880 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT);
882 run_dialog(dialog, e->file_name);
883 #endif
887 /* sets the project base path and the project file name according to the project name */
888 static void on_name_entry_changed(GtkEditable *editable, PropertyDialogElements *e)
890 gchar *base_path;
891 gchar *file_name;
892 gchar *name;
893 const gchar *project_dir = local_prefs.project_file_path;
895 if (entries_modified)
896 return;
898 name = gtk_editable_get_chars(editable, 0, -1);
899 if (NZV(name))
901 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
902 name, G_DIR_SEPARATOR_S, NULL);
903 if (project_prefs.project_file_in_basedir)
904 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, name, G_DIR_SEPARATOR_S,
905 name, "." GEANY_PROJECT_EXT, NULL);
906 else
907 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S,
908 name, "." GEANY_PROJECT_EXT, NULL);
910 else
912 base_path = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
913 file_name = g_strconcat(project_dir, G_DIR_SEPARATOR_S, NULL);
915 g_free(name);
917 gtk_entry_set_text(GTK_ENTRY(e->base_path), base_path);
918 gtk_entry_set_text(GTK_ENTRY(e->file_name), file_name);
920 entries_modified = FALSE;
922 g_free(base_path);
923 g_free(file_name);
927 static void on_entries_changed(GtkEditable *editable, PropertyDialogElements *e)
929 entries_modified = TRUE;
933 static void on_radio_long_line_custom_toggled(GtkToggleButton *radio, GtkWidget *spin_long_line)
935 gtk_widget_set_sensitive(spin_long_line, gtk_toggle_button_get_active(radio));
939 gboolean project_load_file(const gchar *locale_file_name)
941 g_return_val_if_fail(locale_file_name != NULL, FALSE);
943 if (load_config(locale_file_name))
945 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
947 ui_set_statusbar(TRUE, _("Project \"%s\" opened."), app->project->name);
949 ui_add_recent_project_file(utf8_filename);
950 g_free(utf8_filename);
951 return TRUE;
953 else
955 gchar *utf8_filename = utils_get_utf8_from_locale(locale_file_name);
957 ui_set_statusbar(TRUE, _("Project file \"%s\" could not be loaded."), utf8_filename);
958 g_free(utf8_filename);
960 return FALSE;
964 /* Reads the given filename and creates a new project with the data found in the file.
965 * At this point there should not be an already opened project in Geany otherwise it will just
966 * return.
967 * The filename is expected in the locale encoding. */
968 static gboolean load_config(const gchar *filename)
970 GKeyFile *config;
971 GeanyProject *p;
973 /* there should not be an open project */
974 g_return_val_if_fail(app->project == NULL && filename != NULL, FALSE);
976 config = g_key_file_new();
977 if (! g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL))
979 g_key_file_free(config);
980 return FALSE;
983 p = create_project();
985 stash_group_load_from_key_file(indent_group, config);
987 p->name = utils_get_setting_string(config, "project", "name", GEANY_STRING_UNTITLED);
988 p->description = utils_get_setting_string(config, "project", "description", "");
989 p->file_name = utils_get_utf8_from_locale(filename);
990 p->base_path = utils_get_setting_string(config, "project", "base_path", "");
991 p->file_patterns = g_key_file_get_string_list(config, "project", "file_patterns", NULL, NULL);
993 p->long_line_behaviour = utils_get_setting_integer(config, "long line marker",
994 "long_line_behaviour", 1 /* follow global */);
995 p->long_line_column = utils_get_setting_integer(config, "long line marker",
996 "long_line_column", editor_prefs.long_line_column);
997 apply_editor_prefs();
999 build_load_menu(config, GEANY_BCS_PROJ, (gpointer)p);
1000 if (project_prefs.project_session)
1002 /* save current (non-project) session (it could has been changed since program startup) */
1003 configuration_save_default_session();
1004 /* now close all open files */
1005 document_close_all();
1006 /* read session files so they can be opened with configuration_open_files() */
1007 configuration_load_session_files(config, FALSE);
1008 ui_focus_current_document();
1010 g_signal_emit_by_name(geany_object, "project-open", config);
1011 g_key_file_free(config);
1013 update_ui();
1014 return TRUE;
1018 static void apply_editor_prefs(void)
1020 guint i;
1022 foreach_document(i)
1023 editor_apply_update_prefs(documents[i]->editor);
1027 /* Write the project settings as well as the project session files into its configuration files.
1028 * emit_signal defines whether the project-save signal should be emitted. When write_config()
1029 * is called while closing a project, this is used to skip emitting the signal because
1030 * project-close will be emitted afterwards.
1031 * Returns: TRUE if project file was written successfully. */
1032 static gboolean write_config(gboolean emit_signal)
1034 GeanyProject *p;
1035 GKeyFile *config;
1036 gchar *filename;
1037 gchar *data;
1038 gboolean ret = FALSE;
1040 g_return_val_if_fail(app->project != NULL, FALSE);
1042 p = app->project;
1044 config = g_key_file_new();
1045 /* try to load an existing config to keep manually added comments */
1046 filename = utils_get_locale_from_utf8(p->file_name);
1047 g_key_file_load_from_file(config, filename, G_KEY_FILE_NONE, NULL);
1049 stash_group_save_to_key_file(indent_group, config);
1051 g_key_file_set_string(config, "project", "name", p->name);
1052 g_key_file_set_string(config, "project", "base_path", p->base_path);
1054 if (p->description)
1055 g_key_file_set_string(config, "project", "description", p->description);
1056 if (p->file_patterns)
1057 g_key_file_set_string_list(config, "project", "file_patterns",
1058 (const gchar**) p->file_patterns, g_strv_length(p->file_patterns));
1060 g_key_file_set_integer(config, "long line marker", "long_line_behaviour", p->long_line_behaviour);
1061 g_key_file_set_integer(config, "long line marker", "long_line_column", p->long_line_column);
1063 /* store the session files into the project too */
1064 if (project_prefs.project_session)
1065 configuration_save_session_files(config);
1066 build_save_menu(config, (gpointer)p, GEANY_BCS_PROJ);
1067 if (emit_signal)
1069 g_signal_emit_by_name(geany_object, "project-save", config);
1071 /* write the file */
1072 data = g_key_file_to_data(config, NULL, NULL);
1073 ret = (utils_write_file(filename, data) == 0);
1075 g_free(data);
1076 g_free(filename);
1077 g_key_file_free(config);
1079 return ret;
1083 /* Constructs the project's base path which is used for "Make all" and "Execute".
1084 * The result is an absolute string in UTF-8 encoding which is either the same as
1085 * base path if it is absolute or it is built out of project file name's dir and base_path.
1086 * If there is no project or project's base_path is invalid, NULL will be returned.
1087 * The returned string should be freed when no longer needed. */
1088 gchar *project_get_base_path(void)
1090 GeanyProject *project = app->project;
1092 if (project && NZV(project->base_path))
1094 if (g_path_is_absolute(project->base_path))
1095 return g_strdup(project->base_path);
1096 else
1097 { /* build base_path out of project file name's dir and base_path */
1098 gchar *path;
1099 gchar *dir = g_path_get_dirname(project->file_name);
1101 if (utils_str_equal(project->base_path, "./"))
1102 return dir;
1103 else
1104 path = g_strconcat(dir, G_DIR_SEPARATOR_S, project->base_path, NULL);
1105 g_free(dir);
1106 return path;
1109 return NULL;
1113 /* This is to save project-related global settings, NOT project file settings. */
1114 void project_save_prefs(GKeyFile *config)
1116 GeanyProject *project = app->project;
1118 if (cl_options.load_session)
1120 const gchar *utf8_filename = (project == NULL) ? "" : project->file_name;
1122 g_key_file_set_string(config, "project", "session_file", utf8_filename);
1124 g_key_file_set_string(config, "project", "project_file_path",
1125 NVL(local_prefs.project_file_path, ""));
1129 void project_load_prefs(GKeyFile *config)
1131 if (cl_options.load_session)
1133 g_return_if_fail(project_prefs.session_file == NULL);
1134 project_prefs.session_file = utils_get_setting_string(config, "project",
1135 "session_file", "");
1137 local_prefs.project_file_path = utils_get_setting_string(config, "project",
1138 "project_file_path", NULL);
1139 if (local_prefs.project_file_path == NULL)
1141 local_prefs.project_file_path = g_strconcat(g_get_home_dir(),
1142 G_DIR_SEPARATOR_S, PROJECT_DIR, NULL);
1147 /* Initialize project-related preferences in the Preferences dialog. */
1148 void project_setup_prefs(void)
1150 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1151 GtkWidget *path_btn = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_button");
1152 static gboolean callback_setup = FALSE;
1154 g_return_if_fail(local_prefs.project_file_path != NULL);
1156 gtk_entry_set_text(GTK_ENTRY(path_entry), local_prefs.project_file_path);
1157 if (! callback_setup)
1158 { /* connect the callback only once */
1159 callback_setup = TRUE;
1160 ui_setup_open_button_callback(path_btn, NULL,
1161 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER, GTK_ENTRY(path_entry));
1166 /* Update project-related preferences after using the Preferences dialog. */
1167 void project_apply_prefs(void)
1169 GtkWidget *path_entry = ui_lookup_widget(ui_widgets.prefs_dialog, "project_file_path_entry");
1170 const gchar *str;
1172 str = gtk_entry_get_text(GTK_ENTRY(path_entry));
1173 setptr(local_prefs.project_file_path, g_strdup(str));
1177 void project_init(void)
1179 StashGroup *group;
1181 group = stash_group_new("indentation");
1182 /* defaults are copied from editor indent prefs */
1183 stash_group_set_use_defaults(group, FALSE);
1184 indent_group = group;
1186 stash_group_add_spin_button_integer(group, &indentation.width,
1187 "indent_width", 4, "spin_indent_width");
1188 stash_group_add_radio_buttons(group, (gint*)(gpointer)&indentation.type,
1189 "indent_type", GEANY_INDENT_TYPE_TABS,
1190 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
1191 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
1192 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
1193 NULL);
1194 /* This is a 'hidden' pref for backwards-compatibility */
1195 stash_group_add_integer(group, &indentation.hard_tab_width,
1196 "indent_hard_tab_width", 8);
1197 stash_group_add_toggle_button(group, &indentation.detect_type,
1198 "detect_indent", FALSE, "check_detect_indent_type");
1199 stash_group_add_toggle_button(group, &indentation.detect_width,
1200 "detect_indent_width", FALSE, "check_detect_indent_width");
1201 stash_group_add_combo_box(group, (gint*)(gpointer)&indentation.auto_indent_mode,
1202 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
1206 void project_finalize(void)
1208 stash_group_free(indent_group);