1/*
2 * config.vala - This file is part of the Geany MultiTerm plugin
3 *
4 * Copyright (c) 2012 Matthew Brush <matt@geany.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22namespace MultiTerm
23{
24
25	public class Config
26	{
27		internal KeyFile kf;
28		private string _filename;
29		private List<ShellConfig> _shell_configs = new List<ShellConfig>();
30
31		public Config(string filename)
32		{
33			_filename = filename;
34			reload();
35		}
36
37		public bool store()
38		{
39			string data = kf.to_data();
40			try
41			{
42				FileUtils.set_contents(_filename, data);
43				return false;
44			}
45			catch (FileError err)
46			{
47				warning(_("Unable to save config file %s: %s"), _filename, err.message);
48				return true;
49			}
50		}
51
52		public void store_eventually()
53		{
54			Idle.add(() => store());
55		}
56
57		public void reload()
58		{
59			try
60			{
61				kf = new KeyFile();
62
63				/* Delete the old keys and groups */
64				foreach (string group in kf.get_groups())
65				{
66					foreach (string key in kf.get_keys(group))
67						kf.remove_key(group, key);
68					kf.remove_group(group);
69				}
70
71				/* Load from file again */
72				kf.load_from_file(_filename,
73					KeyFileFlags.KEEP_COMMENTS | KeyFileFlags.KEEP_TRANSLATIONS);
74
75				/* Reload shell configurations */
76				_shell_configs = new List<ShellConfig>();
77				foreach (string section in kf.get_groups())
78				{
79					if (section.has_prefix("shell="))
80						_shell_configs.append(new ShellConfig(this, section));
81				}
82			}
83			catch (KeyFileError err)
84			{
85				warning(_("Unable to load config file %s: %s"), _filename, err.message);
86			}
87			catch (FileError err)
88			{
89				warning(_("Unable to load config file %s: %s"), _filename, err.message);
90			}
91		}
92
93		public string filename { get { return _filename; } }
94
95		public bool show_tabs
96		{
97			get
98			{
99				try { return kf.get_boolean("general", "show_tabs"); }
100				catch (KeyFileError err) { return true; }
101			}
102			set
103			{
104				kf.set_boolean("general", "show_tabs", value);
105				store_eventually();
106			}
107		}
108
109		public string external_terminal
110		{
111			owned get
112			{
113				try { return kf.get_string("general", "external_terminal"); }
114				catch (KeyFileError err) { return "xterm"; }
115			}
116			set
117			{
118				kf.set_string("general", "external_terminal", value);
119				store_eventually();
120			}
121		}
122
123		public string location
124		{
125			owned get
126			{
127				try { return kf.get_string("general", "location"); }
128				catch (KeyFileError err) { return "msgwin"; }
129			}
130			set
131			{
132				kf.set_string("general", "location", value);
133				store_eventually();
134			}
135		}
136
137		public List<ShellConfig> shell_configs { get { return _shell_configs; } }
138	}
139}
140