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 void linphone_gtk_quit_chatroom(LinphoneChatRoom *cr) {
27 GtkWidget *main_window=linphone_gtk_get_main_window ();
28 GtkWidget *nb=linphone_gtk_get_widget(main_window,"viewswitch");
29 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
30 GtkWidget *w=g_object_get_data(G_OBJECT(friendlist),"chatview");
31 int idx = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"idx"));
33 g_return_if_fail(w!=NULL);
34 gtk_notebook_remove_page(GTK_NOTEBOOK(nb),idx);
35 linphone_gtk_create_chat_picture(FALSE);
36 g_object_set_data(G_OBJECT(friendlist),"chatview",NULL);
37 g_object_set_data(G_OBJECT(w),"from_message",NULL);
38 g_object_set_data(G_OBJECT(w),"cr",NULL);
39 gtk_widget_destroy(w);
42 const char* get_display_name(const LinphoneAddress *from){
43 const char *display=linphone_address_get_display_name(from);
44 if (display==NULL || display[0]=='\0') {
45 display=linphone_address_get_username(from);
50 GtkWidget *create_tab_chat_header(LinphoneChatRoom *cr,const LinphoneAddress *uri){
51 GtkWidget *w=gtk_hbox_new (FALSE,0);
52 GtkWidget *i=create_pixmap ("chat.png");
54 GtkWidget *image=gtk_image_new_from_stock(GTK_STOCK_CLOSE,GTK_ICON_SIZE_MENU);
55 GtkWidget *b=gtk_button_new();
57 gtk_button_set_image(GTK_BUTTON(b),image);
58 gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE);
59 gtk_widget_set_size_request(b,25,20);
60 g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr);
61 l=gtk_label_new (get_display_name(uri));
62 gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0);
63 gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0);
64 gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0);
65 gtk_widget_show_all(w);
69 void udpate_tab_chat_header(GtkWidget *chat_view,const LinphoneAddress *uri,LinphoneChatRoom *cr){
70 GtkWidget *main_window=linphone_gtk_get_main_window();
71 GtkNotebook *notebook=GTK_NOTEBOOK(linphone_gtk_get_widget(main_window,"viewswitch"));
72 GtkWidget *w=gtk_hbox_new (FALSE,0);
73 GtkWidget *i=create_pixmap ("chat.png");
75 GtkWidget *image=gtk_image_new_from_stock(GTK_STOCK_CLOSE,GTK_ICON_SIZE_MENU);
76 GtkWidget *b=gtk_button_new();
78 gtk_button_set_image(GTK_BUTTON(b),image);
79 gtk_button_set_relief(GTK_BUTTON(b),GTK_RELIEF_NONE);
80 gtk_widget_set_size_request(b,25,20);
81 g_signal_connect_swapped(G_OBJECT(b),"clicked",G_CALLBACK(linphone_gtk_quit_chatroom),cr);
82 l=gtk_label_new (get_display_name(uri));
83 gtk_box_pack_start (GTK_BOX(w),i,FALSE,FALSE,0);
84 gtk_box_pack_start (GTK_BOX(w),l,FALSE,FALSE,0);
85 gtk_box_pack_end(GTK_BOX(w),b,TRUE,TRUE,0);
86 gtk_notebook_set_tab_label(notebook,chat_view,w);
87 gtk_widget_show_all(w);
90 void linphone_gtk_push_text(GtkWidget *w, const LinphoneAddress *from,
91 gboolean me,LinphoneChatRoom *cr,LinphoneChatMessage *msg, gboolean hist){
92 GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(w,"textview"));
93 GtkTextBuffer *buffer=gtk_text_view_get_buffer(text);
94 GtkTextIter iter,begin;
96 char *from_str=linphone_address_as_string_uri_only(from);
97 char *from_message=(char *)g_object_get_data(G_OBJECT(w),"from_message");
98 GList *list=g_object_get_data(G_OBJECT(w),"list");
101 gtk_text_buffer_get_start_iter(buffer,&begin);
102 gtk_text_buffer_get_end_iter(buffer,&iter);
103 off=gtk_text_iter_get_offset(&iter);
104 if(g_strcmp0(from_message,from_str)!=0){
105 gtk_text_buffer_get_iter_at_offset(buffer,&iter,off);
106 gtk_text_buffer_get_end_iter(buffer,&iter);
107 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,get_display_name(from),-1,"bold",me ? "bg":NULL,NULL);
108 gtk_text_buffer_get_end_iter(buffer,&iter);
109 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter," : ",-1,"bold",me ? "bg":NULL,NULL);
110 gtk_text_buffer_get_end_iter(buffer,&iter);
111 gtk_text_buffer_insert(buffer,&iter,"\n",-1);
112 g_object_set_data(G_OBJECT(w),"from_message",from_str);
114 gtk_text_buffer_get_end_iter(buffer,&iter);
115 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,linphone_chat_message_get_text(msg),-1,"margin",me ? "bg":NULL,NULL);
116 gtk_text_buffer_get_end_iter(buffer,&iter);
117 gtk_text_buffer_insert(buffer,&iter,"\n",-1);;
118 gtk_text_buffer_get_end_iter(buffer,&iter);
119 t=linphone_chat_message_get_time(msg);
120 switch (linphone_chat_message_get_state (msg)){
121 case LinphoneChatMessageStateInProgress:
123 list=g_list_append(list,GINT_TO_POINTER(gtk_text_iter_get_line(&iter)));
124 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending .. ",-1,
125 "right","small","italic","font_grey","bg",NULL);
126 g_object_set_data(G_OBJECT(w),"list",list);
129 case LinphoneChatMessageStateDelivered:
131 struct tm *tm=localtime(&t);
133 strftime(buf,80,"%H:%M",tm);
134 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,buf,-1,
135 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
138 case LinphoneChatMessageStateNotDelivered:
139 gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Error",-1,
140 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
142 default : gtk_text_buffer_insert_with_tags_by_name(buffer,&iter,"Sending ..",-1,
143 "right","small","italic","font_grey",me ? "bg":NULL,NULL);
145 gtk_text_buffer_get_end_iter(buffer,&iter);
146 gtk_text_buffer_insert(buffer,&iter,"\n",-1);
147 GtkTextMark *mark=gtk_text_buffer_create_mark(buffer,NULL,&iter,FALSE);
148 gtk_text_view_scroll_mark_onscreen(text,mark);
151 const LinphoneAddress* linphone_gtk_get_used_identity(){
152 LinphoneCore *lc=linphone_gtk_get_core();
153 LinphoneProxyConfig *cfg;
154 linphone_core_get_default_proxy(lc,&cfg);
155 if (cfg) return linphone_address_new(linphone_proxy_config_get_identity(cfg));
156 else return linphone_core_get_primary_contact_parsed(lc);
159 void update_chat_state_message(LinphoneChatMessageState state,LinphoneChatMessage *msg){
160 GtkWidget *main_window=linphone_gtk_get_main_window();
161 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
162 GtkWidget *page=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
163 GList *list=g_object_get_data(G_OBJECT(page),"list");
166 GtkTextView *text=GTK_TEXT_VIEW(linphone_gtk_get_widget(page,"textview"));
167 GtkTextBuffer *b=gtk_text_view_get_buffer(text);
172 gtk_text_buffer_get_iter_at_line(b,&iter,
173 GPOINTER_TO_INT(g_list_nth_data(list,0)));
174 if(gtk_text_iter_get_chars_in_line(&iter) >0) {
175 gtk_text_buffer_get_iter_at_line_offset(b,&start,
176 GPOINTER_TO_INT(g_list_nth_data(list,0)),
177 gtk_text_iter_get_chars_in_line(&iter)-1);
179 gtk_text_buffer_get_iter_at_line_offset(b,&start,
180 GPOINTER_TO_INT(g_list_nth_data(list,0)),0);
182 gtk_text_buffer_get_iter_at_line_offset(b,&end,
183 GPOINTER_TO_INT(g_list_nth_data(list,0)),0);
184 gtk_text_buffer_delete(b,&start,&end);
185 gtk_text_buffer_get_iter_at_line(b,&iter,GPOINTER_TO_INT(g_list_nth_data(list,0)));
188 case LinphoneChatMessageStateInProgress:
191 case LinphoneChatMessageStateDelivered:
194 struct tm *tm=localtime(&t);
196 strftime(buf,80,"%H:%M",tm);
200 case LinphoneChatMessageStateNotDelivered:
203 default : result="Sending ..";
205 gtk_text_buffer_insert_with_tags_by_name(b,&iter,result,-1,
206 "right","small","italic","font_grey","bg",NULL);
207 list=g_list_remove(list,g_list_nth_data(list,0));
208 g_object_set_data(G_OBJECT(page),"list",list);
212 static void on_chat_state_changed(LinphoneChatMessage *msg, LinphoneChatMessageState state, void *user_pointer){
213 update_chat_state_message(state,msg);
216 void linphone_gtk_send_text(){
217 GtkWidget *main_window=linphone_gtk_get_main_window();
218 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
219 GtkWidget *w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
220 GtkWidget *entry=linphone_gtk_get_widget(w,"text_entry");
221 const gchar *entered;
222 LinphoneChatRoom *cr=g_object_get_data(G_OBJECT(w),"cr");
223 entered=gtk_entry_get_text(GTK_ENTRY(entry));
224 if (strlen(entered)>0) {
225 LinphoneChatMessage *msg;
226 msg=linphone_chat_room_create_message(cr,entered);
227 linphone_chat_room_send_message2(cr,msg,on_chat_state_changed,NULL);
228 linphone_gtk_push_text(w,linphone_gtk_get_used_identity(),
230 gtk_entry_set_text(GTK_ENTRY(entry),"");
234 void display_history_message(GtkWidget *chat_view,MSList *messages,const LinphoneAddress *with){
235 if(messages != NULL){
239 for(it=messages;it!=NULL;it=it->next){
240 LinphoneChatMessage *msg=(LinphoneChatMessage *)it->data;
241 from_str=linphone_address_as_string_uri_only(linphone_chat_message_get_from(msg));
242 with_str=linphone_address_as_string_uri_only(with);
243 linphone_gtk_push_text(chat_view,strcmp(from_str,with_str)==0? with :
244 linphone_chat_message_get_from(msg),
245 strcmp(from_str,with_str)==0? FALSE : TRUE,
246 linphone_chat_message_get_chat_room(msg),msg,TRUE);
248 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
254 GtkWidget* linphone_gtk_init_chatroom(LinphoneChatRoom *cr, const LinphoneAddress *with){
255 GtkWidget *chat_view=linphone_gtk_create_widget("main","chatroom_frame");
256 GtkWidget *main_window=linphone_gtk_get_main_window ();
257 GtkNotebook *notebook=(GtkNotebook *)linphone_gtk_get_widget(main_window,"viewswitch");
258 GtkWidget *text=linphone_gtk_get_widget(chat_view,"textview");
273 colorb.green = 60928;
276 with_str=linphone_address_as_string_uri_only(with);
277 linphone_core_set_messages_flag_read(cr,with_str,1);
278 gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text),GTK_WRAP_WORD_CHAR);
279 gtk_text_view_set_editable(GTK_TEXT_VIEW(text),FALSE);
280 gtk_notebook_append_page(notebook,chat_view,create_tab_chat_header(cr,with));
281 idx = gtk_notebook_page_num(notebook, chat_view);
282 gtk_notebook_set_current_page(notebook, idx);
283 gtk_widget_show(chat_view);
284 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
285 g_object_set_data(G_OBJECT(chat_view),"idx",GINT_TO_POINTER(idx));
286 g_object_set_data(G_OBJECT(chat_view),"from_message",NULL);
287 g_object_set_data(G_OBJECT(chat_view),"list",list);
288 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
289 "right","justification", GTK_JUSTIFY_RIGHT,NULL);
290 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
291 "left","justification", GTK_JUSTIFY_LEFT,NULL);
292 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
293 "bold","weight", PANGO_WEIGHT_BOLD,NULL);
294 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
295 "italic","style", PANGO_STYLE_ITALIC,NULL);
296 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
297 "small","size",9*PANGO_SCALE,NULL);
298 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
299 "font_grey","foreground-gdk",&color,NULL);
300 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
301 "margin","indent",10,NULL);
302 gtk_text_buffer_create_tag(gtk_text_view_get_buffer(GTK_TEXT_VIEW(text)),
303 "bg","paragraph-background-gdk",&colorb,NULL);
304 messages = linphone_chat_room_get_history(with_str,cr,10);
305 display_history_message(chat_view,messages,with);
306 button = linphone_gtk_get_widget(chat_view,"send");
307 g_signal_connect_swapped(G_OBJECT(button),"clicked",(GCallback)linphone_gtk_send_text,NULL);
308 entry = linphone_gtk_get_widget(chat_view,"text_entry");
309 g_signal_connect_swapped(G_OBJECT(entry),"activate",(GCallback)linphone_gtk_send_text,NULL);
314 LinphoneChatRoom * linphone_gtk_create_chatroom(const LinphoneAddress *with){
315 LinphoneChatRoom *cr=linphone_core_create_chat_room(linphone_gtk_get_core(),linphone_address_as_string(with));
316 if (!cr) return NULL;
320 void linphone_gtk_load_chatroom(LinphoneChatRoom *cr,const LinphoneAddress *uri,GtkWidget *chat_view){
321 GtkWidget *main_window=linphone_gtk_get_main_window ();
322 LinphoneChatRoom *cr2=(LinphoneChatRoom *)g_object_get_data(G_OBJECT(chat_view),"cr");
323 char *from_str=linphone_address_as_string(linphone_chat_room_get_peer_address (cr2));
324 char *uri_str=linphone_address_as_string(uri);
325 char *uri_only=linphone_address_as_string_uri_only(uri);
326 MSList *messages=NULL;
328 linphone_core_set_messages_flag_read(cr,uri_only,1);
329 if(g_strcmp0(from_str,uri_str)!=0){
330 GtkTextView *text_view=GTK_TEXT_VIEW(linphone_gtk_get_widget(chat_view,"textview"));
333 GtkTextBuffer *text_buffer;
335 text_buffer=gtk_text_view_get_buffer(text_view);
336 gtk_text_buffer_get_bounds(text_buffer, &start, &end);
337 gtk_text_buffer_delete (text_buffer, &start, &end);
338 udpate_tab_chat_header(chat_view,uri,cr);
339 g_object_set_data(G_OBJECT(chat_view),"cr",cr);
340 g_object_set_data(G_OBJECT(linphone_gtk_get_widget(main_window,"contact_list")),"chatview",(gpointer)chat_view);
341 messages = linphone_chat_room_get_history(uri_only,cr,10);
342 g_object_set_data(G_OBJECT(chat_view),"from_message",uri_str);
343 display_history_message(chat_view,messages,uri);
349 void linphone_gtk_chat_destroyed(GtkWidget *w){
350 LinphoneChatRoom *cr=(LinphoneChatRoom*)g_object_get_data(G_OBJECT(w),"cr");
351 linphone_chat_room_destroy(cr);
354 void linphone_gtk_chat_close(GtkWidget *button){
355 GtkWidget *w=gtk_widget_get_toplevel(button);
356 gtk_widget_destroy(w);
360 void linphone_gtk_text_received(LinphoneCore *lc, LinphoneChatRoom *room,
361 LinphoneChatMessage *msg){
362 GtkWidget *main_window=linphone_gtk_get_main_window();
363 GtkWidget *friendlist=linphone_gtk_get_widget(main_window,"contact_list");
366 w=(GtkWidget*)g_object_get_data(G_OBJECT(friendlist),"chatview");
368 linphone_gtk_load_chatroom(room,linphone_chat_message_get_from(msg),w);
370 w=linphone_gtk_init_chatroom(room,linphone_chat_message_get_from(msg));
371 g_object_set_data(G_OBJECT(friendlist),"chatview",(gpointer)w);
372 g_object_set_data(G_OBJECT(friendlist),"from",(gpointer)linphone_chat_message_get_from(msg));
374 get_display_name(linphone_chat_message_get_from(msg));
376 /* Notified when a new message is sent */
377 linphone_gtk_status_icon_set_blinking(TRUE);
379 if(!gtk_window_is_active(GTK_WINDOW(main_window))){
380 if(!GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"is_notified"))){
381 linphone_gtk_notify(NULL,linphone_chat_message_get_text(msg));
382 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(TRUE));
384 g_object_set_data(G_OBJECT(w),"is_notified",GINT_TO_POINTER(FALSE));
388 linphone_gtk_push_text(w,linphone_chat_message_get_from(msg),
389 FALSE,room,msg,FALSE);
390 linphone_gtk_update_chat_picture();
391 //gtk_window_present(GTK_WINDOW(w));
392 /*gtk_window_set_urgency_hint(GTK_WINDOW(w),TRUE);*/