buddylookup.c 11.1 KB
Newer Older
smorlat's avatar
smorlat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
linphone, gtk-glade interface.
Copyright (C) 2008  Simon MORLAT (simon.morlat@linphone.org)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#include "linphone.h"
#include "sipsetup.h"

23 24
static void linphone_gtk_display_lookup_results(GtkWidget *w, const MSList *results);

smorlat's avatar
smorlat committed
25 26 27 28
enum {
	LOOKUP_RESULT_NAME,
	LOOKUP_RESULT_SIP_URI,
	LOOKUP_RESULT_ADDRESS,
29
	LOOKUP_RESULT_ICON,
smorlat's avatar
smorlat committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43
	LOOKUP_RESULT_NCOL
};

void linphone_gtk_buddy_lookup_window_destroyed(GtkWidget *w){
	guint tid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"typing_timeout"));
	if (tid!=0){
		g_source_remove(tid);
	}
	tid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"buddylookup_processing"));
	if (tid!=0){
		g_source_remove(tid);
	}
}

44 45 46 47 48 49 50 51
static void enable_add_buddy_button(GtkWidget *w){
	gtk_widget_set_sensitive(linphone_gtk_get_widget(w,"add_buddy"),TRUE);
}

static void disable_add_buddy_button(GtkWidget *w){
	gtk_widget_set_sensitive(linphone_gtk_get_widget(w,"add_buddy"),FALSE);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
static void buddy_selection_changed(GtkWidget *w){
	GtkWidget *results=linphone_gtk_get_widget(w,"search_results");
	GtkTreeSelection *select;
	GtkTreeIter iter;
	GtkTreeModel *model;
	enable_add_buddy_button(w);
	
	select = gtk_tree_view_get_selection(GTK_TREE_VIEW(results));
	if (gtk_tree_selection_get_selected (select, &model, &iter))
	{
		GtkTreePath *path=gtk_tree_model_get_path(model,&iter);
		gtk_tree_view_collapse_all(GTK_TREE_VIEW(results));
		gtk_tree_view_expand_row(GTK_TREE_VIEW(results),path,FALSE);
		gtk_tree_path_free(path);
	}
}

69
GtkWidget * linphone_gtk_show_buddy_lookup_window(SipSetupContext *ctx){
70 71
	GtkTreeStore *store;
	GtkCellRenderer *renderer,*pbuf_renderer;
smorlat's avatar
smorlat committed
72 73 74 75
	GtkTreeViewColumn *column;
	GtkTreeSelection *select;
	GtkWidget *w=linphone_gtk_create_window("buddylookup");
	GtkWidget *results=linphone_gtk_get_widget(w,"search_results");
smorlat's avatar
smorlat committed
76
	GtkProgressBar *pb=GTK_PROGRESS_BAR(linphone_gtk_get_widget(w,"progressbar"));
smorlat's avatar
smorlat committed
77
	
78
	store = gtk_tree_store_new(LOOKUP_RESULT_NCOL, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, GDK_TYPE_PIXBUF);
smorlat's avatar
smorlat committed
79
	
80
	/*gtk_tree_view_set_hover_expand(GTK_TREE_VIEW(results),TRUE);*/
smorlat's avatar
smorlat committed
81 82 83 84 85 86
	gtk_tree_view_set_model(GTK_TREE_VIEW(results),GTK_TREE_MODEL(store));
	g_object_unref(G_OBJECT(store));

	renderer = gtk_cell_renderer_text_new ();
	column = gtk_tree_view_column_new_with_attributes (_("Firstname, Lastname"),
                                                   renderer,
87
                                                   "markup", LOOKUP_RESULT_NAME,
smorlat's avatar
smorlat committed
88 89
                                                   NULL);
	g_object_set (G_OBJECT(column), "resizable", TRUE, NULL);
90
	
smorlat's avatar
smorlat committed
91

92 93 94 95 96 97 98 99 100
	pbuf_renderer=gtk_cell_renderer_pixbuf_new();
	g_object_set(G_OBJECT(renderer),"is-expander",TRUE,NULL);
	gtk_tree_view_column_pack_start(column,pbuf_renderer,FALSE);
	gtk_tree_view_column_add_attribute  (column,pbuf_renderer,
                                                         "pixbuf",
                                                         LOOKUP_RESULT_ICON);
	gtk_tree_view_append_column (GTK_TREE_VIEW (results), column);
	
/*
smorlat's avatar
smorlat committed
101 102 103 104 105 106
	column = gtk_tree_view_column_new_with_attributes (_("SIP address"),
                                                   renderer,
                                                   "text", LOOKUP_RESULT_SIP_URI,
                                                   NULL);
	g_object_set (G_OBJECT(column), "resizable", TRUE, NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (results), column);
107 108
*/

smorlat's avatar
smorlat committed
109 110
	select = gtk_tree_view_get_selection (GTK_TREE_VIEW (results));
	gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
111 112
	g_signal_connect_swapped(G_OBJECT(select),"changed",(GCallback)buddy_selection_changed,w);
/*
smorlat's avatar
smorlat committed
113
#if GTK_CHECK_VERSION(2,12,0)
smorlat's avatar
smorlat committed
114
	gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(results),LOOKUP_RESULT_ADDRESS);
smorlat's avatar
smorlat committed
115
#endif
116
*/
smorlat's avatar
smorlat committed
117
	g_object_set_data(G_OBJECT(w),"SipSetupContext",ctx);
smorlat's avatar
smorlat committed
118 119
	g_object_weak_ref(G_OBJECT(w),(GWeakNotify)linphone_gtk_buddy_lookup_window_destroyed,w);
	//g_signal_connect_swapped(G_OBJECT(w),"destroy",(GCallback)linphone_gtk_buddy_lookup_window_destroyed,w);
smorlat's avatar
smorlat committed
120 121
	gtk_progress_bar_set_fraction(pb,0);
	gtk_progress_bar_set_text(pb,NULL);
smorlat's avatar
smorlat committed
122 123
	gtk_dialog_add_button(GTK_DIALOG(w),GTK_STOCK_CLOSE,GTK_RESPONSE_CLOSE);
	g_object_set_data(G_OBJECT(w),"last_state",GINT_TO_POINTER(-1));
124

smorlat's avatar
smorlat committed
125
	gtk_widget_show(w);
126
	return w;
smorlat's avatar
smorlat committed
127 128
}

129 130 131

void linphone_gtk_buddy_lookup_set_keyword(GtkWidget *w, const char *kw){
	gtk_entry_set_text(GTK_ENTRY(linphone_gtk_get_widget(w,"keyword")),kw);
smorlat's avatar
smorlat committed
132 133
}

smorlat's avatar
smorlat committed
134 135 136
static gboolean linphone_gtk_process_buddy_lookup(GtkWidget *w){
	BuddyLookupStatus bls;
	SipSetupContext *ctx;
smorlat's avatar
smorlat committed
137 138 139
	int last_state;
	gchar *tmp;
	MSList *results=NULL;
smorlat's avatar
smorlat committed
140
	GtkProgressBar *pb=GTK_PROGRESS_BAR(linphone_gtk_get_widget(w,"progressbar"));
141 142 143
	BuddyLookupRequest *req=(BuddyLookupRequest*)g_object_get_data(G_OBJECT(w),"buddylookup_request");

	ctx=(SipSetupContext*)g_object_get_data(G_OBJECT(w),"SipSetupContext");	
smorlat's avatar
smorlat committed
144
	last_state=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"last_state"));
