status_icon.c 16.7 KB
Newer Older
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) 2015 Belledonne Communications <info@belledonne-communications.com>

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 "status_icon.h"
#include "linphone.h"

23 24 25 26
#ifdef HAVE_GTK_OSX
#include <gtkosxapplication.h>
#endif

27 28
#define STATUS_NOTIFIER_IS_USABLE GLIB_CHECK_VERSION(2, 26, 0)

29 30
#include "status_notifier.h"

31 32 33 34
typedef struct __LinphoneStatusIconDesc _LinphoneStatusIconDesc;

static LinphoneStatusIcon *_linphone_status_icon_instance = NULL;
static const _LinphoneStatusIconDesc *_linphone_status_icon_selected_desc = NULL;
35
static GSList *_linphone_status_icon_impls = NULL;
36 37


38 39
struct _LinphoneStatusIconParams {
	char *title;
40
	char *desc;
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	GtkWidget *menu;
	LinphoneStatusIconOnClickCallback on_click_cb;
	void *user_data;
	int ref;
};

LinphoneStatusIconParams *linphone_status_icon_params_new(void) {
	return g_new0(LinphoneStatusIconParams, 1);
}

LinphoneStatusIconParams *linphone_status_icon_params_ref(LinphoneStatusIconParams *obj) {
	obj->ref++;
	return obj;
}

void linphone_status_icon_params_unref(LinphoneStatusIconParams *obj) {
	obj->ref--;
	if(obj->ref < 0) {
		if(obj->title) g_free(obj->title);
		if(obj->menu) g_object_unref(obj->menu);
		g_free(obj);
	}
}

void linphone_status_icon_params_set_title(LinphoneStatusIconParams *obj, const char *title) {
	if(obj->title) g_free(obj->title);
	if(title) obj->title = g_strdup(title);
	else obj->title = NULL;
}

