[misc] Sycn with master branch.
[apps/core/preloaded/calendar.git] / ug / edit / edit-attendee.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.1 (the "License");
6   *  you may not use this file except in compliance with the License.
7   *  You may obtain a copy of the License at
8   *
9   *       http://floralicense.org/license/
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17
18 #include <ui-gadget.h>
19
20 #include "external-ug.h"
21 #include "edit-attendee.h"
22
23 void cal_edit_attendee_free_list(Eina_List **h)
24 {
25         Eina_List *l = NULL;
26         cal_participant *participant = NULL;
27
28         EINA_LIST_FOREACH(*h, l, participant) {
29                 if (participant->image)
30                         free(participant->image);
31
32                 if (participant->name)
33                         free(participant->name);
34
35                 if (participant->email)
36                         free(participant->email);
37
38                 if (participant->number)
39                         free(participant->number);
40
41                 free(participant);
42         }
43
44         *h = eina_list_free(*h);
45 }
46
47 void cal_edit_attendee_get_person_id_by_email_id(int email_id, int *person_id)
48 {
49         c_ret_if(email_id < 0);
50         c_ret_if(!person_id);
51
52         contacts_record_h email = NULL;
53
54         contacts_error_e error = contacts_db_get_record(_contacts_email._uri, email_id, &email);
55         c_retm_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record(%d) is failed(%x)", email_id, error);
56
57         int contact_id = 0;
58
59         error = contacts_record_get_int(email, _contacts_email.contact_id, &contact_id);
60         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_int() is failed(%x)", error);
61
62         contacts_record_h contact = NULL;
63
64         error = contacts_db_get_record(_contacts_contact._uri, contact_id, &contact);
65         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record(%d) is failed(%x)", contact_id, error);
66
67         error = contacts_record_get_int(contact, _contacts_contact.person_id, person_id);
68         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_int() is failed(%x)", error);
69
70         error = contacts_record_destroy(contact, true);
71         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
72
73         error = contacts_record_destroy(email, true);
74         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
75 }
76
77 Eina_Bool cal_edit_attendee_check_is_exist(Evas_Object *mbe, const char *label, Elm_Object_Item *cur)
78 {
79         Elm_Object_Item *it;
80         const char *l;
81
82         // TODO: check contact id??
83         it = elm_multibuttonentry_first_item_get(mbe);
84         while (it)
85         {
86                 if (cur != it)
87                 {
88                         l = elm_object_item_text_get(it);
89                         if (l && !CAL_STRCMP(l, label))
90                                 return EINA_TRUE;
91                 }
92
93                 it = elm_multibuttonentry_item_next_get(it);
94         }
95
96         return EINA_FALSE;
97 }
98
99 void cal_edit_attendee_get_email_by_person_id(char *buf, int size, int person_id)
100 {
101         c_retm_if(!buf, "buf is null");
102         c_retm_if(size < 1, "size is smaller than 1");
103         c_retm_if(person_id < 0, "email id is invalid.");
104
105         contacts_record_h person = NULL;
106
107         contacts_error_e error = contacts_db_get_record(_contacts_person._uri, person_id, &person);
108         c_retm_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record(%d) is failed(%x)", person_id, error);
109
110         int contact_id = 0;
111
112         error = contacts_record_get_int(person, _contacts_person.display_contact_id, &contact_id);
113         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_int() is failed(%x)", error);
114
115         contacts_record_h contact = NULL;
116
117         error = contacts_db_get_record(_contacts_contact._uri, contact_id, &contact);
118         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record() is failed(%x)", error);
119
120         contacts_record_h email = NULL;
121
122         error = contacts_record_get_child_record_at_p(contact, _contacts_contact.email, 0, &email);
123         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_child_record_at_p() is failed(%x)", error);
124
125         char *email_address = NULL;
126
127         error = contacts_record_get_str_p(email, _contacts_email.email, &email_address);
128         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_str_p() is failed(%x)", error);
129
130         if (CAL_STRLEN(email_address))
131                 snprintf(buf, size, "%s", email_address);
132
133         error = contacts_record_destroy(contact, true);
134         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
135
136         error = contacts_record_destroy(person, true);
137         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
138 }
139
140 void cal_edit_attendee_get_name_by_person_id(char *buf, int sz, int person_id)
141 {
142         c_retm_if(!buf, "buf is null");
143         c_retm_if(sz < 1, "size is smaller than 1");
144         c_retm_if(person_id < 1, "person_id is smaller than 1");
145
146         contacts_record_h person = NULL;
147
148         contacts_error_e error = contacts_db_get_record(_contacts_person._uri, person_id, &person);
149         c_retm_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record(%d) is failed(%x)", person_id, error);
150
151         char *display_name = NULL;
152
153         error = contacts_record_get_str_p(person, _contacts_person.display_name, &display_name);
154         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_str_p(%d) is failed(%x)", person_id, error);
155
156         if (CAL_STRLEN(display_name))
157                 snprintf(buf, sz, "%s", display_name);
158
159         error = contacts_record_destroy(person, true);
160         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
161 }
162
163 Eina_Bool cal_edit_attendee_check_email_vailidity(const char* email)
164 {
165         c_retvm_if(!email, EINA_FALSE, "email is null");
166
167         const char* temp = strchr(email, '@');
168         c_retvm_if(!temp, EINA_FALSE, "%s is invalid address", email);
169         c_retvm_if(*(temp+1) == '.', EINA_FALSE, "%s is invalid address", email);
170
171         temp = strchr(temp, '.');
172         c_retvm_if(!temp, EINA_FALSE, "%s is invalid address", email);
173         c_retvm_if(strlen(temp) <= 1, EINA_FALSE, "%s is invalid address", email);
174
175         return EINA_TRUE;
176 }
177
178 int cal_edit_attendee_check_email_is_in_contact(const char *email)
179 {
180         c_retv_if(!CAL_STRLEN(email), 0);
181
182         contacts_error_e error = CONTACTS_ERROR_NONE;
183
184         contacts_query_h query = NULL;
185
186         error = contacts_query_create(_contacts_person_email._uri, &query);
187         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_query_create() is failed(%x)", error);
188
189         contacts_filter_h filter = NULL;
190
191         error = contacts_filter_create(_contacts_person_email._uri, &filter);
192         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_filter_create() is failed(%x)", error);
193
194         error = contacts_filter_add_str(filter, _contacts_person_email.email, CONTACTS_MATCH_CONTAINS, email);
195         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_filter_add_str() is failed(%x)", error);
196
197         error = contacts_query_set_filter(query, filter);
198         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_query_set_filter() is failed(%x)", error);
199
200         contacts_list_h list = NULL;
201
202         error = contacts_db_get_records_with_query(query, 0, 0, &list);
203         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_records_with_query() is failed(%x)", error);
204
205         error = contacts_list_first(list);
206         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_list_first() is failed(%x)", error);
207
208         contacts_record_h person = NULL;
209
210         error = contacts_list_get_current_record_p(list, &person);
211         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_list_get_current_record_p() is failed(%x)", error);
212
213         int person_id = 0;
214
215         error = contacts_record_get_int(person, _contacts_person.id, &person_id);
216         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_int() is failed(%x)", error);
217
218         error = contacts_list_destroy(list, true);
219         c_warn_if(error != CONTACTS_ERROR_NONE, "calendar_list_destroy() is failed(%x)", error);
220
221         error = contacts_filter_destroy(filter);
222         c_warn_if(error != CONTACTS_ERROR_NONE, "calendar_filter_destroy() is failed(%x)", error);
223
224         error = contacts_query_destroy(query);
225         c_warn_if(error != CONTACTS_ERROR_NONE, "calendar_query_destroy() is failed(%x)", error);
226
227         return person_id;
228 }
229
230 static Eina_Bool __cal_edit_attendee_check_list_exist(Eina_List *list, const char *label)
231 {
232         Eina_List* l;
233         cal_participant* part;
234
235         EINA_LIST_FOREACH(list, l, part) {
236                 if(part)
237                         if(!CAL_STRCMP(part->name, label))
238                                 return EINA_TRUE;
239         }
240
241         return EINA_FALSE;
242 }
243
244 void cal_edit_attendee_copy_items_from_mbe(Evas_Object *from, Eina_List **to)
245 {
246         Elm_Object_Item *it;
247         const char *label;
248         cal_participant* part;
249
250         it = elm_multibuttonentry_first_item_get(from);
251         while (it) {
252                 label = elm_object_item_text_get(it);
253                 if (label) {
254                         if (!__cal_edit_attendee_check_list_exist(*to, label)) {
255
256                                 part = elm_object_item_data_get(it);
257                                 c_retm_if(!part, "part is null");
258
259                                 *to = eina_list_append(*to, part);
260                         }
261                 }
262
263                 it = elm_multibuttonentry_item_next_get(it);
264         }
265 }