buddylookup.c 6.94 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 29 30 31 32
enum {
	LOOKUP_RESULT_NAME,
	LOOKUP_RESULT_SIP_URI,
	LOOKUP_RESULT_ADDRESS,
	LOOKUP_RESULT_NCOL
};

void linphone_gtk_buddy_lookup_window_destroyed(GtkWidget *w){
smorlat's avatar
smorlat committed
33
	g_message("BuddyLookup window is destroyed !");
smorlat's avatar
smorlat committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	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);
	}
	
}

void linphone_gtk_show_buddy_lookup_window(SipSetupContext *ctx){
	GtkListStore *store;
	GtkCellRenderer *renderer;
	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
52
	GtkProgressBar *pb=GTK_PROGRESS_BAR(linphone_gtk_get_widget(w,"progressbar"));
smorlat's avatar
smorlat committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	
	store = gtk_list_store_new(LOOKUP_RESULT_NCOL, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
	
	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,
                                                   "text", LOOKUP_RESULT_NAME,
                                                   NULL);
	g_object_set (G_OBJECT(column), "resizable", TRUE, NULL);
	gtk_tree_view_append_column (GTK_TREE_VIEW (results), column);

	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);
	
	select = gtk_tree_view_get_selection (GTK_TREE_VIEW (results));
	gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);

	gtk_tree_view_set_tooltip_column(GTK_TREE_VIEW(results),LOOKUP_RESULT_ADDRESS);
	g_object_set_data(G_OBJECT(w),"SipSetupContext",ctx);
smorlat's avatar
smorlat committed
79 80 81 82
	//g_object_weak_ref(G_OBJECT(w),(GWeakNotify)linphone_gtk_buddy_lookup_window_destroyed,w);
	g_signal_connect_swapped(G_OBJECT(results),"destroy",(GCallback)linphone_gtk_buddy_lookup_window_destroyed,w);
	gtk_progress_bar_set_fraction(pb,0);
	gtk_progress_bar_set_text(pb,NULL);
smorlat's avatar
smorlat committed
83 84 85 86 87 88 89 90 91
	gtk_widget_show(w);
}

static gboolean linphone_gtk_process_buddy_lookup(GtkWidget *w){
	BuddyLookupStatus bls;
	SipSetupContext *ctx;
	GtkProgressBar *pb=GTK_PROGRESS_BAR(linphone_gtk_get_widget(w,"progressbar"));
	ctx=(SipSetupContext*)g_object_get_data(G_OBJECT(w),"SipSetupContext");
	bls=sip_setup_context_get_buddy_lookup_status(ctx);
92
	MSList *results=NULL;
smorlat's avatar
smorlat committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	switch(bls){
		case BuddyLookupNone:
		case BuddyLookupFailure:
			gtk_progress_bar_set_fraction(pb,0);
			gtk_progress_bar_set_text(pb,NULL);
			break;
		case BuddyLookupConnecting:
			gtk_progress_bar_set_fraction(pb,20);
			gtk_progress_bar_set_text(pb,_("Connecting..."));
			break;
		case BuddyLookupConnected:
			gtk_progress_bar_set_fraction(pb,40);
			gtk_progress_bar_set_text(pb,_("Connected"));
			break;
		case BuddyLookupReceivingResponse:
			gtk_progress_bar_set_fraction(pb,80);
			gtk_progress_bar_set_text(pb,_("Receiving data..."));
			break;
		case BuddyLookupDone:
			gtk_progress_bar_set_fraction(pb,100);
			gtk_progress_bar_set_text(pb,_("Done !"));
114 115 116 117 118 119 120
			sip_setup_context_get_buddy_lookup_results(ctx,&results);
			if (results){
				linphone_gtk_display_lookup_results(
					linphone_gtk_get_widget(w,"search_results"),
					results);
				sip_setup_context_free_results(results);
			}
smorlat's avatar
smorlat committed
121 122 123 124 125 126 127 128 129 130 131 132
			break;
	}
	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
133
	keyword=gtk_entry_get_text(GTK_ENTRY(linphone_gtk_get_widget(w,"keyword")));
smorlat's avatar
smorlat committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	if (strlen(keyword)>=4){
		guint tid2;
		ctx=(SipSetupContext*)g_object_get_data(G_OBJECT(w),"SipSetupContext");
		sip_setup_context_lookup_buddy(ctx,keyword);
		tid2=g_timeout_add(250,(GSourceFunc)linphone_gtk_process_buddy_lookup,w);
		g_object_set_data(G_OBJECT(w),"buddylookup_processing",GINT_TO_POINTER(tid2));
	}
	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));
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173

static void linphone_gtk_display_lookup_results(GtkWidget *w, const MSList *results){
	GtkListStore *store;
	GtkTreeIter iter;
	gchar *tmp;
	const MSList *elem;
	store=GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(w)));
	gtk_list_store_clear(store);
	for(elem=results;elem!=NULL;elem=elem->next){
		BuddyInfo *bi=(BuddyInfo*)elem->data;
		gtk_list_store_append(store,&iter);
		tmp=g_strdup_printf("%s, %s (%s)",bi->firstname,bi->lastname,bi->displayname);
		gtk_list_store_set(store,&iter,LOOKUP_RESULT_NAME, tmp,-1);
		g_free(tmp);
		gtk_list_store_set(store,&iter,LOOKUP_RESULT_SIP_URI, bi->sip_uri,-1);
		tmp=g_strdup_printf("%s, %s %s\n%s",bi->address.street, bi->address.zip, bi->address.town, bi->address.country);
		gtk_list_store_set(store,&iter,LOOKUP_RESULT_ADDRESS, tmp,-1);
		g_free(tmp);
	}
}

smorlat's avatar
smorlat committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
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;
		gtk_tree_model_get (model, &iter,LOOKUP_RESULT_SIP_URI , &uri,LOOKUP_RESULT_NAME, &name -1);
		addr=g_strdup_printf("%s <%s>",name,uri);
		lf=linphone_friend_new_with_addr(addr);
		g_free(addr);
		g_free(uri);
		g_free(name);
		linphone_gtk_show_contact(lf);
	}
}