.
1 /*
2 Copyright (C) 2015 Johan Mattsson
3
4 This library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 3 of the
7 License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13 */
14
15 using Bird;
16
17 namespace BirdFont {
18
19 /** Font specific settings file. */
20 public class FontSettings : GLib.Object {
21
22 string font_name;
23 Gee.HashMap<string, string> settings;
24
25 public FontSettings () {
26 settings = new Gee.HashMap<string, string> ();
27 font_name = "";
28 }
29
30 public string get_setting (string key) {
31 if (settings.has_key (key)) {
32 return settings.get (key);
33 }
34
35 return "";
36 }
37
38 public void set_setting (string key, string v) {
39 settings.set (key, v);
40 save (font_name);
41 }
42
43 File get_settings_file () {
44 File config_directory = BirdFont.get_settings_directory ();
45 return get_child (config_directory, font_name.replace (".bf", ".config"));
46 }
47
48 public void save (string font_file_name) {
49 File f;
50 StringBuilder sb;
51
52 font_name = font_file_name;
53
54 try {
55 f = get_settings_file ();
56
57 if (f.query_exists ()) {
58 f.delete ();
59 }
60
61 sb = new StringBuilder ();
62
63 sb.append ("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n");
64 sb.append ("<settings>\n");
65
66 foreach (var k in settings.keys) {
67 sb.append ("\t<setting key=\"");
68 sb.append (k);
69 sb.append ("\" ");
70 sb.append ("value=\"");
71 sb.append (XmlParser.encode (settings.get (k)));
72 sb.append ("\" />\n");
73 }
74
75 sb.append ("</settings>\n");
76
77 FileUtils.set_contents ((!) f.get_path (), sb.str);
78
79 } catch (Error e) {
80 stderr.printf ("Can not save settings. (%s)", e.message);
81 }
82 }
83
84 public void load (string font_file_name) {
85 File f;
86 string xml_data;
87 XmlParser parser;
88
89 try {
90 settings.clear ();
91 font_name = font_file_name;
92 f = get_settings_file ();
93
94 if (f.query_exists ()) {
95 FileUtils.get_contents((!) f.get_path (), out xml_data);
96 parser = new XmlParser (xml_data);
97 parse_settings (parser.get_root_tag ());
98 }
99 } catch (GLib.Error e) {
100 warning (e.message);
101 }
102 }
103
104 void parse_settings (Tag tag) {
105 foreach (Tag t in tag) {
106 if (t.get_name () == "setting") {
107 parse_setting (t);
108 }
109 }
110 }
111
112 void parse_setting (Tag tag) {
113 string key = "";
114 string v = "";
115 foreach (Attribute a in tag.get_attributes ()) {
116 if (a.get_name () == "key") {
117 key = a.get_content ();
118 }
119
120 if (a.get_name () == "value") {
121 v = XmlParser.decode (a.get_content ());
122 }
123 }
124
125 settings.set (key, v);
126 }
127
128 }
129
130 }
131