~elementary-pantheon/contractor/master

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
/*
 * 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.ContractMatcher {
    /**
     * get contracts which support the passed MIME types
     *
     * @param mime_types the MIME types which have to be supported by the returned contracts
     * @param contracts_to_filter a list of contracts to filter
     *
     * @return a Collection of Contract objects which support the file size
     */
    public Gee.Collection<Contract> get_contracts_for_types (string[] mime_types,
        Gee.Collection<Contract> contracts_to_filter) throws ContractorError
    {
        var valid_contracts = new Gee.LinkedList<Contract> ();
        var valid_mime_types = String.clean_array (mime_types);

        if (valid_mime_types.length == 0) {
            throw new ContractorError.NO_MIMETYPES_GIVEN ("No mimetypes were provided.");
        }

        foreach (var contract in contracts_to_filter) {
            // Check if the contract supports ALL the types listed in mime_types
            bool all_types_supported = true;

            foreach (string mime_type in valid_mime_types) {
                if (!contract.supports_mime_type (mime_type)) {
                    all_types_supported = false;
                    break;
                }
            }

            if (all_types_supported) {
                valid_contracts.add (contract);
            }
        }

        return valid_contracts;
    }

    /**
     * get contracts which support the passed file size
     *
     * @param file_size the file size which has to be supported by the returned contracts
     * @param contracts_to_filter a list of contracts to filter
     *
     * @return a Collection of Contract objects which support the file size
     */
    public Gee.Collection<Contract> get_contracts_for_file_size (int64 file_size,
        Gee.Collection<Contract> contracts_to_filter) throws ContractorError
    {
        var valid_contracts = new Gee.LinkedList<Contract> ();

        foreach (var contract in contracts_to_filter) {
            bool file_size_supported = true;

            if (!contract.supports_file_size (file_size)) {
                file_size_supported = false;
            }

            if (file_size_supported)
                valid_contracts.add (contract);
        }

        return valid_contracts;
    }

    /**
     * get contracts which support the passed MIME types and file size
     *
     * @param mime_types the MIME types which have to be supported by the returned contracts
     * @param file_size the file size which has to be supported by the returned contracts
     * @param contracts_to_filter a list of contracts to filter
     *
     * @return a Collection of Contract objects which support the MIME types and the file size
     */
    public Gee.Collection<Contract> get_contracts_for_types_and_file_size (string[] mime_types,
        int64 file_size, Gee.Collection<Contract> contracts_to_filter) throws ContractorError
    {
        var contracts_for_types = get_contracts_for_types (mime_types, contracts_to_filter);
        var valid_contracts = get_contracts_for_file_size (file_size, contracts_for_types);

        return valid_contracts;
    }
}