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 void display_history_message(GtkWidget *chat_view,MSList *messages,const LinphoneAddress *with){
269 if(messages != NULL){
273 for(it=messages;it!=NULL;it=it->next){
274 LinphoneChatMessage *msg=(LinphoneChatMessage *)it->data;
275 from_str=linphone_address_as_string_uri_only(linphone_chat_message_get_from(msg));
276 with_str=linphone_address_as_string_uri_only(with);
277 linphone_gtk_push_text(chat_view,strcmp(from_str,with_str)==0? with :
278 linphone_chat_message_get_from(msg),
279 strcmp(from_str,with_str)==0? FALSE : TRUE,
280 linphone_chat_message_get_chat_room(msg),msg,TRUE);
282 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
288 GtkWidget* linphone_gtk_init_chatroom(LinphoneChatRoom *cr, const LinphoneAddress *with){
289 GtkWidget *chat_view=linphone_gtk_create_widget("main","chatroom_frame");
290 GtkWidget *main_window=linphone_gtk_get_main_window ();
291 GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch");
292 GtkWidget *text=linphone_gtk_get_widget(chat_view,"textview");
307 colorb.green = 60928;
310 with_str=linphone_address_as_string_uri_only(with);
311 linphone_chat_room_mark_as_read(cr);
312 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_WORD_CHAR);
313 gtk_text_view_set_editable(GTK_TEXT_VIEW(text),FALSE);
314 gtk_notebook_append_page(notebook,chat_view,create_tab_chat_header(cr,with));
315 idx = gtk_notebook_page_num(notebook, chat_view);
316 gtk_notebook_set_current_page(notebook, idx);
317 gtk_widget_show(chat_view);
318 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
319 g_object_set_data(G_OBJECT(chat_view),"idx",GINT_TO_POINTER(idx));
320 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
321 g_object_set_data(G_OBJECT(chat_view),"list",list);
322 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
323 "right","justification", GTK_JUSTIFY_RIGHT,NULL);
324 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
325 "left","justification", GTK_JUSTIFY_LEFT,NULL);
326 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
327 "bold","weight", PANGO_WEIGHT_BOLD,NULL);
328 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
329 "italic","style", PANGO_STYLE_ITALIC,NULL);
330 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
331 "small","size",9*PANGO_SCALE,NULL);
332 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
333 "font_grey","foreground-gdk",&color,NULL);
334 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
335 "margin","indent",10,NULL);
336 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
337 "bg","paragraph-background-gdk",&colorb,NULL);
338 messages = linphone_chat_room_get_history(cr,NB_MSG_HIST);
339 display_history_message(chat_view,messages,with);
340 button = linphone_gtk_get_widget(chat_view,"send");
341 g_signal_connect_swapped(G_OBJECT(button),"clicked",(GCallback)linphone_gtk_send_text,NULL);
342 entry = linphone_gtk_get_widget(chat_view,"text_entry");
343 g_signal_connect_swapped(G_OBJECT(entry),"activate",(GCallback)linphone_gtk_send_text,NULL);
348 LinphoneChatRoom * linphone_gtk_create_chatroom(const LinphoneAddress *with){
349 LinphoneChatRoom *cr=linphone_core_create_chat_room(linphone_gtk_get_core(),linphone_address_as_string(with));
350 if (!cr) return NULL;
354 void linphone_gtk_load_chatroom(LinphoneChatRoom *cr,const LinphoneAddress *uri,GtkWidget *chat_view){
355 GtkWidget *main_window=linphone_gtk_get_main_window ();
356 LinphoneChatRoom *cr2=(LinphoneChatRoom *)g_object_get_data(G_OBJECT(chat_view),"cr");
357 char *from_str=linphone_address_as_string(linphone_chat_room_get_peer_address (cr2));
358 char *uri_str=linphone_address_as_string(uri);
359 MSList *messages=NULL;
361 linphone_chat_room_mark_as_read(cr);
362 if(g_strcmp0(from_str,uri_str)!=0){
363 GtkTextView *text_view=GTK_TEXT_VIEW(linphone_gtk_get_widget(chat_view,"textview"));
366 GtkTextBuffer *text_buffer;
368 text_buffer=gtk_text_view_get_buffer(text_view);
369 gtk_text_buffer_get_bounds(text_buffer, &start, &end);
370 gtk_text_buffer_delete (text_buffer, &start, &end);
371 udpate_tab_chat_header(chat_view,uri,cr);
372 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
373 g_object_set_data(G_OBJECT(linphone_gtk_get_widget(main_window,"contact_list")),"chatview",(gpointer)chat_view);
374 messages = linphone_chat_room_get_history(cr,NB_MSG_HIST);
375 g_object_set_data(G_OBJECT(chat_view),"from_message",uri_str);
376 display_history_message(chat_view,messages,uri);
382 void linphone_gtk_chat_destroyed(GtkWidget *w){
383 LinphoneChatRoom *cr=(LinphoneChatRoom*)g_object_get_data(G_OBJECT(w),"cr");
384 linphone_chat_room_destroy(cr);
387 void linphone_gtk_chat_close(GtkWidget *button){
388 GtkWidget *w=gtk_widget_get_toplevel(button);
389 gtk_widget_destroy(w);
393 void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room,
394 LinphoneChatMessage *msg){
395 GtkWidget *main_window=linphone_gtk_get_main_window();
396 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
399 w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
401 linphone_gtk_load_chatroom(room,linphone_chat_message_get_from(msg),w);
403 w=linphone_gtk_init_chatroom(room,linphone_chat_message_get_from(msg));
404 g_object_set_data(G_OBJECT(friendlist),"chatview",(gpointer)w);
405 char *from=linphone_address_as_string(linphone_chat_message_get_from(msg));
406 g_object_set_data(G_OBJECT(friendlist),"from",from);
408 get_display_name(linphone_chat_message_get_from(msg));
410 /* Notified when a new message is sent */
411 linphone_gtk_status_icon_set_blinking(TRUE);
413 if(!gtk_window_is_active(GTK_WINDOW(main_window))){
414 if(!GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_notified"))){
415 linphone_gtk_notify(NULL,linphone_chat_message_get_text(msg));
416 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(TRUE));
418 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(FALSE));
422 linphone_gtk_push_text(w,linphone_chat_message_get_from(msg),
423 FALSE,room,msg,FALSE);
424 linphone_gtk_update_chat_picture();
425 //gtk_window_present(GTK_WINDOW(w));
426 /*gtk_window_set_urgency_hint(GTK_WINDOW(w),TRUE);*/