99ac2b95297101f30f67cce2be01ed4724527a88
[apps/core/preloaded/calendar.git] / ug / detail / detail-common.c
1 /*
2   *
3   *  Copyright 2012  Samsung Electronics Co., Ltd
4   *
5   *  Licensed under the Flora License, Version 1.0 (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 <appsvc.h>
19 #include <bundle.h>
20
21 #include "detail-common.h"
22 #include "cld.h"
23 #include "external-ug.h"
24
25 void cal_detail_common_ctx_callback_for_voice_call(void *data, Evas_Object *obj, void *ei)
26 {
27         c_retm_if(!data, "data is null");
28         c_retm_if(!obj, "obj is null");
29
30         const char *num = data;
31         bundle *b = NULL;
32         char telnum[255]={0,};
33         b = bundle_create();
34         appsvc_set_operation(b, APPSVC_OPERATION_CALL);
35         snprintf(telnum, sizeof(telnum), "tel:%s", num);
36         appsvc_set_uri(b, telnum);
37         appsvc_add_data(b, "ctindex", "-1");
38         appsvc_run_service(b, 0, NULL, NULL);
39
40         bundle_free(b);
41
42         elm_ctxpopup_dismiss(obj);
43 }
44
45 void cal_detail_common_ctx_callback_for_contact(void *data, Evas_Object *obj, void *ei)
46 {
47         c_retm_if(!data, "data is null");
48
49         c_retm_if(!obj, "obj is null");
50
51         const char *email = data;
52         char type[8] = {0};
53
54         snprintf(type, sizeof(type), "%d", 22);
55         cal_launch_ug_with_var(NULL, CAL_CONTACT_UG, NULL, "type", type, "ct_email", email, NULL);
56
57         elm_ctxpopup_dismiss(obj);
58 }
59
60 void cal_detail_common_get_person_default_number(int person_id, char **default_num)
61 {
62         c_ret_if(person_id < 0);
63
64         contacts_error_e error = CONTACTS_ERROR_NONE;
65
66         contacts_record_h person = NULL;
67
68         error = contacts_db_get_record(_contacts_person._uri, person_id, &person);
69         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record() is failed(%x)", error);
70
71         int contact_id = 0;
72
73         error = contacts_record_get_int(person, _contacts_person.display_contact_id, &contact_id);
74         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_int() is failed(%x)", error);
75
76         contacts_record_h contact = NULL;
77
78         error = contacts_db_get_record(_contacts_contact._uri, contact_id, &contact);
79         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_db_get_record() is failed(%x)", error);
80
81         contacts_record_h number = NULL;
82
83         error = contacts_record_get_child_record_at_p(contact, _contacts_contact.number, 0, &number);
84         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_child_record_at_p() is failed(%x)", error);
85
86         char *default_number = NULL;
87
88         error = contacts_record_get_str_p(number, _contacts_number.number, &default_number);
89         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_get_str_p() is failed(%x)", error);
90
91         if (CAL_STRLEN(default_number))
92                 *default_num = strdup(default_number);
93         else
94                 ERR("person_id(%d)'s default number is null", person_id);
95
96         error = contacts_record_destroy(contact, true);
97         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
98
99         error = contacts_record_destroy(person, false);
100         c_warn_if(error != CONTACTS_ERROR_NONE, "contacts_record_destroy() is failed(%x)", error);
101 }
102
103 static void __cal_detail_get_start_time(calendar_record_h record, char *buf, int sz)
104 {
105         c_ret_if(!record);
106
107         const char* time;
108
109         if (is_hour24)
110                 time = CAL_UTIL_TIME_FORMAT_6;
111         else
112                 time = CAL_UTIL_TIME_FORMAT_1;
113
114         struct tm tm = {0};
115
116         calendar_time_s start_time = {0};
117
118         _calendar_get_start_time(record, &start_time);
119
120         _calendar_convert_calendar_time_to_tm(&start_time, &tm);
121
122         if (_calendar_is_allday_record(record))
123                 cal_util_get_time_text(buf, sz, CAL_UTIL_DATE_FORMAT_1, NULL, &tm);
124         else
125                 cal_util_get_time_text(buf, sz, CAL_UTIL_DATE_FORMAT_1, time, &tm);
126 }
127
128 static void __cal_detail_get_end_time(calendar_record_h record, char *buf, int sz)
129 {
130         c_ret_if(!record);
131
132         const char* time;
133
134         if (is_hour24)
135                 time = CAL_UTIL_TIME_FORMAT_6;
136         else
137                 time = CAL_UTIL_TIME_FORMAT_1;
138
139         struct tm tm = {0};
140
141         calendar_time_s end_time = {0};
142
143         _calendar_get_end_time(record, &end_time);
144
145         _calendar_convert_calendar_time_to_tm(&end_time, &tm);
146
147         if (_calendar_is_allday_record(record))
148                 cal_util_get_time_text(buf, sz, CAL_UTIL_DATE_FORMAT_1, NULL, &tm);
149         else
150                 cal_util_get_time_text(buf, sz, CAL_UTIL_DATE_FORMAT_1, time, &tm);
151 }
152
153 static void __cal_detail_common_get_message_content(calendar_record_h record, char *body , int nsize)
154 {
155         c_ret_if(!record);
156         c_ret_if(!body);
157
158         char *content;
159         char *title = NULL;
160         char *location = NULL;
161         char start[1024] = "Start\n";
162         char end[1024] = "End\n";
163         char buf_start[1024] = {0};
164         char buf_end[1024] = {0};
165
166         // TODO:i18n
167         content = _("Check this schedule, please.");
168
169         title = _calendar_get_summary(record);
170
171         location = _calendar_get_location(record);
172
173         __cal_detail_get_start_time(record, buf_start, sizeof(buf_start));
174
175         __cal_detail_get_end_time(record, buf_end, sizeof(buf_end));
176
177         snprintf(body, nsize, "%s\n%s\n%s\n%s%s\n%s%s",content, title, location, start, buf_start, end, buf_end);
178
179         free(title);
180         CAL_FREE(location);
181 }
182
183 void cal_detail_common_ctx_callback_for_message(void *data, Evas_Object *obj, void *ei)
184 {
185         c_ret_if(!data);
186         c_ret_if(!obj);
187
188         calendar_record_h record = evas_object_data_get(obj, "record");
189
190         const char *num = data;
191
192         char message[512] = {0};
193
194         __cal_detail_common_get_message_content(record, message, sizeof(message));
195
196         cal_launch_ug_with_var(NULL, CAL_MESSAGE_COMPOSER_UG, NULL, "TO", num, "BODY", message, NULL);
197
198         elm_ctxpopup_dismiss(obj);
199 }
200
201 void cal_detail_common_ctx_callback_for_email(void *data, Evas_Object *obj, void *ei)
202 {
203         c_ret_if(!data);
204         c_ret_if(!obj);
205
206         calendar_record_h record = evas_object_data_get(obj, "record");
207
208         const char *email = data;
209
210         char message[512] = {0};
211
212         __cal_detail_common_get_message_content(record, message, sizeof(message));
213
214         cal_launch_ug_with_var(NULL, CAL_EMAIL_COMPOSER_UG, NULL, "RUN_TYPE", "5", "TO", email, "BODY", message, NULL);
215
216         elm_ctxpopup_dismiss(obj);
217 }