2 This file is part of Telegram application for tizen
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "tg_common.h"
23 #include "tg_db_wrapper.h"
24 #include <media_content.h>
25 #include "tg_start_messaging_view.h"
26 #include "tg_search_peer_view.h"
27 #include "tg_settings_view.h"
28 #include "tg_add_contact.h"
30 uint64_t get_time_stamp_in_macro()
33 gettimeofday(&tv,NULL);
34 return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
37 void wait_for(unsigned int secs)
39 int retTime = time(0) + secs;
40 while (time(0) < retTime);
43 void app_get_resource(const char *edj_file_in, char *edj_path_out, int edj_path_max)
45 char *res_path = app_get_resource_path();
47 snprintf(edj_path_out, edj_path_max, "%s%s", res_path, edj_file_in);
52 Eina_Bool compare_date_with_current_date(int rtime)
55 const char *format = " %d/%b/%Y";
56 time_t local_t = time(NULL);
59 (void) localtime_r(&local_t, <);
60 if (strftime(local_res, sizeof(local_res), format, <) == 0) {
65 time_t remote_t = rtime;
68 (void) localtime_r(&remote_t, &rt);
70 if (strftime(remote_res, sizeof(remote_res), format, &rt) == 0) {
73 if (strcasecmp (local_res, remote_res) == 0) {
79 Evas_Object* create_button(Evas_Object *parent, char *style, char *text)
83 button = elm_button_add(parent);
85 elm_object_style_set(button, style);
87 elm_object_text_set(button, text);
88 evas_object_show(button);
93 Evas_Object* create_circle_button(Evas_Object *parent, char* text, char* filepath)
95 Evas_Object *button, *image;
96 button= elm_button_add(parent);
97 elm_object_style_set(button, "circle");
99 elm_object_text_set(button, text);
100 image = elm_image_add(button);
101 elm_image_file_set(image, filepath, NULL);
102 elm_image_resizable_set(image, EINA_TRUE, EINA_TRUE);
103 elm_object_part_content_set(button, "icon", image);
104 evas_object_smart_callback_add(button, "clicked", NULL, NULL);
105 evas_object_show(button);
109 char* get_table_name_from_number(const int id)
112 snprintf(id_str, sizeof(id_str), "tg_%d_msg", id);
113 return strdup(id_str);
116 char* get_budy_state(appdata_s* ad, int buddy_id)
120 char time_str[256]={0,};
121 Eina_List *buddy_details_array = get_buddy_info(buddy_id);
122 if (buddy_details_array && eina_list_count(buddy_details_array) > 0) {
123 Eina_List *buddy_details = eina_list_nth(buddy_details_array, 0);
124 if (buddy_details && eina_list_count(buddy_details) > 0) {
125 int* temp_online = (int *)eina_list_nth(buddy_details, 12);
126 int is_online = *temp_online;
127 int* temp_last_seen = (int *)eina_list_nth(buddy_details, 13);
128 int last_seen = *temp_last_seen;
131 Eina_Bool is_today = compare_date_with_current_date(last_seen);
134 strcpy(time_str, i18n_get_text("IDS_TGRAM_SBODY_ONLINE"));
136 time_t t = last_seen;
139 format = "last seen Today at %I:%M %P";
141 format = "last seen %d/%b/%Y at %I:%M %P";
146 (void) localtime_r(&t, <);
148 if (strftime(res, sizeof(res), format, <) == 0) {
149 (void) fprintf(stderr, "strftime(3): cannot format supplied "
150 "date/time into buffer of size %u "
152 sizeof(res), format);
154 snprintf(time_str, sizeof(time_str), "<align=left><font_size=30><color=#808080>%s</color></font_size></align>", res);
158 EINA_LIST_FREE(buddy_details,val) {
163 eina_list_free(buddy_details_array);
165 return strdup(time_str);
168 int numbers_only(const char *s)
173 static bool mp_util_svc_iterate_for_get_video_item_cb(media_info_h item, void *user_data)
180 static bool __thumbnail_cb(media_info_h item, void *user_data)
182 char **thumbnail_path = (char **) user_data;
184 if(thumbnail_path == NULL)
187 if (media_info_get_thumbnail_path(item, thumbnail_path) != MEDIA_CONTENT_ERROR_NONE) {
190 if (*thumbnail_path) {
195 Eina_Bool get_thumbnail_from_video_url(const char *file_path, char **thumbnail_path)
197 if (thumbnail_path == NULL) {
200 char condition[512 + 1] = { '\0', };
202 //snprintf(condition, 512, "%s ", file_path);
203 snprintf(condition, 512, "%s = \'%s\'", MEDIA_PATH, (char *)file_path);
204 ret = media_content_connect();
206 filter_h filter = NULL;
207 ret = media_filter_create(&filter);
208 if(ret != MEDIA_CONTENT_ERROR_NONE) {
212 ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
213 if (ret != MEDIA_CONTENT_ERROR_NONE) {
217 char *temp_thumbnail_path = NULL;
218 ret = media_info_foreach_media_from_db(filter, __thumbnail_cb, &temp_thumbnail_path);
219 if (ret != MEDIA_CONTENT_ERROR_NONE) {
222 *thumbnail_path = temp_thumbnail_path;
224 media_filter_destroy(filter);
227 ret = media_content_disconnect();
232 media_filter_destroy(filter);
235 ret = media_content_disconnect();
239 void show_loading_popup(appdata_s* ad)
244 if (ad->loading_popup) {
245 evas_object_del(ad->loading_popup);
246 ad->loading_popup = NULL;
250 Evas_Object *progressbar;
253 ad->loading_popup = elm_popup_add(ad->win);
254 elm_popup_align_set(ad->loading_popup, ELM_NOTIFY_ALIGN_FILL, 1.0);
255 evas_object_size_hint_weight_set(ad->loading_popup, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
258 layout = elm_layout_add(ad->loading_popup);
259 char edj_path[PATH_MAX] = {0, };
260 app_get_resource(TELEGRAM_POPUP_VIEW_EDJ, edj_path, (int)PATH_MAX);
261 elm_layout_file_set(layout, edj_path, "processing_view_layout");
262 evas_object_size_hint_weight_set(layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
263 elm_object_part_text_set(layout, "elm.text", i18n_get_text("IDS_COM_BODY_LOADING"));
264 elm_object_domain_translatable_part_text_set(layout, "elm.text", NULL, "IDS_COM_BODY_LOADING");
265 progressbar = elm_progressbar_add(layout);
266 elm_object_style_set(progressbar, "process_medium");
267 evas_object_size_hint_align_set(progressbar, EVAS_HINT_FILL, 0.5);
268 evas_object_size_hint_weight_set(progressbar, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
269 elm_progressbar_pulse(progressbar, EINA_TRUE);
270 elm_object_part_content_set(layout, "processing", progressbar);
272 elm_object_content_set(ad->loading_popup, layout);
274 evas_object_show(ad->loading_popup);
278 void hide_loading_popup(appdata_s* ad)
283 if (ad->loading_popup) {
284 evas_object_del(ad->loading_popup);
285 ad->loading_popup = NULL;
289 void launch_app_control(appdata_s *ad, char *media_type, char *url)
291 if (!media_type || !url) {
294 app_control_h app_control;
295 int ret = app_control_create(&app_control);
296 if(ret != APP_CONTROL_ERROR_NONE) {
297 show_toast(ad, "Error: Can not load image");
300 app_control_set_operation(app_control, APP_CONTROL_OPERATION_VIEW);
301 if (strstr(media_type, "video") != NULL) {
302 app_control_set_mime(app_control,"video/*");
303 } else if (strstr(media_type, "audio") != NULL) {
304 app_control_set_mime(app_control,"audio/*");
305 } else if (strstr(media_type, "image") != NULL) {
306 app_control_set_mime(app_control,"image/*");
307 } else if (strstr(media_type, "location") != NULL) {
308 app_control_set_app_id(app_control, "com.samsung.browser");
310 app_control_set_uri(app_control, url);
313 int nRet = app_control_send_launch_request(app_control, NULL, NULL);
314 if(nRet != APP_CONTROL_ERROR_NONE) {
315 if(nRet == APP_CONTROL_ERROR_INVALID_PARAMETER) {
316 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_INVALID_PARAMETER [0x%x]", nRet);
318 else if(nRet == APP_CONTROL_ERROR_OUT_OF_MEMORY ) {
319 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_OUT_OF_MEMORY [0x%x]", nRet);
321 else if(nRet == APP_CONTROL_ERROR_APP_NOT_FOUND ) {
322 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_APP_NOT_FOUND [0x%x]", nRet);
324 else if(nRet == APP_CONTROL_ERROR_KEY_NOT_FOUND ) {
325 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_KEY_NOT_FOUND [0x%x]", nRet);
327 else if(nRet == APP_CONTROL_ERROR_KEY_REJECTED ) {
328 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_KEY_REJECTED [0x%x]", nRet);
330 else if(nRet == APP_CONTROL_ERROR_INVALID_DATA_TYPE ) {
331 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_INVALID_DATA_TYPE [0x%x]", nRet);
333 else if(nRet == APP_CONTROL_ERROR_LAUNCH_REJECTED ) {
334 printf("app_control_send_launch_request is fail APP_CONTROL_ERROR_LAUNCH_REJECTED [0x%x]", nRet);
337 app_control_destroy(app_control);
341 extern void free_message(tg_message_s **message)
343 if (message == NULL || *message == NULL) {
347 if ((*message)->message) {
348 free((*message)->message);
349 (*message)->message = NULL;
352 if ((*message)->media_id) {
353 free((*message)->media_id);
354 (*message)->media_id = NULL;
361 void free_user_data(user_data_s *user_data)
367 if (user_data->print_name) {
368 free(user_data->print_name);
369 user_data->print_name = NULL;
372 if (user_data->first_name) {
373 free(user_data->first_name);
374 user_data->first_name = NULL;
377 if (user_data->last_name) {
378 free(user_data->last_name);
379 user_data->last_name = NULL;
382 if (user_data->phone) {
383 free(user_data->phone);
384 user_data->phone = NULL;
387 if (user_data->real_first_name) {
388 free(user_data->real_first_name);
389 user_data->real_first_name = NULL;
391 if (user_data->real_last_name) {
392 free(user_data->real_last_name);
393 user_data->real_last_name = NULL;
395 if (user_data->username) {
396 free(user_data->username);
397 user_data->username = NULL;
405 void tg_notification_create(appdata_s *app_data, char * icon_path, const char *title, char *content, char *sound_path, char *app_id)
407 if (app_data && app_data->s_notififcation) {
408 //err = notification_delete(app_data->s_notififcation);
409 notification_delete_all(NOTIFICATION_TYPE_NOTI);
410 app_data->s_notififcation = NULL;
413 notification_error_e ret = NOTIFICATION_ERROR_NONE;
414 app_data->s_notififcation = notification_create(NOTIFICATION_TYPE_NOTI);
415 ret = notification_set_property(app_data->s_notififcation, NOTIFICATION_PROP_DISABLE_TICKERNOTI);
416 ret = notification_set_layout(app_data->s_notififcation, NOTIFICATION_LY_NOTI_EVENT_SINGLE);
419 ret = notification_set_image(app_data->s_notififcation, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
422 ret = notification_set_text(app_data->s_notififcation, NOTIFICATION_TEXT_TYPE_TITLE, title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
425 ret = notification_set_text(app_data->s_notififcation, NOTIFICATION_TEXT_TYPE_CONTENT, content, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
429 ret = notification_set_sound(app_data->s_notififcation, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path);
431 ret = notification_set_sound(app_data->s_notififcation, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL);
433 ret = notification_set_vibration(app_data->s_notififcation, NOTIFICATION_VIBRATION_TYPE_DEFAULT, NULL);
434 if (ret != NOTIFICATION_ERROR_NONE) {
437 app_control_h service = NULL;
438 app_control_create(&service);
439 app_control_set_app_id(service, app_id);
440 app_control_set_operation(service, APP_CONTROL_OPERATION_DEFAULT);
442 //notification_update()
444 ret = notification_set_launch_option(app_data->s_notififcation, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, service);
445 if (ret != NOTIFICATION_ERROR_NONE) {
448 notification_post(app_data->s_notififcation);
449 app_control_destroy(service);
450 ret = notification_free(app_data->s_notififcation);
451 if(ret != NOTIFICATION_ERROR_NONE) {
456 void on_new_message_clicked(void *data, Evas_Object *obj, void *event_info)
458 appdata_s* ad = data;
460 if (!ad->buddy_list || eina_list_count(ad->buddy_list) <= 0) {
461 load_buddy_list_data(ad);
463 //delete_floating_button(ad);
464 launch_start_messaging_view(ad);
469 void on_floating_icon_clicked(void *data, Evas_Object *obj, void *event_info)
471 appdata_s* ad = data;
473 if (ad->menu_popup) {
474 evas_object_del(ad->menu_popup);
475 ad->menu_popup = NULL;
478 Evas_Object* image = (Evas_Object*)evas_object_data_get(ad->floating_btn, "image");
480 switch(ad->current_app_state) {
481 case TG_PEER_SEARCH_VIEW_STATE:
482 //elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
483 ad->is_loading_from_msg_view = EINA_FALSE;
484 ad->is_loading_from_profile_view = EINA_FALSE;
485 on_create_new_contact(ad);
486 //delete_floating_button(ad);
488 case TG_USER_MAIN_VIEW_STATE:
489 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_ADD), NULL);
490 launch_start_peer_search_view(ad);
493 delete_floating_button(ad);
498 void on_add_contact_clicked(void *data, Evas_Object *obj, void *event_info)
500 appdata_s* ad = data;
502 if (!ad->buddy_list || eina_list_count(ad->buddy_list) <= 0) {
503 load_buddy_list_data(ad);
505 show_toast(ad, "on_add_contact_clicked");
508 void create_floating_button(appdata_s* ad)
513 if (ad->floating_btn)
516 Evas_Object *icon = elm_image_add(ad->nf);
518 // elm_image_file_set(icon, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
519 evas_object_show(icon);
520 ad->floating_btn = eext_floatingbutton_add(ad->layout);
521 evas_object_color_set(ad->floating_btn, 255, 255, 255, 255);
522 elm_object_part_content_set(ad->layout, "elm.swallow.floatingbutton", ad->floating_btn);
523 /* Floating Button 1 */
524 Evas_Object *btn = elm_button_add(ad->floating_btn);
525 evas_object_smart_callback_add(btn, "clicked", on_floating_icon_clicked, ad);
527 elm_object_part_content_set(ad->floating_btn, "button1", btn);
528 elm_object_part_content_set(btn, "icon", icon);
530 evas_object_data_set(ad->floating_btn, "image", icon);
531 update_floating_button(ad, ad->current_app_state);
534 void delete_floating_button(appdata_s* ad)
539 if (ad->floating_btn){
540 //elm_layout_signal_emit(ad->floating_btn, "elm,state,floatingbutton,hidden", "elm");
541 evas_object_del(ad->floating_btn);
542 ad->floating_btn = NULL;
546 void show_floating_button(appdata_s* ad)
551 if (ad->floating_btn){
552 update_floating_button(ad, ad->current_app_state);
553 elm_layout_signal_emit(ad->floating_btn, "elm,state,floatingbutton,visible", "elm");
556 delete_floating_button(ad);
557 create_floating_button(ad);
562 void update_floating_button(appdata_s* ad, int mode)
564 // To do thi function
565 if (!ad->floating_btn)
568 Evas_Object* image = (Evas_Object*)evas_object_data_get(ad->floating_btn, "image");
571 case TG_PEER_SEARCH_VIEW_STATE:
572 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_ADD), NULL);
574 case TG_USER_MAIN_VIEW_STATE:
575 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
578 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
582 elm_layout_signal_emit(ad->floating_btn, "elm,state,floatingbutton,visible", "elm");
585 char *str_replace(char *orig, char *rep, char *with)
587 char *result; // the return string
588 char *ins; // the next insert point
590 int len_rep; // length of rep
591 int len_with; // length of with
592 int len_front; // distance between rep and end of last rep
593 int count; // number of replacements
599 len_rep = strlen(rep);
602 len_with = strlen(with);
605 for (count = 0; (tmp = strstr(ins, rep)); ++count) {
609 // first time through the loop, all the variable are set correctly
611 // tmp points to the end of the result string
612 // ins points to the next occurrence of rep in orig
613 // orig points to the remainder of orig after "end of rep"
614 tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
620 ins = strstr(orig, rep);
621 len_front = ins - orig;
622 tmp = strncpy(tmp, orig, len_front) + len_front;
623 tmp = strcpy(tmp, with) + len_with;
624 orig += len_front + len_rep; // move to next "end of rep"
632 char* get_display_name_from_contact(tg_peer_info_s* peer_info)
634 char *first_name = NULL;
635 char *last_name = NULL;
636 char *phone_num = NULL;
638 get_buddy_contact_details_from_db(peer_info->peer_id, &first_name, &last_name, &phone_num);
640 if (first_name && strstr(first_name, "null") != 0) {
645 if (!first_name && !last_name && phone_num)
646 first_name = phone_num;
648 if (last_name && strstr(last_name, "null") != 0) {
654 snprintf(user_name, sizeof(user_name), "%s %s", first_name?first_name:"", last_name?last_name:"");
663 return strdup(user_name);
666 char* get_peer_name(tg_peer_info_s* peer_info)
668 char *tmp_name = NULL;
670 switch(peer_info->peer_type) {
672 if (get_buddy_delete_status(peer_info->peer_id)) {
674 return strdup(i18n_get_text("IDS_TGRAM_OPT_DELETE"));
675 } else if (get_buddy_unknown_status(peer_info->peer_id)) {
677 char *number = get_buddy_phone_num_from_id(peer_info->peer_id);
678 return (number && strlen(number)) ? number : strdup(" ");
680 tmp_name = get_display_name_from_contact(peer_info);
681 return tmp_name ? tmp_name : strdup(" ");
686 tmp_name = replace(peer_info->print_name, '_', " ");
687 return tmp_name ? tmp_name : strdup(" ");
694 void set_peer_names(tg_peer_info_s* peer_info,
695 tg_main_list_item_s* main_list_item)
697 char *tmp_name = NULL;
699 switch(peer_info->peer_type) {
701 if (get_buddy_delete_status(peer_info->peer_id)) {
702 main_list_item->buddy_display_name = strdup(i18n_get_text("IDS_TGRAM_OPT_DELETE"));
703 main_list_item->peer_print_name = strdup(i18n_get_text("IDS_TGRAM_OPT_DELETE"));
704 } else if (get_buddy_unknown_status(peer_info->peer_id)) {
706 char *number = get_buddy_phone_num_from_id(peer_info->peer_id);
707 if (number && strlen(number))
708 main_list_item->peer_print_name = number;
710 main_list_item->peer_print_name = strdup(" ");
712 tmp_name = get_display_name_from_contact(peer_info);
713 main_list_item->buddy_display_name = tmp_name ? tmp_name : strdup(" ");
714 main_list_item->peer_print_name = strdup(peer_info->print_name);
719 tmp_name = replace(peer_info->print_name, '_', " ");
720 main_list_item->buddy_display_name = tmp_name ? tmp_name : strdup(" ");
721 main_list_item->peer_print_name = strdup(peer_info->print_name);
725 main_list_item->buddy_display_name = strdup(" ");
726 main_list_item->peer_print_name = strdup(peer_info->print_name);