1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright (C) 2011-2017 elementary Developers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Contractor {
/**
* a contract which defines an action available for certain files
*/
public class Contract : Object {
/**
* the contracts ID based on the file name, e.g. file-roller-compress
* (file-roller-compress.contract)
*/
public string id { get; private set; }
/**
* the name displayed in the GUI
*/
public string name { get; private set; }
public string icon { get; private set; default = ""; }
public string description { get; private set; default = ""; }
/**
* the maximal file size a file or list of files are allowed to have to
* be applicable for this contract
*/
public int64 max_file_size { get; private set; default = -1; }
private MimeTypeManager mimetype_manager;
/**
* the object used to get individual fields from the .contract file
*/
private ContractKeyFile keyfile;
/**
* the constructor used to create a Contract object containing a
* ContractFile object based on the passed File object
*
* @param file the file of which a ContractFile object should be created
*/
public Contract (File file) throws Error {
var contract_file = new ContractFile (file);
keyfile = new ContractKeyFile (contract_file);
id = contract_file.get_id ();
load_mandatory_fields ();
load_non_mandatory_fields ();
}
/**
* returns true if the MIME type is supported by this contract; false
* otherwise
*
* @param mime_type the MIME type of the file or list of files on which the contract should be applied
*
* @return true if the MIME type is supported by this contract; false otherwise
*/
public bool supports_mime_type (string mime_type) {
return mimetype_manager.is_type_supported (mime_type);
}
/**
* returns true if the file size is supported by this contract; false
* otherwise
*
* @param file_size the file size of the file or list of files on which the contract should be applied
*
* @return true if the file size is supported by this contract; false otherwise
*/
public bool supports_file_size (int64 file_size) {
return file_size == -1 || file_size <= max_file_size;
}
public void launch_uris (string[] uris) throws Error {
var uri_list = String.array_to_list (uris);
keyfile.get_app_info ().launch_uris (uri_list, null);
}
/**
* creates and returns a new GenericContract object and fills it with
* data from this Contract object (id, name, description, icon)
*/
public GenericContract get_generic () {
return GenericContract () {
id = id,
name = name,
description = description,
icon = icon
};
}
/**
* loads mandatory fields from the key file
*/
private void load_mandatory_fields () throws Error {
name = keyfile.get_name ();
string[] mimetypes = keyfile.get_mimetypes ();
mimetype_manager = new MimeTypeManager (mimetypes);
}
/**
* loads non-mandatory fields from the key file
*/
private void load_non_mandatory_fields () {
try {
description = keyfile.get_description ();
} catch (Error err) {
warning ("Contract '%s' does not provide a description (%s)", id, err.message);
}
try {
icon = keyfile.get_icon ();
} catch (Error err) {
warning ("Contract '%s' does not provide an icon (%s)", id, err.message);
}
try {
max_file_size = keyfile.get_max_file_size ();
} catch (Error err) {
debug ("Contract '%s' does not provide a max file size (%s)", id, err.message);
}
}
}
}
|