145

smorlat's avatar
smorlat committed
146 147 148 149
	if (req==NULL) {
		g_object_set_data(G_OBJECT(w),"buddylookup_processing",GINT_TO_POINTER(0));
		return FALSE;
	}
150
	bls=req->status;
smorlat's avatar
smorlat committed
151
	if (last_state==bls) return TRUE;
smorlat's avatar
smorlat committed
152
	
smorlat's avatar
smorlat committed
153 154 155 156 157
	switch(bls){
		case BuddyLookupNone:
			gtk_progress_bar_set_fraction(pb,0);
			gtk_progress_bar_set_text(pb,NULL);
			break;
smorlat's avatar
smorlat committed
158 159 160 161
		case BuddyLookupFailure:
			gtk_progress_bar_set_fraction(pb,0);
			gtk_progress_bar_set_text(pb,_("Error communicating with server."));
			break;
smorlat's avatar
smorlat committed
162
		case BuddyLookupConnecting:
smorlat's avatar
smorlat committed
163
			gtk_progress_bar_set_fraction(pb,0.2);
smorlat's avatar
smorlat committed
164 165 166
			gtk_progress_bar_set_text(pb,_("Connecting..."));
			break;
		case BuddyLookupConnected:
smorlat's avatar
smorlat committed
167
			gtk_progress_bar_set_fraction(pb,0.4);
smorlat's avatar
smorlat committed
168 169 170
			gtk_progress_bar_set_text(pb,_("Connected"));
			break;
		case BuddyLookupReceivingResponse:
smorlat's avatar
smorlat committed
171
			gtk_progress_bar_set_fraction(pb,0.8);
smorlat's avatar
smorlat committed
172 173 174
			gtk_progress_bar_set_text(pb,_("Receiving data..."));
			break;
		case BuddyLookupDone:
175
			results=req->results;
smorlat's avatar
smorlat committed
176
			linphone_gtk_display_lookup_results(
177 178
					linphone_gtk_get_widget(w,"search_results"),
					results);
smorlat's avatar
smorlat committed
179
			gtk_progress_bar_set_fraction(pb,1);
180 181 182
			tmp=g_strdup_printf(ngettext("Found %i contact",
                        "Found %i contacts", ms_list_size(results)),
                    ms_list_size(results));
smorlat's avatar
smorlat committed
183 184
			gtk_progress_bar_set_text(pb,tmp);
			g_free(tmp);
185 186
			sip_setup_context_buddy_lookup_free(ctx,req);
			g_object_set_data(G_OBJECT(w),"buddylookup_request",NULL);
smorlat's avatar
smorlat committed
187 188
			break;
	}
