Merge commit 'upstream/0.2.85' into experimental
[debian/pino.git] / src / settings.vala
1 using Gee; 
2 using Xml;
3 using PinoEnums;
4
5 public class Settings : Object {
6         
7         public int vpaned_position {get; set; default = 400;}
8         public int hpaned_position {get; set; default = 150;}
9         
10         public string current_item {get; set; default = "";}
11         
12         public ArrayList<string> selected_for_posting {get; set; default = new ArrayList<string>();}
13         
14         private string settings_path;
15         
16         public Settings() {
17                 string app_dir = Environment.get_user_config_dir() + "/%s".printf(Config.APPNAME);
18                 
19                 var dir = File.new_for_path(app_dir);
20                 
21                 if(!dir.query_exists(null)) {
22                         try {
23                                 dir.make_directory(null);
24                         } catch(GLib.Error e) {
25                                 debug(e.message); //TODO
26                         }
27                 }
28                 
29                 dir = null;
30                 
31                 settings_path = app_dir + "/settings.xml";
32                 File opt_file = File.new_for_path(settings_path);
33                 
34                 if(opt_file.query_exists(null)) {
35                         FileInputStream fstream;
36                         try {
37                                 fstream = opt_file.read(null);
38                         } catch(GLib.Error e) {
39                                 debug(e.message); //TODO
40                                 return;
41                         }
42                         DataInputStream stream = new DataInputStream(fstream);
43                         
44                         string data;
45                         try {
46                                 data = stream.read_until("", null, null);
47                         } catch(GLib.Error e) {
48                                 debug(e.message); //TODO
49                                 return;
50                         }
51                         from_xml(data);
52                 }
53         }
54         
55         public void sync() {
56                 File file = File.new_for_path(settings_path);
57                 
58                 FileOutputStream stream;
59                 try {
60                         stream = file.replace(null, false, FileCreateFlags.NONE, null);
61                 } catch(GLib.Error e) {
62                         debug(e.message); //TODO
63                         return;
64                 }
65                 
66                 DataOutputStream data_stream = new DataOutputStream(stream);
67                 
68                 try {
69                         data_stream.put_string(to_xml(), null);
70                 } catch(GLib.Error e) {
71                         debug(e.message); //TODO
72                 }
73         }
74         
75         /** Converting values to string */
76         public static string value_to_string(Value v, Type t) {
77                 switch(t.name()) {
78                 case "gint":
79                         return v.get_int().to_string();
80                 
81                 case "gdouble":
82                         return v.get_double().to_string();
83                 
84                 case "gboolean":
85                         return v.get_boolean().to_string();
86                 
87                 case "gchararray":
88                         return v.get_string();
89                 
90                 case "PinoEnumsStreamEnum":
91                         return v.get_enum().to_string();
92                 
93                 case "GeeArrayList": //strings only!
94                         ArrayList<string> obj = (ArrayList<string>) v.get_object();
95                         string result = "";
96                         
97                         foreach(string s in obj) {
98                                 if(s != "")
99                                         result += s + "\t==+-=";
100                         }
101                         
102                         return result;
103                 
104                 default:
105                         return "";
106                 }
107         }
108         
109         /** Restoring values from string */
110         public static Value? value_from_string(string data, Type t) {
111                 Value? v = Value(t);
112                 
113                 switch(t.name()) {
114                 case "gint":
115                         v.set_int(data.to_int());
116                         break;
117                 
118                 case "gdouble":
119                         v.set_double(data.to_double());
120                         break;
121                 
122                 case "gboolean":
123                         v.set_boolean(data.to_bool());
124                         break;
125                 
126                 case "gchararray":
127                         v.set_string(data);
128                         break;
129                 
130                 case "PinoEnumsStreamEnum":
131                         v.set_enum(data.to_int());
132                         break;
133                 
134                 case "GeeArrayList":
135                         ArrayList<string> string_list = new ArrayList<string>();
136                         string[] strings = data.split("\t==+-=");
137                         foreach(string s in strings) {
138                                 if(s != "")
139                                         string_list.add(s);
140                         }
141                         
142                         v.set_object(string_list);
143                         break;
144                 
145                 default:
146                         v = null;
147                         break;
148                 }
149                 
150                 return v;
151         }
152         
153         /** Save settings properties to xml */
154         public string to_xml() {
155                 string props = "";
156                 
157                 var obj = (ObjectClass) this.get_type().class_ref();
158                 
159                 foreach(var p in obj.list_properties()) {
160                         Value v = Value(p.value_type);
161                         this.get_property(p.get_name(), ref v);
162                         
163                         string s_val = value_to_string(v, p.value_type);
164                         if(s_val != "")
165                                 props += "\t<%s>%s</%s>\n".printf(p.get_name(), s_val, p.get_name());
166                 }
167                 
168                 string result = "\n<settings>\n%s</settings>".printf(props);
169                 
170                 return result;
171         }
172         
173         /** Restore settings from xml */
174         public void from_xml(string data) {
175                 Doc* xml_doc = Parser.parse_memory(data, (int) data.size());
176                 Xml.Node* root_node = xml_doc->get_root_element();
177                 
178                 if(root_node == null)
179                         return;
180                 
181                 HashMap<string, string> opt_map = new HashMap<string, string>();
182                 
183                 for(Xml.Node* iter = root_node->children; iter != null; iter = iter->next) {
184                         if(iter->type != ElementType.ELEMENT_NODE)
185                                 continue;
186                         
187                         debug(iter->name);
188                         opt_map.set(iter->name, iter->get_content());
189                 }
190                 
191                 var obj = (ObjectClass) this.get_type().class_ref();
192                 
193                 foreach(var p in obj.list_properties()) {
194                         string? pval = opt_map.get(p.get_name());
195                         
196                         if(pval == null)
197                                 continue;
198                         
199                         Value? val = Settings.value_from_string(pval, p.value_type);
200                         
201                         if(val == null) {
202                                 debug("this value type is not supported");
203                                 continue;
204                         }
205                         
206                         this.set_property(p.get_name(), val);
207                 }
208         }
209 }