add telegram account, fix db insert error.
[apps/native/telegram-tizen.git] / TelegramTizen / tg_utils / tg_common.c
1 /*
2     This file is part of Telegram application for tizen
3
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.
8
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.
13
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
17  */
18
19 #include <stdio.h>
20 #include <unistd.h>
21 #include <app.h>
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"
29
30 uint64_t get_time_stamp_in_macro()
31 {
32     struct timeval tv;
33     gettimeofday(&tv,NULL);
34     return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
35 }
36
37 void wait_for(unsigned int secs)
38 {
39     int retTime = time(0) + secs;
40     while (time(0) < retTime);
41 }
42
43 void app_get_resource(const char *edj_file_in, char *edj_path_out, int edj_path_max)
44 {
45         char *res_path = app_get_resource_path();
46         if (res_path) {
47                 snprintf(edj_path_out, edj_path_max, "%s%s", res_path, edj_file_in);
48                 free(res_path);
49         }
50 }
51
52 Eina_Bool compare_date_with_current_date(int rtime)
53 {
54
55         const char *format = " %d/%b/%Y";
56         time_t local_t = time(NULL);
57         struct tm lt;
58         char local_res[256];
59         (void) localtime_r(&local_t, &lt);
60         if (strftime(local_res, sizeof(local_res), format, &lt) == 0) {
61                 return EINA_FALSE;
62         }
63
64
65         time_t remote_t = rtime;
66         struct tm rt;
67         char remote_res[32];
68         (void) localtime_r(&remote_t, &rt);
69
70         if (strftime(remote_res, sizeof(remote_res), format, &rt) == 0) {
71                 return EINA_FALSE;
72         }
73         if (strcasecmp (local_res, remote_res) == 0) {
74                 return EINA_TRUE;
75         }
76         return EINA_FALSE;
77 }
78
79 Evas_Object* create_button(Evas_Object *parent, char *style, char *text)
80 {
81         Evas_Object *button;
82
83         button = elm_button_add(parent);
84         if (style)
85                 elm_object_style_set(button, style);
86         if (text)
87                 elm_object_text_set(button, text);
88         evas_object_show(button);
89
90         return button;
91 }
92
93 Evas_Object* create_circle_button(Evas_Object *parent, char* text, char* filepath)
94 {
95         Evas_Object *button, *image;
96         button= elm_button_add(parent);
97         elm_object_style_set(button, "circle");
98         if (text)
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);
106         return button;
107 }
108
109 char* get_table_name_from_number(const int id)
110 {
111         char id_str[64];
112         snprintf(id_str, sizeof(id_str), "tg_%d_msg", id);
113         return strdup(id_str);
114 }
115
116 char* get_budy_state(appdata_s* ad, int buddy_id)
117 {
118         if (!ad)
119                 return NULL;
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;
129
130                         char *format = NULL;
131                         Eina_Bool is_today = compare_date_with_current_date(last_seen);
132
133                         if (is_online > 0) {
134                                 strcpy(time_str, i18n_get_text("IDS_TGRAM_SBODY_ONLINE"));
135                         } else {
136                                 time_t t = last_seen;
137
138                                 if (is_today) {
139                                         format = "last seen Today at %I:%M %P";
140                                 } else {
141                                         format = "last seen %d/%b/%Y at %I:%M %P";
142                                 }
143
144                                 struct tm lt;
145                                 char res[256];
146                                 (void) localtime_r(&t, &lt);
147
148                                 if (strftime(res, sizeof(res), format, &lt) == 0) {
149                                         (void) fprintf(stderr,  "strftime(3): cannot format supplied "
150                                                         "date/time into buffer of size %u "
151                                                         "using: '%s'\n",
152                                                         sizeof(res), format);
153                                 }
154                                 snprintf(time_str, sizeof(time_str), "<align=left><font_size=30><color=#808080>%s</color></font_size></align>", res);
155                         }
156
157                         void* val = NULL;
158                         EINA_LIST_FREE(buddy_details,val) {
159                                 if (val)
160                                         free(val);
161                         }
162                 }
163                 eina_list_free(buddy_details_array);
164         }
165         return strdup(time_str);
166 }
167
168 int numbers_only(const char *s)
169 {
170         return 1;
171 }
172
173 static bool mp_util_svc_iterate_for_get_video_item_cb(media_info_h item, void *user_data)
174 {
175         return TRUE;
176 }
177
178
179
180 static bool __thumbnail_cb(media_info_h item, void *user_data)
181 {
182         char **thumbnail_path = (char **) user_data;
183
184         if(thumbnail_path == NULL)
185                 return FALSE;
186
187         if (media_info_get_thumbnail_path(item, thumbnail_path) != MEDIA_CONTENT_ERROR_NONE) {
188                 return FALSE;
189         }
190         if (*thumbnail_path) {
191         }
192         return TRUE;
193 }
194
195 Eina_Bool get_thumbnail_from_video_url(const char *file_path, char **thumbnail_path)
196 {
197         if (thumbnail_path == NULL) {
198                 return FALSE;
199         }
200         char condition[512 + 1] = { '\0', };
201         int ret=-1;
202         //snprintf(condition, 512, "%s ", file_path);
203         snprintf(condition, 512, "%s = \'%s\'", MEDIA_PATH, (char *)file_path);
204         ret = media_content_connect();
205
206         filter_h filter = NULL;
207         ret = media_filter_create(&filter);
208         if(ret != MEDIA_CONTENT_ERROR_NONE)  {
209                 goto ERROR_THROW;
210         }
211
212         ret = media_filter_set_condition(filter, condition, MEDIA_CONTENT_COLLATE_DEFAULT);
213         if (ret != MEDIA_CONTENT_ERROR_NONE) {
214                 goto ERROR_THROW;
215         }
216
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) {
220                 goto ERROR_THROW;
221         }
222         *thumbnail_path = temp_thumbnail_path;
223         if (filter) {
224                 media_filter_destroy(filter);
225         }
226         filter = NULL;
227         ret = media_content_disconnect();
228         return TRUE;
229
230 ERROR_THROW:
231         if (filter) {
232                 media_filter_destroy(filter);
233         }
234         filter = NULL;
235         ret = media_content_disconnect();
236         return FALSE;
237 }
238
239 void show_loading_popup(appdata_s* ad)
240 {
241         if (!ad)
242                 return;
243
244         if (ad->loading_popup) {
245                 evas_object_del(ad->loading_popup);
246                 ad->loading_popup = NULL;
247         }
248
249         Evas_Object *layout;
250         Evas_Object *progressbar;
251
252
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);
256
257         /* layout */
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);
271
272         elm_object_content_set(ad->loading_popup, layout);
273
274         evas_object_show(ad->loading_popup);
275
276 }
277
278 void hide_loading_popup(appdata_s* ad)
279 {
280         if (!ad)
281                 return;
282
283         if (ad->loading_popup) {
284                 evas_object_del(ad->loading_popup);
285                 ad->loading_popup = NULL;
286         }
287 }
288
289 void launch_app_control(appdata_s *ad, char *media_type, char *url)
290 {
291         if (!media_type || !url) {
292                 return;
293         }
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");
298                 return;
299         }
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");
309         }
310         app_control_set_uri(app_control, url);
311
312
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);
317                 }
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);
320                 }
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);
323                 }
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);
326                 }
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);
329                 }
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);
332                 }
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);
335                 }
336         }
337         app_control_destroy(app_control);
338
339 }
340
341 extern void free_message(tg_message_s **message)
342 {
343         if (message == NULL || *message == NULL) {
344                 return;
345         }
346
347         if ((*message)->message) {
348                 free((*message)->message);
349                 (*message)->message = NULL;
350         }
351
352         if ((*message)->media_id) {
353                 free((*message)->media_id);
354                 (*message)->media_id = NULL;
355         }
356
357         free(*message);
358         *message = NULL;
359 }
360
361 void free_user_data(user_data_s *user_data)
362 {
363         if (!user_data) {
364                 return;
365         }
366
367         if (user_data->print_name) {
368                 free(user_data->print_name);
369                 user_data->print_name = NULL;
370         }
371
372         if (user_data->first_name) {
373                 free(user_data->first_name);
374                 user_data->first_name = NULL;
375         }
376
377         if (user_data->last_name) {
378                 free(user_data->last_name);
379                 user_data->last_name = NULL;
380         }
381
382         if (user_data->phone) {
383                 free(user_data->phone);
384                 user_data->phone = NULL;
385         }
386
387         if (user_data->real_first_name) {
388                 free(user_data->real_first_name);
389                 user_data->real_first_name = NULL;
390         }
391         if (user_data->real_last_name) {
392                 free(user_data->real_last_name);
393                 user_data->real_last_name = NULL;
394         }
395         if (user_data->username) {
396                 free(user_data->username);
397                 user_data->username = NULL;
398         }
399
400         free(user_data);
401         user_data = NULL;
402 }
403
404
405 void tg_notification_create(appdata_s *app_data, char * icon_path, const char *title, char *content, char *sound_path, char *app_id)
406 {
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;
411         }
412
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);
417
418         if (icon_path) {
419                 ret = notification_set_image(app_data->s_notififcation, NOTIFICATION_IMAGE_TYPE_ICON, icon_path);
420         }
421         if (title) {
422                 ret = notification_set_text(app_data->s_notififcation, NOTIFICATION_TEXT_TYPE_TITLE, title, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
423         }
424         if (content) {
425                 ret = notification_set_text(app_data->s_notififcation, NOTIFICATION_TEXT_TYPE_CONTENT, content, NULL, NOTIFICATION_VARIABLE_TYPE_NONE);
426         }
427
428         if (sound_path) {
429                 ret = notification_set_sound(app_data->s_notififcation, NOTIFICATION_SOUND_TYPE_USER_DATA, sound_path);
430         } else {
431                 ret = notification_set_sound(app_data->s_notififcation, NOTIFICATION_SOUND_TYPE_DEFAULT, NULL);
432         }
433         ret = notification_set_vibration(app_data->s_notififcation, NOTIFICATION_VIBRATION_TYPE_DEFAULT, NULL);
434         if (ret != NOTIFICATION_ERROR_NONE) {
435                 //failed
436         }
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);
441
442         //notification_update()
443
444         ret  = notification_set_launch_option(app_data->s_notififcation, NOTIFICATION_LAUNCH_OPTION_APP_CONTROL, service);
445         if (ret != NOTIFICATION_ERROR_NONE) {
446                 //failed
447         }
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) {
452         }
453         return;
454 }
455
456 void on_new_message_clicked(void *data, Evas_Object *obj, void *event_info)
457 {
458         appdata_s* ad = data;
459
460         if (!ad->buddy_list || eina_list_count(ad->buddy_list) <= 0) {
461                 load_buddy_list_data(ad);
462         }
463         //delete_floating_button(ad);
464         launch_start_messaging_view(ad);
465 }
466
467
468
469 void on_floating_icon_clicked(void *data, Evas_Object *obj, void *event_info)
470 {
471         appdata_s* ad = data;
472
473         if (ad->menu_popup) {
474                 evas_object_del(ad->menu_popup);
475                 ad->menu_popup = NULL;
476         }
477
478         Evas_Object* image = (Evas_Object*)evas_object_data_get(ad->floating_btn, "image");
479
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);
487                 break;
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);
491                 break;
492         default :
493                 delete_floating_button(ad);
494                 break;
495         }
496 }
497
498 void on_add_contact_clicked(void *data, Evas_Object *obj, void *event_info)
499 {
500         appdata_s* ad = data;
501
502         if (!ad->buddy_list || eina_list_count(ad->buddy_list) <= 0) {
503                 load_buddy_list_data(ad);
504         }
505         show_toast(ad, "on_add_contact_clicked");
506 }
507
508 void create_floating_button(appdata_s* ad)
509 {
510         if (!ad)
511                 return;
512
513         if (ad->floating_btn)
514                 return;
515
516         Evas_Object *icon = elm_image_add(ad->nf);
517
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);
526
527         elm_object_part_content_set(ad->floating_btn, "button1", btn);
528         elm_object_part_content_set(btn, "icon", icon);
529
530         evas_object_data_set(ad->floating_btn, "image", icon);
531         update_floating_button(ad, ad->current_app_state);
532 }
533
534 void delete_floating_button(appdata_s* ad)
535 {
536         if (!ad)
537                 return;
538
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;
543         }
544 }
545
546 void show_floating_button(appdata_s* ad)
547 {
548         if (!ad)
549                 return;
550 #if 0
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");
554         }
555 #else
556         delete_floating_button(ad);
557         create_floating_button(ad);
558 #endif
559
560 }
561
562 void update_floating_button(appdata_s* ad, int mode)
563 {
564         // To do thi function
565         if (!ad->floating_btn)
566                 return;
567
568         Evas_Object* image = (Evas_Object*)evas_object_data_get(ad->floating_btn, "image");
569
570         switch(mode) {
571         case TG_PEER_SEARCH_VIEW_STATE:
572                 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_ADD), NULL);
573                 break;
574         case TG_USER_MAIN_VIEW_STATE:
575                 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
576                 break;
577         default :
578                 elm_image_file_set(image, ui_utils_get_resource(TG_ICON_FLOATING_PENCIL), NULL);
579                 break;
580         }
581
582         elm_layout_signal_emit(ad->floating_btn, "elm,state,floatingbutton,visible", "elm");
583 }
584
585 char *str_replace(char *orig, char *rep, char *with)
586 {
587         char *result; // the return string
588         char *ins;    // the next insert point
589         char *tmp;    // varies
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
594
595         if (!orig)
596                 return NULL;
597         if (!rep)
598                 rep = "";
599         len_rep = strlen(rep);
600         if (!with)
601                 with = "";
602         len_with = strlen(with);
603
604         ins = orig;
605         for (count = 0; (tmp = strstr(ins, rep)); ++count) {
606                 ins = tmp + len_rep;
607         }
608
609         // first time through the loop, all the variable are set correctly
610         // from here on,
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);
615
616         if (!result)
617                 return NULL;
618
619         while (count--) {
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"
625         }
626         strcpy(tmp, orig);
627         return result;
628 }
629
630
631
632 char* get_display_name_from_contact(tg_peer_info_s* peer_info)
633 {
634         char *first_name = NULL;
635         char *last_name = NULL;
636         char *phone_num = NULL;
637
638         get_buddy_contact_details_from_db(peer_info->peer_id, &first_name, &last_name, &phone_num);
639
640         if (first_name && strstr(first_name, "null") != 0) {
641                 free(first_name);
642                 first_name = NULL;
643         }
644
645         if (!first_name && !last_name && phone_num)
646                 first_name = phone_num;
647
648         if (last_name && strstr(last_name, "null") != 0) {
649                 free(last_name);
650                 last_name = NULL;
651         }
652
653         char user_name[128];
654         snprintf(user_name, sizeof(user_name), "%s %s", first_name?first_name:"", last_name?last_name:"");
655
656         if (first_name)
657                 free(first_name);
658         if (last_name)
659                 free(last_name);
660         if (phone_num)
661                 free(phone_num);
662
663         return strdup(user_name);
664 }
665
666 char* get_peer_name(tg_peer_info_s* peer_info)
667 {
668         char *tmp_name = NULL;
669
670         switch(peer_info->peer_type) {
671         case TGL_PEER_USER:
672                 if (get_buddy_delete_status(peer_info->peer_id)) {
673
674                         return strdup(i18n_get_text("IDS_TGRAM_OPT_DELETE"));
675                 } else if (get_buddy_unknown_status(peer_info->peer_id)) {
676
677                         char *number = get_buddy_phone_num_from_id(peer_info->peer_id);
678                         return (number && strlen(number)) ? number : strdup(" ");
679                 } else {
680                         tmp_name = get_display_name_from_contact(peer_info);
681                         return tmp_name ? tmp_name : strdup(" ");
682                 }
683                 break;
684         case TGL_PEER_CHAT:
685         {
686                 tmp_name = replace(peer_info->print_name, '_', " ");
687                 return tmp_name ? tmp_name : strdup(" ");
688         }
689         default:
690                 return strdup(" ");
691         }
692 }
693
694 void set_peer_names(tg_peer_info_s* peer_info,
695                 tg_main_list_item_s* main_list_item)
696 {
697         char *tmp_name = NULL;
698
699         switch(peer_info->peer_type) {
700         case TGL_PEER_USER:
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)) {
705
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;
709                         else
710                                 main_list_item->peer_print_name = strdup(" ");
711                 } else {
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);
715                 }
716                 break;
717         case TGL_PEER_CHAT:
718         {
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);
722         }
723         break;
724         default:
725                 main_list_item->buddy_display_name = strdup(" ");
726                 main_list_item->peer_print_name = strdup(peer_info->print_name);
727         }
728 }