~dhananjaysathe/nuvola-player/TelepathyExt

« back to all changes in this revision

Viewing changes to src/fenryxo.vala

  • Committer: Jiří Janoušek
  • Date: 2011-08-23 23:07:29 UTC
  • Revision ID: janousek.jiri@gmail.com-20110823230729-0lfuuttyps70qd58
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
Fenryxo's library :-)
 
4
 
 
5
Copyright 2011 Jiří Janoušek <janousek.jiri@gmail.com>
 
6
 
 
7
* Based on code from Wingpanel <https://launchpad.net/wingpanel>,
 
8
        Copyright 2010-2011 Wingpanel developers
 
9
 
 
10
This program is free software: you can redistribute it and/or modify it 
 
11
under the terms of the GNU General Public License version 3, as published 
 
12
by the Free Software Foundation.
 
13
 
 
14
This program is distributed in the hope that it will be useful, but 
 
15
WITHOUT ANY WARRANTY; without even the implied warranties of 
 
16
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
 
17
PURPOSE.  See the GNU General Public License for more details.
 
18
 
 
19
You should have received a copy of the GNU General Public License along 
 
20
with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
*/
 
22
 
 
23
/* TODO:
 
24
 * create real shared library
 
25
 */
 
26
using Gee;
 
27
 
 
28
namespace Fenryxo{
 
29
/**
 
30
         * Class parsing key-value configuration file.
 
31
         * 
 
32
         * Class parser configuration files which contain one key-value pair per line.
 
33
         * Leading and traling white characters are removed.
 
34
         * Key and value are separated by "=" (value may contain "=" character).
 
35
         * Lines starting with a hash "#" are ignored.
 
36
         * {{{
 
37
         * foo = bar
 
38
         * # nice comment
 
39
         * apple = pear
 
40
         * # This is also valid:
 
41
         * comparation = <, =, >
 
42
         * }}}
 
43
         */
 
44
        public class ConfigFile: HashMap<string, string>{
 
45
                private File file;
 
46
                public bool readonly{get; set;}
 
47
                /**
 
48
                 * Creates new map representation of given file.
 
49
                 * 
 
50
                 * Creates new map representation of given file. Prints warning on parse errors.
 
51
                 * Note: key names are always lowercased.
 
52
                 * 
 
53
                 * @param file file to parse
 
54
                 */
 
55
                public ConfigFile(File file, bool readonly=false){
 
56
                        base();
 
57
                        this.file = file;
 
58
                        this.readonly = readonly;
 
59
                        try{
 
60
                                var dis = new DataInputStream(file.read());
 
61
                                string line;
 
62
                                int lineno = 0;
 
63
                                while ((line = dis.read_line (null)) != null){
 
64
                                        lineno++;
 
65
                                        string item = line.strip();
 
66
                                        // Ignore empty lines or comments
 
67
                                        if(item.length == 0 || item[0] == '#') continue;
 
68
                                        string[] parts = item.split("=", 2);
 
69
                                        if(parts.length != 2){
 
70
                                                // Implicit keys without value are not supported
 
71
                                                warning("ConfigFile: Parse error at %s:%i",
 
72
                                                        file.get_uri(), lineno);
 
73
                                                continue;
 
74
                                        }
 
75
                                        string key = parts[0].strip();
 
76
                                        if(key.length == 0){
 
77
                                                // Empty key
 
78
                                                warning("ConfigFile: Parse error at %s:%i",
 
79
                                                        file.get_uri(), lineno);
 
80
                                                continue;
 
81
                                        }
 
82
                                        this[key] = parts[1].strip();
 
83
                                }
 
84
                        }
 
85
                        catch(Error e){
 
86
                                debug("ConfigFile: Unable to read %s", file.get_uri());
 
87
                        }
 
88
                }
 
89
                /**
 
90
                 * Returns boolean value.
 
91
                 * 
 
92
                 * @param key option identifier
 
93
                 * @return false if value is empty or "false" (case insensitive), true otherwise
 
94
                 */
 
95
                public bool get_bool(string key){
 
96
                        string? val = this[key];
 
97
                        return  val != null && val != "" && val.down() != "false";
 
98
                }
 
99
                
 
100
                /**
 
101
                 * Saves configuration to file.
 
102
                 * 
 
103
                 * @throw Error on failure
 
104
                 */
 
105
                public void save() throws Error{
 
106
                        if(readonly) throw new IOError.READ_ONLY("Configuration file is opened read-only");
 
107
                        var builder = new StringBuilder();
 
108
                        foreach(var e in this.entries){
 
109
                                builder.append(e.key);
 
110
                                builder.append(" = ");
 
111
                                builder.append(e.value);
 
112
                                builder.append_c('\n');
 
113
                        }
 
114
                        string str = builder.str;
 
115
                        try{
 
116
                                file.get_parent().make_directory_with_parents();
 
117
                        }
 
118
                        catch(Error e){}
 
119
                        file.replace_contents(str, str.length, null, false, FileCreateFlags.NONE, null);
 
120
                }
 
121
        }
 
122
        
 
123
        
 
124
        /**
 
125
         * Search user data directory and system data directories for subdirectories.
 
126
         * 
 
127
         * @param name name of subdirectory
 
128
         * @return Existing subdirectories or empty array in no subdirectory has been found.
 
129
         */
 
130
        public static File[] get_data_dirs(string name){
 
131
                File[] dirs = {};
 
132
                File dir = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
 
133
                if(dir.query_file_type(0) == FileType.DIRECTORY) dirs += dir;
 
134
                foreach(string path in Environment.get_system_data_dirs()){
 
135
                        dir = File.new_for_path(path).get_child(name);
 
136
                        if(dir.query_file_type(0) == FileType.DIRECTORY) dirs += dir;
 
137
                }
 
138
                return dirs;
 
139
        }
 
140
        
 
141
        /**
 
142
         * Search user data directory and system data directories for subdirectory.
 
143
         * 
 
144
         * @param name name of subdirectory
 
145
         * @return First existing subdirectory or null in no subdirectory has been found.
 
146
         */
 
147
        public static File? get_data_dir(string name){
 
148
                File dir = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
 
149
                if(dir.query_file_type(0) == FileType.DIRECTORY) return dir;
 
150
                foreach(string path in Environment.get_system_data_dirs()){
 
151
                        dir = File.new_for_path(path).get_child(name);
 
152
                        if(dir.query_file_type(0) == FileType.DIRECTORY) return dir;
 
153
                }
 
154
                return null;
 
155
        }
 
156
        
 
157
        /**
 
158
         * Search user data directory and system data directories for file.
 
159
         * 
 
160
         * @param name name of the file
 
161
         * @return First existing file or null in no file has been found.
 
162
         * @todo unit testing
 
163
         */
 
164
        public static File? get_data_file(string name){
 
165
                File f = File.new_for_path(Environment.get_user_data_dir()).get_child(name);
 
166
                if(f.query_file_type(0) == FileType.REGULAR) return f;
 
167
                foreach(string path in Environment.get_system_data_dirs()){
 
168
                        f = File.new_for_path(path).get_child(name);
 
169
                        if(f.query_file_type(0) == FileType.REGULAR) return f;
 
170
                }
 
171
                return null;
 
172
        }
 
173
        
 
174
        public static File get_default_config_file(string dir, string file){
 
175
                return File.new_for_path(Environment.get_user_config_dir())
 
176
                        .get_child(dir).get_child(file);
 
177
        }
 
178
}