.
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 B;
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 string settings_file = font_name.replace (".bf", ".config");
46 settings_file = settings_file.replace (".birdfont", ".config");
47 return get_child (config_directory, settings_file);
48 }
49
50 public void save (string font_file_name) {
51 File f;
52 StringBuilder sb;
53
54 font_name = font_file_name;
55
56 try {
57 f = get_settings_file ();
58
59 if (f.query_exists ()) {
60 f.delete ();
61 }
62
63 sb = new StringBuilder ();
64
65 sb.append ("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n");
66 sb.append ("<settings>\n");
67
68 foreach (var k in settings.keys) {
69 sb.append ("\t<setting key=\"");
70 sb.append (BirdFontFile.encode (k));
71 sb.append ("\" ");
72 sb.append ("value=\"");
73 sb.append (BirdFontFile.encode (settings.get (k)));
74 sb.append ("\" />\n");
75 }
76
77 sb.append ("</settings>\n");
78
79 FileUtils.set_contents ((!) f.get_path (), sb.str);
80
81 } catch (Error e) {
82 stderr.printf ("Can not save settings. (%s)", e.message);
83 }
84 }
85
86 public void load (string font_file_name) {
87 File f;
88 string xml_data;
89 XmlParser parser;
90
91 try {
92 settings.clear ();
93 font_name = font_file_name;
94 f = get_settings_file ();
95
96 if (f.query_exists ()) {
97 FileUtils.get_contents((!) f.get_path (), out xml_data);
98 parser = new XmlParser (xml_data);
99 parse_settings (parser.get_root_tag ());
100 }
101 } catch (GLib.Error e) {
102 warning (e.message);
103 }
104 }
105
106 void parse_settings (Tag tag) {
107 foreach (Tag t in tag) {
108 if (t.get_name () == "setting") {
109 parse_setting (t);
110 }
111 }
112 }
113
114 void parse_setting (Tag tag) {
115 string key = "";
116 string v = "";
117 foreach (Attribute a in tag.get_attributes ()) {
118 if (a.get_name () == "key") {
119 key = BirdFontFile.decode (a.get_content ());
120 }
121
122 if (a.get_name () == "value") {
123 v = BirdFontFile.decode (a.get_content ());
124 }
125 }
126
127 settings.set (key, v);
128 }
129
130 }
131
132 }
133