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