2 linphone, gtk-glade interface.
3 Copyright (C) 2008 Simon MORLAT (simon.morlat@linphone.org)
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.
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.
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.
23 #include <gtkosxapplication.h>
26 #define NB_MSG_HIST 250
28 #define CONFIG_FILE ".linphone-history.db"
30 const char *linphone_gtk_message_storage_get_db_file(const char *filename){
31 const int path_max=1024;
32 static char *db_file=NULL;
34 if (db_file) return db_file;
36 db_file=(char *)malloc(path_max*sizeof(char));
37 if (filename==NULL) filename=CONFIG_FILE;
38 /*try accessing a local file first if exists*/
39 if (access(CONFIG_FILE,F_OK)==0){
40 snprintf(db_file,path_max,"%s",filename);
43 const char *appdata=getenv("APPDATA");
45 snprintf(db_file,path_max,"%s\\%s",appdata,LINPHONE_CONFIG_DIR);
46 CreateDirectory(db_file,NULL);
47 snprintf(db_file,path_max,"%s\\%s\\%s",appdata,LINPHONE_CONFIG_DIR,filename);
50 const char *home=getenv("HOME");
51 if (home==NULL) home=".";
52 snprintf(db_file,path_max,"%s/%s",home,filename);
59 void linphone_gtk_quit_chatroom(LinphoneChatRoom *cr) {
60 GtkWidget *main_window=linphone_gtk_get_main_window ();
61 GtkWidget *nb=linphone_gtk_get_widget(main_window,"viewswitch");
62 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
63 GtkWidget *w=g_object_get_data(G_OBJECT(friendlist),"chatview");
64 int idx = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"idx"));
66 g_return_if_fail(w!=NULL);
67 gtk_notebook_remove_page(GTK_NOTEBOOK(nb),idx);
68 //linphone_gtk_create_chat_picture(FALSE);
69 g_object_set_data(G_OBJECT(friendlist),"chatview",NULL);
70 g_object_set_data(G_OBJECT(w),"from_message",NULL);
71 g_object_set_data(G_OBJECT(w),"cr",NULL);
72 g_object_set_data(G_OBJECT(friendlist),"from",NULL);
73 gtk_widget_destroy(w);
76 const char* get_display_name(const LinphoneAddress *from){
77 const char *display=linphone_address_get_display_name(from);
78 if (display==NULL || display[0]=='\0') {
79 display=linphone_address_get_username(from);
84 GtkWidget *create_tab_chat_header(LinphoneChatRoom *cr,const LinphoneAddress *uri){
85 GtkWidget *w=gtk_hbox_new (FALSE,0);
86 GtkWidget *i=create_pixmap ("chat.png");
88 GtkWidget *image=gtk_image_new_from_stock(GTK_STOCK_CLOSE,GTK_ICON_SIZE_MENU);
89 GtkWidget *b=gtk_button_new();
91 gtk_button_set_image(GTK_BUTTON(b),image);
92 gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE);
93 gtk_widget_set_size_request(b,25,20);
94 g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr);
95 l=gtk_label_new (get_display_name(uri));
96 gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0);
97 gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0);
98 gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0);
99 gtk_widget_show_all(w);
103 void udpate_tab_chat_header(GtkWidget *chat_view,const LinphoneAddress *uri,LinphoneChatRoom *cr){
104 GtkWidget *main_window=linphone_gtk_get_main_window();
105 GtkNotebook *notebook=GTK_NOTEBOOK(linphone_gtk_get_widget(main_window,"viewswitch"));
106 GtkWidget *w=gtk_hbox_new (FALSE,0);
107 GtkWidget *i=create_pixmap ("chat.png");
109 GtkWidget *image=gtk_image_new_from_stock(GTK_STOCK_CLOSE,GTK_ICON_SIZE_MENU);
110 GtkWidget *b=gtk_button_new();
112 gtk_button_set_image(GTK_BUTTON(b),image);
113 gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE);
114 gtk_widget_set_size_request(b,25,20);
115 g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr);
116 l=gtk_label_new (get_display_name(uri));
117 gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0);
118 gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0);
119 gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0);
120 gtk_notebook_set_tab_label(notebook,chat_view,w);
121 gtk_widget_show_all(w);
124 void linphone_gtk_push_text(GtkWidget *w, const LinphoneAddress *from,
125 gboolean me,LinphoneChatRoom *cr,LinphoneChatMessage *msg, gboolean hist){
126 GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(w,"textview"));
127 GtkTextBuffer *buffer=gtk_text_view_get_buffer(text);
128 GtkTextIter iter,begin;
130 char *from_str=linphone_address_as_string_uri_only(from);
131 char *from_message=(char *)g_object_get_data(G_OBJECT(w),"from_message");
132 GList *list=g_object_get_data(G_OBJECT(w),"list");
135 gtk_text_buffer_get_start_iter(buffer,&begin);
136 gtk_text_buffer_get_end_iter(buffer,&iter);
137 off=gtk_text_iter_get_offset(&iter);
138 if(g_strcmp0(from_message,from_str)!=0){
139 gtk_text_buffer_get_iter_at_offset(buffer,&iter,off);
140 gtk_text_buffer_get_end_iter(buffer,&iter);
141 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,get_display_name(from),-1,"bold",me ? "bg":NULL,NULL);
142 gtk_text_buffer_get_end_iter(buffer,&iter);
143 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter," : ",-1,"bold",me ? "bg":NULL,NULL);
144 gtk_text_buffer_get_end_iter(buffer,&iter);
145 gtk_text_buffer_insert(buffer,&iter,"\n",-1);
146 g_object_set_data(G_OBJECT(w),"from_message",from_str);
148 gtk_text_buffer_get_end_iter(buffer,&iter);
149 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,linphone_chat_message_get_text(msg),-1,"margin",me ? "bg":NULL,NULL);
150 gtk_text_buffer_get_end_iter(buffer,&iter);
151 gtk_text_buffer_insert(buffer,&iter,"\n",-1);;
152 gtk_text_buffer_get_end_iter(buffer,&iter);
153 t=linphone_chat_message_get_time(msg);
154 switch (linphone_chat_message_get_state (msg)){
155 case LinphoneChatMessageStateInProgress:
157 list=g_list_append(list,GINT_TO_POINTER(gtk_text_iter_get_line(&iter)));
158 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending .. ",-1,
159 "right","small","italic","font_grey","bg",NULL);
160 g_object_set_data(G_OBJECT(w),"list",list);
163 case LinphoneChatMessageStateDelivered:
165 struct tm *tm=localtime(&t);
167 strftime(buf,80,"%H:%M",tm);
168 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,buf,-1,
169 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
172 case LinphoneChatMessageStateNotDelivered:
173 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Error",-1,
174 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
176 default : gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending ..",-1,
177 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
179 gtk_text_buffer_get_end_iter(buffer,&iter);
180 gtk_text_buffer_insert(buffer,&iter,"\n",-1);
181 GtkTextMark *mark=gtk_text_buffer_create_mark(buffer,NULL,&iter,FALSE);
182 gtk_text_view_scroll_mark_onscreen(text,mark);
185 const LinphoneAddress* linphone_gtk_get_used_identity(){
186 LinphoneCore *lc=linphone_gtk_get_core();
187 LinphoneProxyConfig *cfg;
188 linphone_core_get_default_proxy(lc,&cfg);
189 if (cfg) return linphone_address_new(linphone_proxy_config_get_identity(cfg));
190 else return linphone_core_get_primary_contact_parsed(lc);
193 void update_chat_state_message(LinphoneChatMessageState state,LinphoneChatMessage *msg){
194 GtkWidget *main_window=linphone_gtk_get_main_window();
195 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
196 GtkWidget *page=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
197 GList *list=g_object_get_data(G_OBJECT(page),"list");
200 GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(page,"textview"));
201 GtkTextBuffer *b=gtk_text_view_get_buffer(text);
206 gtk_text_buffer_get_iter_at_line(b,&iter,
207 GPOINTER_TO_INT(g_list_nth_data(list,0)));
208 if(gtk_text_iter_get_chars_in_line(&iter) >0) {
209 gtk_text_buffer_get_iter_at_line_offset(b,&start,
210 GPOINTER_TO_INT(g_list_nth_data(list,0)),
211 gtk_text_iter_get_chars_in_line(&iter)-1);
213 gtk_text_buffer_get_iter_at_line_offset(b,&start,
214 GPOINTER_TO_INT(g_list_nth_data(list,0)),0);
216 gtk_text_buffer_get_iter_at_line_offset(b,&end,
217 GPOINTER_TO_INT(g_list_nth_data(list,0)),0);
218 gtk_text_buffer_delete(b,&start,&end);
219 gtk_text_buffer_get_iter_at_line(b,&iter,GPOINTER_TO_INT(g_list_nth_data(list,0)));
222 case LinphoneChatMessageStateInProgress:
225 case LinphoneChatMessageStateDelivered:
228 struct tm *tm=localtime(&t);
230 strftime(buf,80,"%H:%M",tm);
234 case LinphoneChatMessageStateNotDelivered:
237 default : result="Sending ..";
239 gtk_text_buffer_insert_with_tags_by_name(b,&iter,result,-1,
240 "right","small","italic","font_grey","bg",NULL);
241 list=g_list_remove(list,g_list_nth_data(list,0));
242 g_object_set_data(G_OBJECT(page),"list",list);
246 static void on_chat_state_changed(LinphoneChatMessage *msg, LinphoneChatMessageState state, void *user_pointer){
247 update_chat_state_message(state,msg);
250 void linphone_gtk_send_text(){
251 GtkWidget *main_window=linphone_gtk_get_main_window();
252 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
253 GtkWidget *w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
254 GtkWidget *entry=linphone_gtk_get_widget(w,"text_entry");
255 const gchar *entered;
256 LinphoneChatRoom *cr=g_object_get_data(G_OBJECT(w),"cr");
257 entered=gtk_entry_get_text(GTK_ENTRY(entry));
258 if (strlen(entered)>0) {
259 LinphoneChatMessage *msg;
260 msg=linphone_chat_room_create_message(cr,entered);
261 linphone_chat_room_send_message2(cr,msg,on_chat_state_changed,NULL);
262 linphone_gtk_push_text(w,linphone_gtk_get_used_identity(),
264 gtk_entry_set_text(GTK_ENTRY(entry),"");
268 static void linphone_gtk_chat_message_destroy(LinphoneChatMessage *msg){
269 linphone_chat_message_destroy(msg);
272 void linphone_gtk_free_list(MSList *messages){
273 ms_list_for_each(messages,(void (*)(void*))linphone_gtk_chat_message_destroy);
274 ms_list_free(messages);
277 void display_history_message(GtkWidget *chat_view,MSList *messages,const LinphoneAddress *with){
278 if(messages != NULL){
282 for(it=messages;it!=NULL;it=it->next){
283 LinphoneChatMessage *msg=(LinphoneChatMessage *)it->data;
284 from_str=linphone_address_as_string_uri_only(linphone_chat_message_get_from(msg));
285 with_str=linphone_address_as_string_uri_only(with);
286 linphone_gtk_push_text(chat_view,strcmp(from_str,with_str)==0? with :
287 linphone_chat_message_get_from(msg),
288 strcmp(from_str,with_str)==0? FALSE : TRUE,
289 linphone_chat_message_get_chat_room(msg),msg,TRUE);
291 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
294 linphone_gtk_free_list(messages);
298 GtkWidget* linphone_gtk_init_chatroom(LinphoneChatRoom *cr, const LinphoneAddress *with){
299 GtkWidget *chat_view=linphone_gtk_create_widget("main","chatroom_frame");
300 GtkWidget *main_window=linphone_gtk_get_main_window ();
301 GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch");
302 GtkWidget *text=linphone_gtk_get_widget(chat_view,"textview");
317 colorb.green = 60928;
320 with_str=linphone_address_as_string_uri_only(with);
321 linphone_chat_room_mark_as_read(cr);
322 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_WORD_CHAR);
323 gtk_text_view_set_editable(GTK_TEXT_VIEW(text),FALSE);
324 gtk_notebook_append_page(notebook,chat_view,create_tab_chat_header(cr,with));
325 idx = gtk_notebook_page_num(notebook, chat_view);
326 gtk_notebook_set_current_page(notebook, idx);
327 gtk_widget_show(chat_view);
328 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
329 g_object_set_data(G_OBJECT(chat_view),"idx",GINT_TO_POINTER(idx));
330 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
331 g_object_set_data(G_OBJECT(chat_view),"list",list);
332 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
333 "right","justification", GTK_JUSTIFY_RIGHT,NULL);
334 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
335 "left","justification", GTK_JUSTIFY_LEFT,NULL);
336 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
337 "bold","weight", PANGO_WEIGHT_BOLD,NULL);
338 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
339 "italic","style", PANGO_STYLE_ITALIC,NULL);
340 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
341 "small","size",9*PANGO_SCALE,NULL);
342 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
343 "font_grey","foreground-gdk",&color,NULL);
344 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
345 "margin","indent",10,NULL);
346 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
347 "bg","paragraph-background-gdk",&colorb,NULL);
348 messages = linphone_chat_room_get_history(cr,NB_MSG_HIST);
349 display_history_message(chat_view,messages,with);
350 button = linphone_gtk_get_widget(chat_view,"send");
351 g_signal_connect_swapped(G_OBJECT(button),"clicked",(GCallback)linphone_gtk_send_text,NULL);
352 entry = linphone_gtk_get_widget(chat_view,"text_entry");
353 g_signal_connect_swapped(G_OBJECT(entry),"activate",(GCallback)linphone_gtk_send_text,NULL);
358 LinphoneChatRoom * linphone_gtk_create_chatroom(const LinphoneAddress *with){
359 LinphoneChatRoom *cr=linphone_core_create_chat_room(linphone_gtk_get_core(),linphone_address_as_string(with));
360 if (!cr) return NULL;
364 void linphone_gtk_load_chatroom(LinphoneChatRoom *cr,const LinphoneAddress *uri,GtkWidget *chat_view){
365 GtkWidget *main_window=linphone_gtk_get_main_window ();
366 LinphoneChatRoom *cr2=(LinphoneChatRoom *)g_object_get_data(G_OBJECT(chat_view),"cr");
367 const LinphoneAddress *from=linphone_chat_room_get_peer_address(cr2);
368 char *from_str=linphone_address_as_string_uri_only(from);
369 char *uri_str=linphone_address_as_string(uri);
370 char *uri_only=linphone_address_as_string_uri_only(uri);
371 MSList *messages=NULL;
373 linphone_chat_room_mark_as_read(cr);
374 if(g_strcmp0(from_str,uri_only)!=0){
375 GtkTextView *text_view=GTK_TEXT_VIEW(linphone_gtk_get_widget(chat_view,"textview"));
378 GtkTextBuffer *text_buffer;
380 text_buffer=gtk_text_view_get_buffer(text_view);
381 gtk_text_buffer_get_bounds(text_buffer, &start, &end);
382 gtk_text_buffer_delete (text_buffer, &start, &end);
383 udpate_tab_chat_header(chat_view,uri,cr);
384 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
385 g_object_set_data(G_OBJECT(linphone_gtk_get_widget(main_window,"contact_list")),"chatview",(gpointer)chat_view);
386 messages=linphone_chat_room_get_history(cr,NB_MSG_HIST);
387 g_object_set_data(G_OBJECT(chat_view),"from_message",uri_str);
388 display_history_message(chat_view,messages,uri);
394 void linphone_gtk_chat_destroyed(GtkWidget *w){
395 LinphoneChatRoom *cr=(LinphoneChatRoom*)g_object_get_data(G_OBJECT(w),"cr");
396 linphone_chat_room_destroy(cr);
399 void linphone_gtk_chat_close(GtkWidget *button){
400 GtkWidget *w=gtk_widget_get_toplevel(button);
401 gtk_widget_destroy(w);
405 void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room,
406 LinphoneChatMessage *msg){
407 GtkWidget *main_window=linphone_gtk_get_main_window();
408 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
412 char *from=linphone_address_as_string(linphone_chat_message_get_from(msg));
413 w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
415 char *from_chatview=(char *)g_object_get_data(G_OBJECT(friendlist),"from");
416 if(g_strcmp0(from,from_chatview)==0){
422 w=linphone_gtk_init_chatroom(room,linphone_chat_message_get_from(msg));
423 g_object_set_data(G_OBJECT(friendlist),"chatview",(gpointer)w);
424 g_object_set_data(G_OBJECT(friendlist),"from",from);
426 get_display_name(linphone_chat_message_get_from(msg));
428 /* Notified when a new message is sent */
429 linphone_gtk_status_icon_set_blinking(TRUE);
431 if(!gtk_window_is_active(GTK_WINDOW(main_window))){
432 if(!GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_notified"))){
433 linphone_gtk_notify(NULL,linphone_chat_message_get_text(msg));
434 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(TRUE));
436 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(FALSE));
441 linphone_gtk_push_text(w,linphone_chat_message_get_from(msg),
442 FALSE,room,msg,FALSE);
444 linphone_gtk_show_friends();
445 //linphone_gtk_friend_list_update_message(msg);
447 //linphone_gtk_update_chat_picture();
448 //TODO: update la zone de notification dans les contacts (problème : lors du refresh de la liste
449 //connaitre tous les contacts qui ont des messages non lus ...
450 //gtk_window_present(GTK_WINDOW(w));
451 /*gtk_window_set_urgency_hint(GTK_WINDOW(w),TRUE);*/