71 72 73 74 75 76
void linphone_status_icon_params_set_description(LinphoneStatusIconParams *obj, const char *desc) {
	if(obj->desc) g_free(obj->desc);
	if(desc) obj->desc = g_strdup(desc);
	else obj->desc = NULL;
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
void linphone_status_icon_params_set_menu(LinphoneStatusIconParams *obj, GtkWidget *menu) {
	if(obj->menu) g_object_unref(obj->menu);
	if(menu) obj->menu = g_object_ref(menu);
	else obj->menu = NULL;
}

void linphone_status_icon_params_set_on_click_cb(LinphoneStatusIconParams *obj, LinphoneStatusIconOnClickCallback cb, void *user_data) {
	obj->on_click_cb = cb;
	obj->user_data = user_data;
}


typedef void (*LinphoneStatusIconDescInitFunc)(LinphoneStatusIcon *obj);
typedef void (*LinphoneStatusIconDescUninitFunc)(LinphoneStatusIcon *obj);
typedef void (*LinphoneStatusIconDescStartFunc)(LinphoneStatusIcon *obj);
typedef void (*LinphoneStatusIconDescEnableBlinkingFunc)(LinphoneStatusIcon *obj, gboolean enable);
93 94 95 96 97 98 99 100 101 102
typedef void (*LinphoneStatusIconDescIsSupportedResultCb)(const _LinphoneStatusIconDesc *obj, gboolean result, void *user_data);
typedef gboolean (*LinphoneStatusIconDescIsSupportedFunc)(
	const _LinphoneStatusIconDesc *desc,
	gboolean *result,
	LinphoneStatusIconDescIsSupportedResultCb cb,
	void *user_data
);
typedef void (*LinphoneStatusIconDescFindResultCb)(const _LinphoneStatusIconDesc *desc, void *user_data);

struct __LinphoneStatusIconDesc {
103 104 105 106 107
	const char *impl_name;
	LinphoneStatusIconDescInitFunc init;
	LinphoneStatusIconDescUninitFunc uninit;
	LinphoneStatusIconDescStartFunc start;
	LinphoneStatusIconDescEnableBlinkingFunc enable_blinking;
108 109
	LinphoneStatusIconDescIsSupportedFunc is_supported;
};
110

111 112 113 114 115 116 117 118
static gboolean _linphone_status_icon_desc_is_supported(
	const _LinphoneStatusIconDesc *desc,
	gboolean *result,
	LinphoneStatusIconDescIsSupportedResultCb cb,
	void *user_data) {
	
	return desc->is_supported(desc, result, cb, user_data);
}
119

120
typedef struct {
121
	GSList *i;
122 123 124 125
	LinphoneStatusIconDescFindResultCb cb;
	void *user_data;
} _LinphoneStatusIconDescSearchCtx;

126
static void _linphone_status_icon_desc_is_supported_result_cb(
127 128 129 130 131
	const _LinphoneStatusIconDesc *desc,
	gboolean result,
	_LinphoneStatusIconDescSearchCtx *ctx) {
	
	if(!result) {
132 133
		;
		for(; ctx->i; ctx->i = g_slist_next(ctx->i)) {
134
			if(_linphone_status_icon_desc_is_supported(
135
				(const _LinphoneStatusIconDesc *)g_slist_nth_data(ctx->i, 0),
136
				&result,
137
				(LinphoneStatusIconDescIsSupportedResultCb)_linphone_status_icon_desc_is_supported_result_cb,
138 139 140 141 142 143
				ctx)) {
			
				if(result) break;
			} else return;
		}
	}
144
	if(ctx->cb) ctx->cb((const _LinphoneStatusIconDesc *)g_slist_nth_data(ctx->i, 0), ctx->user_data);
145 146 147 148 149 150 151 152 153 154 155 156 157
	g_free(ctx);
}

static gboolean _linphone_status_icon_find_first_available_impl(
	const _LinphoneStatusIconDesc **desc,
	LinphoneStatusIconDescFindResultCb cb,
	void *user_data) {
	
	gboolean result;
	_LinphoneStatusIconDescSearchCtx *ctx = g_new0(_LinphoneStatusIconDescSearchCtx, 1);
	ctx->cb = cb;
	ctx->user_data = user_data;
	
158
	for(ctx->i=_linphone_status_icon_impls; ctx->i; ctx->i = g_slist_next(ctx->i)) {
159
		if(_linphone_status_icon_desc_is_supported(
160
			(const _LinphoneStatusIconDesc *)g_slist_nth_data(ctx->i, 0),
161
			&result,
162
			(LinphoneStatusIconDescIsSupportedResultCb)_linphone_status_icon_desc_is_supported_result_cb,
163 164 165
			ctx)) {
			
			if(result) {
166
				*desc = (const _LinphoneStatusIconDesc *)g_slist_nth_data(ctx->i, 0);
167 168 169 170 171 172 173 174 175 176 177
				goto sync_return;
			}
		} else {
			return 0;
		}
	}
	*desc = NULL;
	
sync_return:
	g_free(ctx);
	return 1;
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
}


struct _LinphoneStatusIcon {
	const _LinphoneStatusIconDesc *desc;
	LinphoneStatusIconParams *params;
	void *data;
};

static LinphoneStatusIcon *_linphone_status_icon_new(const _LinphoneStatusIconDesc *desc) {
	LinphoneStatusIcon *si = (LinphoneStatusIcon *)g_new0(LinphoneStatusIcon, 1);
	si->desc = desc;
	if(desc->init) desc->init(si);
	return si;
}

static void _linphone_status_icon_free(LinphoneStatusIcon *obj) {
	if(obj->desc->uninit) obj->desc->uninit(obj->data);
196
	if(obj->params) linphone_status_icon_params_unref(obj->params);
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	g_free(obj);
}

const char *linphone_status_icon_get_implementation_name(const LinphoneStatusIcon *obj) {
	return obj->desc->impl_name;
}

void linphone_status_icon_start(LinphoneStatusIcon *obj, LinphoneStatusIconParams *params) {
	obj->params = linphone_status_icon_params_ref(params);
	obj->desc->start(obj);
}

void linphone_status_icon_enable_blinking(LinphoneStatusIcon *obj, gboolean enable) {
	obj->desc->enable_blinking(obj, enable);
}

static void _linphone_status_icon_notify_click(LinphoneStatusIcon *obj) {
	if(obj->params->on_click_cb) {
		obj->params->on_click_cb(obj, obj->params->user_data);
	}
}


220 221 222 223 224 225 226 227
void _linphone_status_icon_init_cb(const _LinphoneStatusIconDesc *desc, void *user_data) {
	void **ctx = (void **)user_data;
	LinphoneStatusIconReadyCb cb = (LinphoneStatusIconReadyCb)ctx[0];
	_linphone_status_icon_selected_desc = desc;
	if(cb) cb(ctx[1]);
	g_free(ctx);
}

Ghislain MARY's avatar
Ghislain MARY committed
228 229 230 231 232 233 234 235 236
#if STATUS_NOTIFIER_IS_USABLE
static const _LinphoneStatusIconDesc _linphone_status_icon_impl_status_notifier;
#endif
#ifdef HAVE_GTK_OSX
static const _LinphoneStatusIconDesc _linphone_status_icon_impl_gtkosx_app_desc;
#else
static const _LinphoneStatusIconDesc _linphone_status_icon_impl_gtk_desc;
#endif

237 238
void _linphone_status_icon_create_implementations_list(void) {
#if STATUS_NOTIFIER_IS_USABLE
239
	_linphone_status_icon_impls = g_slist_append(_linphone_status_icon_impls, (void *)&_linphone_status_icon_impl_status_notifier);
240 241
#endif
#if HAVE_GTK_OSX
242
	_linphone_status_icon_impls = g_slist_append(_linphone_status_icon_impls, (void *)&_linphone_status_icon_impl_gtkosx_app_desc);
243
#else
244
	_linphone_status_icon_impls = g_slist_append(_linphone_status_icon_impls, (void *)&_linphone_status_icon_impl_gtk_desc);
245 246 247
#endif
}

248 249
gboolean linphone_status_icon_init(LinphoneStatusIconReadyCb ready_cb, void *user_data) {
	const _LinphoneStatusIconDesc *desc;
250 251 252 253 254
	void **ctx;
	
	_linphone_status_icon_create_implementations_list();
	
	ctx = g_new(void *, 2);
255 256
	ctx[0] = ready_cb;
	ctx[1] = user_data;
257 258 259 260 261 262
	if(_linphone_status_icon_find_first_available_impl(&desc, _linphone_status_icon_init_cb, ctx)) {
		_linphone_status_icon_selected_desc = desc;
		g_free(ctx);
		return 1;
	} else return 0;
}
263

264 265
void linphone_status_icon_uninit(void) {
	if(_linphone_status_icon_instance) _linphone_status_icon_free(_linphone_status_icon_instance);
266
	if(_linphone_status_icon_impls) g_slist_free(_linphone_status_icon_impls);
267 268 269 270
}

LinphoneStatusIcon *linphone_status_icon_get(void) {
	if(_linphone_status_icon_instance == NULL) {
271 272
		if(_linphone_status_icon_selected_desc)
			_linphone_status_icon_instance = _linphone_status_icon_new(_linphone_status_icon_selected_desc);
273 274 275 276 277 278
	}
	return _linphone_status_icon_instance;
}


/* GtkStatusIcon implementation */
279
#ifndef HAVE_GTK_OSX
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
static void _linphone_status_icon_impl_gtk_on_click_cb(LinphoneStatusIcon *si) {
	_linphone_status_icon_notify_click(si);
}

static void _linphone_status_icon_impl_gtk_popup_menu(GtkStatusIcon *status_icon, guint button, guint activate_time, LinphoneStatusIcon *si){
	GtkWidget *menu = si->params->menu;
	gtk_menu_popup(GTK_MENU(menu),NULL,NULL,gtk_status_icon_position_menu,status_icon,button,activate_time);
}

static void _linphone_status_icon_impl_gtk_init(LinphoneStatusIcon *si) {
	const char *icon_path=linphone_gtk_get_ui_config("icon",LINPHONE_ICON);
	const char *call_icon_path=linphone_gtk_get_ui_config("start_call_icon","startcall-green.png");
	GdkPixbuf *pbuf=create_pixbuf(icon_path);
	GtkStatusIcon *icon=gtk_status_icon_new_from_pixbuf(pbuf);
	g_signal_connect_swapped(G_OBJECT(icon),"activate", G_CALLBACK(_linphone_status_icon_impl_gtk_on_click_cb), si);
	g_signal_connect(G_OBJECT(icon), "popup-menu", G_CALLBACK(_linphone_status_icon_impl_gtk_popup_menu), si);
296
	g_object_set_data_full(G_OBJECT(icon),"icon",pbuf, g_object_unref);
297 298
	g_object_unref(pbuf);
	pbuf=create_pixbuf(call_icon_path);
299
	g_object_set_data_full(G_OBJECT(icon),"call_icon",pbuf, g_object_unref);
300 301 302 303 304 305 306 307 308 309 310
	g_object_unref(pbuf);
	si->data = icon;
}

// static void _linphone_status_icon_impl_gtk_uninit(LinphoneStatusIcon *si) {
// 	g_object_unref((GtkStatusIcon *)si->data);
// }

static void _linphone_status_icon_impl_gtk_start(LinphoneStatusIcon *si) {
	GtkStatusIcon *icon = GTK_STATUS_ICON(si->data);
#if GTK_CHECK_VERSION(2,20,2)
311 312 313
	char *name = g_strdup_printf("%s - %s", si->params->title, si->params->desc);
	gtk_status_icon_set_name(icon, name);
	g_free(name);
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
#endif
	gtk_status_icon_set_visible(icon,TRUE);
}

static gboolean _linphone_status_icon_impl_gtk_do_icon_blink_cb(GtkStatusIcon *gi){
	GdkPixbuf *call_icon=g_object_get_data(G_OBJECT(gi),"call_icon");
	GdkPixbuf *normal_icon=g_object_get_data(G_OBJECT(gi),"icon");
	GdkPixbuf *cur_icon=gtk_status_icon_get_pixbuf(gi);
	if (cur_icon==call_icon){
		gtk_status_icon_set_from_pixbuf(gi,normal_icon);
	}else{
		gtk_status_icon_set_from_pixbuf(gi,call_icon);
	}
	return TRUE;
}

static void _linphone_status_icon_impl_enable_blinking(LinphoneStatusIcon *si, gboolean val) {
	GtkStatusIcon *icon = GTK_STATUS_ICON(si->data);
	guint tout;
	tout=(unsigned)GPOINTER_TO_INT(g_object_get_data(G_OBJECT(icon),"timeout"));
	if (val && tout==0){
		tout=g_timeout_add(500,(GSourceFunc)_linphone_status_icon_impl_gtk_do_icon_blink_cb,icon);
		g_object_set_data(G_OBJECT(icon),"timeout",GINT_TO_POINTER(tout));
	}else if (!val && tout!=0){
		GdkPixbuf *normal_icon=g_object_get_data(G_OBJECT(icon),"icon");
		g_source_remove(tout);
		g_object_set_data(G_OBJECT(icon),"timeout",NULL);
		gtk_status_icon_set_from_pixbuf(icon,normal_icon);
	}
}

345 346 347 348 349 350 351
static gboolean _linphone_status_icon_impl_is_supported(
	const _LinphoneStatusIconDesc *desc,
	gboolean *result,
	LinphoneStatusIconDescIsSupportedResultCb cb,
	void *user_data) {
	
	*result = 1;
352 353 354 355
	return 1;
}

static const _LinphoneStatusIconDesc _linphone_status_icon_impl_gtk_desc = {
356 357 358 359 360 361
	"gtk_status_icon",
	_linphone_status_icon_impl_gtk_init,
	NULL,
	_linphone_status_icon_impl_gtk_start,
	_linphone_status_icon_impl_enable_blinking,
	_linphone_status_icon_impl_is_supported
362
};
363 364 365 366 367
#endif


/* GtkosxApplication implementation */
#ifdef HAVE_GTK_OSX
François Grisez's avatar
François Grisez committed
368
static void _linphone_status_icon_impl_gtkosx_app_enable_blinking(LinphoneStatusIcon *si, gboolean val) {
369 370 371 372 373 374 375 376 377 378
	GtkosxApplication *theMacApp=gtkosx_application_get();
	gint *attention_id = (gint *)&si->data;
	if (val && *attention_id == 0) {
		*attention_id=gtkosx_application_attention_request(theMacApp,CRITICAL_REQUEST);
	} else if(!val && *attention_id != 0) {
		gtkosx_application_cancel_attention_request(theMacApp, *attention_id);
		*attention_id = 0;
	}
}

379
static gboolean _linphone_status_icon_impl_gtkosx_app_is_supported(
380 381 382 383 384 385
	const _LinphoneStatusIconDesc *desc,
	gboolean *result,
	LinphoneStatusIconDescIsSupportedResultCb cb,
	void *user_data) {
	
	*result = 1;
386 387 388 389 390 391 392 393 394
	return 1;
}

static const _LinphoneStatusIconDesc _linphone_status_icon_impl_gtkosx_app_desc = {
	.impl_name = "gtkosx_application",
	.init = NULL,
	.uninit = NULL,
	.start = NULL,
	.enable_blinking = _linphone_status_icon_impl_gtkosx_app_enable_blinking,
François Grisez's avatar
François Grisez committed
395
	.is_supported = _linphone_status_icon_impl_gtkosx_app_is_supported
396 397 398
};
#endif

399

400
/* Implementation based on the StatusNotifier Freedesktop standard */
401
#if STATUS_NOTIFIER_IS_USABLE
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
typedef struct {
	int x;
	int y;
} _LinphoneStatusIconPosition;

static void _linphone_status_icon_impl_sn_init(LinphoneStatusIcon *si) {
	si->data = bc_status_notifier_new();
}

static void _linphone_status_icon_impl_sn_uninit(LinphoneStatusIcon *si) {
	bc_status_notifier_unref((BcStatusNotifier *)si->data);
}

static void _linphone_status_icon_impl_sn_activated_cb(BcStatusNotifier *sn, int x, int y, void *user_data) {
	LinphoneStatusIcon *si = (LinphoneStatusIcon *)user_data;
	_linphone_status_icon_notify_click(si);
}

static void _linphone_status_icon_impr_sn_get_position(GtkMenu *menu, int *x, int *y, gboolean *push_in, gpointer data) {
	_LinphoneStatusIconPosition *pos = (_LinphoneStatusIconPosition *)data;
	*x = pos->x;
	*y = pos->y;
	*push_in = TRUE;
}

static void _linphone_status_icon_impl_sn_menu_called_cb(BcStatusNotifier *sn, int x, int y, void *user_data) {
	LinphoneStatusIcon *si = (LinphoneStatusIcon *)user_data;
	GtkWidget *menu = si->params->menu;
	_LinphoneStatusIconPosition pos = {x, y};
	gtk_menu_popup(
		GTK_MENU(menu),
		NULL,
		NULL,
		_linphone_status_icon_impr_sn_get_position,
		&pos,
		0,
		gtk_get_current_event_time()
	);
}

static void _linphone_status_icon_impl_sn_start(LinphoneStatusIcon *si) {
	BcStatusNotifier *sn = (BcStatusNotifier *)si->data;
	BcStatusNotifierParams *params;
445
	BcStatusNotifierToolTip *tooltip = bc_status_notifier_tool_tip_new("linphone", si->params->title, si->params->desc);
446 447 448 449 450 451 452 453 454
	BcStatusNotifierSignalsVTable vtable = {NULL};
	
	vtable.activate_called_cb = _linphone_status_icon_impl_sn_activated_cb;
	vtable.context_menu_called_cb = _linphone_status_icon_impl_sn_menu_called_cb;
	
	params = bc_status_notifier_params_new();
	bc_status_notifier_params_set_dbus_prefix(params, "org.kde");
	bc_status_notifier_params_set_category(params, BcStatusNotifierCategoryCommunications);
	bc_status_notifier_params_set_id(params, "linphone");
455
	bc_status_notifier_params_set_title(params, si->params->title);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	bc_status_notifier_params_set_icon_name(params, "linphone");
	bc_status_notifier_params_set_tool_tip(params, tooltip);
	bc_status_notifier_params_set_vtable(params, &vtable, si);
	
	bc_status_notifier_start(sn, params, NULL, NULL);
	
	bc_status_notifier_tool_tip_unref(tooltip);
	bc_status_notifier_params_unref(params);
}

static void _linphone_status_icon_impl_sn_enable_blinking(LinphoneStatusIcon *si, gboolean val) {
	BcStatusNotifier *sn = (BcStatusNotifier *)si->data; 
	if(val) {
		bc_status_notifier_update_status(sn, BcStatusNotifierStatusNeedsAttention);
	} else {
		bc_status_notifier_update_status(sn, BcStatusNotifierStatusPassive);
	}
}

static void _linphone_status_icon_impl_is_supported_cb(const char *prefix, gboolean result, void **data) {
	_LinphoneStatusIconDesc *desc = (_LinphoneStatusIconDesc *)data[0];
	LinphoneStatusIconDescIsSupportedResultCb cb = (LinphoneStatusIconDescIsSupportedResultCb)data[1];
	if(cb) cb(desc, result, data[2]);
	g_free(data);
	g_free(desc);
}

static gboolean _linphone_status_icon_impl_sn_is_supported(
	const _LinphoneStatusIconDesc *desc,
	gboolean *result,
	LinphoneStatusIconDescIsSupportedResultCb cb,
	void *user_data) {
	
	_LinphoneStatusIconDesc *desc2 = g_new(_LinphoneStatusIconDesc, 1);
	void **data = g_new(void *, 3);
	
	*desc2 = *desc;
	data[0] = desc2;
	data[1] = cb;
	data[2] = user_data;
	bc_status_notifier_is_supported(
		"org.kde",
		(BcStatusNotifierSupportDetectionCb)_linphone_status_icon_impl_is_supported_cb,
		data
	);
	return 0;
}

static const _LinphoneStatusIconDesc _linphone_status_icon_impl_status_notifier = {
	.impl_name = "status_notifier",
	.init = _linphone_status_icon_impl_sn_init,
	.uninit = _linphone_status_icon_impl_sn_uninit,
	.start = _linphone_status_icon_impl_sn_start,
	.enable_blinking = _linphone_status_icon_impl_sn_enable_blinking,
	.is_supported = _linphone_status_icon_impl_sn_is_supported
};
512
#endif