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 : 6 : g_autolist (EVCardAttribute) phone_numbers = NULL;
32 : 3 : g_autolist (EVCardAttribute) email_addresses = NULL;
33 : 3 : g_autolist (EVCardAttribute) urls = NULL;
34 : : // g_autolist (EVCardAttribute) postal_addresses = NULL;
35 : 3 : g_autoptr (EContactDate) birthdate = NULL;
36 [ + + ]: 3 : g_autofree char *vcard = NULL;
37 : 3 : static struct
38 : : {
39 : : EContactField field;
40 : : const char *property;
41 : : } contact_fields[] = {
42 : : {
43 : : .field = E_CONTACT_UID,
44 : : .property = "nco:contactUID",
45 : : },
46 : : {
47 : : .field = E_CONTACT_FULL_NAME,
48 : : .property = "nco:fullname",
49 : : },
50 : : {
51 : : .field = E_CONTACT_NICKNAME,
52 : : .property = "nco:nickname",
53 : : },
54 : : {
55 : : .field = E_CONTACT_NOTE,
56 : : .property = "nco:note",
57 : : },
58 : : #if 0
59 : : {
60 : : .field = E_CONTACT_PHOTO,
61 : : .property = "nco:photo",
62 : : },
63 : : #endif
64 : : };
65 : :
66 [ + - + - : 3 : g_return_val_if_fail (E_IS_CONTACT (contact), NULL);
- + - - ]
67 : :
68 : : /* NOTE: nco:PersonContact is used unconditionally, because it's the only
69 : : * class which receives change notification.
70 : : */
71 : 3 : resource = tracker_resource_new (NULL);
72 : 3 : tracker_resource_set_uri (resource, "rdf:type", "nco:PersonContact");
73 : :
74 : 3 : vcard = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_21);
75 : 3 : tracker_resource_set_string (resource, "nie:plainTextContent", vcard);
76 : :
77 [ + + ]: 15 : for (size_t i = 0; i < G_N_ELEMENTS (contact_fields); i++)
78 : : {
79 : 12 : const char *value = NULL;
80 : :
81 : 12 : value = e_contact_get_const (contact, contact_fields[i].field);
82 [ + + + - ]: 12 : if (value != NULL && *value != '\0')
83 : 6 : tracker_resource_set_string (resource, contact_fields[i].property, value);
84 : : }
85 : :
86 : 3 : birthdate = e_contact_get (contact, E_CONTACT_BIRTH_DATE);
87 [ + + ]: 3 : if (birthdate != NULL)
88 : : {
89 : 4 : g_autoptr (GDateTime) date = NULL;
90 : :
91 : 2 : date = g_date_time_new_local (birthdate->year,
92 : 1 : birthdate->month,
93 : 1 : birthdate->day,
94 : : 0, 0, 0.0);
95 [ + - ]: 1 : tracker_resource_set_datetime (resource, "nco:birthDate", date);
96 : : }
97 : :
98 : 3 : phone_numbers = e_contact_get_attributes (contact, E_CONTACT_TEL);
99 [ + + ]: 6 : for (const GList *iter = phone_numbers; iter != NULL; iter = iter->next)
100 : : {
101 : 3 : EVCardAttribute *attr = iter->data;
102 : 3 : g_autofree char *medium = NULL;
103 : 3 : g_autoptr (EPhoneNumber) number = NULL;
104 [ + - ]: 3 : g_autofree char *medium_iri = NULL;
105 : 3 : TrackerResource *medium_resource = NULL;
106 : 3 : const char *medium_type = NULL;
107 : :
108 [ + - ]: 3 : if (e_vcard_attribute_has_type (attr, "CAR"))
109 : : medium_type = "nco:CarPhoneNumber";
110 [ + + ]: 3 : else if (e_vcard_attribute_has_type (attr, "CELL"))
111 : : medium_type = "nco:MessagingNumber";
112 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "FAX"))
113 : : medium_type = "nco:FaxNumber";
114 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "ISDN"))
115 : : medium_type = "nco:IsdnNumber";
116 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "PAGER"))
117 : : medium_type = "nco:PagerNumber";
118 [ + - ]: 1 : else if (e_vcard_attribute_has_type (attr, "VOICE"))
119 : : medium_type = "nco:VoicePhoneNumber";
120 : : else
121 : 1 : medium_type = "nco:PhoneNumber";
122 : :
123 : 3 : medium = e_vcard_attribute_get_value (attr);
124 : 3 : number = e_phone_number_from_string (medium, NULL, NULL);
125 [ + - ]: 3 : if (number != NULL)
126 : 3 : medium_iri = e_phone_number_to_string (number, E_PHONE_NUMBER_FORMAT_RFC3966);
127 : : else
128 : 0 : medium_iri = g_strdup_printf ("tel:%s", medium);
129 : :
130 : 3 : medium_resource = tracker_resource_new (medium_iri);
131 : 3 : tracker_resource_set_uri (medium_resource, "rdf:type", medium_type);
132 : 3 : tracker_resource_set_string (medium_resource, "nco:phoneNumber", medium);
133 : 3 : tracker_resource_add_take_relation (resource,
134 : : "nco:hasPhoneNumber",
135 : 3 : g_steal_pointer (&medium_resource));
136 : : }
137 : :
138 : 3 : email_addresses = e_contact_get_attributes (contact, E_CONTACT_EMAIL);
139 [ + + ]: 4 : for (const GList *iter = email_addresses; iter != NULL; iter = iter->next)
140 : : {
141 : 1 : EVCardAttribute *attr = iter->data;
142 : 1 : g_autofree char *medium = NULL;
143 : 1 : g_autofree char *medium_iri = NULL;
144 : 1 : TrackerResource *medium_resource = NULL;
145 : :
146 : 1 : medium = e_vcard_attribute_get_value (attr);
147 : 1 : medium_iri = g_strdup_printf ("mailto:%s", medium);
148 : 1 : medium_resource = tracker_resource_new (medium_iri);
149 : 1 : tracker_resource_set_uri (medium_resource, "rdf:type", "nco:EmailAddress");
150 : 1 : tracker_resource_set_string (medium_resource, "nco:emailAddress", medium);
151 : 1 : tracker_resource_add_take_relation (resource,
152 : : "nco:hasEmailAddress",
153 : 1 : g_steal_pointer (&medium_resource));
154 : : }
155 : :
156 : : #if 0
157 : : postal_addresses = e_contact_get_attributes (contact, E_CONTACT_ADDRESS);
158 : : for (const GList *iter = postal_addresses; iter; iter = iter->next)
159 : : {
160 : : EVCardAttribute *attr = iter->data;
161 : : GList *values = e_vcard_attribute_get_values_decoded (attr);
162 : : g_autofree char *medium = NULL;
163 : : g_autofree char *medium_iri = NULL;
164 : : TrackerResource *medium_resource = NULL;
165 : : const char *medium_type = NULL;
166 : : uint8_t v = 0;
167 : :
168 : : if (e_vcard_attribute_has_type (attr, "DOM"))
169 : : medium_type = "nco:DomesticDeliveryAddress";
170 : : else if (e_vcard_attribute_has_type (attr, "INTL"))
171 : : medium_type = "nco:InternationalDeliveryAddress";
172 : : else if (e_vcard_attribute_has_type (attr, "PARCEL"))
173 : : medium_type = "nco:ParcelDeliveryAddress";
174 : : else
175 : : medium_type = "nco:PostalAddress";
176 : :
177 : : medium = e_vcard_attribute_get_value (attr);
178 : : medium_iri = g_strdup_printf ("mailto:%s", medium);
179 : : medium_resource = tracker_resource_new (medium_iri);
180 : : tracker_resource_set_uri (medium_resource, "rdf:type", medium_type);
181 : :
182 : : for (const GList *viter = values; viter != NULL; viter = viter->next)
183 : : {
184 : : const GString *decoded = values->data;
185 : : static const char *adr_fields[] = {
186 : : "nco:pobox",
187 : : "nco:extendedAddress",
188 : : "nco:streetAddress",
189 : : "nco:locality",
190 : : "nco:region",
191 : : "nco:postalcode",
192 : : "nco:country",
193 : : };
194 : :
195 : : if (v < G_N_ELEMENTS (adr_fields) && decoded->len > 0)
196 : : {
197 : : tracker_resource_set_string (medium_resource,
198 : : adr_fields[v],
199 : : decoded->str);
200 : : }
201 : :
202 : : v++;
203 : : }
204 : :
205 : : tracker_resource_add_take_relation (resource,
206 : : "nco:hasPostalAddress",
207 : : g_steal_pointer (&medium_resource));
208 : : }
209 : : #endif
210 : :
211 : 3 : urls = e_contact_get_attributes (contact, E_CONTACT_HOMEPAGE_URL);
212 [ + + ]: 4 : for (const GList *iter = urls; iter != NULL; iter = iter->next)
213 : : {
214 : 1 : EVCardAttribute *attr = iter->data;
215 : 1 : g_autoptr (GString) url = NULL;
216 : :
217 : 1 : url = e_vcard_attribute_get_value_decoded (attr);
218 [ + - + - ]: 1 : if (url != NULL && g_uri_is_valid (url->str, G_URI_FLAGS_PARSE_RELAXED, NULL))
219 : 1 : tracker_resource_add_uri (resource, "nco:url", url->str);
220 : : }
221 : :
222 : : return g_steal_pointer (&resource);
223 : : }
224 : :
|