~abreu-alexandre/libunity-webapps/bump-2.4.4-fix-distcheck

« back to all changes in this revision

Viewing changes to src/unity-webapps-indicator-context.c

  • Committer: Robert Carr
  • Date: 2012-02-04 06:17:39 UTC
  • mfrom: (503.1.2 build-system-fixes)
  • Revision ID: racarr@canonical.com-20120204061739-9ws9mbfc1n78f1kw
MergeĀ lp:~robertcarr/libunity-webapps/build-system-fixes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * unity-webapps-indicator-context.c
3
 
 * Copyright (C) Canonical LTD 2011
4
 
 *
5
 
 * Author: Robert Carr <racarr@canonical.com>
6
 
 * 
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.
11
 
 * 
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.
16
 
 * 
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/>.";
19
 
 */
20
 
 
21
 
#include <stdio.h>
22
 
 
23
 
#include "unity-webapps-indicator-context.h"
24
 
#include "unity-webapps-context-private.h"
25
 
 
26
 
#include "unity-webapps-sanitizer.h"
27
 
 
28
 
#include "unity-webapps-debug.h"
29
 
 
30
 
static void
31
 
_indicator_context_action_invoked (UnityWebappsGenIndicator *indicator,
32
 
                                   const gchar *label,
33
 
                                   gpointer user_data)
34
 
{
35
 
  UnityWebappsIndicatorContext *indicator_context;
36
 
  UnityWebappsIndicatorCallback callback;
37
 
  
38
 
  indicator_context = (UnityWebappsIndicatorContext *)user_data;
39
 
  
40
 
  callback = g_hash_table_lookup (indicator_context->menu_callbacks_by_name, label);
41
 
  
42
 
  if (callback != NULL)
43
 
    {
44
 
      UNITY_WEBAPPS_NOTE (INDICATOR, "Menu action invoked: %s", label);
45
 
      callback();
46
 
      return;
47
 
    }
48
 
  
49
 
  callback = g_hash_table_lookup (indicator_context->indicator_callbacks_by_name, label);
50
 
  
51
 
  if (callback != NULL)
52
 
    {
53
 
      UNITY_WEBAPPS_NOTE (INDICATOR, "Indicator action invoked: %s", label);
54
 
      callback();
55
 
      return;
56
 
    }
57
 
 
58
 
}
59
 
 
60
 
 
61
 
static void
62
 
presence_changed_notify (GObject *object,
63
 
                         GParamSpec *pspec,
64
 
                         gpointer user_data)
65
 
{
66
 
  UnityWebappsIndicatorContext *indicator_context = (UnityWebappsIndicatorContext *)user_data;
67
 
  GList *walk;
68
 
  
69
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Presence changed notification");
70
 
  
71
 
  for (walk = indicator_context->presence_callbacks_list; walk != NULL; walk = g_list_next (walk))
72
 
    {
73
 
      UnityWebappsPresenceCallback callback;
74
 
      
75
 
      callback = (UnityWebappsPresenceCallback)(walk->data);
76
 
      
77
 
      if (callback)
78
 
        callback();
79
 
    }
80
 
}
81
 
 
82
 
UnityWebappsIndicatorContext *
83
 
unity_webapps_indicator_context_new (GDBusConnection *connection, const gchar *name, GError **error)
84
 
{
85
 
  UnityWebappsIndicatorContext *context;
86
 
  
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);
90
 
  
91
 
  context = g_malloc0 (sizeof (UnityWebappsIndicatorContext));
92
 
  context->indicator_rate = 0;
93
 
  context->presence_callbacks_list = NULL;
94
 
  
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);
97
 
  
98
 
  context->indicator_proxy = 
99
 
    unity_webapps_gen_indicator_proxy_new_sync (connection ,
100
 
                                                G_DBUS_PROXY_FLAGS_NONE,
101
 
                                                name,
102
 
                                                UNITY_WEBAPPS_INDICATOR_PATH, 
103
 
                                                NULL /* Cancellable */,
104
 
                                                error);
