| 1 | /* i4ucconfiguration.vala |
|---|
| 2 | * |
|---|
| 3 | * Copyright (C) 2009 Matias De la Puente |
|---|
| 4 | * |
|---|
| 5 | * This program is free software: you can redistribute it and/or modify |
|---|
| 6 | * it under the terms of the GNU General Public License as published by |
|---|
| 7 | * the Free Software Foundation, either version 3 of the License, or |
|---|
| 8 | * (at your option) any later version. |
|---|
| 9 | * |
|---|
| 10 | * This program is distributed in the hope that it will be useful, |
|---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 13 | * GNU General Public License for more details. |
|---|
| 14 | * |
|---|
| 15 | * You should have received a copy of the GNU General Public License |
|---|
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 17 | * |
|---|
| 18 | * Author: |
|---|
| 19 | * Matias De la Puente <mfpuente.ar@gmail.com> |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | public class I4uc.Configuration: GLib.Object |
|---|
| 23 | { |
|---|
| 24 | private const string _I4UC_CONFIG = "i4uc config"; |
|---|
| 25 | private const string _SOURCE_FILES_CONFIG = "Source files preferences"; |
|---|
| 26 | |
|---|
| 27 | private static Configuration _config; |
|---|
| 28 | private string _filename; |
|---|
| 29 | |
|---|
| 30 | public bool is_open { set; get; } |
|---|
| 31 | |
|---|
| 32 | public bool projects_panel_visible { set; get; } |
|---|
| 33 | public bool logs_panel_visible { set; get; } |
|---|
| 34 | public string working_directory { set; get; default = Environment.get_home_dir (); } |
|---|
| 35 | |
|---|
| 36 | public string source_files_font { set; get; default = "Monospace 11"; } |
|---|
| 37 | public int source_files_tab_width { set; get; default = 8; } |
|---|
| 38 | public bool source_files_auto_indent { set; get; } |
|---|
| 39 | public bool source_files_highlight_current_line { set; get; } |
|---|
| 40 | public bool source_files_insert_spaces_instead_of_tabs { set; get; } |
|---|
| 41 | public bool source_files_show_line_numbers { set; get; } |
|---|
| 42 | |
|---|
| 43 | public static Configuration get_instance () |
|---|
| 44 | { |
|---|
| 45 | if (_config == null) |
|---|
| 46 | _config = new Configuration (); |
|---|
| 47 | return _config; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | public bool open () |
|---|
| 51 | { |
|---|
| 52 | _filename = Path.build_filename (Environment.get_home_dir (), ".config", "i4uc.conf"); |
|---|
| 53 | if (!FileUtils.test (Path.get_dirname (_filename), FileTest.EXISTS)) |
|---|
| 54 | DirUtils.create (Path.get_dirname (_filename), 0700); |
|---|
| 55 | |
|---|
| 56 | if (!FileUtils.test (_filename, FileTest.EXISTS)) |
|---|
| 57 | return false; |
|---|
| 58 | |
|---|
| 59 | var key_file = new KeyFile (); |
|---|
| 60 | if (!key_file.load_from_file (_filename, KeyFileFlags.NONE)) |
|---|
| 61 | { |
|---|
| 62 | print ("Error opening %s", _filename); |
|---|
| 63 | this.is_open = false; |
|---|
| 64 | return false; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | this.projects_panel_visible = key_file.get_boolean (_I4UC_CONFIG, "projects_panel_visible"); |
|---|
| 68 | this.logs_panel_visible = key_file.get_boolean (_I4UC_CONFIG, "logs_panel_visible"); |
|---|
| 69 | this.working_directory = key_file.get_string (_I4UC_CONFIG, "working_directory"); |
|---|
| 70 | |
|---|
| 71 | this.source_files_font = key_file.get_string (_SOURCE_FILES_CONFIG, "font"); |
|---|
| 72 | this.source_files_tab_width = key_file.get_integer (_SOURCE_FILES_CONFIG, "tab_width"); |
|---|
| 73 | this.source_files_auto_indent = key_file.get_boolean (_SOURCE_FILES_CONFIG, "auto_indent"); |
|---|
| 74 | this.source_files_highlight_current_line = key_file.get_boolean (_SOURCE_FILES_CONFIG, "highlight_current_line"); |
|---|
| 75 | this.source_files_insert_spaces_instead_of_tabs = key_file.get_boolean (_SOURCE_FILES_CONFIG, "insert_spaces_instead_of_tabs"); |
|---|
| 76 | this.source_files_show_line_numbers = key_file.get_boolean (_SOURCE_FILES_CONFIG, "show_line_numbers"); |
|---|
| 77 | |
|---|
| 78 | this.is_open = true; |
|---|
| 79 | return true; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | public void save () |
|---|
| 83 | { |
|---|
| 84 | var key_file = new KeyFile (); |
|---|
| 85 | |
|---|
| 86 | key_file.set_boolean (_I4UC_CONFIG, "projects_panel_visible", _projects_panel_visible); |
|---|
| 87 | key_file.set_boolean (_I4UC_CONFIG, "logs_panel_visible", _logs_panel_visible); |
|---|
| 88 | key_file.set_string (_I4UC_CONFIG, "working_directory", _working_directory); |
|---|
| 89 | |
|---|
| 90 | key_file.set_string (_SOURCE_FILES_CONFIG, "font", _source_files_font); |
|---|
| 91 | key_file.set_integer (_SOURCE_FILES_CONFIG, "tab_width", _source_files_tab_width); |
|---|
| 92 | key_file.set_boolean (_SOURCE_FILES_CONFIG, "auto_indent", _source_files_auto_indent); |
|---|
| 93 | key_file.set_boolean (_SOURCE_FILES_CONFIG, "highlight_current_line", _source_files_highlight_current_line); |
|---|
| 94 | key_file.set_boolean (_SOURCE_FILES_CONFIG, "insert_spaces_instead_of_tabs", _source_files_insert_spaces_instead_of_tabs); |
|---|
| 95 | key_file.set_boolean (_SOURCE_FILES_CONFIG, "show_line_numbers", _source_files_show_line_numbers); |
|---|
| 96 | |
|---|
| 97 | FileUtils.set_contents (_filename, key_file.to_data (null)); |
|---|
| 98 | } |
|---|
| 99 | } |
|---|