Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-contact"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <libebook-contacts/libebook-contacts.h>
10 : : #include <libtracker-sparql/tracker-sparql.h>
11 : :
12 : : #include "valent-contact.h"
13 : :
14 : :
15 : : //
16 : :
17 : : /**
18 : : * valent_contact_resource_from_econtact:
19 : : * @contact: an `EContact`
20 : : *
21 : : * Pack an [class@EBookContacts.Contact] into a [class@Tracker.Resource].
22 : : *
23 : : * Returns: (transfer full): a new SPARQL resource
24 : : *
25 : : * Since: 1.0
26 : : */
27 : : TrackerResource *
28 : 3 : valent_contact_resource_from_econtact (EContact *contact)
29 : : {
30 : 6 : g_autoptr (TrackerResource) resource = NULL;
31 : : #if EDS_CHECK_VERSION (3, 59, 0)
32 : : g_autoptr (GList) phone_numbers = NULL;
33 : : g_autoptr (GList) email_addresses = NULL;
34 : : g_autoptr (GList) urls = NULL;
35 : : // g_autoptr (GList) postal_addresses = NULL;
36 : : #else
37 : 6 : g_autolist (EVCardAttribute) phone_numbers = NULL;
38 : 3 : g_autolist (EVCardAttribute) email_addresses = NULL;
39 : 3 : g_autolist (EVCardAttribute) urls = NULL;
40 : : // g_autolist (EVCardAttribute) postal_addresses = NULL;
41 : : #endif
42 : 3 : g_autoptr (EContactDate) birthdate = NULL;
43 [ + + ]: 3 : g_autofree char *vcard = NULL;
44 : 3 : static struct
45 : : {
46 : : EContactField field;
47 : : const char *property;
48 : : } contact_fields[] = {
49 : : {
50 : : .field = E_CONTACT_UID,
51 : : .property = "nco:contactUID",
52 : : },
53 : : {
54 : : .field = E_CONTACT_FULL_NAME,
55 : : .property = "nco:fullname",
56 : : },
57 : : {
58 : : .field = E_CONTACT_NICKNAME,
59 : : .property = "nco:nickname",
60 : : },
61 : : {
62 : : .field = E_CONTACT_NOTE,
63 : : .property = "nco:note",
64 : : },
65 : : #if 0
66 : : {
67 : : .field = E_CONTACT_PHOTO,
68 : : .property = "nco:photo",
69 : : },
70 : : #endif
71 : : };
72 : :
73 [ + - + - : 3 : g_return_val_if_fail (E_IS_CONTACT (contact), NULL);
- + - - ]
74 : :
75 : : /* NOTE: nco:PersonContact is used unconditionally, because it's the only
76 : : * class which receives change notification.
77 : : */
78 : 3 : resource = tracker_resource_new (NULL);
79 : 3 : tracker_resource_set_uri (resource, "rdf:type", "nco:PersonContact");
80 : :
81 : : #if EDS_CHECK_VERSION (3, 59, 0)
82 : : vcard = e_vcard_to_string (E_VCARD (contact));
83 : : #else
84 : 3 : vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_21);
85 : : #endif
86 : 3 : tracker_resource_set_string (resource, "nie:plainTextContent", vcard);
87 : :
88 [ + + ]: 15 : for (size_t i = 0; i < G_N_ELEMENTS (contact_fields); i++)
89 : : {
90 : 12 : const char *value = NULL;
91 : :
92 : 12 : value = e_contact_get_const (contact, contact_fields[i].field);
93 [ + + + - ]: 12 : if (value != NULL && *value != '\0')
94 : 6 : tracker_resource_set_string (resource, contact_fields[i].property, value);
95 : : }
96 : :
97 : 3 : birthdate = e_contact_get (contact, E_CONTACT_BIRTH_DATE);
98 [ + + ]: 3 : if (birthdate != NULL)
99 : : {
100 : 4 : g_autoptr (GDateTime) date = NULL;
101 : :
102 : 2 : date = g_date_time_new_local (birthdate->year,
103 : 1 : birthdate->month,
104 : 1 : birthdate->day,
105 : : 0, 0, 0.0);
106 [ + - ]: 1 : tracker_resource_set_datetime (resource, "nco:birthDate", date);
107 : : }
108 : :
109 : : #if EDS_CHECK_VERSION (3, 59, 0)
110 : : phone_numbers = e_vcard_get_attributes_by_name (E_VCARD (contact), EVC_TEL);
111 : : #else
112 : 3 : phone_numbers = e_contact_get_attributes (contact, E_CONTACT_TEL);
113 : : #endif
114 [ + + ]: 6 : for (const GList *iter = phone_numbers; iter != NULL; iter = iter->next)
115 : : {
116 : 3 : EVCardAttribute *attr = iter->data;
117 : 3 : g_autofree char *medium = NULL;
118 : 3 : g_autoptr (EPhoneNumber) number = NULL;
119 [ + - ]: 3 : g_autofree char *medium_iri = NULL;
120 : 3 : TrackerResource *medium_resource = NULL;
121 : 3 : const char *medium_type = NULL;
122 : :
123 [ + - ]: 3 : if (e_vcard_attribute_has_type (attr, "CAR"))
124 : : medium_type = "nco:CarPhoneNumber";
125 [ + + ]: 3 : else if (e_vcard_attribute_has_type (attr, "CELL"))
126 : : medium_type = "nco:MessagingNumber";
127 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "FAX"))
128 : : medium_type = "nco:FaxNumber";
129 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "ISDN"))
130 : : medium_type = "nco:IsdnNumber";
131 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "PAGER"))
132 : : medium_type = "nco:PagerNumber";
133 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "VOICE"))
134 : : medium_type = "nco:VoicePhoneNumber";
135 : : else
136 : 1 : medium_type = "nco:PhoneNumber";
137 : :
138 : 3 : medium = e_vcard_attribute_get_value (attr);
139 : 3 : number = e_phone_number_from_string (medium, NULL, NULL);
140 [ + - ]: 3 : if (number != NULL)
141 : 3 : medium_iri = e_phone_number_to_string (number, E_PHONE_NUMBER_FORMAT_RFC3966);
142 : : else
143 : 0 : medium_iri = g_strdup_printf ("tel:%s", medium);
144 : :
145 : 3 : medium_resource = tracker_resource_new (medium_iri);
146 : 3 : tracker_resource_set_uri (medium_resource, "rdf:type", medium_type);
147 : 3 : tracker_resource_set_string (medium_resource, "nco:phoneNumber", medium);
148 : 3 : tracker_resource_add_take_relation (resource,
149 : : "nco:hasPhoneNumber",
150 : 3 : g_steal_pointer (&medium_resource));
151 : : }
152 : :
153 : : #if EDS_CHECK_VERSION (3, 59, 0)
154 : : email_addresses = e_vcard_get_attributes_by_name (E_VCARD (contact), EVC_EMAIL);
155 : : #else
156 : 3 : email_addresses = e_contact_get_attributes (contact, E_CONTACT_EMAIL);
157 : : #endif
158 [ + + ]: 4 : for (const GList *iter = email_addresses; iter != NULL; iter = iter->next)
159 : : {
160 : 1 : EVCardAttribute *attr = iter->data;
161 : 1 : g_autofree char *medium = NULL;
162 : 1 : g_autofree char *medium_iri = NULL;
163 : 1 : TrackerResource *medium_resource = NULL;
164 : :
165 : 1 : medium = e_vcard_attribute_get_value (attr);
166 : 1 : medium_iri = g_strdup_printf ("mailto:%s", medium);
167 : 1 : medium_resource = tracker_resource_new (medium_iri);
168 : 1 : tracker_resource_set_uri (medium_resource, "rdf:type", "nco:EmailAddress");
169 : 1 : tracker_resource_set_string (medium_resource, "nco:emailAddress", medium);
170 : 1 : tracker_resource_add_take_relation (resource,
171 : : "nco:hasEmailAddress",
172 : 1 : g_steal_pointer (&medium_resource));
173 : : }
174 : :
175 : : #if 0
176 : : #if EDS_CHECK_VERSION (3, 59, 0)
177 : : postal_addresses = e_vcard_get_attributes_by_name (E_VCARD (contact), EVC_ADR);
178 : : #else
179 : : postal_addresses = e_contact_get_attributes (contact, E_CONTACT_ADDRESS);
180 : : #endif
181 : : for (const GList *iter = postal_addresses; iter; iter = iter->next)
182 : : {
183 : : EVCardAttribute *attr = iter->data;
184 : : GList *values = e_vcard_attribute_get_values_decoded (attr);
185 : : g_autofree char *medium = NULL;
186 : : g_autofree char *medium_iri = NULL;
187 : : TrackerResource *medium_resource = NULL;
188 : : const char *medium_type = NULL;
189 : : uint8_t v = 0;
190 : :
191 : : if (e_vcard_attribute_has_type (attr, "DOM"))
192 : : medium_type = "nco:DomesticDeliveryAddress";
193 : : else if (e_vcard_attribute_has_type (attr, "INTL"))
194 : : medium_type = "nco:InternationalDeliveryAddress";
195 : : else if (e_vcard_attribute_has_type (attr, "PARCEL"))
196 : : medium_type = "nco:ParcelDeliveryAddress";
197 : : else
198 : : medium_type = "nco:PostalAddress";
199 : :
200 : : medium = e_vcard_attribute_get_value (attr);
201 : : medium_iri = g_strdup_printf ("mailto:%s", medium);
202 : : medium_resource = tracker_resource_new (medium_iri);
203 : : tracker_resource_set_uri (medium_resource, "rdf:type", medium_type);
204 : :
205 : : for (const GList *viter = values; viter != NULL; viter = viter->next)
206 : : {
207 : : const GString *decoded = values->data;
208 : : static const char *adr_fields[] = {
209 : : "nco:pobox",
210 : : "nco:extendedAddress",
211 : : "nco:streetAddress",
212 : : "nco:locality",
213 : : "nco:region",
214 : : "nco:postalcode",
215 : : "nco:country",
216 : : };
217 : :
218 : : if (v < G_N_ELEMENTS (adr_fields) && decoded->len > 0)
219 : : {
220 : : tracker_resource_set_string (medium_resource,
221 : : adr_fields[v],
222 : : decoded->str);
223 : : }
224 : :
225 : : v++;
226 : : }
227 : :
228 : : tracker_resource_add_take_relation (resource,
229 : : "nco:hasPostalAddress",
230 : : g_steal_pointer (&medium_resource));
231 : : }
232 : : #endif
233 : :
234 : : #if EDS_CHECK_VERSION (3, 59, 0)
235 : : urls = e_vcard_get_attributes_by_name (E_VCARD (contact), EVC_URL);
236 : : #else
237 : 3 : urls = e_contact_get_attributes (contact, E_CONTACT_HOMEPAGE_URL);
238 : : #endif
239 [ + + ]: 4 : for (const GList *iter = urls; iter != NULL; iter = iter->next)
240 : : {
241 : 1 : EVCardAttribute *attr = iter->data;
242 : 1 : g_autoptr (GString) url = NULL;
243 : :
244 : 1 : url = e_vcard_attribute_get_value_decoded (attr);
245 [ + - + - ]: 1 : if (url != NULL && g_uri_is_valid (url->str, G_URI_FLAGS_PARSE_RELAXED, NULL))
246 : 1 : tracker_resource_add_uri (resource, "nco:url", url->str);
247 : : }
248 : :
249 : : return g_steal_pointer (&resource);
250 : : }
251 : :
|