105
 
  
106
 
  if (error && (*error != NULL))
107
 
    {
108
 
      g_critical ("Error creating indicator context proxy object for %s: %s", name, (*error)->message);
109
 
      
110
 
      return NULL;
111
 
    }
112
 
  
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);
115
 
  
116
 
  return context;
117
 
}
118
 
 
119
 
void 
120
 
unity_webapps_indicator_context_free (UnityWebappsIndicatorContext *context)
121
 
{
122
 
  g_return_if_fail (context != NULL);
123
 
 
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);
127
 
 
128
 
 
129
 
  g_object_unref (G_OBJECT (context->indicator_proxy));
130
 
  
131
 
  g_free (context);
132
 
}
133
 
 
134
 
static void
135
 
show_indicator_complete_callback (GObject *source_object,
136
 
                                  GAsyncResult *res,
137
 
                                  gpointer user_data)
138
 
{
139
 
  UnityWebappsGenIndicator *proxy;
140
 
  GError *error;
141
 
  
142
 
  proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
143
 
  
144
 
  error = NULL;
145
 
  
146
 
  unity_webapps_gen_indicator_call_show_indicator_finish (proxy, res, &error);
147
 
  
148
 
  if (error != NULL)
149
 
    {
150
 
      g_warning ("Error calling ShowIndicator method of Indicator context: %s", error->message);
151
 
      
152
 
      g_error_free (error);
153
 
      
154
 
      return;
155
 
    }
156
 
  
157
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, indicator successfully showed");
158
 
}
159
 
 
160
 
#define MAXIMUM_NAME_LENGTH 45
161
 
 
162
 
void
163
 
unity_webapps_indicator_show_indicator (UnityWebappsContext *context,
164
 
                                        const gchar *name)
165
 
{
166
 
  gchar *sanitized_name;
167
 
 
168
 
  g_return_if_fail (context != NULL);
169
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
170
 
  g_return_if_fail (name != NULL);
171
 
  
172
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
173
 
    {
174
 
      return;
175
 
    }
176
 
  
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,
179
 
                                                   sanitized_name,
180
 
                                                   NULL /* Cancellable */,
181
 
                                                   show_indicator_complete_callback,
182
 
                                                   context);
183
 
 
184
 
  g_free (sanitized_name);
185
 
 
186
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Showed indicator: %s", name);
187
 
}
188
 
 
189
 
static void
190
 
clear_indicator_complete_callback (GObject *source_object,
191
 
                                  GAsyncResult *res,
192
 
                                  gpointer user_data)
193
 
{
194
 
  UnityWebappsGenIndicator *proxy;
195
 
  GError *error;
196
 
  
197
 
  proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
198
 
  
199
 
  error = NULL;
200
 
  
201
 
  unity_webapps_gen_indicator_call_clear_indicator_finish (proxy, res, &error);
202
 
  
203
 
  if (error != NULL)
204
 
    {
205
 
      g_warning ("Error calling ShowIndicator method of Indicator context: %s", error->message);
206
 
      
207
 
      g_error_free (error);
208
 
      
209
 
      return;
210
 
    }
211
 
  
212
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, indicator successfully cleared");
213
 
}
214
 
 
215
 
 
216
 
void
217
 
unity_webapps_indicator_clear_indicator (UnityWebappsContext *context,
218
 
                                         const gchar *name)
219
 
{
220
 
  gchar *sanitized_name;
221
 
 
222
 
  g_return_if_fail (context != NULL);
223
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
224
 
  g_return_if_fail (name != NULL);
225
 
  
226
 
  sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
227
 
 
228
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
229
 
    {
230
 
      return;
231
 
    }
232
 
  
233
 
  g_return_if_fail (name != NULL);
234
 
  
235
 
  unity_webapps_gen_indicator_call_clear_indicator (context->priv->indicator_context->indicator_proxy,
236
 
                                                    sanitized_name,
237
 
                                                    NULL /* Cancellable */,
238
 
                                                    clear_indicator_complete_callback,
239
 
                                                    context);
240
 
  
241
 
  g_free (sanitized_name);
242
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Cleared indicator: %s", name);
243
 
  
244
 
}
245
 
 
246
 
