2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <notification.h>
20 #include "item_manager.h"
21 #include "clipdrawer.h"
23 static void show_notification(CNP_ITEM *item, const char *msg)
25 if (item && item->data && item->len > 0)
26 notification_status_message_post(msg);
29 static void item_free(CNP_ITEM *item, Eina_Bool storage)
34 ERR("WRONG PARAMETER in %s", __func__);
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);
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)
53 ecore_file_remove(item->file);
59 if (item->ad->clip_selected_item == item)
60 item->ad->clip_selected_item = NULL;
65 CNP_ITEM *item_add_by_CNP_ITEM(AppData *ad, CNP_ITEM *item, Eina_Bool storage, Eina_Bool show_msg)
69 ERR("WRONG PARAMETER in %s", __func__);
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");
77 ERR("WRONG PARAMETER in %s, ad: 0x%p, item: 0x%p", __func__, ad, item);
82 Eina_Bool duplicated = EINA_FALSE;
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);
90 while (ITEM_CNT_MAX < eina_list_count(ad->item_list))
92 CNP_ITEM *ditem = eina_list_nth(ad->item_list, ITEM_CNT_MAX);
94 ad->item_list = eina_list_remove(ad->item_list, ditem);
95 item_free(ditem, EINA_TRUE);
98 slot_property_set(ad, -1);
99 slot_item_count_set(ad);
104 show_notification(item, S_EXIST);
106 show_notification(item, S_COPY);
112 static void downloaded_cb(void *data, const char *file_, int status)
114 CNP_ITEM *item = data;
118 item->img_from_web = EINA_TRUE;
119 DBG("image download success\n");
123 item->img_from_web = EINA_FALSE;
124 DBG("image download fail\n");
128 static Eina_Bool get_network_state()
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);
136 if (net_conf == 0 && net_status == VCONFKEY_NETWORK_OFF)
138 DBG("No wifi and No 3G\n");
144 static void image_name_get(char *filename)
151 now = localtime(&tim);
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");
159 else //localtime returns NULL if some process hold this function
161 now = localtime(&tim);
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");
167 sprintf(filename, "fromweb-%d%02d%02d%02d%02d%02d%s",
168 1900, 1, 1, 0, 0, 0,".jpg");
172 char* html_img_save_frm_local(char *copied_path)
174 char *html_img_url = NULL;
175 char html_img_name[PATH_MAX];
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);
183 image_name_get(html_img_name);
184 DBG("copied file name = %s\n", html_img_name);
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);
195 char* html_img_save(char *copied_path, CNP_ITEM *item)
197 char *html_img_url = NULL;
198 char html_img_name[PATH_MAX];
201 Eina_Bool is_network_enable;
203 is_network_enable = get_network_state();
204 DBG("network enable = %d\n", is_network_enable);
205 if (is_network_enable)
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);
212 image_name_get(html_img_name);
213 DBG("copied file name = %s\n", html_img_name);
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);
221 DBG("Download start");
229 CNP_ITEM *item_add_by_data(AppData *ad, Ecore_X_Atom type, void *data, int len, Eina_Bool show_msg)
231 char *entry_text = NULL;
232 char *orig_path = NULL;
233 char *copied_path = NULL;
234 const char *file_name = NULL;
239 ERR("WRONG PARAMETER in %s", __func__);
243 item = CALLOC(1, sizeof(CNP_ITEM));
248 item->type_index = atom_type_index_get(ad, type);
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)
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);
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);
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);
265 else if (item->type_index == ATOM_INDEX_EFL)
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);
270 else if (item->type_index == ATOM_INDEX_IMAGE)
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);
278 snprintf(copied_path, size_path, COPIED_DATA_STORAGE_DIR"/%s", file_name);
280 len = SAFE_STRLEN(copied_path) + 1;
282 // Reallocate memory for item->file
283 copied_path = MALLOC(sizeof(char) * len);
285 snprintf(copied_path, len, "%s", (char *)data);
290 item->file = copied_path;
291 item->file_len = SAFE_STRLEN(copied_path) + 1;
294 if (entry_text && copied_path)
296 item->gitem_style = GRID_ITEM_STYLE_COMBINED;
301 item->gitem_style = GRID_ITEM_STYLE_TEXT;
304 else if (copied_path)
305 item->gitem_style = GRID_ITEM_STYLE_IMAGE;
310 item = item_add_by_CNP_ITEM(ad, item, EINA_TRUE, show_msg);
312 if ((item->type_index == ATOM_INDEX_IMAGE) && orig_path && copied_path)
314 if (!ecore_file_cp(orig_path, copied_path))
315 DBG("ecore_file_cp fail!");
319 // Set vconf value to notify copy event to popsync application
320 vconf_set_str(VCONFKEY_POPSYNC_COPY_EVENT_SET_KEY, "1");
324 CNP_ITEM *item_get_by_index(AppData *ad, int index)
326 if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
328 ERR("WRONG PARAMETER in %s", __func__);
332 item = eina_list_nth(ad->item_list, index);
336 CNP_ITEM *item_get_by_data(AppData *ad, void *data, int len)
343 ERR("WRONG PARAMETER in %s", __func__);
347 EINA_LIST_FOREACH(ad->item_list, l, item)
349 if (item && (item->len == len) && (!SAFE_STRNCMP(item->data, data, len)))
356 CNP_ITEM *item_get_last(AppData *ad)
360 ERR("WRONG PARAMETER in %s", __func__);
363 return eina_list_data_get(ad->item_list);
366 void item_delete_by_CNP_ITEM(AppData *ad, CNP_ITEM *item)
369 ClipdrawerData *cd = ad->clipdrawer;
373 ERR("WRONG PARAMETER in %s", __func__);
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);
381 if (item_count_get(ad, ATOM_INDEX_COUNT_ALL) == 0)
382 _delete_mode_set(ad, EINA_FALSE);
385 void item_delete_by_data(AppData *ad, void *data, int len)
390 ERR("WRONG PARAMETER in %s", __func__);
394 item = item_get_by_data(ad, data, len);
395 item_delete_by_CNP_ITEM(ad, item);
398 void item_delete_by_index(AppData *ad, int index)
401 if (!ad || eina_list_count(ad->item_list) <= index || 0 > index)
403 ERR("WRONG PARAMETER in %s", __func__);
407 item = item_get_by_index(ad, index);
408 item_delete_by_CNP_ITEM(ad, item);
411 void item_clear_all(AppData *ad)
416 CNP_ITEM *item = eina_list_data_get(ad->item_list);
417 ad->item_list = eina_list_remove(ad->item_list, item);
419 item_free(item, EINA_FALSE);
423 int item_count_get(AppData *ad, int atom_index)
429 if (!ad || !ad->item_list) return 0;
431 // Get the number of all items
432 if (atom_index == ATOM_INDEX_COUNT_ALL)
434 icount = eina_list_count(ad->item_list);
436 // Get the number of text type items
437 else if (atom_index == ATOM_INDEX_COUNT_TEXT)
439 EINA_LIST_FOREACH(ad->item_list, l, item)
442 (item->type_index == ATOM_INDEX_TEXT ||
443 item->type_index == ATOM_INDEX_HTML ||
444 item->type_index == ATOM_INDEX_EFL))
448 // Get the number of image type items
449 else if (atom_index == ATOM_INDEX_COUNT_IMAGE)
451 EINA_LIST_FOREACH(ad->item_list, l, item)
454 (item->type_index == ATOM_INDEX_IMAGE))