]> sjero.net Git - linphone/blob - gtk/calllogs.c
add number of unread messages
[linphone] / gtk / calllogs.c
1 /*
2 linphone, gtk-glade interface.
3 Copyright (C) 2008  Simon MORLAT (simon.morlat@linphone.org)
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 */
19
20 #include "linphone.h"
21
22
23 static void fill_renderers(GtkTreeView *v){
24         GtkTreeViewColumn *c;
25         GtkCellRenderer *r;
26         r=gtk_cell_renderer_pixbuf_new();
27
28         c=gtk_tree_view_column_new_with_attributes("icon",r,"pixbuf",0,NULL);
29         gtk_tree_view_append_column (v,c);
30
31         r=gtk_cell_renderer_text_new ();
32         c=gtk_tree_view_column_new_with_attributes("sipaddress",r,"markup",1,NULL);
33         gtk_tree_view_append_column (v,c);
34 }
35
36 void call_log_selection_changed(GtkTreeView *v){
37         GtkTreeSelection *select;
38         GtkTreeIter iter;
39         GtkTreeModel *model;
40         
41         select = gtk_tree_view_get_selection(v);
42         if (gtk_tree_selection_get_selected (select, &model, &iter)){
43                 GtkTreePath *path=gtk_tree_model_get_path(model,&iter);
44                 gtk_tree_view_collapse_all(v);
45                 gtk_tree_view_expand_row(v,path,TRUE);
46                 gtk_tree_path_free(path);
47         }
48 }
49
50 void linphone_gtk_call_log_update(GtkWidget *w){
51         GtkTreeView *v=GTK_TREE_VIEW(linphone_gtk_get_widget(w,"logs_view"));
52         GtkTreeStore *store;
53         const MSList *logs;
54         GtkTreeSelection *select;
55
56         store=(GtkTreeStore*)gtk_tree_view_get_model(v);
57         if (store==NULL){
58                 store=gtk_tree_store_new(3,GDK_TYPE_PIXBUF,G_TYPE_STRING,G_TYPE_POINTER);
59                 gtk_tree_view_set_model(v,GTK_TREE_MODEL(store));
60                 g_object_unref(G_OBJECT(store));
61                 fill_renderers(GTK_TREE_VIEW(linphone_gtk_get_widget(w,"logs_view")));
62                 select=gtk_tree_view_get_selection(v);
63                 gtk_tree_selection_set_mode(select, GTK_SELECTION_SINGLE);
64                 g_signal_connect_swapped(G_OBJECT(select),"changed",(GCallback)call_log_selection_changed,v);
65 //              gtk_button_set_image(GTK_BUTTON(linphone_gtk_get_widget(w,"call_back_button")),
66 //                                   create_pixmap (linphone_gtk_get_ui_config("callback_button","status-green.png")));
67         }
68         gtk_tree_store_clear (store);
69
70         for (logs=linphone_core_get_call_logs(linphone_gtk_get_core());logs!=NULL;logs=logs->next){
71                 LinphoneCallLog *cl=(LinphoneCallLog*)logs->data;
72                 GtkTreeIter iter, iter2;
73                 LinphoneAddress *la=linphone_call_log_get_dir(cl)==LinphoneCallIncoming ? linphone_call_log_get_from(cl) : linphone_call_log_get_to(cl);
74                 char *addr= linphone_address_as_string_uri_only (la);
75                 const char *display;
76                 gchar *logtxt, *headtxt, *minutes, *seconds;
77                 gchar quality[20];
78                 const char *status=NULL;
79                 gchar *start_date=NULL;
80                 int duration=linphone_call_log_get_duration(cl);
81                 time_t start_date_time=linphone_call_log_get_start_date(cl);
82                 
83 #if GLIB_CHECK_VERSION(2,26,0)
84                 if (start_date_time){
85                         GDateTime *dt=g_date_time_new_from_unix_local(start_date_time);
86                         start_date=g_date_time_format(dt,"%c");
87                         g_date_time_unref(dt);
88                 }
89 #else
90                 start_date=g_strdup(ctime(&start_date_time));
91 #endif
92                 
93                 display=linphone_address_get_display_name (la);
94                 if (display==NULL){
95                         display=linphone_address_get_username (la);
96                         if (display==NULL)
97                                 display=linphone_address_get_domain (la);
98                 }
99                 if (linphone_call_log_get_quality(cl)!=-1){
100                         snprintf(quality,sizeof(quality),"%.1f",linphone_call_log_get_quality(cl));
101                 }else snprintf(quality,sizeof(quality)-1,"%s",_("n/a"));
102                 switch(linphone_call_log_get_status(cl)){
103                         case LinphoneCallAborted:
104                                 status=_("Aborted");
105                         break;
106                         case LinphoneCallMissed:
107                                 status=_("Missed");
108                         break;
109                         case LinphoneCallDeclined:
110                                 status=_("Declined");
111                         break;
112                         default:
113                         break;
114                 }
115                 minutes=g_markup_printf_escaped(
116                         ngettext("%i minute", "%i minutes", duration/60),
117                         duration/60);
118                 seconds=g_markup_printf_escaped(
119                         ngettext("%i second", "%i seconds", duration%60),
120                         duration%60);
121                 if (status==NULL) {
122                                 headtxt=g_markup_printf_escaped(_("<big><b>%s</b></big>\t%s"),display,start_date ? start_date : "");
123                                 logtxt=g_markup_printf_escaped(
124                                 _("<small><i>%s</i>\t" 
125                                   "<i>Quality: %s</i></small>\n%s\t%s\t"),
126                                 addr, quality, minutes, seconds);
127                 } else {
128                         headtxt=g_markup_printf_escaped(_("<big><b>%s</b></big>\t%s"),display,start_date ? start_date : "");
129                         logtxt=g_markup_printf_escaped(
130                         _("<small><i>%s</i></small>\t"
131                                 "\n%s"),addr, status);
132                 }
133                 g_free(minutes);
134                 g_free(seconds);
135                 if (start_date) g_free(start_date);
136                 gtk_tree_store_append (store,&iter,NULL);
137
138                 GdkPixbuf *incoming = create_pixbuf("call_status_incoming.png");
139                 GdkPixbuf *outgoing = create_pixbuf("call_status_outgoing.png");
140                 gtk_tree_store_set (store,&iter,
141                                0, linphone_call_log_get_dir(cl)==LinphoneCallOutgoing ? outgoing : incoming,
142                                1, headtxt,2,la,-1);     
143                 gtk_tree_store_append (store,&iter2,&iter);
144                 gtk_tree_store_set (store,&iter2,1,logtxt,2,la,-1);
145                 ms_free(addr);
146                 g_free(logtxt);
147                 g_free(headtxt);
148         }
149         
150 }
151
152 static bool_t put_selection_to_uribar(GtkWidget *treeview){
153         GtkTreeSelection *sel;
154
155         sel=gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
156         if (sel!=NULL){
157                 GtkTreeModel *model=NULL;
158                 GtkTreeIter iter;
159                 if (gtk_tree_selection_get_selected (sel,&model,&iter)){
160                         gpointer pla;
161                         LinphoneAddress *la;
162                         char *tmp;
163                         gtk_tree_model_get(model,&iter,2,&pla,-1);
164                         la=(LinphoneAddress*)pla;
165                         tmp=linphone_address_as_string (la);
166                         gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(linphone_gtk_get_main_window(),"uribar")),tmp);
167                         ms_free(tmp);
168                         return TRUE;
169                 }
170         }
171         return FALSE;
172 }
173
174 void linphone_gtk_history_row_activated(GtkWidget *treeview){
175         if (put_selection_to_uribar(treeview)){
176                 GtkWidget *mw=linphone_gtk_get_main_window();
177                 linphone_gtk_start_call(linphone_gtk_get_widget(mw,"start_call"));
178         }
179 }
180
181 void linphone_gtk_history_row_selected(GtkWidget *treeview){
182         put_selection_to_uribar(treeview);
183 }
184
185 void linphone_gtk_clear_call_logs(GtkWidget *button){
186         linphone_core_clear_call_logs (linphone_gtk_get_core());
187         linphone_gtk_call_log_update(gtk_widget_get_toplevel(button));
188 }
189
190 void linphone_gtk_call_log_callback(GtkWidget *button){
191         GtkWidget *mw=linphone_gtk_get_main_window();
192         if (put_selection_to_uribar(linphone_gtk_get_widget(mw,"logs_view")))
193                         linphone_gtk_start_call(linphone_gtk_get_widget(mw,"start_call"));
194 }
195
196 void linphone_gtk_call_log_response(GtkWidget *w, guint response_id){
197         GtkWidget *mw=linphone_gtk_get_main_window();
198         if (response_id==1){
199                 if (put_selection_to_uribar(linphone_gtk_get_widget(w,"logs_view")))
200                         linphone_gtk_start_call(linphone_gtk_get_widget(mw,"start_call"));
201         }else if (response_id==2){
202                 linphone_core_clear_call_logs (linphone_gtk_get_core());
203                 linphone_gtk_call_log_update(w);
204                 return;
205         }
206         g_object_set_data(G_OBJECT(mw),"call_logs",NULL);
207         gtk_widget_destroy(w);
208 }
209
210
211
212 GtkWidget * linphone_gtk_show_call_logs(void){
213         GtkWidget *mw=linphone_gtk_get_main_window();
214
215         GtkWidget *w=(GtkWidget*)g_object_get_data(G_OBJECT(linphone_gtk_get_main_window()),"call_logs");
216         if (w==NULL){
217                 w=linphone_gtk_create_window("call_logs");
218 //              gtk_button_set_image(GTK_BUTTON(linphone_gtk_get_widget(w,"call_back_button")),
219 //                                   create_pixmap (linphone_gtk_get_ui_config("callback_button","status-green.png")));
220                 g_object_set_data(G_OBJECT(mw),"call_logs",w);
221                 g_signal_connect(G_OBJECT(w),"response",(GCallback)linphone_gtk_call_log_response,NULL);
222                 gtk_widget_show(w);
223                 linphone_gtk_call_log_update(w);
224         }else gtk_window_present(GTK_WINDOW(w));
225         return w;
226 }
227