static void
247
 
add_action_complete_callback (GObject *source_object,
248
 
                              GAsyncResult *res,
249
 
                              gpointer user_data)
250
 
{
251
 
  UnityWebappsGenIndicator *proxy;
252
 
  GError *error;
253
 
  
254
 
  proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
255
 
  
256
 
  error = NULL;
257
 
  
258
 
  unity_webapps_gen_indicator_call_add_action_finish (proxy, res, &error);
259
 
  
260
 
  if (error != NULL)
261
 
    {
262
 
      g_warning ("Error calling AddAction method of Indicator context: %s", error->message);
263
 
      
264
 
      g_error_free (error);
265
 
      
266
 
      return;
267
 
    }
268
 
  
269
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, action succesfully added");
270
 
}
271
 
 
272
 
#define MAXIMUM_LABEL_LENGTH 60
273
 
 
274
 
void
275
 
unity_webapps_indicator_add_action (UnityWebappsContext *context,
276
 
                                    const gchar *label,
277
 
                                    UnityWebappsIndicatorCallback callback)
278
 
{
279
 
  gchar *sanitized_label;
280
 
 
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);
285
 
 
286
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
287
 
    {
288
 
      return;
289
 
    }
290
 
  
291
 
  sanitized_label = unity_webapps_sanitizer_limit_string_argument (label, MAXIMUM_LABEL_LENGTH);
292
 
 
293
 
  unity_webapps_gen_indicator_call_add_action (context->priv->indicator_context->indicator_proxy,
294
 
                                               sanitized_label,
295
 
                                               NULL /* Cancellable */,
296
 
                                               add_action_complete_callback,
297
 
                                               context);
298
 
  
299
 
  
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);
302
 
  
303
 
  g_free (sanitized_label);
304
 
  
305
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Added action: %s", label);
306
 
}
307
 
 
308
 
static void
309
 
set_property_complete_callback (GObject *source_object,
310
 
                              GAsyncResult *res,
311
 
                              gpointer user_data)
312
 
{
313
 
  UnityWebappsGenIndicator *proxy;
314
 
  GError *error;
315
 
  
316
 
  proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
317
 
  
318
 
  error = NULL;
319
 
  
320
 
  unity_webapps_gen_indicator_call_set_property_finish (proxy, res, &error);
321
 
  
322
 
  if (error != NULL)
323
 
    {
324
 
      g_warning ("Error calling SetProperty of Indicator context: %s", error->message);
325
 
      
326
 
      g_error_free (error);
327
 
      
328
 
      return;
329
 
    }
330
 
  
331
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, property succesfully set");
332
 
}
333
 
 
334
 
#define MAXIMUM_PROPERTY_LENGTH 40
335
 
#define MAXIMUM_VALUE_LENGTH 1024
336
 
 
337
 
 
338
 
void 
339
 
unity_webapps_indicator_set_property (UnityWebappsContext *context, 
340
 
                                      const gchar *name, 
341
 
                                      const gchar *property, 
342
 
                                      const gchar *value)
343
 
{
344
 
  gchar *sanitized_name, *sanitized_property, *sanitized_value;
345
 
 
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);
351
 
 
352
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
353
 
    {
354
 
      return;
355
 
    }
356
 
  
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);
360
 
  
361
 
  unity_webapps_gen_indicator_call_set_property (context->priv->indicator_context->indicator_proxy,
362
 
                                                 sanitized_name,
363
 
                                                 sanitized_property,
364
 
                                                 sanitized_value,
365
 
                                                 NULL /* Cancellable */,
366
 
                                                 set_property_complete_callback,
367
 
                                                 context);
368
 
  
369
 
  g_free (sanitized_name);
370
 
  g_free (sanitized_property);
371
 
  g_free (sanitized_value);
372
 
  
373
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Setting property of indicator %s: %s->%s", name, property, value);
374
 
}
375
 
 
376
 
 
377
 