smorlat's avatar
smorlat committed
189
	g_object_set_data(G_OBJECT(w),"last_state",GINT_TO_POINTER(bls));
smorlat's avatar
smorlat committed
190 191 192 193 194 195 196 197 198 199
	return TRUE;
}

static gboolean keyword_typing_finished(GtkWidget *w){
	guint tid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"typing_timeout"));
	const char *keyword;
	SipSetupContext *ctx;
	if (tid!=0){
		g_source_remove(tid);
	}
smorlat's avatar
smorlat committed
200
	keyword=gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(w,"keyword")));
201
	if (strlen(keyword)>=1){
202
		BuddyLookupRequest *req;
smorlat's avatar
smorlat committed
203 204
		guint tid2;
		ctx=(SipSetupContext*)g_object_get_data(G_OBJECT(w),"SipSetupContext");
205 206 207 208 209 210 211 212
		req=(BuddyLookupRequest*)g_object_get_data(G_OBJECT(w),"buddylookup_request");
		if (req!=NULL){
			sip_setup_context_buddy_lookup_free(ctx,req);
		}
		req=sip_setup_context_create_buddy_lookup_request(ctx);
		buddy_lookup_request_set_key(req,keyword);
		sip_setup_context_buddy_lookup_submit(ctx,req);
		g_object_set_data(G_OBJECT(w),"buddylookup_request",req);
smorlat's avatar
smorlat committed
213 214 215 216
		if (g_object_get_data(G_OBJECT(w),"buddylookup_processing")==NULL){
			tid2=g_timeout_add(20,(GSourceFunc)linphone_gtk_process_buddy_lookup,w);
			g_object_set_data(G_OBJECT(w),"buddylookup_processing",GINT_TO_POINTER(tid2));
		}
smorlat's avatar
smorlat committed
217 218 219 220 221 222 223 224 225 226 227 228 229
	}
	return FALSE;
}

void linphone_gtk_keyword_changed(GtkEditable *e){
	GtkWidget *w=gtk_widget_get_toplevel(GTK_WIDGET(e));
	guint tid=GPOINTER_TO_INT(g_object_get_data(G_OBJECT(w),"typing_timeout"));
	if (tid!=0){
		g_source_remove(tid);
	}
	tid=g_timeout_add(2000,(GSourceFunc)keyword_typing_finished,w);
	g_object_set_data(G_OBJECT(w),"typing_timeout",GINT_TO_POINTER(tid));
}
230 231

