2
* Copyright (C) 2011-2013 elementary Developers
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.
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.
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/>.
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>
22
* The original Contractor implementation in Python was created by:
23
* Allen Lowe <lallenlowe@gmail.com>
26
namespace Contractor {
28
This representation of a contract is returned to clients via D-bus API
30
public struct GenericContract {
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";
59
public FileInfo(File file) {
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);
73
this.id = get_contract_id (file);
75
init_from_keyfile (keyfile);
77
throw new IOError.NOT_FOUND ("something went wrong on this %s file",this.id);
80
warning ("%s", err.message);
87
private void init_from_keyfile (KeyFile keyfile) {
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);
97
if (textdomain != null)
98
name = GLib.dgettext (textdomain, name).dup ();
101
warning ("Couldn't read Name field %s", e.message);
106
exec = keyfile.get_string (GROUP, "Exec");
108
warning ("Couldn't read Exec field %s", e.message);
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);
121
if (textdomain != null)
122
description = GLib.dgettext (textdomain, description).dup ();
124
warning ("Couldn't read title field %s", e.message);
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);
136
mime_types = keyfile.get_string_list (GROUP, "MimeType");
139
warning ("Couldn't read MimeType field %s",e.message);
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);
153
warning ("Couldn't read Icon field %s", e.message);
158
if (keyfile.has_key (GROUP, "ExecString"))
159
exec_string = keyfile.get_string (GROUP, "ExecString");
161
warning ("Couldn't read ExecString field %s", e.message);
169
private string get_contract_id (File file) {
170
GLib.FileInfo q_info = new GLib.FileInfo ();
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");
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);
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 ())
198
return parent.get_basename () + "/" + get_parent_until (parent, until_dir);
204
public GenericContract to_generic_contract () {
205
return GenericContract () {
207
display_name = this.name,
208
description = this.description,
209
icon_path = this.icon_name