Merge lp:~florian-angermeier/contractor/filter-functions-based-on-file-size into lp:contractor/0.3
- filter-functions-based-on-file-size
- Merge into trunk
Status: | Merged | ||||
---|---|---|---|---|---|
Approved by: | xapantu | ||||
Approved revision: | 147 | ||||
Merged at revision: | 137 | ||||
Proposed branch: | lp:~florian-angermeier/contractor/filter-functions-based-on-file-size | ||||
Merge into: | lp:contractor/0.3 | ||||
Diff against target: |
710 lines (+326/-22) 13 files modified
src/Contract.vala (+60/-0) src/ContractDirectory.vala (+2/-1) src/ContractFile.vala (+42/-1) src/ContractKeyFile.vala (+44/-3) src/ContractMatcher.vala (+59/-4) src/ContractSorter.vala (+4/-2) src/ContractSource.vala (+4/-2) src/DBusService.vala (+95/-1) src/FileEnumerator.vala (+2/-1) src/FileService.vala (+4/-2) src/MimeTypeManager.vala (+4/-2) src/String.vala (+4/-2) src/Translations.vala (+2/-1) |
||||
To merge this branch: | bzr merge lp:~florian-angermeier/contractor/filter-functions-based-on-file-size | ||||
Related bugs: |
|
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
xapantu (community) | Approve | ||
Review via email: mp+271211@code.launchpad.net |
This proposal supersedes a proposal from 2015-09-13.
Commit message
Implement filter functions based on file size:
* Get the max file size (int64, size in bytes) from a .contract file (optional key MaxFileSize)
* Add methods to the D-Bus service:
- get_contracts_
- get_contracts_
- get_contracts_
Add documentation
Description of the change
Implement filter functions based on file size:
* Get the max file size (int64, size in bytes) from a .contract file (optional key MaxFileSize)
* Add methods to the D-Bus service:
- get_contracts_
- get_contracts_
- get_contracts_
Add documentation
xapantu (xapantu) wrote : Posted in a previous version of this proposal | # |
Florian Angermeier (florian-angermeier) wrote : Posted in a previous version of this proposal | # |
> Nice work, thanks :)
>
> See below a few comments about code style and some other things.
>
> We also need documentations for the public methods to get this merged, which
> will be used to generate the api docs (see other files (e.g. the widgets are
> well documented) if you are looking for examples).
=== modified file 'src/Contract.vala'
--- src/Contract.vala 2013-05-20 04:55:11 +0000
+++ src/Contract.vala 2015-09-13 00:54:10 +0000
@@ -72,6 +77,12 @@
} catch (Error err) {
}
+
+ try {
+ max_file_size = keyfile.
+ } catch (Error err) {
+ warning ("Contract '%s' does not provide a max file size (%s)", id, err.message);
Should I change the warning outputs of all optional contract fields to debug outputs?
xapantu (xapantu) wrote : Posted in a previous version of this proposal | # |
Well, for now, we can let them here. For instance, for the icon one, while it is strictly speaking optional, we do want to force developers to put an icon. Whereas for the max file size, it does not make sense for a lot of contracts (print for instance), so it is really an optional field (and not an optional-
kay van der Zander (kay20) wrote : | # |
Hey try to keep the unmerged revisions to one. not 11 ;)
use the uncommit command. http://
xapantu (xapantu) wrote : | # |
Thanks for everything, including the documentation. It may need some rephrasing/
Preview Diff
1 | === modified file 'src/Contract.vala' |
2 | --- src/Contract.vala 2013-05-20 04:55:11 +0000 |
3 | +++ src/Contract.vala 2015-09-15 23:13:26 +0000 |
4 | @@ -16,15 +16,39 @@ |
5 | */ |
6 | |
7 | namespace Contractor { |
8 | + /** |
9 | + * a contract which defines an action available for certain files |
10 | + */ |
11 | public class Contract : Object { |
12 | + /** |
13 | + * the contracts ID based on the file name, e.g. file-roller-compress |
14 | + * (file-roller-compress.contract) |
15 | + */ |
16 | public string id { get; private set; } |
17 | + /** |
18 | + * the name displayed in the GUI |
19 | + */ |
20 | public string name { get; private set; } |
21 | public string icon { get; private set; default = ""; } |
22 | public string description { get; private set; default = ""; } |
23 | + /** |
24 | + * the maximal file size a file or list of files are allowed to have to |
25 | + * be applicable for this contract |
26 | + */ |
27 | + public int64 max_file_size { get; private set; default = -1; } |
28 | |
29 | private MimeTypeManager mimetype_manager; |
30 | + /** |
31 | + * the object used to get individual fields from the .contract file |
32 | + */ |
33 | private ContractKeyFile keyfile; |
34 | |
35 | + /** |
36 | + * the constructor used to create a Contract object containing a |
37 | + * ContractFile object based on the passed File object |
38 | + * |
39 | + * @param file the file of which a ContractFile object should be created |
40 | + */ |
41 | public Contract (File file) throws Error { |
42 | var contract_file = new ContractFile (file); |
43 | keyfile = new ContractKeyFile (contract_file); |
44 | @@ -35,15 +59,39 @@ |
45 | load_non_mandatory_fields (); |
46 | } |
47 | |
48 | + /** |
49 | + * returns true if the MIME type is supported by this contract; false |
50 | + * otherwise |
51 | + * |
52 | + * @param mime_type the MIME type of the file or list of files on which the contract should be applied |
53 | + * |
54 | + * @return true if the MIME type is supported by this contract; false otherwise |
55 | + */ |
56 | public bool supports_mime_type (string mime_type) { |
57 | return mimetype_manager.is_type_supported (mime_type); |
58 | } |
59 | |
60 | + /** |
61 | + * returns true if the file size is supported by this contract; false |
62 | + * otherwise |
63 | + * |
64 | + * @param file_size the file size of the file or list of files on which the contract should be applied |
65 | + * |
66 | + * @return true if the file size is supported by this contract; false otherwise |
67 | + */ |
68 | + public bool supports_file_size (int64 file_size) { |
69 | + return file_size == -1 || file_size <= max_file_size; |
70 | + } |
71 | + |
72 | public void launch_uris (string[] uris) throws Error { |
73 | var uri_list = String.array_to_list (uris); |
74 | keyfile.get_app_info ().launch_uris (uri_list, null); |
75 | } |
76 | |
77 | + /** |
78 | + * creates and returns a new GenericContract object and fills it with |
79 | + * data from this Contract object (id, name, description, icon) |
80 | + */ |
81 | public GenericContract get_generic () { |
82 | return GenericContract () { |
83 | id = id, |
84 | @@ -53,6 +101,9 @@ |
85 | }; |
86 | } |
87 | |
88 | + /** |
89 | + * loads mandatory fields from the key file |
90 | + */ |
91 | private void load_mandatory_fields () throws Error { |
92 | name = keyfile.get_name (); |
93 | |
94 | @@ -60,6 +111,9 @@ |
95 | mimetype_manager = new MimeTypeManager (mimetypes); |
96 | } |
97 | |
98 | + /** |
99 | + * loads non-mandatory fields from the key file |
100 | + */ |
101 | private void load_non_mandatory_fields () { |
102 | try { |
103 | description = keyfile.get_description (); |
104 | @@ -72,6 +126,12 @@ |
105 | } catch (Error err) { |
106 | warning ("Contract '%s' does not provide an icon (%s)", id, err.message); |
107 | } |
108 | + |
109 | + try { |
110 | + max_file_size = keyfile.get_max_file_size (); |
111 | + } catch (Error err) { |
112 | + debug ("Contract '%s' does not provide a max file size (%s)", id, err.message); |
113 | + } |
114 | } |
115 | } |
116 | } |
117 | |
118 | === modified file 'src/ContractDirectory.vala' |
119 | --- src/ContractDirectory.vala 2013-05-11 03:05:47 +0000 |
120 | +++ src/ContractDirectory.vala 2015-09-15 23:13:26 +0000 |
121 | @@ -50,8 +50,9 @@ |
122 | } |
123 | |
124 | private async void on_change_event (File file, File? other_file, FileMonitorEvent event) { |
125 | - if (update_pending) |
126 | + if (update_pending) { |
127 | return; |
128 | + } |
129 | |
130 | update_pending = true; |
131 | |
132 | |
133 | === modified file 'src/ContractFile.vala' |
134 | --- src/ContractFile.vala 2013-04-30 16:32:09 +0000 |
135 | +++ src/ContractFile.vala 2015-09-15 23:13:26 +0000 |
136 | @@ -15,32 +15,73 @@ |
137 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
138 | */ |
139 | |
140 | +/** |
141 | + * used to access the Contracts File object and read its content |
142 | + */ |
143 | public class Contractor.ContractFile : Object { |
144 | + /** |
145 | + * contract files filename extension |
146 | + */ |
147 | private const string EXTENSION = ".contract"; |
148 | |
149 | + /** |
150 | + * the File object used to access its content |
151 | + */ |
152 | private File file; |
153 | |
154 | + /** |
155 | + * the constructor to create ContractFile object which contains the passed |
156 | + * File object |
157 | + * |
158 | + * @param file the file to contain |
159 | + */ |
160 | public ContractFile (File file) { |
161 | this.file = file; |
162 | } |
163 | |
164 | + /** |
165 | + * get the contract ID from the filename, e.g. file-roller-compress |
166 | + * (file-roller-compress.contract) |
167 | + * |
168 | + * @return the contracts ID, e.g. file-roller-compress |
169 | + */ |
170 | public string get_id () { |
171 | return remove_extension (file.get_basename ()); |
172 | } |
173 | |
174 | + /** |
175 | + * loads and returns the internally stored files content |
176 | + * |
177 | + * @return the files content as string |
178 | + */ |
179 | public string get_contents () throws Error { |
180 | uint8[] file_data; |
181 | |
182 | - if (file.load_contents (null, out file_data, null)) |
183 | + if (file.load_contents (null, out file_data, null)) { |
184 | return (string) file_data; |
185 | + } |
186 | |
187 | return ""; |
188 | } |
189 | |
190 | + /** |
191 | + * checks if the filename extension is '.contract' |
192 | + * |
193 | + * @param filename the full filename incl. the filename extension |
194 | + * |
195 | + * @return true if the filename extension is '.contract'; false otherwise |
196 | + */ |
197 | public static bool is_valid_filename (string filename) { |
198 | return filename[- EXTENSION.length : filename.length] == EXTENSION; |
199 | } |
200 | |
201 | + /** |
202 | + * removes the filename extension and returns the result |
203 | + * |
204 | + * @param file_name the filename incl. the filename extesnion |
205 | + * |
206 | + * @return the filename without the filename extension |
207 | + */ |
208 | private static string remove_extension (string file_name) { |
209 | return file_name[0 : - EXTENSION.length]; |
210 | } |
211 | |
212 | === modified file 'src/ContractKeyFile.vala' |
213 | --- src/ContractKeyFile.vala 2013-05-19 20:19:40 +0000 |
214 | +++ src/ContractKeyFile.vala 2015-09-15 23:13:26 +0000 |
215 | @@ -23,6 +23,7 @@ |
216 | private const string DESCRIPTION_KEY = "Description"; |
217 | private const string ICON_KEY = KeyFileDesktop.KEY_ICON; |
218 | private const string MIMETYPE_KEY = KeyFileDesktop.KEY_MIME_TYPE; |
219 | + private const string MAX_FILE_SIZE_KEY = "MaxFileSize"; |
220 | private const string EXEC_KEY = KeyFileDesktop.KEY_EXEC; |
221 | private const string TRY_EXEC_KEY = KeyFileDesktop.KEY_TRY_EXEC; |
222 | |
223 | @@ -35,6 +36,13 @@ |
224 | private string text_domain; |
225 | private KeyFile keyfile; |
226 | |
227 | + /** |
228 | + * the constructor to create a ContractKeyFile object which loads the |
229 | + * content of the passed ContractFile object and sets up an internally |
230 | + * stored KeyFile object to access individual contract fields |
231 | + * |
232 | + * @param contract_file ContractFile object from which the content should be loaded |
233 | + */ |
234 | public ContractKeyFile (ContractFile contract_file) throws Error { |
235 | string contract_file_contents = contract_file.get_contents (); |
236 | string contents = preprocess_contents (contract_file_contents); |
237 | @@ -57,28 +65,59 @@ |
238 | public AppInfo get_app_info () throws Error { |
239 | var app_info = new DesktopAppInfo.from_keyfile (keyfile); |
240 | |
241 | - if (app_info == null) |
242 | + if (app_info == null) { |
243 | throw new FileError.NOENT ("%s's file is probably missing.", TRY_EXEC_KEY); |
244 | + } |
245 | |
246 | return app_info; |
247 | } |
248 | |
249 | + /** |
250 | + * gets the contracts name from the key file |
251 | + * |
252 | + * @return the contracts name |
253 | + */ |
254 | public string get_name () throws Error { |
255 | return get_locale_string (NAME_KEY); |
256 | } |
257 | |
258 | + /** |
259 | + * gets the contracts description from the key file |
260 | + * |
261 | + * @return the contracts description |
262 | + */ |
263 | public string get_description () throws Error { |
264 | return get_locale_string (DESCRIPTION_KEY); |
265 | } |
266 | |
267 | + /** |
268 | + * gets the contracts icon from the key file |
269 | + * |
270 | + * @return the contracts icon, e.g. add-files-to-archive |
271 | + */ |
272 | public string get_icon () throws Error { |
273 | return keyfile.get_string (DESKTOP_GROUP, ICON_KEY); |
274 | } |
275 | |
276 | + /** |
277 | + * gets the contracts supported MIME types from the key file |
278 | + * |
279 | + * @return an array of MIME type strings, e.g. text, image |
280 | + */ |
281 | public string[] get_mimetypes () throws Error { |
282 | return keyfile.get_string_list (DESKTOP_GROUP, MIMETYPE_KEY); |
283 | } |
284 | |
285 | + /** |
286 | + * gets the contracts supported maximal file size from the key file |
287 | + * the return value can be used directly in GLib |
288 | + * |
289 | + * @return the maximal file size in bytes as int64 |
290 | + */ |
291 | + public int64 get_max_file_size () throws Error { |
292 | + return keyfile.get_int64 (DESKTOP_GROUP, MAX_FILE_SIZE_KEY); |
293 | + } |
294 | + |
295 | private void verify_exec () throws Error { |
296 | string exec = keyfile.get_string (DESKTOP_GROUP, EXEC_KEY); |
297 | verify_string (exec, EXEC_KEY); |
298 | @@ -86,8 +125,9 @@ |
299 | |
300 | private string get_text_domain () throws Error { |
301 | foreach (var domain_key in SUPPORTED_GETTEXT_DOMAIN_KEYS) { |
302 | - if (keyfile.has_key (DESKTOP_GROUP, domain_key)) |
303 | + if (keyfile.has_key (DESKTOP_GROUP, domain_key)) { |
304 | return keyfile.get_string (DESKTOP_GROUP, domain_key); |
305 | + } |
306 | } |
307 | |
308 | return ""; |
309 | @@ -100,8 +140,9 @@ |
310 | } |
311 | |
312 | private static void verify_string (string? str, string key) throws Error { |
313 | - if (String.is_empty (str)) |
314 | + if (String.is_empty (str)) { |
315 | throw new KeyFileError.INVALID_VALUE ("%s key is empty.", key); |
316 | + } |
317 | } |
318 | |
319 | private static string preprocess_contents (string contents) { |
320 | |
321 | === modified file 'src/ContractMatcher.vala' |
322 | --- src/ContractMatcher.vala 2013-10-03 08:37:57 +0000 |
323 | +++ src/ContractMatcher.vala 2015-09-15 23:13:26 +0000 |
324 | @@ -16,14 +16,23 @@ |
325 | */ |
326 | |
327 | namespace Contractor.ContractMatcher { |
328 | + /** |
329 | + * get contracts which support the passed MIME types |
330 | + * |
331 | + * @param mime_types the MIME types which have to be supported by the returned contracts |
332 | + * @param contracts_to_filter a list of contracts to filter |
333 | + * |
334 | + * @return a Collection of Contract objects which support the file size |
335 | + */ |
336 | public Gee.Collection<Contract> get_contracts_for_types (string[] mime_types, |
337 | Gee.Collection<Contract> contracts_to_filter) throws ContractorError |
338 | { |
339 | var valid_contracts = new Gee.LinkedList<Contract> (); |
340 | var valid_mime_types = String.clean_array (mime_types); |
341 | |
342 | - if (valid_mime_types.length == 0) |
343 | + if (valid_mime_types.length == 0) { |
344 | throw new ContractorError.NO_MIMETYPES_GIVEN ("No mimetypes were provided."); |
345 | + } |
346 | |
347 | foreach (var contract in contracts_to_filter) { |
348 | // Check if the contract supports ALL the types listed in mime_types |
349 | @@ -36,9 +45,55 @@ |
350 | } |
351 | } |
352 | |
353 | - if (all_types_supported) |
354 | - valid_contracts.add (contract); |
355 | - } |
356 | + if (all_types_supported) { |
357 | + valid_contracts.add (contract); |
358 | + } |
359 | + } |
360 | + |
361 | + return valid_contracts; |
362 | + } |
363 | + |
364 | + /** |
365 | + * get contracts which support the passed file size |
366 | + * |
367 | + * @param file_size the file size which has to be supported by the returned contracts |
368 | + * @param contracts_to_filter a list of contracts to filter |
369 | + * |
370 | + * @return a Collection of Contract objects which support the file size |
371 | + */ |
372 | + public Gee.Collection<Contract> get_contracts_for_file_size (int64 file_size, |
373 | + Gee.Collection<Contract> contracts_to_filter) throws ContractorError |
374 | + { |
375 | + var valid_contracts = new Gee.LinkedList<Contract> (); |
376 | + |
377 | + foreach (var contract in contracts_to_filter) { |
378 | + bool file_size_supported = true; |
379 | + |
380 | + if (!contract.supports_file_size (file_size)) { |
381 | + file_size_supported = false; |
382 | + } |
383 | + |
384 | + if (file_size_supported) |
385 | + valid_contracts.add (contract); |
386 | + } |
387 | + |
388 | + return valid_contracts; |
389 | + } |
390 | + |
391 | + /** |
392 | + * get contracts which support the passed MIME types and file size |
393 | + * |
394 | + * @param mime_types the MIME types which have to be supported by the returned contracts |
395 | + * @param file_size the file size which has to be supported by the returned contracts |
396 | + * @param contracts_to_filter a list of contracts to filter |
397 | + * |
398 | + * @return a Collection of Contract objects which support the MIME types and the file size |
399 | + */ |
400 | + public Gee.Collection<Contract> get_contracts_for_types_and_file_size (string[] mime_types, |
401 | + int64 file_size, Gee.Collection<Contract> contracts_to_filter) throws ContractorError |
402 | + { |
403 | + var contracts_for_types = get_contracts_for_types (mime_types, contracts_to_filter); |
404 | + var valid_contracts = get_contracts_for_file_size (file_size, contracts_for_types); |
405 | |
406 | return valid_contracts; |
407 | } |
408 | |
409 | === modified file 'src/ContractSorter.vala' |
410 | --- src/ContractSorter.vala 2013-05-25 08:08:58 +0000 |
411 | +++ src/ContractSorter.vala 2015-09-15 23:13:26 +0000 |
412 | @@ -17,11 +17,13 @@ |
413 | |
414 | namespace Contractor.ContractSorter { |
415 | public int compare_func (Contract? a, Contract? b) { |
416 | - if (a == null) |
417 | + if (a == null) { |
418 | return (b == null) ? 0 : -1; |
419 | + } |
420 | |
421 | - if (b == null) |
422 | + if (b == null) { |
423 | return 1; |
424 | + } |
425 | |
426 | return strcmp (a.name.collate_key (), b.name.collate_key ()); |
427 | } |
428 | |
429 | === modified file 'src/ContractSource.vala' |
430 | --- src/ContractSource.vala 2013-05-20 04:55:11 +0000 |
431 | +++ src/ContractSource.vala 2015-09-15 23:13:26 +0000 |
432 | @@ -39,8 +39,9 @@ |
433 | public Contract lookup_by_id (string contract_id) throws Error { |
434 | var contract = contracts.get (contract_id); |
435 | |
436 | - if (contract == null) |
437 | + if (contract == null) { |
438 | throw new IOError.NOT_FOUND ("Requested invalid contract: %s", contract_id); |
439 | + } |
440 | |
441 | return contract; |
442 | } |
443 | @@ -50,8 +51,9 @@ |
444 | |
445 | var contract_files_to_load = file_service.load_contract_files (); |
446 | |
447 | - foreach (var contract_file in contract_files_to_load) |
448 | + foreach (var contract_file in contract_files_to_load) { |
449 | load_contract (contract_file); |
450 | + } |
451 | |
452 | changed (); |
453 | } |
454 | |
455 | === modified file 'src/DBusService.vala' |
456 | --- src/DBusService.vala 2013-10-03 08:37:57 +0000 |
457 | +++ src/DBusService.vala 2015-09-15 23:13:26 +0000 |
458 | @@ -24,32 +24,113 @@ |
459 | |
460 | namespace Contractor { |
461 | [DBus (name = "org.elementary.ContractorError")] |
462 | + /** |
463 | + * Errors specific to the Contractor D-Bus service. |
464 | + */ |
465 | public errordomain ContractorError { |
466 | + /** |
467 | + * Error if no MIME type was passed where mandatory. |
468 | + */ |
469 | NO_MIMETYPES_GIVEN |
470 | } |
471 | |
472 | + /** |
473 | + * A D-Bus service handling requests for Contracts defined by .contract |
474 | + * files. |
475 | + */ |
476 | [DBus (name = "org.elementary.Contractor")] |
477 | public class DBusService : Object { |
478 | + /** |
479 | + * Signal which gets sent when Contracts change. |
480 | + */ |
481 | public signal void contracts_changed (); |
482 | |
483 | + /** |
484 | + * Source from where .Contracts are loaded. |
485 | + */ |
486 | private ContractSource contract_source; |
487 | |
488 | + /** |
489 | + * Constructor to create a DBusService object and setup a |
490 | + * ContractSource. |
491 | + */ |
492 | public DBusService () { |
493 | contract_source = new ContractSource (); |
494 | contract_source.changed.connect (() => contracts_changed ()); |
495 | } |
496 | |
497 | + /** |
498 | + * This method gets an array of GenericContracts filtered by a |
499 | + * MIME type. |
500 | + * |
501 | + * @param mime_type a MIME type string, e.g. text, image |
502 | + * |
503 | + * @return an array of GenericContracts |
504 | + */ |
505 | public GenericContract[] get_contracts_by_mime (string mime_type) throws Error { |
506 | string[] mime_types = { mime_type }; |
507 | return get_contracts_by_mimelist (mime_types); |
508 | } |
509 | |
510 | + /** |
511 | + * This method gets an array of GenericContracts filtered by an array of |
512 | + * MIME types. |
513 | + * |
514 | + * @param mime_types an array of MIME type strings, e.g. text, image |
515 | + * |
516 | + * @return an array of GenericContracts |
517 | + */ |
518 | public GenericContract[] get_contracts_by_mimelist (string[] mime_types) throws Error { |
519 | var all_contracts = contract_source.get_contracts (); |
520 | var contracts = ContractMatcher.get_contracts_for_types (mime_types, all_contracts); |
521 | return convert_to_generic_contracts (contracts); |
522 | } |
523 | |
524 | + /** |
525 | + * This method gets an array of GenericContracts filtered by the file |
526 | + * size in bytes. |
527 | + * |
528 | + * @param file_size the file size in bytes, e.g. from FileInfo.get_size () |
529 | + * |
530 | + * @return an array of GenericContracts |
531 | + */ |
532 | + public GenericContract[] get_contracts_by_file_size (int64 file_size) throws Error { |
533 | + var all_contracts = contract_source.get_contracts (); |
534 | + var contracts = ContractMatcher.get_contracts_for_file_size (file_size, all_contracts); |
535 | + return convert_to_generic_contracts (contracts); |
536 | + } |
537 | + |
538 | + /** |
539 | + * This method gets an array of GenericContracts filtered by a MIME type |
540 | + * and the file size in bytes. |
541 | + * |
542 | + * @param mime_type a MIME type string, e.g. text, image |
543 | + * @param file_size the file size in bytes, e.g. from FileInfo.get_size () |
544 | + * |
545 | + * @return an array of GenericContracts |
546 | + */ |
547 | + public GenericContract[] get_contracts_by_mime_and_file_size (string mime_type, int64 file_size) throws Error { |
548 | + string[] mime_types = { mime_type }; |
549 | + return get_contracts_by_mimelist_and_file_size (mime_types, file_size); |
550 | + } |
551 | + |
552 | + /** |
553 | + * This method gets an array of GenericContracts filtered by an array of |
554 | + * MIME types and the file size in bytes. |
555 | + * |
556 | + * The file size should probably be the sum of all files file size. |
557 | + * |
558 | + * @param mime_types an array of MIME type strings, e.g. text, image |
559 | + * @param file_size the file size in bytes, e.g. from FileInfo.get_size () |
560 | + * |
561 | + * @return an array of GenericContracts |
562 | + */ |
563 | + public GenericContract[] get_contracts_by_mimelist_and_file_size (string[] mime_types, int64 file_size) throws Error { |
564 | + var all_contracts = contract_source.get_contracts (); |
565 | + var contracts = ContractMatcher.get_contracts_for_types_and_file_size (mime_types, file_size, all_contracts); |
566 | + return convert_to_generic_contracts (contracts); |
567 | + } |
568 | + |
569 | public void execute_with_uri (string id, string uri) throws Error { |
570 | string[] uris = { uri }; |
571 | execute_with_uri_list (id, uris); |
572 | @@ -60,16 +141,29 @@ |
573 | contract.launch_uris (uris); |
574 | } |
575 | |
576 | + /** |
577 | + * This method gets an array of GenericContracts containing all |
578 | + * contracts that exist. |
579 | + * |
580 | + * @return an array of GenericContracts containing all contracts |
581 | + */ |
582 | public GenericContract[] list_all_contracts () { |
583 | var contracts = contract_source.get_contracts (); |
584 | return convert_to_generic_contracts (contracts); |
585 | } |
586 | |
587 | + /** |
588 | + * Converts a Collection of Contracts into an array of |
589 | + * GenericContracts. |
590 | + * |
591 | + * @return an array of GenericContracts |
592 | + */ |
593 | private static GenericContract[] convert_to_generic_contracts (Gee.Collection<Contract> contracts) { |
594 | var generic_contracts = new GenericContract[0]; |
595 | |
596 | - foreach (var contract in contracts) |
597 | + foreach (var contract in contracts) { |
598 | generic_contracts += contract.get_generic (); |
599 | + } |
600 | |
601 | return generic_contracts; |
602 | } |
603 | |
604 | === modified file 'src/FileEnumerator.vala' |
605 | --- src/FileEnumerator.vala 2013-06-20 21:10:16 +0000 |
606 | +++ src/FileEnumerator.vala 2015-09-15 23:13:26 +0000 |
607 | @@ -49,8 +49,9 @@ |
608 | var child = directory.get_child (name); |
609 | |
610 | if (file_type == FileType.REGULAR) { |
611 | - if (ContractFile.is_valid_filename (name)) |
612 | + if (ContractFile.is_valid_filename (name)) { |
613 | files.add (child); |
614 | + } |
615 | } else { |
616 | warning ("'%s' is not a regular file. Skipping it...", child.get_path ()); |
617 | } |
618 | |
619 | === modified file 'src/FileService.vala' |
620 | --- src/FileService.vala 2013-05-11 03:05:47 +0000 |
621 | +++ src/FileService.vala 2015-09-15 23:13:26 +0000 |
622 | @@ -43,8 +43,9 @@ |
623 | public Gee.List<File> load_contract_files () { |
624 | var contract_files = new Gee.LinkedList<File> (); |
625 | |
626 | - foreach (var directory in directories) |
627 | + foreach (var directory in directories) { |
628 | contract_files.add_all (directory.lookup_contract_files ()); |
629 | + } |
630 | |
631 | return contract_files; |
632 | } |
633 | @@ -55,8 +56,9 @@ |
634 | // The user's data dir takes priority over system-wide data directories |
635 | data_dir_paths += Environment.get_user_data_dir (); |
636 | |
637 | - foreach (string data_dir in Environment.get_system_data_dirs ()) |
638 | + foreach (string data_dir in Environment.get_system_data_dirs ()) { |
639 | data_dir_paths += data_dir; |
640 | + } |
641 | |
642 | foreach (var path in data_dir_paths) { |
643 | var directory = File.new_for_path (path).get_child (CONTRACT_DATA_DIR_NAME); |
644 | |
645 | === modified file 'src/MimeTypeManager.vala' |
646 | --- src/MimeTypeManager.vala 2013-06-12 21:50:38 +0000 |
647 | +++ src/MimeTypeManager.vala 2015-09-15 23:13:26 +0000 |
648 | @@ -27,8 +27,9 @@ |
649 | |
650 | values = String.clean_array (mimetypes); |
651 | |
652 | - if (values.length == 0) |
653 | + if (values.length == 0) { |
654 | throw new KeyFileError.INVALID_VALUE ("No values specified for MimeType."); |
655 | + } |
656 | } |
657 | |
658 | public bool is_type_supported (string mime_type) { |
659 | @@ -38,8 +39,9 @@ |
660 | |
661 | private bool contains_mimetype (string mime_type) { |
662 | foreach (string local_mime_type in values) { |
663 | - if (compare (mime_type, local_mime_type)) |
664 | + if (compare (mime_type, local_mime_type)) { |
665 | return true; |
666 | + } |
667 | } |
668 | |
669 | return false; |
670 | |
671 | === modified file 'src/String.vala' |
672 | --- src/String.vala 2013-05-20 04:55:11 +0000 |
673 | +++ src/String.vala 2015-09-15 23:13:26 +0000 |
674 | @@ -26,8 +26,9 @@ |
675 | if (array != null && array.length > 0) { |
676 | list = new List<string> (); |
677 | |
678 | - foreach (var str in array) |
679 | + foreach (var str in array) { |
680 | list.prepend (str); |
681 | + } |
682 | |
683 | list.reverse (); |
684 | } |
685 | @@ -45,8 +46,9 @@ |
686 | if (str != null) { |
687 | string clean_str = str.strip (); |
688 | |
689 | - if (clean_str != "" && !container.contains (clean_str)) |
690 | + if (clean_str != "" && !container.contains (clean_str)) { |
691 | container.add (clean_str); |
692 | + } |
693 | } |
694 | } |
695 | |
696 | |
697 | === modified file 'src/Translations.vala' |
698 | --- src/Translations.vala 2013-05-20 06:07:44 +0000 |
699 | +++ src/Translations.vala 2015-09-15 23:13:26 +0000 |
700 | @@ -36,8 +36,9 @@ |
701 | } |
702 | |
703 | private static void add_domain (string domain) { |
704 | - if (domains.contains (domain)) |
705 | + if (domains.contains (domain)) { |
706 | return; |
707 | + } |
708 | |
709 | domains.add (domain); |
710 | Intl.textdomain (domain); |
Nice work, thanks :)
See below a few comments about code style and some other things.
We also need documentations for the public methods to get this merged, which will be used to generate the api docs (see other files (e.g. the widgets are well documented) if you are looking for examples).