Tizen 2.0 Release
[profile/ivi/memo.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 #include "memo_log.h"
28
29 static void _str_append_utf8(char *s, int size, const char *utf8)
30 {
31     char *content = NULL;
32     content = elm_entry_utf8_to_markup(utf8);
33     RETIF(content == NULL);
34     sncat(s,size, content);
35     SFREE(content);
36 }
37 /**
38  * memo_gl_label_get
39  *
40  * @brief
41  *
42  * @param   [in] data   user specified data
43  *
44  * @param   [in] obj    evas_object of elm_genlist_item
45  *
46  * @param   [in] part   part name in genlist group
47  *
48  * @return
49  *
50  * @exception    None
51  *
52  * @remark       None
53  *
54  * @see
55  *
56  */
57 char *memo_gl_label_get(void *data, Evas_Object *obj, const char *part)
58 {
59     MEMO_FUN_BEG();
60     gl_data_t *gld = (gl_data_t *)data;
61     char buf[MEMO_BUFFER_SIZE] = {0};
62     char input[MEMO_BUFFER_SIZE] = {0};
63     char output[MEMO_BUFFER_SIZE] = {0};
64     const char *p = NULL;
65     const char *pre = NULL;
66
67     if (!strcmp(part, "elm.text.date")) {
68         memo_time_format(buf, MEMO_BUFFER_SIZE, gld->mod_time);
69         MEMO_FUN_END();
70         return strdup(buf);
71     } else if (!strcmp(part, "elm.slide.text.1")) {
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         MEMO_FUN_END();
77         return strdup(input);
78     } else if (!strcmp(part, "elm.text")) {
79         memo_data_t *md = memo_get_data(gld->index);
80         /* limit input */
81         if (md->comment != NULL) {
82             snprintf(input, MEMO_BUFFER_SIZE, "%s", md->comment);
83         } else {
84             snprintf(input, MEMO_BUFFER_SIZE, "%s", md->content);
85         }
86         /* truncate to single line */
87         p = strstr(input, "\342\200\251");
88         if (p != NULL) {
89             input[p-input] = '\0';
90         }
91         /* font color information */
92         unsigned char *color = (unsigned char *)&md->font_color;
93         snprintf(output, MEMO_BUFFER_SIZE, "<color=#%02x%02x%02x>", color[2], color[1], color[0]);
94         /* append text information */
95         if (gld->search == NULL) {
96             _str_append_utf8(output, MEMO_BUFFER_SIZE, input);
97         } else {
98             pre = input;
99             /* search and composite */
100             p = (const char *)strcasestr(pre, gld->search);
101             if (p != NULL) {
102                 /* append characters before matched string */
103                 if (p != pre) {
104                     buf[0]='\0';
105                     strncat(buf, pre, p-pre);
106                     _str_append_utf8(output, MEMO_BUFFER_SIZE, buf);
107                 }
108                 /* highlight str */
109                 sncat(output, MEMO_BUFFER_SIZE, "<color=#3F8DAD>");
110                 buf[0]='\0';
111                 strncat(buf, p, strlen(gld->search));
112                 _str_append_utf8(output, MEMO_BUFFER_SIZE, buf);
113                 sncat(output, MEMO_BUFFER_SIZE, "</color>");
114                 /* set pointer after matched string */
115                 pre = p+strlen(gld->search);
116             }
117             /* append remaining string */
118             _str_append_utf8(output, MEMO_BUFFER_SIZE, pre);
119         }
120         memo_free_data(md);
121         MEMO_FUN_END();
122         return strdup(output);
123     }
124     MEMO_FUN_END();
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     MEMO_FUN_BEG();
151     gl_data_t *gld = (gl_data_t *)data;
152     char buf[MEMO_BUFFER_SIZE];
153     Evas_Object *eo = NULL;
154
155     if (!strcmp(part, "elm.swallow.doodle")) {
156         if (gld->has_doodle != 0) {
157             snprintf(buf, MEMO_BUFFER_SIZE, DOODLEDIR "/%d.png", gld->index);
158             eo = elm_icon_create(obj, buf);
159         }
160     } else if (!strcmp(part, "elm.swallow.check")) {
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     } else if (!strcmp(part, "elm.slide.swallow.1")) {
167         eo = elm_button_create(obj, MEMO_I18N_DELETE, gld->on_delete, gld);
168     } else if (!strcmp(part, "elm.slide.swallow.2")) {
169         eo = elm_button_create(obj, MEMO_I18N_SHARE, gld->on_send, gld);
170     }
171     MEMO_FUN_END();
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