static void
378
 
set_property_icon_complete_callback (GObject *source_object,
379
 
                                     GAsyncResult *res,
380
 
                                     gpointer user_data)
381
 
{
382
 
  UnityWebappsGenIndicator *proxy;
383
 
  GError *error;
384
 
  
385
 
  proxy = UNITY_WEBAPPS_GEN_INDICATOR (source_object);
386
 
  
387
 
  error = NULL;
388
 
  
389
 
  unity_webapps_gen_indicator_call_set_property_icon_finish (proxy, res, &error);
390
 
  
391
 
  if (error != NULL)
392
 
    {
393
 
      g_warning ("Error calling SetPropertyIcon of Indicator context: %s", error->message);
394
 
      
395
 
      g_error_free (error);
396
 
      
397
 
      return;
398
 
    }
399
 
  
400
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, icon property succesfully set");
401
 
}
402
 
 
403
 
 
404
 
 
405
 
void 
406
 
unity_webapps_indicator_set_property_icon (UnityWebappsContext *context, 
407
 
                                      const gchar *name, 
408
 
                                      const gchar *property, 
409
 
                                      const gchar *value)
410
 
{
411
 
  gchar *sanitized_name, *sanitized_property, *sanitized_value;
412
 
 
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);
418
 
 
419
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
420
 
    {
421
 
      return;
422
 
    }
423
 
 
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);
427
 
  
428
 
  
429
 
  unity_webapps_gen_indicator_call_set_property_icon (context->priv->indicator_context->indicator_proxy,
430
 
                                                      sanitized_name,
431
 
                                                      sanitized_property,
432
 
                                                      sanitized_value,
433
 
                                                      NULL /* Cancellable */,
434
 
                                                      set_property_icon_complete_callback,
435
 
                                                      context);
436
 
 
437
 
  g_free (sanitized_name);
438
 
  g_free (sanitized_property);
439
 
  g_free (sanitized_value);
440
 
  
441
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Setting icon property of indicator %s: %s->%s", name,
442
 
                      property, value);
443
 
}
444
 
 
445
 
void 
446
 
unity_webapps_indicator_set_callback (UnityWebappsContext *context, 
447
 
                                      const gchar *name, 
448
 
                                      UnityWebappsIndicatorCallback callback)
449
 
{
450
 
  UnityWebappsIndicatorContext *indicator_context;
451
 
  gchar *sanitized_name;
452
 
 
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);
457
 
  
458
 
  // The hash table takes ownership of this.
459
 
  sanitized_name = unity_webapps_sanitizer_limit_string_argument (name, MAXIMUM_NAME_LENGTH);
460
 
                   
461
 
  
462
 
  indicator_context = context->priv->indicator_context;
463
 
  
464
 
  g_hash_table_insert (indicator_context->indicator_callbacks_by_name, 
465
 
                       (gpointer) sanitized_name, 
466
 
                       (gpointer) callback);
467
 
}
468
 
 
469
 
gchar *
470
 
unity_webapps_indicator_get_presence (UnityWebappsContext *context)
471
 
{
472
 
  g_return_val_if_fail (context != NULL, NULL);
473
 
  g_return_val_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context), NULL);
474
 
  
475
 
  if (unity_webapps_rate_check_indicator_rate_limit (context) == FALSE)
476
 
    {
477
 
      return FALSE;
478
 
    }
479
 
  return g_strdup (unity_webapps_gen_indicator_get_presence (context->priv->indicator_context->indicator_proxy));
480
 
}
481
 
 
482
 
void 
483
 
unity_webapps_indicator_on_presence_changed_callback (UnityWebappsContext *context,
484
 
                                                      UnityWebappsPresenceCallback callback)
485
 
{
486
 
  g_return_if_fail (context != NULL);
487
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
488
 
  g_return_if_fail (callback != NULL);
489
 
  
490
 
  context->priv->indicator_context->presence_callbacks_list = g_list_append (context->priv->indicator_context->presence_callbacks_list, callback);
491
 
}