4933581b820a83388f467e74d74a917d3eb0cd79
[framework/uifw/cbhm.git] / 2.3-mobile / src / item_manager.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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
18 #include <notification.h>
19 #include <time.h>
20 #include "item_manager.h"
21 #include "clipdrawer.h"
22
23 static void show_notification(CNP_ITEM *item, const char *msg)
24 {
25         if (item && item->data && item->len > 0)
26                 notification_status_message_post(msg);
27 }
28
29 static void item_free(CNP_ITEM *item, Eina_Bool storage)
30 {
31         CALLED();
32         if (!item)
33         {
34                 ERR("WRONG PARAMETER in %s", __func__);
35                 return;
36         }
37         // remove gengrid
38         if (item->ad)
39         {
40                 if (item->ad->draw_item_del)
41                         item->ad->draw_item_del(item->ad, item);
42                 if (storage && item->ad->storage_item_del)
43                         item->ad->storage_item_del(item->ad, item);
44         }
45
46         if (item->data)
47                 FREE(item->data);
48
49         // When deleting combined style item, image file at the original location
50         // (i.e file at the image path given while creating combined item) should not get deleted.
51         if (item->file && item->gitem_style != GRID_ITEM_STYLE_COMBINED)
52         {
53                 ecore_file_remove(item->file);
54                 FREE(item->file);
55         }
56
57         if (item->ad)
58         {
59                 if (item->ad->clip_selected_item == item)
60                 item->ad->clip_selected_item = NULL;
61         }
62         FREE(item);
63 }
64
65 CNP_ITEM *item_add_by_CNP_ITEM(AppData *ad, CNP_ITEM *item, Eina_Bool storage, Eina_Bool show_msg)
66 {
67         if (!ad || !item)
68         {
69                 ERR("WRONG PARAMETER in %s", __func__);
70                 return NULL;
71         }
72         ClipdrawerData *cd = ad->clipdrawer;
73         if ((item_count_get(ad, ATOM_INDEX_COUNT_ALL) - cd->locked_item_count) == 0)
74                 elm_object_signal_emit(cd->main_layout, "elm,state,enable,del", "elm");
75         if (!item)
76         {
77                 ERR("WRONG PARAMETER in %s, ad: 0x%p, item: 0x%p", __func__, ad, item);
78                 return NULL;
79         }
80         item->ad = ad;
81
82         Eina_Bool duplicated = EINA_FALSE;
83
84         ad->item_list = eina_list_prepend(ad->item_list, item);
85         if (ad && ad->draw_item_add)
86                 duplicated = ad->draw_item_add(ad, item);
87         if (storage && ad && ad->storage_item_add)
88                 ad->storage_item_add(ad, item);
89
90         while (ITEM_CNT_MAX < eina_list_count(ad->item_list))
91         {
92                 CNP_ITEM *ditem = eina_list_nth(ad->item_list, ITEM_CNT_MAX);
93
94                 ad->item_list = eina_list_remove(ad->item_list, ditem);
95                 item_free(ditem, EINA_TRUE);
96         }
97
98         slot_property_set(ad, -1);
99         slot_item_count_set(ad);
100
101         if (show_msg)
102         {
103                 if (duplicated)
104                         show_notification(item, S_EXIST);
105                 else
106                         show_notification(item, S_COPY);
107         }
108
109         return item;
110 }
111
112 static void downloaded_cb(void *data, const char *file_, int status)
113 {
114         CNP_ITEM *item = data;
115
116         if (status == 200)
117         {
118                 item->img_from_web = EINA_TRUE;
119                 DBG("image download success\n");
120         }
121         else
122         {
123                 item->img_from_web = EINA_FALSE;
124                 DBG("image download fail\n");
125         }
126 }
127
128 static Eina_Bool get_network_state()
129 {
130         int net_conf;
131         int net_status;
132         vconf_get_int(VCONFKEY_NETWORK_CONFIGURATION_CHANGE_IND, &net_conf);
133         vconf_get_int(VCONFKEY_NETWORK_STATUS, &net_status);
134         DBG("current network configuration (%d), Network status (%d)\n", net_conf, net_status);
135
136         if (net_conf == 0 && net_status == VCONFKEY_NETWORK_OFF)
137         {
138                 DBG("No wifi and No 3G\n");
139                 return EINA_FALSE;
140         }
141         return EINA_TRUE;
142 }
143
144 static void image_name_get(char *filename)
145 {
146         time_t tim;
147         struct tm *now;
148
149         //file path get
150         tim = time(NULL);
151         now = localtime(&tim);
152
153         if (now)
154         {
155                 sprintf(filename, "fromweb-%d%02d%02d%02d%02d%02d%s",
156                                   now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
157                                   now->tm_hour, now->tm_min, now->tm_sec, ".jpg");
158         }
159         else //localtime returns NULL if some process hold this function
160         {
161                 now = localtime(&tim);
162                 if (now)
163                   sprintf(filename, "fromweb-%d%02d%02d%02d%02d%02d%s",
164                                           now->tm_year + 1900, now->tm_mon + 1, now->tm_mday,
165                                           now->tm_hour, now->tm_min, now->tm_sec, ".jpg");
166                 else
167                   sprintf(filename, "fromweb-%d%02d%02d%02d%02d%02d%s",
168                                          1900, 1, 1, 0, 0, 0,".jpg");
169         }
170 }
171
172 char* html_img_save_frm_local(char *copied_path)
173 {
174         char *html_img_url = NULL;
175         char html_img_name[PATH_MAX];
176         int size_path = 0;
177
178         size_path = snprintf(NULL, 0, "%s", copied_path) + 1;
179         html_img_url = MALLOC(sizeof(char) * size_path);
180         snprintf(html_img_url, size_path, "%s", copied_path);
181         DBG("html_img_url = %s\n", html_img_url);
182
183         image_name_get(html_img_name);
184         DBG("copied file name = %s\n", html_img_name);
185
186         size_path = snprintf(NULL, 0, COPIED_DATA_STORAGE_DIR"/%s", html_img_name) + 1;
187         copied_path = MALLOC(sizeof(char) * size_path);
188         snprintf(copied_path, size_path, COPIED_DATA_STORAGE_DIR"/%s", html_img_name);
189         DBG("copied path = %s\n", copied_path);
190         ecore_file_cp(html_img_url, copied_path);
191         FREE(html_img_url);
192         return copied_path;
193 }
194
195 char* html_img_save(char *copied_path, CNP_ITEM *item)
196 {
197         char *html_img_url = NULL;
198         char html_img_name[PATH_MAX];
199         int size_path = 0;
200         int ret;
201         Eina_Bool is_network_enable;
202
203         is_network_enable = get_network_state();
204         DBG("network enable = %d\n", is_network_enable);
205         if (is_network_enable)
206         {
207                 size_path = snprintf(NULL, 0, "http:/""%s", copied_path) + 1;
208                 html_img_url = MALLOC(sizeof(char) * size_path);
209                 snprintf(html_img_url, size_path, "http:/""%s", copied_path);
210                 DBG("html_img_url = %s\n", html_img_url);
211
212                 image_name_get(html_img_name);
213                 DBG("copied file name = %s\n", html_img_name);
214
215                 size_path = snprintf(NULL, 0, COPIED_DATA_STORAGE_DIR"/%s", html_img_name) + 1;
216                 copied_path = MALLOC(sizeof(char) * size_path);
217                 snprintf(copied_path, size_path, COPIED_DATA_STORAGE_DIR"/%s", html_img_name);
218                 DBG("copied path = %s\n", copied_path);
219                 ret = ecore_file_download_full(html_img_url, copied_path, downloaded_cb, NULL, item, NULL, NULL);
220                 if(ret)
221                         DBG("Download start");
222
223                 FREE(html_img_url);
224                 return copied_path;
225         }
226         return NULL;
227 }
228
229 CNP_ITEM *item_add_by_data(AppData *ad, Ecore_X_Atom type, void *data, int len, Eina_Bool show_msg)
230 {
231         char *entry_text = NULL;
232         char *orig_path = NULL;
233         char *copied_path = NULL;
234         const char *file_name = NULL;
235         int size_path = 0;
236
237         if (!ad || !data)
238         {
239                 ERR("WRONG PARAMETER in %s", __func__);
240                 return NULL;
241         }
242         CNP_ITEM *item;
243         item = CALLOC(1, sizeof(CNP_ITEM));
244
245         if (!item)
246                 return NULL;
247
248         item->type_index = atom_type_index_get(ad, type);
249
250         if (item->type_index == ATOM_INDEX_TEXT)
251                 item->gitem_style = GRID_ITEM_STYLE_TEXT;
252         else if (item->type_index == ATOM_INDEX_HTML)
253         {
254                 copied_path = string_for_image_path_get(ad, ATOM_INDEX_HTML, data);
255                 DBG("found img path in html tags = %s\n", copied_path);
256
257                 if (copied_path && ad->clipdrawer->http_path)
258                         copied_path = html_img_save(copied_path, item);
259                 else if (copied_path)
260                         copied_path = html_img_save_frm_local(copied_path);
261
262                 entry_text = string_for_entry_get(ad, ATOM_INDEX_HTML, data);
263                 DBG("entry_text = %s\n copied_path = %s\n", entry_text, copied_path);
264         }
265         else if (item->type_index == ATOM_INDEX_EFL)
266         {
267                 entry_text = string_for_entry_get(ad, ATOM_INDEX_EFL, data);
268                 copied_path = string_for_image_path_get(ad, ATOM_INDEX_EFL, data);
269         }
270         else if (item->type_index == ATOM_INDEX_IMAGE)
271         {
272                 orig_path = data;
273                 file_name = ecore_file_file_get(orig_path);
274                 size_path = snprintf(NULL, 0, COPIED_DATA_STORAGE_DIR"/%s", file_name) + 1;
275                 copied_path = MALLOC(sizeof(char) * size_path);
276                 if (copied_path)
277                 {
278                         snprintf(copied_path, size_path, COPIED_DATA_STORAGE_DIR"/%s", file_name);
279                         data = copied_path;
280                         len = SAFE_STRLEN(copied_path) + 1;
281                 }
282                 // Reallocate memory for item->file
283                 copied_path = MALLOC(sizeof(char) * len);
284                 if (copied_path)
285                         snprintf(copied_path, len, "%s", (char *)data);
286         }
287
288         if (copied_path)
289         {
290                 item->file = copied_path;
291                 item->file_len = SAFE_STRLEN(copied_path) + 1;
292         }
293
294         if (entry_text && copied_path)
295         {
296                 item->gitem_style = GRID_ITEM_STYLE_COMBINED;
297                 FREE(entry_text);
298         }
299         else if (entry_text)
300         {
301                 item->gitem_style = GRID_ITEM_STYLE_TEXT;
302                 FREE(entry_text);
303         }
304         else if (copied_path)
305                 item->gitem_style = GRID_ITEM_STYLE_IMAGE;
306
307         item->data = data;
308         item->len = len;
309
310         item = item_add_by_CNP_ITEM(ad, item, EINA_TRUE, show_msg);
311
312         if ((item->type_index == ATOM_INDEX_IMAGE) && orig_path && copied_path)
313         {
314                 if (!ecore_file_cp(orig_path, copied_path))
315                         DBG("ecore_file_cp fail!");
316                 FREE(orig_path);
317         }
318
319         // Set vconf value to notify copy event to popsync application
320         vconf_set_str(VCONFKEY_POPSYNC_COPY_EVENT_SET_KEY, "1");
321         return item;
322 }
323
324 CNP_ITEM *item_get_by_index(AppData *ad, int index)
325 {
326         if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
327         {
328                 ERR("WRONG PARAMETER in %s", __func__);
329                 return NULL;
330         }
331         CNP_ITEM *item;
332         item = eina_list_nth(ad->item_list, index);
333         return item;
334 }
335
336 CNP_ITEM *item_get_by_data(AppData *ad, void *data, int len)
337 {
338         CNP_ITEM *item;
339         Eina_List *l;
340
341         if (!ad || !data)
342         {
343                 ERR("WRONG PARAMETER in %s", __func__);
344                 return NULL;
345         }
346
347         EINA_LIST_FOREACH(ad->item_list, l, item)
348         {
349                 if (item && (item->len == len) && (!SAFE_STRNCMP(item->data, data, len)))
350                         return item;
351         }
352
353         return NULL;
354 }
355
356 CNP_ITEM *item_get_last(AppData *ad)
357 {
358         if (!ad)
359         {
360                 ERR("WRONG PARAMETER in %s", __func__);
361                 return NULL;
362         }
363         return eina_list_data_get(ad->item_list);
364 }
365
366 void item_delete_by_CNP_ITEM(AppData *ad, CNP_ITEM *item)
367 {
368         CALLED();
369         ClipdrawerData *cd = ad->clipdrawer;
370
371         if (!item)
372         {
373                 ERR("WRONG PARAMETER in %s", __func__);
374                 return;
375         }
376         ad->item_list = eina_list_remove(ad->item_list, item);
377         item_free(item, EINA_TRUE);
378         slot_property_set(ad, -1);
379         slot_item_count_set(ad);
380
381         if (item_count_get(ad, ATOM_INDEX_COUNT_ALL) == 0)
382                 _delete_mode_set(ad, EINA_FALSE);
383 }
384
385 void item_delete_by_data(AppData *ad, void *data, int len)
386 {
387         CALLED();
388         if (!ad || !data)
389         {
390                 ERR("WRONG PARAMETER in %s", __func__);
391                 return;
392         }
393         CNP_ITEM *item;
394         item = item_get_by_data(ad, data, len);
395         item_delete_by_CNP_ITEM(ad, item);
396 }
397
398 void item_delete_by_index(AppData *ad, int index)
399 {
400         CALLED();
401         if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
402         {
403                 ERR("WRONG PARAMETER in %s", __func__);
404                 return;
405         }
406         CNP_ITEM *item;
407         item = item_get_by_index(ad, index);
408         item_delete_by_CNP_ITEM(ad, item);
409 }
410
411 void item_clear_all(AppData *ad)
412 {
413         CALLED();
414         while(ad->item_list)
415         {
416                 CNP_ITEM *item = eina_list_data_get(ad->item_list);
417                 ad->item_list = eina_list_remove(ad->item_list, item);
418                 if (item)
419                         item_free(item, EINA_FALSE);
420         }
421 }
422
423 int item_count_get(AppData *ad, int atom_index)
424 {
425         int icount = 0;
426         Eina_List *l;
427         CNP_ITEM *item;
428
429         if (!ad || !ad->item_list) return 0;
430
431         // Get the number of all items
432         if (atom_index == ATOM_INDEX_COUNT_ALL)
433         {
434                 icount = eina_list_count(ad->item_list);
435         }
436         // Get the number of text type items
437         else if (atom_index == ATOM_INDEX_COUNT_TEXT)
438         {
439                 EINA_LIST_FOREACH(ad->item_list, l, item)
440                 {
441                         if (item &&
442                                  (item->type_index == ATOM_INDEX_TEXT ||
443                                   item->type_index == ATOM_INDEX_HTML ||
444                                   item->type_index == ATOM_INDEX_EFL))
445                                 icount++;
446                 }
447         }
448         // Get the number of image type items
449         else if (atom_index == ATOM_INDEX_COUNT_IMAGE)
450         {
451                 EINA_LIST_FOREACH(ad->item_list, l, item)
452                 {
453                         if (item &&
454                                  (item->type_index == ATOM_INDEX_IMAGE))
455                                 icount++;
456                 }
457         }
458
459         return icount;
460 }