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
|
/*
* Copyright (C) 2013-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/>.
*/
public class Contractor.ContractSource : Object {
public signal void changed ();
private FileService file_service;
private Gee.List<Contract> sorted_contracts;
private Gee.HashMap<string, Contract> contracts;
public ContractSource () {
file_service = new FileService ();
sorted_contracts = new Gee.LinkedList<Contract> ();
contracts = new Gee.HashMap<string, Contract> ();
load_contracts ();
file_service.contract_files_changed.connect (load_contracts);
}
public Gee.Collection<Contract> get_contracts () {
return sorted_contracts;
}
public Contract lookup_by_id (string contract_id) throws Error {
var contract = contracts.get (contract_id);
if (contract == null) {
throw new IOError.NOT_FOUND ("Requested invalid contract: %s", contract_id);
}
return contract;
}
private void load_contracts () {
clear_loaded_contracts ();
var contract_files_to_load = file_service.load_contract_files ();
foreach (var contract_file in contract_files_to_load) {
load_contract (contract_file);
}
changed ();
}
private void load_contract (File file) {
try {
var contract = new Contract (file);
add_contract (contract);
message ("Contract file '%s' loaded successfully.", file.get_path ());
} catch (Error err) {
warning ("Could not load contract at '%s': %s", file.get_path (), err.message);
}
}
private void add_contract (Contract contract) {
string contract_id = contract.id;
if (contracts.has_key (contract_id)) {
warning ("A contract with ID '%s' exists already. Not adding another one.", contract_id);
return;
}
contracts.set (contract_id, contract);
sorted_contracts.add (contract);
// Sort contracts here so that clients don't have to sort them again
sorted_contracts.sort (ContractSorter.compare_func);
}
private void clear_loaded_contracts () {
contracts.clear ();
sorted_contracts.clear ();
}
}
|