~elementary-pantheon/contractor/master

« back to all changes in this revision

Viewing changes to src/ContractMatcher.vala

  • Committer: RabbitBot
  • Author(s): Florian R. A. Angermeier
  • Date: 2015-09-21 13:31:07 UTC
  • mfrom: (136.1.11)
  • Revision ID: git-v1:4a5e95794024146bc89ee39053530c11e4977103
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_by_file_size
  - get_contracts_by_mime_and_file_size
  - get_contracts_by_mimelist_and_file_size

Add documentation

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 */
17
17
 
18
18
namespace Contractor.ContractMatcher {
 
19
    /**
 
20
     * get contracts which support the passed MIME types
 
21
     *
 
22
     * @param mime_types the MIME types which have to be supported by the returned contracts
 
23
     * @param contracts_to_filter a list of contracts to filter
 
24
     *
 
25
     * @return a Collection of Contract objects which support the file size
 
26
     */
19
27
    public Gee.Collection<Contract> get_contracts_for_types (string[] mime_types,
20
28
        Gee.Collection<Contract> contracts_to_filter) throws ContractorError
21
29
    {
22
30
        var valid_contracts = new Gee.LinkedList<Contract> ();
23
31
        var valid_mime_types = String.clean_array (mime_types);
24
32
 
25
 
        if (valid_mime_types.length == 0)
 
33
        if (valid_mime_types.length == 0) {
26
34
            throw new ContractorError.NO_MIMETYPES_GIVEN ("No mimetypes were provided.");
 
35
        }
27
36
 
28
37
        foreach (var contract in contracts_to_filter) {
29
38
            // Check if the contract supports ALL the types listed in mime_types
36
45
                }
37
46
            }
38
47
 
39
 
            if (all_types_supported)
40
 
                valid_contracts.add (contract);
41
 
        }
 
48
            if (all_types_supported) {
 
49
                valid_contracts.add (contract);
 
50
            }
 
51
        }
 
52
 
 
53
        return valid_contracts;
 
54
    }
 
55
 
 
56
    /**
 
57
     * get contracts which support the passed file size
 
58
     *
 
59
     * @param file_size the file size which has to be supported by the returned contracts
 
60
     * @param contracts_to_filter a list of contracts to filter
 
61
     *
 
62
     * @return a Collection of Contract objects which support the file size
 
63
     */
 
64
    public Gee.Collection<Contract> get_contracts_for_file_size (int64 file_size,
 
65
        Gee.Collection<Contract> contracts_to_filter) throws ContractorError
 
66
    {
 
67
        var valid_contracts = new Gee.LinkedList<Contract> ();
 
68
 
 
69
        foreach (var contract in contracts_to_filter) {
 
70
            bool file_size_supported = true;
 
71
 
 
72
            if (!contract.supports_file_size (file_size)) {
 
73
                file_size_supported = false;
 
74
            }
 
75
 
 
76
            if (file_size_supported)
 
77
                valid_contracts.add (contract);
 
78
        }
 
79
 
 
80
        return valid_contracts;
 
81
    }
 
82
 
 
83
    /**
 
84
     * get contracts which support the passed MIME types and file size
 
85
     *
 
86
     * @param mime_types the MIME types which have to be supported by the returned contracts
 
87
     * @param file_size the file size which has to be supported by the returned contracts
 
88
     * @param contracts_to_filter a list of contracts to filter
 
89
     *
 
90
     * @return a Collection of Contract objects which support the MIME types and the file size
 
91
     */
 
92
    public Gee.Collection<Contract> get_contracts_for_types_and_file_size (string[] mime_types,
 
93
        int64 file_size, Gee.Collection<Contract> contracts_to_filter) throws ContractorError
 
94
    {
 
95
        var contracts_for_types = get_contracts_for_types (mime_types, contracts_to_filter);
 
96
        var valid_contracts = get_contracts_for_file_size (file_size, contracts_for_types);
42
97
 
43
98
        return valid_contracts;
44
99
    }