r5068
[geany-mirror.git] / src / keyfile.c
blob4b26dc36f33affdb62857b31da074422aeece655
1 /*
2 * keyfile.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 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.
21 * $Id$
25 * geany.conf preferences file loading and saving.
29 * Session file format:
30 * filename_xx=pos;filetype UID;read only;encoding idx;use_tabs;auto_indent;line_wrapping;filename
33 #include <stdlib.h>
34 #include <string.h>
36 #include "geany.h"
38 #ifdef HAVE_VTE
39 #include <pwd.h>
40 #include <sys/types.h>
41 #include <unistd.h>
42 #endif
44 #include "support.h"
45 #include "keyfile.h"
46 #include "prefs.h"
47 #include "ui_utils.h"
48 #include "utils.h"
49 #include "document.h"
50 #include "filetypes.h"
51 #include "sciwrappers.h"
52 #include "encodings.h"
53 #include "vte.h"
54 #include "main.h"
55 #include "msgwindow.h"
56 #include "search.h"
57 #include "project.h"
58 #include "editor.h"
59 #include "printing.h"
60 #include "templates.h"
61 #include "toolbar.h"
62 #include "stash.h"
65 /* some default settings which are used at the very first start of Geany to fill
66 * the configuration file */
67 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
68 #define GEANY_MIN_SYMBOLLIST_CHARS 4
69 #define GEANY_MSGWIN_HEIGHT 208
70 #define GEANY_DISK_CHECK_TIMEOUT 30
71 #define GEANY_DEFAULT_TOOLS_MAKE "make"
72 #ifdef G_OS_WIN32
73 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe"
74 #else
75 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm"
76 #endif
77 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
78 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
79 #define GEANY_DEFAULT_TOOLS_GREP "grep"
80 #define GEANY_DEFAULT_MRU_LENGTH 10
81 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
82 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Sans 9"
83 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
84 #define GEANY_TOGGLE_MARK "~ "
85 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
88 static gchar *scribble_text = NULL;
89 static gint scribble_pos = -1;
90 static GPtrArray *session_files = NULL;
91 static gint session_notebook_page;
92 static gint hpan_position;
93 static gint vpan_position;
95 static GPtrArray *keyfile_groups = NULL;
96 GPtrArray *pref_groups = NULL;
99 /* The group will be free'd on quitting.
100 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
101 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
103 g_ptr_array_add(keyfile_groups, group);
105 if (for_prefs_dialog)
106 g_ptr_array_add(pref_groups, group);
110 static void init_pref_groups(void)
112 StashGroup *group;
114 group = stash_group_new(PACKAGE);
115 configuration_add_pref_group(group, TRUE);
116 stash_group_add_entry(group, &prefs.default_open_path,
117 "default_open_path", "", "startup_path_entry");
119 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
120 "cmdline_new_files", TRUE, "check_cmdline_new_files");
122 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
123 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
124 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
125 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
126 "sidebar_pos", GTK_POS_LEFT,
127 "radio_sidebar_left", GTK_POS_LEFT,
128 "radio_sidebar_right", GTK_POS_RIGHT,
129 NULL);
131 /* editor display */
132 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
133 "highlighting_invert_all", FALSE, "check_highlighting_invert");
135 stash_group_add_toggle_button(group, &search_prefs.suppress_dialogs,
136 "pref_main_suppress_search_dialogs", FALSE, "check_ask_suppress_search_dialogs");
137 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
138 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
140 /* editor */
141 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
142 "check_detect_indent", FALSE, "check_detect_indent");
143 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
144 "use_tab_to_indent", TRUE, "check_tab_key_indents");
145 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
146 "pref_editor_tab_width", 4, "spin_indent_width");
147 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
148 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
149 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
150 "indent_type", GEANY_INDENT_TYPE_TABS,
151 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
152 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
153 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
154 NULL);
155 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
156 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
157 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
158 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
159 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
160 NULL);
161 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
162 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
163 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
164 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
165 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
166 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
167 "spin_autocompletion_max_entries");
168 stash_group_add_string(group, &editor_prefs.color_scheme,
169 "color_scheme", NULL);
171 /* files */
172 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
173 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
174 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
175 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
177 /* hidden prefs (don't overwrite them so users can edit them manually) */
178 group = stash_group_new(PACKAGE);
179 configuration_add_pref_group(group, FALSE);
180 stash_group_set_write_once(group, TRUE);
182 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
183 "show_editor_scrollbars", TRUE);
184 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
185 "brace_match_ltgt", FALSE);
186 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
187 "use_gtk_word_boundaries", TRUE);
188 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
189 "complete_snippets_whilst_editing", FALSE);
190 stash_group_add_boolean(group, &interface_prefs.show_symbol_list_expanders,
191 "show_symbol_list_expanders", TRUE);
192 stash_group_add_boolean(group, &interface_prefs.compiler_tab_autoscroll,
193 "compiler_tab_autoscroll", TRUE);
194 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
195 "allow_always_save", FALSE);
196 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
197 "use_safe_file_saving", FALSE);
198 /* for backwards-compatibility */
199 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
200 "indent_hard_tab_width", 8);
204 typedef enum SettingAction
206 SETTING_READ,
207 SETTING_WRITE
209 SettingAction;
211 static void settings_action(GKeyFile *config, SettingAction action)
213 guint i;
214 StashGroup *group;
216 foreach_ptr_array(group, i, keyfile_groups)
218 switch (action)
220 case SETTING_READ:
221 stash_group_load_from_key_file(group, config); break;
222 case SETTING_WRITE:
223 stash_group_save_to_key_file(group, config); break;
229 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
231 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
232 guint i;
234 for (i = 0; i < file_prefs.mru_length; i++)
236 if (! g_queue_is_empty(queue))
238 /* copy the values, this is necessary when this function is called from the
239 * preferences dialog or when quitting is canceled to keep the queue intact */
240 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
242 else
244 recent_files[i] = NULL;
245 break;
248 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
249 recent_files[file_prefs.mru_length] = NULL;
250 g_key_file_set_string_list(config, "files", key,
251 (const gchar**)recent_files, file_prefs.mru_length);
252 g_strfreev(recent_files);
256 static gchar *get_session_file_string(GeanyDocument *doc)
258 gchar *fname;
259 gchar *locale_filename;
260 GeanyFiletype *ft = doc->file_type;
262 if (ft == NULL) /* can happen when saving a new file when quitting */
263 ft = filetypes[GEANY_FILETYPES_NONE];
265 locale_filename = utils_get_locale_from_utf8(doc->file_name);
266 /* If the filename contains any ';' (semi-colons) we need to escape them otherwise
267 * g_key_file_get_string_list() would fail reading them, so we replace them before
268 * writing with usual colons which must never appear in a filename and replace them
269 * back when we read the file again from the file.
270 * (g_path_skip_root() to skip C:\... on Windows) */
271 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ";", ':');
273 fname = g_strdup_printf("%d;%s;%d;%d;%d;%d;%d;%s;%d",
274 sci_get_current_position(doc->editor->sci),
275 ft->name,
276 doc->readonly,
277 encodings_get_idx_from_charset(doc->encoding),
278 doc->editor->indent_type,
279 doc->editor->auto_indent,
280 doc->editor->line_wrapping,
281 locale_filename,
282 doc->editor->line_breaking);
283 g_free(locale_filename);
284 return fname;
288 void configuration_save_session_files(GKeyFile *config)
290 gint npage;
291 gchar *tmp;
292 gchar entry[16];
293 guint i = 0, j = 0, max;
294 GeanyDocument *doc;
296 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
297 g_key_file_set_integer(config, "files", "current_page", npage);
299 /* store the filenames to reopen them the next time */
300 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
301 for (i = 0; i < max; i++)
303 doc = document_get_from_page(i);
304 if (doc != NULL && doc->real_path != NULL)
306 gchar *fname;
308 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
309 fname = get_session_file_string(doc);
310 g_key_file_set_string(config, "files", entry, fname);
311 g_free(fname);
312 j++;
315 /* if open filenames less than saved session files, delete existing entries in the list */
316 i = j;
317 while (TRUE)
319 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
320 tmp = g_key_file_get_string(config, "files", entry, NULL);
321 if (G_UNLIKELY(tmp == NULL))
323 break;
325 else
327 g_key_file_remove_key(config, "files", entry, NULL);
328 g_free(tmp);
329 i++;
335 static void save_dialog_prefs(GKeyFile *config)
337 /* new settings should be added in init_pref_groups() */
338 settings_action(config, SETTING_WRITE);
340 /* Some of the key names are not consistent, but this is for backwards compatibility */
342 /* general */
343 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
344 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
345 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
346 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
347 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
348 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
349 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
350 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
351 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
353 /* interface */
354 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
355 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
356 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
357 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
358 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
359 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
360 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
361 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
362 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
363 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
364 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
365 g_key_file_set_boolean(config, PACKAGE, "msgwin_status_visible", interface_prefs.msgwin_status_visible);
366 g_key_file_set_boolean(config, PACKAGE, "msgwin_compiler_visible", interface_prefs.msgwin_compiler_visible);
367 g_key_file_set_boolean(config, PACKAGE, "msgwin_messages_visible", interface_prefs.msgwin_messages_visible);
368 g_key_file_set_boolean(config, PACKAGE, "msgwin_scribble_visible", interface_prefs.msgwin_scribble_visible);
369 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
371 /* display */
372 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
373 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
374 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
375 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
376 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
377 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_global_enabled);
378 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_global_type);
379 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_global_column);
380 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
382 /* editor */
383 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
384 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
385 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
386 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
387 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
388 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
389 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
390 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
391 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
392 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
393 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
394 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
395 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
396 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
397 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
398 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
399 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
401 /* files */
402 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
403 if (file_prefs.default_open_encoding == -1)
404 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
405 else
406 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
407 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
408 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
409 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
410 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
412 /* toolbar */
413 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
414 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
415 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
416 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
417 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
418 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
420 /* templates */
421 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
422 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
423 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
424 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
425 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
426 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
427 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
428 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
430 /* tools settings */
431 g_key_file_set_string(config, "tools", "term_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
432 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
433 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
434 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
436 /* build menu */
437 build_save_menu(config, NULL, GEANY_BCS_PREF);
439 /* printing */
440 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
441 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
442 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
443 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
444 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
445 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
446 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
448 /* VTE */
449 #ifdef HAVE_VTE
450 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
451 if (vte_info.load_vte && vc != NULL)
453 gchar *tmp_string;
455 if (!g_key_file_has_key(config, "VTE", "emulation", NULL)) /* hidden */
456 g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
457 if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL)) /* hidden */
458 g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
459 vc->send_selection_unsafe);
460 g_key_file_set_string(config, "VTE", "font", vc->font);
461 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
462 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
463 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
464 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
465 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
466 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
467 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
468 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
469 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
470 g_key_file_set_string(config, "VTE", "font", vc->font);
471 g_key_file_set_string(config, "VTE", "shell", vc->shell);
472 tmp_string = utils_get_hex_from_color(vc->colour_fore);
473 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
474 g_free(tmp_string);
475 tmp_string = utils_get_hex_from_color(vc->colour_back);
476 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
477 g_free(tmp_string);
478 vte_get_working_directory(); /* refresh vte_info.dir */
479 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
481 #endif
485 static void save_ui_prefs(GKeyFile *config)
487 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
488 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
489 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
490 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
492 /* get the text from the scribble textview */
494 GtkTextBuffer *buffer;
495 GtkTextIter start, end, iter;
496 GtkTextMark *mark;
498 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
499 gtk_text_buffer_get_bounds(buffer, &start, &end);
500 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
501 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
502 g_free(scribble_text);
504 mark = gtk_text_buffer_get_insert(buffer);
505 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
506 scribble_pos = gtk_text_iter_get_offset(&iter);
507 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
510 if (prefs.save_winpos)
512 g_key_file_set_integer(config, PACKAGE, "treeview_position",
513 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
514 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
515 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
517 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
518 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
519 if (gdk_window_get_state(main_widgets.window->window) & GDK_WINDOW_STATE_MAXIMIZED)
520 ui_prefs.geometry[4] = 1;
521 else
522 ui_prefs.geometry[4] = 0;
524 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
527 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
528 if (ui_prefs.custom_commands != NULL)
530 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
531 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
536 void configuration_save(void)
538 GKeyFile *config = g_key_file_new();
539 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
540 gchar *data;
542 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
544 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
545 g_signal_emit_by_name(geany_object, "save-settings", config);
547 save_dialog_prefs(config);
548 save_ui_prefs(config);
549 project_save_prefs(config); /* save project filename, etc. */
550 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
551 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
553 if (cl_options.load_session)
554 configuration_save_session_files(config);
556 /* write the file */
557 data = g_key_file_to_data(config, NULL, NULL);
558 utils_write_file(configfile, data);
559 g_free(data);
561 g_key_file_free(config);
562 g_free(configfile);
566 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
568 gchar **recent_files;
569 gsize i, len = 0;
571 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
572 if (recent_files != NULL)
574 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
576 gchar *filename = g_strdup(recent_files[i]);
577 g_queue_push_tail(queue, filename);
579 g_strfreev(recent_files);
585 * Load session list from the given keyfile, and store it in the global
586 * session_files variable for later file loading
587 * */
588 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
590 guint i;
591 gboolean have_session_files;
592 gchar entry[16];
593 gchar **tmp_array;
594 GError *error = NULL;
596 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
598 if (read_recent_files)
600 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
601 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
604 /* the project may load another list than the main setting */
605 if (session_files != NULL)
606 g_ptr_array_free(session_files, TRUE);
608 session_files = g_ptr_array_new();
609 have_session_files = TRUE;
610 i = 0;
611 while (have_session_files)
613 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
614 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
615 if (! tmp_array || error)
617 g_error_free(error);
618 error = NULL;
619 have_session_files = FALSE;
621 g_ptr_array_add(session_files, tmp_array);
622 i++;
627 static void load_dialog_prefs(GKeyFile *config)
629 gchar *tmp_string, *tmp_string2;
630 const gchar *default_charset = NULL;
632 /* new settings should be added in init_pref_groups() */
633 settings_action(config, SETTING_READ);
635 /* general */
636 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
637 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
638 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
639 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
640 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
641 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
642 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
643 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
644 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
646 /* interface */
647 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
648 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
649 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
650 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
651 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
652 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
653 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
654 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
655 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
656 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
657 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
658 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
659 interface_prefs.msgwin_status_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_status_visible", TRUE);
660 interface_prefs.msgwin_compiler_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_compiler_visible", TRUE);
661 interface_prefs.msgwin_messages_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_messages_visible", TRUE);
662 interface_prefs.msgwin_scribble_visible = utils_get_setting_boolean(config, PACKAGE, "msgwin_scribble_visible", TRUE);
663 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
665 /* display, editor */
666 editor_prefs.long_line_global_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
667 editor_prefs.long_line_global_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
668 if (editor_prefs.long_line_global_type == 2) /* backward compatibility */
670 editor_prefs.long_line_global_type = 0;
671 editor_prefs.long_line_global_enabled = FALSE;
673 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
674 editor_prefs.long_line_global_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
675 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
676 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
677 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
678 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
679 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
680 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
681 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
682 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
683 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
684 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
685 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
686 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
687 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
688 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
689 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
690 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
691 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
692 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
693 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
694 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
695 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
696 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
698 /* Files
699 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
700 g_get_charset(&default_charset);
701 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
702 default_charset);
703 if (tmp_string)
705 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
706 if (enc != NULL)
707 file_prefs.default_new_encoding = enc->idx;
708 else
709 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
711 g_free(tmp_string);
713 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
714 "none");
715 if (tmp_string)
717 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
718 if (enc != NULL)
719 file_prefs.default_open_encoding = enc->idx;
720 else
721 file_prefs.default_open_encoding = -1;
723 g_free(tmp_string);
725 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
726 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
727 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
728 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
730 /* toolbar */
731 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
732 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
734 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
735 if (! toolbar_prefs.use_gtk_default_style)
736 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
738 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
739 if (! toolbar_prefs.use_gtk_default_icon)
740 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
743 /* VTE */
744 #ifdef HAVE_VTE
745 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
746 if (vte_info.load_vte)
748 struct passwd *pw = getpwuid(getuid());
749 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
751 vc = g_new0(VteConfig, 1);
752 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
753 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
754 /* last dir is not set, fallback to user's home directory */
755 vte_info.dir = g_strdup(pw->pw_dir);
756 else if (vte_info.dir == NULL && pw == NULL)
757 /* fallback to root */
758 vte_info.dir = g_strdup("/");
760 vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
761 vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
762 "send_selection_unsafe", FALSE);
763 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
764 vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
765 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
766 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
767 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", FALSE);
768 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
769 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
770 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
771 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
772 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
773 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
774 vc->colour_fore = g_new0(GdkColor, 1);
775 vc->colour_back = g_new0(GdkColor, 1);
776 tmp_string = utils_get_setting_string(config, "VTE", "colour_fore", "#ffffff");
777 gdk_color_parse(tmp_string, vc->colour_fore);
778 g_free(tmp_string);
779 tmp_string = utils_get_setting_string(config, "VTE", "colour_back", "#000000");
780 gdk_color_parse(tmp_string, vc->colour_back);
781 g_free(tmp_string);
783 #endif
784 /* templates */
785 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
786 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
787 tmp_string = utils_get_initials(template_prefs.developer);
788 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
789 g_free(tmp_string);
791 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
793 tmp_string2 = utils_get_hostname();
794 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), tmp_string2);
795 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
796 g_free(tmp_string);
797 g_free(tmp_string2);
798 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", "%Y");
799 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", "%Y-%m-%d");
800 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", "%d.%m.%Y %H:%M:%S %Z");
802 /* tools */
803 tool_prefs.term_cmd = utils_get_setting_string(config, "tools", "term_cmd", GEANY_DEFAULT_TOOLS_TERMINAL);
804 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
805 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
807 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
809 /* build menu */
810 build_set_group_count(GEANY_GBG_FT, utils_get_setting_integer(config, "build-menu", "number_ft_menu_items", 0));
811 build_set_group_count(GEANY_GBG_NON_FT, utils_get_setting_integer(config, "build-menu", "number_non_ft_menu_items", 0));
812 build_set_group_count(GEANY_GBG_EXEC, utils_get_setting_integer(config, "build-menu", "number_exec_menu_items", 0));
813 build_load_menu(config, GEANY_BCS_PREF, NULL);
815 /* printing */
816 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
817 #ifdef G_OS_WIN32
818 /* single quote paths on Win32 for g_spawn_command_line_async */
819 tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
820 #else
821 tmp_string = g_strconcat(tmp_string2, " %f", NULL);
822 #endif
823 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
824 g_free(tmp_string);
825 g_free(tmp_string2);
827 #if GTK_CHECK_VERSION(2, 10, 0)
828 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
829 #else
830 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", FALSE);
831 #endif
832 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
833 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
834 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
835 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
836 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
840 static void load_ui_prefs(GKeyFile *config)
842 gint *geo;
843 GError *error = NULL;
845 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
846 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
847 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
848 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
849 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
851 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
852 _("Type here what you want, use it as a notice/scratch board"));
853 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
855 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", NULL, &error);
856 if (error)
858 ui_prefs.geometry[0] = -1;
859 g_error_free(error);
860 error = NULL;
862 else
864 gint i;
866 ui_prefs.geometry[0] = geo[0];
867 ui_prefs.geometry[1] = geo[1];
868 ui_prefs.geometry[2] = geo[2];
869 ui_prefs.geometry[3] = geo[3];
870 ui_prefs.geometry[4] = geo[4];
872 /* don't use insane values but when main windows was maximized last time, pos might be
873 * negative (due to differences in root window and window decorations) */
874 if (ui_prefs.geometry[4] != 1)
876 for (i = 2; i < 4; i++)
878 if (ui_prefs.geometry[i] < -1)
879 ui_prefs.geometry[i] = -1;
883 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
884 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
885 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
886 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
888 g_free(geo);
893 * Save current session in default configuration file
895 void configuration_save_default_session(void)
897 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
898 gchar *data;
899 GKeyFile *config = g_key_file_new();
901 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
903 if (cl_options.load_session)
904 configuration_save_session_files(config);
906 /* write the file */
907 data = g_key_file_to_data(config, NULL, NULL);
908 utils_write_file(configfile, data);
909 g_free(data);
911 g_key_file_free(config);
912 g_free(configfile);
917 * Only reload the session part of the default configuration
919 void configuration_reload_default_session(void)
921 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
922 GKeyFile *config = g_key_file_new();
924 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
926 configuration_load_session_files(config, FALSE);
928 g_key_file_free(config);
929 g_free(configfile);
933 gboolean configuration_load(void)
935 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
936 GKeyFile *config = g_key_file_new();
938 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
939 { /* config file does not (yet) exist, so try to load a global config file which may be */
940 /* created by distributors */
941 geany_debug("No user config file found, trying to use global configuration.");
942 setptr(configfile, g_strconcat(app->datadir, G_DIR_SEPARATOR_S "geany.conf", NULL));
945 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
947 load_dialog_prefs(config);
948 load_ui_prefs(config);
949 project_load_prefs(config);
950 configuration_load_session_files(config, TRUE);
952 /* this signal can be used e.g. to delay building UI elements until settings have been read */
953 g_signal_emit_by_name(geany_object, "load-settings", config);
955 g_key_file_free(config);
956 g_free(configfile);
957 return TRUE;
961 static gboolean open_session_file(gchar **tmp, guint len)
963 guint pos;
964 const gchar *ft_name;
965 gchar *locale_filename;
966 gint enc_idx, indent_type;
967 gboolean ro, auto_indent, line_wrapping;
968 /** TODO when we have a global pref for line breaking, use its value */
969 gboolean line_breaking = FALSE;
970 gboolean ret = FALSE;
972 pos = atoi(tmp[0]);
973 ft_name = tmp[1];
974 ro = atoi(tmp[2]);
975 enc_idx = atoi(tmp[3]);
976 indent_type = atoi(tmp[4]);
977 auto_indent = atoi(tmp[5]);
978 line_wrapping = atoi(tmp[6]);
979 /* try to get the locale equivalent for the filename */
980 locale_filename = utils_get_locale_from_utf8(tmp[7]);
981 /* replace ':' back with ';' (see get_session_file_string for details) */
982 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ":", ';');
984 if (len > 8)
985 line_breaking = atoi(tmp[8]);
987 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK))
989 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
990 GeanyDocument *doc = document_open_file_full(
991 NULL, locale_filename, pos, ro, ft,
992 (enc_idx >= 0 && enc_idx < GEANY_ENCODINGS_MAX) ?
993 encodings[enc_idx].charset : NULL);
995 if (doc)
997 editor_set_indent_type(doc->editor, indent_type);
998 editor_set_line_wrapping(doc->editor, line_wrapping);
999 doc->editor->line_breaking = line_breaking;
1000 doc->editor->auto_indent = auto_indent;
1001 ret = TRUE;
1004 else
1006 geany_debug("Could not find file '%s'.", tmp[7]);
1009 g_free(locale_filename);
1010 return ret;
1014 /* Open session files
1015 * Note: notebook page switch handler and adding to recent files list is always disabled
1016 * for all files opened within this function */
1017 void configuration_open_files(void)
1019 gint i;
1020 gboolean failure = FALSE;
1022 /* necessary to set it to TRUE for project session support */
1023 main_status.opening_session_files = TRUE;
1025 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1026 while (TRUE)
1028 gchar **tmp = g_ptr_array_index(session_files, i);
1029 guint len;
1031 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1033 if (! open_session_file(tmp, len))
1034 failure = TRUE;
1036 g_strfreev(tmp);
1038 if (file_prefs.tab_order_ltr)
1040 i++;
1041 if (i >= (gint)session_files->len)
1042 break;
1044 else
1046 i--;
1047 if (i < 0)
1048 break;
1052 g_ptr_array_free(session_files, TRUE);
1053 session_files = NULL;
1055 if (failure)
1056 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1057 else if (session_notebook_page >= 0)
1059 /* explicitly allow notebook switch page callback to be called for window title,
1060 * encoding settings and so other things */
1061 main_status.opening_session_files = FALSE;
1062 /** TODO if session_notebook_page is equal to the current notebook tab(the last opened)
1063 ** the notebook switch page callback isn't triggered and e.g. menu items are not updated */
1064 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), session_notebook_page);
1066 main_status.opening_session_files = FALSE;
1070 /* set some settings which are already read from the config file, but need other things, like the
1071 * realisation of the main window */
1072 void configuration_apply_settings(void)
1074 if (scribble_text)
1075 { /* update the scribble widget, because now it's realized */
1076 GtkTextIter iter;
1077 GtkTextBuffer *buffer =
1078 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1080 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1081 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1082 gtk_text_buffer_place_cursor(buffer, &iter);
1084 g_free(scribble_text);
1086 /* set the position of the hpaned and vpaned */
1087 if (prefs.save_winpos)
1089 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1090 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1093 /* set fullscreen after initial draw so that returning to normal view is the right size.
1094 * fullscreen mode is disabled by default, so act only if it is true */
1095 if (ui_prefs.fullscreen)
1097 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1098 ui_prefs.fullscreen = TRUE;
1099 ui_set_fullscreen();
1102 msgwin_show_hide_tabs();
1106 void configuration_init(void)
1108 keyfile_groups = g_ptr_array_new();
1109 pref_groups = g_ptr_array_new();
1110 init_pref_groups();
1114 void configuration_finalize(void)
1116 guint i;
1117 StashGroup *group;
1119 foreach_ptr_array(group, i, keyfile_groups)
1120 stash_group_free(group);
1122 g_ptr_array_free(keyfile_groups, TRUE);
1123 g_ptr_array_free(pref_groups, TRUE);