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