Imported Upstream version 0.2.1
[debian/pino.git] / src / prefs.vala
1 /* prefs.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using GLib;
23 using Xml;
24
25 public class Prefs : Object {
26         
27         private string prefFilePath;
28         
29         private int _updateInterval = 5; //minutes
30         public int updateInterval {
31                 get{ return _updateInterval; }
32                 set{ _updateInterval = value; }
33         }
34         
35         private int _numberStatuses = 20;
36         public int numberStatuses {
37                 get{ return _numberStatuses; }
38                 set{ _numberStatuses = value; }
39         }
40         
41         private string _urlShorten = "goo.gl";
42         public string urlShorten {
43                 get{ return _urlShorten; }
44                 set{ _urlShorten = value; }
45         }
46         
47         private bool _showTimelineNotify = true;
48         public bool showTimelineNotify {
49                 get{ return _showTimelineNotify; }
50                 set{ _showTimelineNotify = value; }
51         }
52         
53         private bool _showMentionsNotify = true;
54         public bool showMentionsNotify {
55                 get{ return _showMentionsNotify; }
56                 set{ _showMentionsNotify = value; }
57         }
58         
59         private bool _showDirectNotify = true;
60         public bool showDirectNotify {
61                 get{ return _showDirectNotify; }
62                 set{ _showDirectNotify = value; }
63         }
64         /*
65         private bool _showFullNotify = true;
66         public bool showFullNotify {
67                 get{ return _showFullNotify; }
68                 set{ _showFullNotify = value; }
69         }
70         */
71         private string _retweetStyle = "UNI";
72         public ReTweet.Style retweetStyle {
73                 get{
74                         switch(_retweetStyle) {
75                                 case "CLASSIC":
76                                         return ReTweet.Style.CLASSIC;
77                                 
78                                 case "UNI":
79                                         return ReTweet.Style.UNI;
80                                 
81                                 case "VIA":
82                                         return ReTweet.Style.VIA;
83                                 
84                                 default:
85                                         return ReTweet.Style.UNI;
86                         }
87                 }
88                 set{
89                         switch(value) {
90                                 case ReTweet.Style.CLASSIC:
91                                         _retweetStyle = "CLASSIC";
92                                         break;
93                                 
94                                 case ReTweet.Style.UNI:
95                                         _retweetStyle = "UNI";
96                                         break;
97                                 
98                                 case ReTweet.Style.VIA:
99                                         _retweetStyle = "VIA";
100                                         break;
101                                 
102                                 default:
103                                         _retweetStyle = "UNI";
104                                         break;
105                         }
106                 }
107         }
108         
109         public signal void roundedAvatarsChanged();
110         private bool _roundedAvatars = true;
111         public bool roundedAvatars {
112                 get{ return _roundedAvatars; }
113                 set{
114                         if(_roundedAvatars != value) {
115                                 _roundedAvatars = value;
116                                 roundedAvatarsChanged();
117                         }
118                 }
119         }
120         
121         public signal void opacityTweetsChanged();
122         private string _opacityTweets = "0.4";
123         public string opacityTweets {
124                 get{ return _opacityTweets; }
125                 set{
126                         if(_opacityTweets != value) {
127                                 _opacityTweets = value;
128                                 opacityTweetsChanged();
129                         }
130                 }
131         }
132         
133         public signal void rtlChanged();
134         private bool _rtlSupport = false;
135         public bool rtlSupport {
136                 get{ return _rtlSupport; }
137                 set{
138                         if(_rtlSupport != value) {
139                                 _rtlSupport = value;
140                                 rtlChanged();
141                         }
142                 }
143         }
144         
145         public signal void fullNamesChanged();
146         private bool _fullNames = false;
147         public bool fullNames {
148                 get{ return _fullNames; }
149                 set{
150                         if(_fullNames != value) {
151                                 _fullNames = value;
152                                 fullNamesChanged();
153                         }
154                 }
155         }
156         
157         public signal void fontChanged();
158         private int _deFontSize = 9;
159         public int deFontSize {
160                 get { return _deFontSize; }
161         }
162         private string _deFontName = "Dejavu Sans";
163         public string deFontName {
164                 get { return _deFontName; }
165         }
166         public string deFont {
167                 set{
168                         _deFontName = "";
169                         
170                         Regex bold_italic = new Regex("(.*)(Bold Italic)(.*)");
171                         Regex re_style = new Regex("([a-zA-Z0-9 _]+) (BoldItalic|Bold|Italic|Medium|Oblique|BoldOblique) ([0-9]+)");
172                         Regex re_normal = new Regex("([a-zA-Z0-9 _]+) ([0-9]+)");
173                         
174                         string my_val = bold_italic.replace(value, -1, 0, "\\1BoldItalic\\3");
175                         
176                         if(re_style.match(value)) {
177                                 _deFontName = re_style.replace(my_val, -1, 0, "\\1");
178                                 _deFontSize = re_style.replace(my_val, -1, 0, "\\3").to_int();
179                                 
180                         } else {
181                                 _deFontName = re_normal.replace(my_val, -1, 0, "\\1");
182                                 _deFontSize = re_normal.replace(my_val, -1, 0, "\\2").to_int();
183                         }
184                         
185                         fontChanged();
186                 }
187         }
188         
189         private string _freshColor = "rgba(179, 110, 117, 0.32)";
190         public string freshColor {
191                 get { return _freshColor; }
192                 set { _freshColor = value; }
193         }
194         
195         private int _width = 250;
196         public int width {
197                 get{ return _width; }
198                 set{ _width = value; }
199         }
200         
201         private int _height = 500;
202         public int height {
203                 get{ return _height; }
204                 set{ _height = value; }
205         }
206         
207         private int _left = -1;
208         public int left {
209                 get{ return _left; }
210                 set{ _left = value; }
211         }
212         
213         private int _top = -1;
214         public int top {
215                 get{ return _top; }
216                 set{ _top = value; }
217         }
218         
219         private bool _menuShow = true;
220         public bool menuShow {
221                 get{ return _menuShow; }
222                 set{ _menuShow = value; }
223         }
224         
225         private bool _toolbarShow = true;
226         public bool toolbarShow {
227                 get{ return _toolbarShow; }
228                 set{ _toolbarShow = value; }
229         }
230         
231         private bool _startMin = false;
232         public bool startMin {
233                 get{ return _startMin; }
234                 set{ _startMin = value; }
235         }
236         
237         public signal void showTrayChanged();
238         private bool _showTray = true;
239         public bool showTray {
240                 get{ return _showTray; }
241                 set{
242                         _showTray = value;
243                         showTrayChanged();
244                 }
245         }
246         
247         public enum LoadStatus { OK, EMPTY, ERROR }
248         public enum WriteStatus { OK, ERROR }
249         
250         public bool is_new = false;
251         
252         public Prefs() {
253                 var load_status = load();
254                 
255                 if(load_status == LoadStatus.EMPTY) {
256                         write();
257                 }
258         }
259         
260         public LoadStatus load() {
261                 string conf_dir = Environment.get_home_dir() + "/.config/";
262                 
263                 var dir = File.new_for_path(conf_dir);
264                 if(!dir.query_exists(null))
265                         dir.make_directory(null);
266                 
267                 string pino_dir = conf_dir + "/pino";
268                 
269                 dir = File.new_for_path(pino_dir);
270                 if(!dir.query_exists(null))
271                         dir.make_directory(null);
272                 
273                 dir = null;
274                 
275                 //checking for settings file and creating if necessary
276                 prefFilePath = pino_dir + "/settings.xml";
277                 var pref_file = File.new_for_path(prefFilePath);
278                 
279                 if(!pref_file.query_exists(null)) {
280                         //var pref_stream = pref_file.create(FileCreateFlags.NONE, null);
281                         is_new = true;
282                         return LoadStatus.EMPTY;
283                 }
284                 
285                 //reading content
286                 string content;
287                 {
288                         var stream = new DataInputStream(pref_file.read(null));
289                         content = stream.read_until("", null, null);
290                 }
291                 
292                 return parse(content);
293         }
294         
295         private LoadStatus parse(string content) {
296                 //parsing config
297                 Xml.Doc* xmlDoc = Parser.parse_memory(content, (int)content.size());
298                 Xml.Node* rootNode = xmlDoc->get_root_element();
299                 
300                 for(Xml.Node* iter = rootNode->children; iter != null; iter = iter->next) {
301                         if (iter->type != ElementType.ELEMENT_NODE)
302                                 continue;
303                         
304                         switch(iter->name) {
305                                 case "updateInterval":
306                                         _updateInterval = iter->get_content().to_int();
307                                         break;
308                                 
309                                 case "numberStatuses":
310                                         _numberStatuses = iter->get_content().to_int();
311                                         break;
312                                 
313                                 case "urlShorten":
314                                         _urlShorten = iter->get_content();
315                                         break;
316                                 
317                                 case "showTimelineNotify":
318                                         _showTimelineNotify = iter->get_content().to_bool();
319                                         break;
320                                 
321                                 case "showMentionsNotify":
322                                         _showMentionsNotify = iter->get_content().to_bool();
323                                         break;
324                                 
325                                 case "showDirectNotify":
326                                         _showDirectNotify = iter->get_content().to_bool();
327                                         break;
328                                 /*
329                                 case "showFullNotify":
330                                         _showFullNotify = iter->get_content().to_bool();
331                                         break;
332                                 */
333                                 case "retweetStyle":
334                                         _retweetStyle = iter->get_content();
335                                         break;
336                                 
337                                 case "roundedAvatars":
338                                         _roundedAvatars = iter->get_content().to_bool();
339                                         break;
340                                 
341                                 case "opacityTweets":
342                                         _opacityTweets = iter->get_content();
343                                         break;
344                                 
345                                 case "rtlSupport":
346                                         _rtlSupport = iter->get_content().to_bool();
347                                         break;
348                                 
349                                 case "fullNames":
350                                         _fullNames = iter->get_content().to_bool();
351                                         break;
352                                 
353                                 case "deFontName":
354                                         _deFontName = iter->get_content();
355                                         break;
356                                 
357                                 case "deFontSize":
358                                         _deFontSize = iter->get_content().to_int();
359                                         break;
360                                 
361                                 case "freshColor":
362                                         _freshColor = iter->get_content();
363                                         break;
364                                 
365                                 case "width":
366                                         _width = iter->get_content().to_int();
367                                         break;
368                                 
369                                 case "height":
370                                         _height = iter->get_content().to_int();
371                                         break;
372                                 
373                                 case "left":
374                                         _left = iter->get_content().to_int();
375                                         break;
376                                 
377                                 case "top":
378                                         _top = iter->get_content().to_int();
379                                         break;
380                                 
381                                 case "menuShow":
382                                         _menuShow = iter->get_content().to_bool();
383                                         break;
384                                 
385                                 case "toolbarShow":
386                                         _toolbarShow = iter->get_content().to_bool();
387                                         break;
388                                 
389                                 case "startMin":
390                                         _startMin = iter->get_content().to_bool();
391                                         break;
392                                 
393                                 case "showTray":
394                                         _showTray = iter->get_content().to_bool();
395                                         break;
396                         }
397                 }
398                 
399                 return LoadStatus.OK;
400         }
401         
402         public WriteStatus write() {
403                 //creating xml document
404                 Xml.Doc* xmldoc = new Xml.Doc("1.0");
405                 Xml.Ns* ns = new Xml.Ns(null, null, null);
406                 ns->type = Xml.ElementType.ELEMENT_NODE;
407                 Xml.Node* root = new Xml.Node(ns, "settings");
408                 xmldoc->set_root_element(root);
409         
410         //creating properties
411         root->add_content("\n");
412         root->new_text_child(ns, "updateInterval", _updateInterval.to_string());
413         root->add_content("\n");
414         root->new_text_child(ns, "numberStatuses", _numberStatuses.to_string());
415         root->add_content("\n");
416         root->new_text_child(ns, "urlShorten", _urlShorten);
417         root->add_content("\n");
418         root->new_text_child(ns, "showTimelineNotify", _showTimelineNotify.to_string());
419         root->add_content("\n");
420         root->new_text_child(ns, "showMentionsNotify", _showMentionsNotify.to_string());
421         root->add_content("\n");
422         root->new_text_child(ns, "showDirectNotify", _showDirectNotify.to_string());
423         root->add_content("\n");
424         /*root->new_text_child(ns, "showFullNotify", _showFullNotify.to_string());
425         root->add_content("\n");*/
426         root->new_text_child(ns, "retweetStyle", _retweetStyle);
427         root->add_content("\n");
428         root->new_text_child(ns, "roundedAvatars", _roundedAvatars.to_string());
429         root->add_content("\n");
430         root->new_text_child(ns, "opacityTweets", _opacityTweets);
431         root->add_content("\n");
432         root->new_text_child(ns, "rtlSupport", _rtlSupport.to_string());
433         root->add_content("\n");
434         root->new_text_child(ns, "fullNames", _fullNames.to_string());
435         root->add_content("\n");
436         root->new_text_child(ns, "deFontName", _deFontName);
437         root->add_content("\n");
438         root->new_text_child(ns, "deFontSize", _deFontSize.to_string());
439         root->add_content("\n");
440         root->new_text_child(ns, "freshColor", _freshColor);
441         root->add_content("\n");
442         root->new_text_child(ns, "width", _width.to_string());
443         root->add_content("\n");
444         root->new_text_child(ns, "height", _height.to_string());
445         root->add_content("\n");
446         root->new_text_child(ns, "left", _left.to_string());
447         root->add_content("\n");
448         root->new_text_child(ns, "top", _top.to_string());
449         root->add_content("\n");
450         root->new_text_child(ns, "menuShow", _menuShow.to_string());
451         root->add_content("\n");
452         root->new_text_child(ns, "toolbarShow", _toolbarShow.to_string());
453         root->add_content("\n");
454         root->new_text_child(ns, "startMin", _startMin.to_string());
455         root->add_content("\n");
456         root->new_text_child(ns, "showTray", _showTray.to_string());
457         root->add_content("\n");
458                 
459                 //write this document to the pref file
460                 var stream = FileStream.open(prefFilePath, "w");
461                 //how does this work...
462                 xmldoc->dump(stream);
463                 
464                 return WriteStatus.OK;
465         }
466 }