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