2
* unity-webapps-indicator-context.c
3
* Copyright (C) Canonical LTD 2011
5
* Author: Robert Carr <racarr@canonical.com>
7
unity-webapps is free software: you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as published
9
* by the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* unity-webapps is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
* See the GNU Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
23
#include "unity-webapps-indicator-context.h"
24
#include "unity-webapps-context-private.h"
26
#include "unity-webapps-sanitizer.h"
28
#include "unity-webapps-debug.h"
31
_indicator_context_action_invoked (UnityWebappsGenIndicator *indicator,
35
UnityWebappsIndicatorContext *indicator_context;
36
UnityWebappsIndicatorCallback callback;
38
indicator_context = (UnityWebappsIndicatorContext *)user_data;
40
callback = g_hash_table_lookup (indicator_context->menu_callbacks_by_name, label);
44
UNITY_WEBAPPS_NOTE (INDICATOR, "Menu action invoked: %s", label);
49
callback = g_hash_table_lookup (indicator_context->indicator_callbacks_by_name, label);
53
UNITY_WEBAPPS_NOTE (INDICATOR, "Indicator action invoked: %s", label);
62
presence_changed_notify (GObject *object,
66
UnityWebappsIndicatorContext *indicator_context = (UnityWebappsIndicatorContext *)user_data;
69
UNITY_WEBAPPS_NOTE (INDICATOR, "Presence changed notification");
71
for (walk = indicator_context->presence_callbacks_list; walk != NULL; walk = g_list_next (walk))
73
UnityWebappsPresenceCallback callback;
75
callback = (UnityWebappsPresenceCallback)(walk->data);
82
UnityWebappsIndicatorContext *
83
unity_webapps_indicator_context_new (GDBusConnection *connection, const gchar *name, GError **error)
85
UnityWebappsIndicatorContext *context;
87
g_return_val_if_fail (connection != NULL, NULL);
88
g_return_val_if_fail (name != NULL, NULL);
89
g_return_val_if_fail (g_dbus_is_name (name), NULL);
91
context = g_malloc0 (sizeof (UnityWebappsIndicatorContext));
92
context->indicator_rate = 0;
93
context->presence_callbacks_list = NULL;
95
context->menu_callbacks_by_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
96
context->indicator_callbacks_by_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
98
context->indicator_proxy =
99
unity_webapps_gen_indicator_proxy_new_sync (connection ,
100
G_DBUS_PROXY_FLAGS_NONE,
102
UNITY_WEBAPPS_INDICATOR_PATH,
103
NULL /* Cancellable */,
106
if (error && (*error != NULL))
108
g_critical ("Error creating indicator context proxy object for %s: %s", name, (*error)->message);
113
g_signal_connect (context->indicator_proxy, "action-invoked", G_CALLBACK (_indicator_context_action_invoked), context);
114
g_signal_connect (context->indicator_proxy, "notify::presence", G_CALLBACK (presence_changed_notify), context);
120
unity_webapps_indicator_context_free (UnityWebappsIndicatorContext *context)
122
g_return_if_fail (context != NULL);
124
g_hash_table_destroy (context->menu_callbacks_by_name);
125
g_hash_table_destroy (context->indicator_callbacks_by_name);
126
g_list_free (context->presence_callbacks_list);
129
g_object_unref (G_OBJECT (context->indicator_proxy));
135
show_indicator_complete_callback (GObject *source_object,
139
UnityWebappsGenIndicator *proxy;
142
proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
146
unity_webapps_gen_indicator_call_show_indicator_finish (proxy, res, &error);
150
g_warning ("Error calling ShowIndicator method of Indicator context: %s", error->message);
152
g_error_free (error);
157
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, indicator successfully showed");
160
#define MAXIMUM_NAME_LENGTH 45
163
unity_webapps_indicator_show_indicator (UnityWebappsContext *context,
166
gchar *sanitized_name;
168
g_return_if_fail (context != NULL);
169
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
170
g_return_if_fail (name != NULL);
172
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
177
sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
178
unity_webapps_gen_indicator_call_show_indicator (context->priv->indicator_context->indicator_proxy,
180
NULL /* Cancellable */,
181
show_indicator_complete_callback,
184
g_free (sanitized_name);
186
UNITY_WEBAPPS_NOTE (INDICATOR, "Showed indicator: %s", name);
190
clear_indicator_complete_callback (GObject *source_object,
194
UnityWebappsGenIndicator *proxy;
197
proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
201
unity_webapps_gen_indicator_call_clear_indicator_finish (proxy, res, &error);
205
g_warning ("Error calling ShowIndicator method of Indicator context: %s", error->message);
207
g_error_free (error);
212
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, indicator successfully cleared");
217
unity_webapps_indicator_clear_indicator (UnityWebappsContext *context,
220
gchar *sanitized_name;
222
g_return_if_fail (context != NULL);
223
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
224
g_return_if_fail (name != NULL);
226
sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
228
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
233
g_return_if_fail (name != NULL);
235
unity_webapps_gen_indicator_call_clear_indicator (context->priv->indicator_context->indicator_proxy,
237
NULL /* Cancellable */,
238
clear_indicator_complete_callback,
241
g_free (sanitized_name);
242
UNITY_WEBAPPS_NOTE (INDICATOR, "Cleared indicator: %s", name);
247
add_action_complete_callback (GObject *source_object,
251
UnityWebappsGenIndicator *proxy;
254
proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
258
unity_webapps_gen_indicator_call_add_action_finish (proxy, res, &error);
262
g_warning ("Error calling AddAction method of Indicator context: %s", error->message);
264
g_error_free (error);
269
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, action succesfully added");
272
#define MAXIMUM_LABEL_LENGTH 60
275
unity_webapps_indicator_add_action (UnityWebappsContext *context,
277
UnityWebappsIndicatorCallback callback)
279
gchar *sanitized_label;
281
g_return_if_fail (context != NULL);
282
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
283
g_return_if_fail (label != NULL);
284
g_return_if_fail (callback != NULL);
286
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
291
sanitized_label = unity_webapps_sanitizer_limit_string_argument (label, MAXIMUM_LABEL_LENGTH);
293
unity_webapps_gen_indicator_call_add_action (context->priv->indicator_context->indicator_proxy,
295
NULL /* Cancellable */,
296
add_action_complete_callback,
300
// TODO: Analyze the error case with duplicates.
301
g_hash_table_insert (context->priv->indicator_context->menu_callbacks_by_name, g_strdup (label), callback);
303
g_free (sanitized_label);
305
UNITY_WEBAPPS_NOTE (INDICATOR, "Added action: %s", label);
309
set_property_complete_callback (GObject *source_object,
313
UnityWebappsGenIndicator *proxy;
316
proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
320
unity_webapps_gen_indicator_call_set_property_finish (proxy, res, &error);
324
g_warning ("Error calling SetProperty of Indicator context: %s", error->message);
326
g_error_free (error);
331
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, property succesfully set");
334
#define MAXIMUM_PROPERTY_LENGTH 40
335
#define MAXIMUM_VALUE_LENGTH 1024
339
unity_webapps_indicator_set_property (UnityWebappsContext *context,
341
const gchar *property,
344
gchar *sanitized_name, *sanitized_property, *sanitized_value;
346
g_return_if_fail (context != NULL);
347
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
348
g_return_if_fail (name != NULL);
349
g_return_if_fail (property != NULL);
350
g_return_if_fail (value != NULL);
352
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
357
sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
358
sanitized_property = unity_webapps_sanitizer_limit_string_argument (property, MAXIMUM_PROPERTY_LENGTH);
359
sanitized_value = unity_webapps_sanitizer_limit_string_argument (value, MAXIMUM_VALUE_LENGTH);
361
unity_webapps_gen_indicator_call_set_property (context->priv->indicator_context->indicator_proxy,
365
NULL /* Cancellable */,
366
set_property_complete_callback,
369
g_free (sanitized_name);
370
g_free (sanitized_property);
371
g_free (sanitized_value);
373
UNITY_WEBAPPS_NOTE (INDICATOR, "Setting property of indicator %s: %s->%s", name, property, value);
378
set_property_icon_complete_callback (GObject *source_object,
382
UnityWebappsGenIndicator *proxy;
385
proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
389
unity_webapps_gen_indicator_call_set_property_icon_finish (proxy, res, &error);
393
g_warning ("Error calling SetPropertyIcon of Indicator context: %s", error->message);
395
g_error_free (error);
400
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, icon property succesfully set");
406
unity_webapps_indicator_set_property_icon (UnityWebappsContext *context,
408
const gchar *property,
411
gchar *sanitized_name, *sanitized_property, *sanitized_value;
413
g_return_if_fail (context != NULL);
414
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
415
g_return_if_fail (name != NULL);
416
g_return_if_fail (property != NULL);
417
g_return_if_fail (value != NULL);
419
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
424
sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
425
sanitized_property = unity_webapps_sanitizer_limit_string_argument (property, MAXIMUM_PROPERTY_LENGTH);
426
sanitized_value = unity_webapps_sanitizer_limit_string_argument (value, MAXIMUM_VALUE_LENGTH);
429
unity_webapps_gen_indicator_call_set_property_icon (context->priv->indicator_context->indicator_proxy,
433
NULL /* Cancellable */,
434
set_property_icon_complete_callback,
437
g_free (sanitized_name);
438
g_free (sanitized_property);
439
g_free (sanitized_value);
441
UNITY_WEBAPPS_NOTE (INDICATOR, "Setting icon property of indicator %s: %s->%s", name,
446
unity_webapps_indicator_set_callback (UnityWebappsContext *context,
448
UnityWebappsIndicatorCallback callback)
450
UnityWebappsIndicatorContext *indicator_context;
451
gchar *sanitized_name;
453
g_return_if_fail (context != NULL);
454
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
455
g_return_if_fail (name != NULL);
456
g_return_if_fail (callback != NULL);
458
// The hash table takes ownership of this.
459
sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
462
indicator_context = context->priv->indicator_context;
464
g_hash_table_insert (indicator_context->indicator_callbacks_by_name,
465
(gpointer) sanitized_name,
466
(gpointer) callback);
470
unity_webapps_indicator_get_presence (UnityWebappsContext *context)
472
g_return_val_if_fail (context != NULL, NULL);
473
g_return_val_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context), NULL);
475
if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
479
return g_strdup (unity_webapps_gen_indicator_get_presence (context->priv->indicator_context->indicator_proxy));
483
unity_webapps_indicator_on_presence_changed_callback (UnityWebappsContext *context,
484
UnityWebappsPresenceCallback callback)
486
g_return_if_fail (context != NULL);
487
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
488
g_return_if_fail (callback != NULL);
490
context->priv->indicator_context->presence_callbacks_list = g_list_append (context->priv->indicator_context->presence_callbacks_list, callback);