static void linphone_gtk_display_lookup_results(GtkWidget *w, const MSList *results){
232
	GtkTreeStore *store;
233 234 235
	GtkTreeIter iter;
	gchar *tmp;
	const MSList *elem;
236 237
	store=GTK_TREE_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
	gtk_tree_store_clear(store);
238
	disable_add_buddy_button(gtk_widget_get_toplevel(w));
239 240
	for(elem=results;elem!=NULL;elem=elem->next){
		BuddyInfo *bi=(BuddyInfo*)elem->data;
241 242 243
		GdkPixbuf *pbuf;
		GtkTreeIter depth1;
		gtk_tree_store_append(store,&iter,NULL);
244
		tmp=g_strdup_printf("%s, %s (%s)",bi->firstname,bi->lastname,bi->displayname);
245
		gtk_tree_store_set(store,&iter,LOOKUP_RESULT_NAME, tmp,-1);
246
		g_free(tmp);
247
		gtk_tree_store_set(store,&iter,LOOKUP_RESULT_SIP_URI, bi->sip_uri,-1);
248
		tmp=g_strdup_printf("%s, %s %s\n%s",bi->address.street, bi->address.zip, bi->address.town, bi->address.country);
249
		gtk_tree_store_set(store,&iter,LOOKUP_RESULT_ADDRESS, tmp,-1);
250
		g_free(tmp);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		if (bi->image_data!=NULL){
			pbuf=_gdk_pixbuf_new_from_memory_at_scale(bi->image_data,bi->image_length,-1,40,TRUE);
			if (pbuf) {
				gtk_tree_store_set(store,&iter,LOOKUP_RESULT_ICON,pbuf,-1);
				g_object_unref(G_OBJECT(pbuf));
			}
		}
		gtk_tree_store_append(store,&depth1,&iter);
		tmp=g_strdup_printf("<big>%s, %s (%s)</big>\n<i>%s</i>, <b>%s</b> %s\n%s\n%s",
			bi->firstname,bi->lastname,bi->displayname,bi->address.street,
			bi->address.zip, bi->address.town, bi->address.country,bi->sip_uri);
		gtk_tree_store_set(store,&depth1,LOOKUP_RESULT_NAME,tmp,-1);
		g_free(tmp);
		if (bi->image_data!=NULL){
			pbuf=_gdk_pixbuf_new_from_memory_at_scale(bi->image_data,bi->image_length,-1,-1,TRUE);
			if (pbuf) {
				gtk_tree_store_set(store,&depth1,LOOKUP_RESULT_ICON,pbuf,-1);
				g_object_unref(G_OBJECT(pbuf));
			}
		}
271 272 273
	}
}

smorlat's avatar
smorlat committed
274 275 276 277 278 279 280 281 282 283 284 285
void linphone_gtk_add_buddy_from_database(GtkWidget *button){
	GtkWidget *w=gtk_widget_get_toplevel(button);
	GtkTreeSelection *select;
	GtkTreeIter iter;
	GtkTreeModel *model;
	select = gtk_tree_view_get_selection(GTK_TREE_VIEW(linphone_gtk_get_widget(w,"search_results")));
	if (gtk_tree_selection_get_selected (select, &model, &iter))
	{
		char *uri;
		char *name;
		char *addr;
		LinphoneFriend *lf;
286
		int presence=linphone_gtk_get_ui_config_int("use_subscribe_notify",1);
smorlat's avatar
smorlat committed
287
		gtk_tree_model_get (model, &iter,LOOKUP_RESULT_SIP_URI , &uri,LOOKUP_RESULT_NAME, &name, -1);
smorlat's avatar
smorlat committed
288
		addr=g_strdup_printf("%s <%s>",name,uri);
289

smorlat's avatar
smorlat committed
290
		lf=linphone_friend_new_with_addr(addr);
291 292
		linphone_friend_set_inc_subscribe_policy(lf,presence ? LinphoneSPAccept : LinphoneSPDeny);
		linphone_friend_send_subscribe(lf,presence);
293 294
		linphone_core_add_friend(linphone_gtk_get_core(),lf);
		linphone_gtk_show_friends();
smorlat's avatar
smorlat committed
295 296 297 298 299 300
		g_free(addr);
		g_free(uri);
		g_free(name);
	}
}

301 302 303 304 305
/*called when double clicking on a contact */
void linphone_gtk_buddy_lookup_contact_activated(GtkWidget *treeview){
	linphone_gtk_add_buddy_from_database(treeview);
	gtk_widget_destroy(gtk_widget_get_toplevel(treeview));
}