1/* 2 * Copyright (C) 2011-2017 elementary Developers 3 * 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. 8 * 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. 13 * 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/>. 16 */ 17 18namespace 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 */ 27 public Gee.Collection<Contract> get_contracts_for_types (string[] mime_types, 28 Gee.Collection<Contract> contracts_to_filter) throws ContractorError 29 { 30 var valid_contracts = new Gee.LinkedList<Contract> (); 31 var valid_mime_types = String.clean_array (mime_types); 32 33 if (valid_mime_types.length == 0) { 34 throw new ContractorError.NO_MIMETYPES_GIVEN ("No mimetypes were provided."); 35 } 36 37 foreach (var contract in contracts_to_filter) { 38 // Check if the contract supports ALL the types listed in mime_types 39 bool all_types_supported = true; 40 41 foreach (string mime_type in valid_mime_types) { 42 if (!contract.supports_mime_type (mime_type)) { 43 all_types_supported = false; 44 break; 45 } 46 } 47 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); 97 98 return valid_contracts; 99 } 100} 101