~elementary-pantheon/contractor/master

« back to all changes in this revision

Viewing changes to src/ContractorFileInfo.vala

  • Committer: Akshay Shekher
  • Date: 2013-04-10 15:52:07 UTC
  • Revision ID: git-v1:7582ecd897c5577debef3fadb2998089ef4cdeec
print warning if uri doesn't contain ://

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011-2013 elementary Developers
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation, either version 3 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *
 
17
 * Authors: ammonkey <am.monkeyd@gmail.com>,
 
18
 *          lampe2 <michael@lazarski.me>,
 
19
 *          Akshay Shekher <voldyman666@gmail.com>,
 
20
 *          Sergey "Shnatsel" Davidoff <sergey@elementaryos.org>
 
21
 *
 
22
 * The original Contractor implementation in Python was created by:
 
23
 *          Allen Lowe <lallenlowe@gmail.com>
 
24
 */
 
25
 
 
26
namespace Contractor {
 
27
    /*
 
28
        This representation of a contract is returned to clients via D-bus API
 
29
    */
 
30
    public struct GenericContract {
 
31
           string id;
 
32
           string display_name;
 
33
           string description;
 
34
           string icon_path;
 
35
    }
 
36
 
 
37
    public class FileInfo: Object {
 
38
        public string id { get; construct set; }
 
39
        public string name { get; construct set; }
 
40
        public string exec { get; set; }
 
41
        public string exec_string { get; set; }
 
42
        public string description { get; set; }
 
43
        public string[] mime_types = null;
 
44
        public string conditional_mime;
 
45
        public string icon_name { get; construct set; default = ""; }
 
46
        public bool take_multi_args { get; set; }
 
47
        public bool take_uri_args { get; set; }
 
48
        public string filename { get; construct set; }
 
49
        public bool is_valid { get; private set; default = true; }
 
50
        public bool is_conditional { get; private set; default = false; }
 
51
        /* used in the context of multiples arguments. If true, all arguments should respect the condition. If false, at least one argument should respect it. Default true */
 
52
        public bool strict_condition { get; private set; default = true; }
 
53
        private const string[] SUPPORTED_GETTEXT_DOMAINS_KEYS = { "Gettext-Domain", "X-Ubuntu-Gettext-Domain", "X-GNOME-Gettext-Domain" };
 
54
        private static const string GROUP = "Contractor Entry";
 
55
        /*
 
56
        *
 
57
        * status: TODO
 
58
        */
 
59
        public FileInfo(File file) {
 
60
            try {
 
61
                uint8[] contents;
 
62
                // check if there is a file
 
63
                bool success = file.load_contents (null, out contents, null);
 
64
                // parse content to string
 
65
                var contents_str = (string) contents;
 
66
                // get the length of the
 
67
                size_t len = contents_str.length;
 
68
                if (success && len > 0) {
 
69
                    // creating a new KeyFile
 
70
                    var keyfile = new KeyFile ();
 
71
                    keyfile.load_from_data (contents_str, len, 0);
 
72
                    // addin a ID
 
73
                    this.id = get_contract_id (file);
 
74
                    //initing the keyfile
 
75
                    init_from_keyfile (keyfile);
 
76
                }else {
 
77
                    throw new IOError.NOT_FOUND ("something went wrong on this %s file",this.id);
 
78
                }
 
79
            } catch (Error err) {
 
80
                 warning ("%s", err.message);
 
81
            }
 
82
        }
 
83
        /*
 
84
        *
 
85
        * status: TODO
 
86
        */
 
87
        private void init_from_keyfile (KeyFile keyfile) {
 
88
            try {
 
89
                name = keyfile.get_locale_string (GROUP, "Name");
 
90
                string? textdomain = null;
 
91
                foreach (var domain_key in SUPPORTED_GETTEXT_DOMAINS_KEYS) {
 
92
                    if (keyfile.has_key (GROUP, domain_key)) {
 
93
                        textdomain = keyfile.get_string (GROUP, domain_key);
 
94
                        break;
 
95
                    }
 
96
                }
 
97
                if (textdomain != null)
 
98
                    name = GLib.dgettext (textdomain, name).dup ();
 
99
 
 
100
            } catch (Error e) {
 
101
                warning ("Couldn't read Name field %s", e.message);
 
102
                is_valid = false;
 
103
            }
 
104
 
 
105
            try {
 
106
                exec = keyfile.get_string (GROUP, "Exec");
 
107
            } catch (Error e) {
 
108
                warning ("Couldn't read Exec field %s", e.message);
 
109
                is_valid = false;
 
110
            }
 
111
 
 
112
            try {
 
113
                description = keyfile.get_locale_string (GROUP, "Description");
 
114
                string? textdomain = null;
 
115
                foreach (var domain_key in SUPPORTED_GETTEXT_DOMAINS_KEYS) {
 
116
                    if (keyfile.has_key (GROUP, domain_key)) {
 
117
                        textdomain = keyfile.get_string (GROUP, domain_key);
 
118
                        break;
 
119
                    }
 
120
                }
 
121
                if (textdomain != null)
 
122
                    description = GLib.dgettext (textdomain, description).dup ();
 
123
            } catch (Error e) {
 
124
                warning ("Couldn't read title field %s", e.message);
 
125
                is_valid = false;
 
126
            }
 
127
 
 
128
            try {
 
129
                conditional_mime = keyfile.get_string (GROUP, "MimeType");
 
130
                if (conditional_mime.contains ("!")) {
 
131
                    is_conditional = true;
 
132
                    strict_condition = keyfile.get_boolean (GROUP, "StrictCondition");
 
133
                    if (conditional_mime.contains (";"))
 
134
                        warning ("%s: multi arguments in conditional mimetype are not currently supported: %s", name, conditional_mime);
 
135
                } else {
 
136
                    mime_types = keyfile.get_string_list (GROUP, "MimeType");
 
137
                }
 
138
            } catch (Error e) {
 
139
                warning ("Couldn't read MimeType field %s",e.message);
 
140
                is_valid = false;}
 
141
 
 
142
            try {
 
143
                if (keyfile.has_key (GROUP, "Icon")) {
 
144
                    icon_name = keyfile.get_locale_string (GROUP, "Icon");
 
145
                    if (!Path.is_absolute (icon_name) &&
 
146
                       (icon_name.has_suffix (".png") ||
 
147
                        icon_name.has_suffix (".svg") ||
 
148
                        icon_name.has_suffix (".xpm"))) {
 
149
                        icon_name = icon_name.substring (0, icon_name.length - 4);
 
150
                    }
 
151
                }
 
152
            } catch (Error e) {
 
153
                warning ("Couldn't read Icon field %s", e.message);
 
154
                is_valid = false;
 
155
            }
 
156
 
 
157
            try {
 
158
                if (keyfile.has_key (GROUP, "ExecString"))
 
159
                    exec_string = keyfile.get_string (GROUP, "ExecString");
 
160
            } catch (Error e) {
 
161
                warning ("Couldn't read ExecString field %s", e.message);
 
162
                is_valid = false;
 
163
            }
 
164
        }
 
165
        /*
 
166
        *
 
167
        * status: TODO
 
168
        */
 
169
        private string get_contract_id (File file) {
 
170
            GLib.FileInfo q_info = new GLib.FileInfo ();
 
171
            try {
 
172
                q_info = file.query_info ("*", FileQueryInfoFlags.NONE);
 
173
            } catch (Error e) { warning (e.message);}
 
174
            return get_parent_until (file, "contractor") + strip_file_extension (q_info.get_name (), "contract");
 
175
        }
 
176
        /*
 
177
        *
 
178
        * status: TODO
 
179
        */
 
180
        private string strip_file_extension (string filename, string extension) {
 
181
            //usage: strip_file_extension ("/path/to/file.extension", "extension")
 
182
            var index_of_last_dot = filename.last_index_of (".");
 
183
            if (filename.slice (index_of_last_dot, filename.length) == "." + extension) {
 
184
                return filename.slice (0, index_of_last_dot);
 
185
            } else {
 
186
                return filename;
 
187
            }
 
188
        }
 
189
        /*
 
190
        *
 
191
        * status: TODO
 
192
        */
 
193
        private string get_parent_until (File file, string until_dir) {
 
194
            File parent = file.get_parent ();
 
195
            if (parent.get_basename ().down () == until_dir.down ())
 
196
                return "";
 
197
            else
 
198
                return parent.get_basename () + "/" + get_parent_until (parent, until_dir);
 
199
        }
 
200
        /*
 
201
        *
 
202
        * status: TODO
 
203
        */
 
204
        public GenericContract to_generic_contract () {
 
205
            return GenericContract () {
 
206
                id = this.id,
 
207
                display_name = this.name,
 
208
                description = this.description,
 
209
                icon_path = this.icon_name
 
210
            };
 
211
        }
 
212
    }
 
213
}