Tizen 2.0 Release
[apps/home/ug-memo-efl.git] / src / memo-genlist.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
19 #include <Elementary.h>
20 #include <gravel.h>
21 #include <extended-elm.h>
22 #include <memo-db.h>
23 #include <appcore-common.h>
24 #include <memo-assist.h>
25 #include <memo_string.h>
26 #include <memo-genlist.h>
27
28 static void _str_append_utf8(char *s, int size, const char *utf8)
29 {
30     char *content = NULL;
31     content = elm_entry_utf8_to_markup(utf8);
32     RETIF(content == NULL);
33     sncat(s,size, content);
34     SFREE(content);
35 }
36 /**
37  * memo_gl_label_get
38  *
39  * @brief
40  *
41  * @param   [in] data   user specified data
42  *
43  * @param   [in] obj    evas_object of elm_genlist_item
44  *
45  * @param   [in] part   part name in genlist group
46  *
47  * @return
48  *
49  * @exception    None
50  *
51  * @remark       None
52  *
53  * @see
54  *
55  */
56 char *memo_gl_label_get(void *data, Evas_Object *obj, const char *part)
57 {
58     gl_data_t *gld = (gl_data_t *)data;
59     char buf[MEMO_BUFFER_SIZE] = {0};
60     char input[MEMO_BUFFER_SIZE] = {0};
61     char output[MEMO_BUFFER_SIZE] = {0};
62     const char *p = NULL;
63     const char *pre = NULL;
64
65     if (!strcmp(part, "elm.text.date")) {
66         memo_time_format(buf, MEMO_BUFFER_SIZE, gld->mod_time);
67         return strdup(buf);
68     } else if (!strcmp(part, "elm.slide.text.1")) {
69         if (gld->index == -1) {
70             return strdup(MEMO_I18N_WELCOME_TO_TIZEN_MEMO);
71         } else {
72             memo_data_t *md = memo_get_data(gld->index);
73             snprintf(input, MEMO_BUFFER_SIZE, "%s",
74                 (md->comment == NULL ? md->content : md->comment)); /* limit input */
75             memo_free_data(md);
76             return strdup(input);
77         }
78     } else if (!strcmp(part, "elm.text")) {
79         if (gld->index == -1) {
80             return strdup(MEMO_I18N_WELCOME_TO_TIZEN_MEMO);
81         } else {
82             memo_data_t *md = memo_get_data(gld->index);
83             /* limit input */
84             if (md->comment != NULL) {
85                 snprintf(input, MEMO_BUFFER_SIZE, "%s", md->comment);
86             } else {
87                 snprintf(input, MEMO_BUFFER_SIZE, "%s", md->content);
88             }
89             /* truncate to single line */
90             p = strstr(input, "\342\200\251");
91             if (p != NULL) {
92                 input[p-input] = '\0';
93             }
94             /* font color information */
95             unsigned char *color = (unsigned char *)&md->font_color;
96             snprintf(output, MEMO_BUFFER_SIZE, "<color=#%02x%02x%02x>", color[2], color[1], color[0]);
97             /* append text information */
98             if (gld->search == NULL) {
99                 _str_append_utf8(output, MEMO_BUFFER_SIZE, input);
100             } else {
101                 pre = input;
102                 /* search and composite */
103                 p = (const char *)strcasestr(pre, gld->search);
104                 if (p != NULL) {
105                     /* append characters before matched string */
106                     if (p != pre) {
107                         buf[0]='\0';
108                         strncat(buf, pre, p-pre);
109                         _str_append_utf8(output, MEMO_BUFFER_SIZE, buf);
110                     }
111                     /* highlight str */
112                     sncat(output, MEMO_BUFFER_SIZE, "<color=#FF0000>");
113                     buf[0]='\0';
114                     strncat(buf, p, strlen(gld->search));
115                     _str_append_utf8(output, MEMO_BUFFER_SIZE, buf);
116                     sncat(output, MEMO_BUFFER_SIZE, "</color>");
117                     /* set pointer after matched string */
118                     pre = p+strlen(gld->search);
119                 }
120                 /* append remaining string */
121                 _str_append_utf8(output, MEMO_BUFFER_SIZE, pre);
122             }
123             memo_free_data(md);
124             return strdup(output);
125         }
126     }
127     return NULL;
128 }
129
130 /**
131  * memo_gl_icon_get
132  *
133  * @brief
134  *
135  * @param   [in] data   user specified data
136  *
137  * @param   [in] obj    evas_object of elm_genlist_item
138  *
139  * @param   [in] part   part name in genlist group
140  *
141  * @return
142  *
143  * @exception    None
144  *
145  * @remark       None
146  *
147  * @see
148  *
149  */
150 Evas_Object *memo_gl_icon_get(void *data, Evas_Object *obj, const char *part)
151 {
152     gl_data_t *gld = (gl_data_t *)data;
153     char buf[MEMO_BUFFER_SIZE];
154     Evas_Object *eo = NULL;
155
156     if (!strcmp(part, "elm.swallow.doodle")) {
157         if (gld->has_doodle != 0) {
158             snprintf(buf, MEMO_BUFFER_SIZE, DOODLEDIR "/%d.png", gld->index);
159             eo = elm_icon_create(obj, buf);
160         }
161     } else if (!strcmp(part, "elm.swallow.check")) {
162         if (gld->index != -1) {    /* only add checkbox for non-sentinel data, as required by CQ defect H0100123202 */
163             eo = elm_check_add(obj);
164             elm_check_state_pointer_set(eo, &gld->check);
165             if (gld->on_change != NULL) {
166                 evas_object_smart_callback_add(eo, "changed", gld->on_change, gld);
167             }
168         }
169     } else if (!strcmp(part, "elm.slide.swallow.1")) {
170         eo = elm_button_create(obj, MEMO_I18N_DELETE, gld->on_delete, gld);
171     } else if (!strcmp(part, "elm.slide.swallow.2")) {
172         eo = elm_button_create(obj, MEMO_I18N_SHARE, gld->on_send, gld);
173     }
174     return eo;
175 }
176
177 Eina_Bool memo_gl_state_get(void *data, Evas_Object *obj, const char *part)
178 {
179     return EINA_FALSE;
180 }
181
182 /**
183  * memo_gl_del
184  *
185  * @brief
186  *
187  * @param   [in] data   user specified data
188  *
189  * @param   [in] obj    evas_object of elm_genlist_item
190  *
191  * @return
192  *
193  * @exception    None
194  *
195  * @remark       None
196  *
197  * @see
198  *
199  */
200 void memo_gl_del(void *data, Evas_Object *obj)
201 {
202     gl_data_t *gld = (gl_data_t *)data;
203     SFREE(gld);
204     return;
205 }
206
207 /**
208  * memo_gl_itc_init
209  *
210  * @brief
211  *
212  * @param   [in] itc    handle of Elm_Genlist_Item_Class
213  *
214  * @param   [in] style
215  *
216  * @return
217  *
218  * @exception    None
219  *
220  * @remark       None
221  *
222  * @see
223  *
224  */
225 void memo_gl_itc_init(Elm_Genlist_Item_Class *itc, const char *style)
226 {
227     itc->item_style = style;
228     itc->func.text_get = memo_gl_label_get;
229     itc->func.content_get = memo_gl_icon_get;
230     itc->func.state_get = memo_gl_state_get;
231     itc->func.del = memo_gl_del;
232 }
233
234 /**
235  * memo_gld_init
236  *
237  * @brief
238  *
239  * @param   [in] data   handle of gl_data_t
240  *
241  * @param   [in] md     original memo record data
242  *
243  * @param   [in] user_data user specified data
244  *
245  * @param   [in] on_select callback function when genlist item is selected
246  *
247  * @return
248  *
249  * @exception    None
250  *
251  * @remark       None
252  *
253  * @see
254  *
255  */
256 void memo_gld_init(gl_data_t *data, memo_data_t *md, void *user_data, Evas_Smart_Cb on_select)
257 {
258     data->index = md->id;
259     data->has_doodle = md->has_doodle;
260     data->mod_time = md->modi_time;
261     data->on_select = on_select;
262     data->user_data = user_data;
263 }
264
265 /**
266  * memo_gld_sentinel_create
267  *
268  * @brief
269  *
270  * @param   [in] user_data
271  *
272  * @return
273  *
274  * @exception    None
275  *
276  * @remark       None
277  *
278  * @see
279  *
280  */
281 gl_data_t * memo_gld_sentinel_create(void *user_data)
282 {
283     gl_data_t *data = SMALLOC(gl_data_t);
284     RETVIF(data == NULL, NULL);
285     data->index = -1;
286     data->mod_time = memo_get_binary_release_date();
287     data->user_data = user_data;
288     return data;
289 }
290