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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/* contact.vala
*
* Copyright (C) 2008 Johann Prieur <johann.prieur@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
using Gee;
using People.Utils;
namespace People {
public abstract class Contact : AsyncOperationHelper {
public string id {
protected get;
protected construct;
}
/* Public API */
public signal void contact_updated ();
public abstract AsyncOperation<Gee.Set<string> > get_defined_fields ();
public abstract AsyncOperation<Gee.Map<string, string> > get_fields (Gee.Set<string> fields);
/* End of public API */
}
public class SingleContact : Contact {
public Backend.Contact contact {
private get;
private construct;
}
public SingleContact (string id, Backend.Contact contact) {
this.id = id;
this.contact = contact;
}
public override AsyncOperation<Gee.Set<string> > get_defined_fields () {
return contact.get_defined_fields ();
}
public override AsyncOperation<Gee.Map<string, string> > get_fields (Gee.Set<string> fields) {
return contact.get_fields (fields);
}
}
public class MultiContact : Contact {
private Set<MultiAsyncOperation> pending_multi_operations =
new Gee.HashSet<MultiAsyncOperation> ();
public Gee.List<Backend.Contact> contacts {
public get;
private construct;
}
public MultiContact (string id, Gee.List<Backend.Contact> contacts) {
this.id = id;
this.contacts = contacts;
}
/* Public API */
public override AsyncOperation<Gee.Set<string> > get_defined_fields () {
var ops = new Gee.ArrayList<AsyncOperation<Gee.Set <string> > > ();
foreach (Backend.Contact contact in contacts) {
if (contact != null) { // Contacts that we failed to fetch
ops.add (contact.get_defined_fields ());
}
}
if (ops.size >= 1) { // FIXME: optimize to not use MultiAsync for a single contact
var op = async_operation_init ();
var multi = new MultiAsyncOperation<Gee.Set<string> > (ops);
multi.add_callback (get_defined_fields_callback, op);
pending_multi_operations.add (multi);
return op;
} else {
return sync_result (new Gee.HashSet<string> (str_hash, str_equal));
}
}
private void get_defined_fields_callback(MultiAsyncOperation<Gee.Set<string> > multi,
void* data) {
var results = new Gee.HashSet<string> (str_hash, str_equal);
AsyncOperation op = (AsyncOperation) data;
foreach (Set<string> result in multi.get_result ()) {
foreach (string r in result) {
if (r != null) {
results.add (r);
}
}
}
async_operation_emit (op, results);
pending_multi_operations.remove (multi);
}
public override AsyncOperation<Gee.Map<string, string> > get_fields (Gee.Set<string> fields) {
var ops = new Gee.ArrayList<AsyncOperation<Gee.Map <string, string> > > ();
foreach (Backend.Contact contact in contacts) {
if (contact != null) { // Contacts that we failed to fetch
ops.add (contact.get_fields (fields));
}
}
if (ops.size >= 1) { // FIXME: optimize to not use MultiAsync for a single contact
var op = async_operation_init ();
var multi = new MultiAsyncOperation<Gee.Map<string, string> > (ops);
multi.add_callback (get_fields_callback, op);
pending_multi_operations.add (multi);
return op;
} else {
return sync_result (new Gee.HashMap<string, string> (str_hash, str_equal, str_equal));
}
}
private void get_fields_callback(MultiAsyncOperation<Gee.Map<string, string> > multi,
void* data) {
var results = new Gee.HashMap<string, string> (str_hash, str_equal, str_equal);
AsyncOperation op = (AsyncOperation) data;
foreach (Map<string, string> result in multi.get_result ()) {
if (result != null) {
foreach (string key in result.get_keys ()) {
if (!result.contains (key)) {
results.set (key, result.get (key));
}
}
}
}
async_operation_emit (op, results);
pending_multi_operations.remove (multi);
}
/* End of public API */
}
}
|