~alexlauni/libunity-webapps/multiple-messaging-menu-accounts

« back to all changes in this revision

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

  • Committer: Robert Carr
  • Date: 2012-02-04 06:08:49 UTC
  • mto: This revision was merged to the branch mainline in revision 504.
  • Revision ID: racarr@canonical.com-20120204060849-dq4j2ajmn6d5ri2p
Various build system reorganization

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * unity-webapps-launcher-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-launcher-context.h"
24
 
#include "unity-webapps-context-private.h"
25
 
 
26
 
#include "unity-webapps-sanitizer.h"
27
 
#include "unity-webapps-debug.h"
28
 
 
29
 
static void
30
 
_launcher_context_action_invoked (UnityWebappsGenLauncher *launcher,
31
 
                                  const gchar *label,
32
 
                                  gpointer user_data)
33
 
{
34
 
  UnityWebappsLauncherContext *launcher_context;
35
 
  UnityWebappsLauncherCallback callback;
36
 
  
37
 
  launcher_context = (UnityWebappsLauncherContext *)user_data;
38
 
  
39
 
  callback = g_hash_table_lookup (launcher_context->quicklist_callbacks_by_name, label);
40
 
  
41
 
  if (callback != NULL)
42
 
    {
43
 
      UNITY_WEBAPPS_NOTE (LAUNCHER, "Quicklist action invoked: %s", label);
44
 
 
45
 
      callback();
46
 
      return;
47
 
    }
48
 
  
49
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Quicklist action invoked, but we do not have a handler: %s", label);
50
 
}
51
 
 
52
 
UnityWebappsLauncherContext *
53
 
unity_webapps_launcher_context_new (GDBusConnection *connection, const gchar *name, GError **error)
54
 
{
55
 
  UnityWebappsLauncherContext *context;
56
 
  
57
 
  g_return_val_if_fail (connection != NULL, NULL);
58
 
  g_return_val_if_fail (name != NULL, NULL);
59
 
  g_return_val_if_fail (g_dbus_is_name (name), NULL);
60
 
  
61
 
  context = g_malloc0 (sizeof (UnityWebappsLauncherContext));
62
 
  context->launcher_rate = 0;
63
 
  
64
 
  context->launcher_proxy = 
65
 
    unity_webapps_gen_launcher_proxy_new_sync (connection ,
66
 
                                                   G_DBUS_PROXY_FLAGS_NONE,
67
 
                                                   name,
68
 
                                                   UNITY_WEBAPPS_LAUNCHER_PATH, 
69
 
                                                   NULL /* Cancellable */,
70
 
                                                   error);
71
 
  
72
 
  if (error && (*error != NULL))
73
 
    {
74
 
      g_critical ("Error creating launcher context proxy object for %s: %s", name, (*error)->message);
75
 
      
76
 
      return NULL;
77
 
    }
78
 
  
79
 
  context->quicklist_callbacks_by_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
80
 
  
81
 
  g_signal_connect (context->launcher_proxy, "action-invoked", G_CALLBACK (_launcher_context_action_invoked), context);
82
 
  
83
 
  return context;
84
 
}
85
 
 
86
 
void 
87
 
unity_webapps_launcher_context_free (UnityWebappsLauncherContext *context)
88
 
{
89
 
  g_return_if_fail (context != NULL);
90
 
  g_object_unref (G_OBJECT (context->launcher_proxy));
91
 
  
92
 
  g_hash_table_destroy (context->quicklist_callbacks_by_name);
93
 
  
94
 
  g_free (context);
95
 
}
96
 
 
97
 
static void
98
 
set_count_complete_callback (GObject *source_object,
99
 
                             GAsyncResult *res,
100
 
                             gpointer user_data)
101
 
{
102
 
  UnityWebappsGenLauncher *proxy;
103
 
  GError *error;
104
 
  
105
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
106
 
  
107
 
  error = NULL;
108
 
  
109
 
  unity_webapps_gen_launcher_call_set_count_finish (proxy, res, &error);
110
 
  
111
 
  if (error != NULL)
112
 
    {
113
 
      g_warning ("Error calling SetCount method of Launcher context: %s", error->message);
114
 
      
115
 
      g_error_free (error);
116
 
      
117
 
      return;
118
 
    }
119
 
  
120
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, count succesfully set");
121
 
}
122
 
 
123
 
void
124
 
unity_webapps_launcher_set_count (UnityWebappsContext *context,
125
 
                                  gint count)
126
 
{
127
 
  g_return_if_fail (context != NULL);
128
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
129
 
  
130
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
131
 
    {
132
 
      return;
133
 
    }
134
 
  
135
 
  unity_webapps_gen_launcher_call_set_count (context->priv->launcher_context->launcher_proxy,
136
 
                                             count,
137
 
                                             NULL /* Cancellable */,
138
 
                                             set_count_complete_callback,
139
 
                                             context);
140
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting count: %d", count);
141
 
}
142
 
 
143
 
static void
144
 
set_progress_complete_callback (GObject *source_object,
145
 
                             GAsyncResult *res,
146
 
                             gpointer user_data)
147
 
{
148
 
  UnityWebappsGenLauncher *proxy;
149
 
  GError *error;
150
 
  
151
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
152
 
  
153
 
  error = NULL;
154
 
  
155
 
  unity_webapps_gen_launcher_call_set_progress_finish (proxy, res, &error);
156
 
  
157
 
  if (error != NULL)
158
 
    {
159
 
      g_warning ("Error calling SetProgress of Launcher context: %s", error->message);
160
 
      
161
 
      g_error_free (error);
162
 
      
163
 
      return;
164
 
    }
165
 
  
166
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, progress succesfully set");
167
 
}
168
 
 
169
 
void
170
 
unity_webapps_launcher_set_progress (UnityWebappsContext *context,
171
 
                                     gdouble progress)
172
 
{
173
 
  g_return_if_fail (context != NULL);
174
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
175
 
  
176
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
177
 
    {
178
 
      return;
179
 
    }
180
 
  
181
 
  unity_webapps_gen_launcher_call_set_progress (context->priv->launcher_context->launcher_proxy,
182
 
                                                progress,
183
 
                                                NULL /* Cancellable */,
184
 
                                                set_progress_complete_callback,
185
 
                                                context);
186
 
 
187
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting progress: %f", progress);
188
 
}
189
 
 
190
 
static void
191
 
clear_count_complete_callback (GObject *source_object,
192
 
                             GAsyncResult *res,
193
 
                             gpointer user_data)
194
 
{
195
 
  UnityWebappsGenLauncher *proxy;
196
 
  GError *error;
197
 
  
198
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
199
 
  
200
 
  error = NULL;
201
 
  
202
 
  unity_webapps_gen_launcher_call_clear_count_finish (proxy, res, &error);
203
 
  
204
 
  if (error != NULL)
205
 
    {
206
 
      g_warning ("Error calling ClearCount method of Launcher context: %s", error->message);
207
 
      
208
 
      g_error_free (error);
209
 
      
210
 
      return;
211
 
    }
212
 
  
213
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, count succesfully cleared");
214
 
}
215
 
 
216
 
void
217
 
unity_webapps_launcher_clear_count (UnityWebappsContext *context)
218
 
                                  
219
 
{
220
 
  g_return_if_fail (context != NULL);
221
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
222
 
  
223
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
224
 
    {
225
 
      return;
226
 
    }
227
 
  
228
 
  unity_webapps_gen_launcher_call_clear_count (context->priv->launcher_context->launcher_proxy,
229
 
                                               NULL /* Cancellable */,
230
 
                                               clear_count_complete_callback,
231
 
                                               context);
232
 
  
233
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Clearing count");
234
 
}
235
 
 
236
 
static void
237
 
clear_progress_complete_callback (GObject *source_object,
238
 
                                  GAsyncResult *res,
239
 
                                  gpointer user_data)
240
 
{
241
 
  UnityWebappsGenLauncher *proxy;
242
 
  GError *error;
243
 
  
244
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
245
 
  
246
 
  error = NULL;
247
 
  
248
 
  unity_webapps_gen_launcher_call_clear_progress_finish (proxy, res, &error);
249
 
  
250
 
  if (error != NULL)
251
 
    {
252
 
      g_warning ("Error calling ClearProgress method of Launcher context: %s", error->message);
253
 
      
254
 
      g_error_free (error);
255
 
      
256
 
      return;
257
 
    }
258
 
  
259
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, progress succesfully cleared");
260
 
}
261
 
 
262
 
void
263
 
unity_webapps_launcher_clear_progress (UnityWebappsContext *context)
264
 
                                  
265
 
{
266
 
  g_return_if_fail (context != NULL);
267
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
268
 
  
269
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
270
 
    {
271
 
      return;
272
 
    }
273
 
  
274
 
  unity_webapps_gen_launcher_call_clear_progress (context->priv->launcher_context->launcher_proxy,
275
 
                                                  NULL /* Cancellable */,
276
 
                                                  clear_progress_complete_callback,
277
 
                                                  context);
278
 
                                                
279
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Clearing progress");
280
 
}
281
 
 
282
 
static void
283
 
set_urgent_complete_callback (GObject *source_object,
284
 
                             GAsyncResult *res,
285
 
                             gpointer user_data)
286
 
{
287
 
  UnityWebappsGenLauncher *proxy;
288
 
  GError *error;
289
 
  
290
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
291
 
  
292
 
  error = NULL;
293
 
  
294
 
  unity_webapps_gen_launcher_call_set_urgent_finish (proxy, res, &error);
295
 
  
296
 
  if (error != NULL)
297
 
    {
298
 
      g_warning ("Error calling SetUrgent of Launcher context: %s", error->message);
299
 
      
300
 
      g_error_free (error);
301
 
      
302
 
      return;
303
 
    }
304
 
  
305
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, urgent succesfully set");
306
 
}
307
 
 
308
 
void
309
 
unity_webapps_launcher_set_urgent (UnityWebappsContext *context)
310
 
                                  
311
 
{
312
 
  g_return_if_fail (context != NULL);
313
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
314
 
  
315
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
316
 
    {
317
 
      return;
318
 
    }
319
 
  
320
 
  unity_webapps_gen_launcher_call_set_urgent (context->priv->launcher_context->launcher_proxy,
321
 
                                              NULL /* Cancellable */,
322
 
                                              set_urgent_complete_callback,
323
 
                                              context);
324
 
  
325
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting urgent state");
326
 
}
327
 
 
328
 
static void
329
 
add_action_complete_callback (GObject *source_object,
330
 
                              GAsyncResult *res,
331
 
                              gpointer user_data)
332
 
{
333
 
  UnityWebappsGenLauncher *proxy;
334
 
  GError *error;
335
 
  
336
 
  proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
337
 
  
338
 
  error = NULL;
339
 
  
340
 
  unity_webapps_gen_launcher_call_add_action_finish (proxy, res, &error);
341
 
  
342
 
  if (error != NULL)
343
 
    {
344
 
      g_warning ("Error calling AddAction method of Launcher context: %s", error->message);
345
 
      
346
 
      g_error_free (error);
347
 
      
348
 
      return;
349
 
    }
350
 
  
351
 
  UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, action succesfully added");
352
 
}
353
 
 
354
 
 
355
 
#define MAXIMUM_LABEL_LENGTH 60
356
 
 
357
 
void
358
 
unity_webapps_launcher_add_action (UnityWebappsContext *context, const gchar *label, UnityWebappsLauncherCallback callback)
359
 
                                  
360
 
{
361
 
  gchar *sanitized_label;
362
 
 
363
 
  g_return_if_fail (context != NULL);
364
 
  g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
365
 
  g_return_if_fail (callback != NULL);
366
 
 
367
 
 
368
 
  if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
369
 
    {
370
 
      return;
371
 
    }
372
 
  
373
 
  // The Hash Table takes ownership of this.
374
 
  sanitized_label = unity_webapps_sanitizer_limit_string_argument (label, MAXIMUM_LABEL_LENGTH);
375
 
  
376
 
  unity_webapps_gen_launcher_call_add_action (context->priv->launcher_context->launcher_proxy,
377
 
                                              sanitized_label,
378
 
                                              NULL /* Cancellable */,
379
 
                                              add_action_complete_callback,
380
 
                                              context);
381
 
  
382
 
  g_hash_table_insert (context->priv->launcher_context->quicklist_callbacks_by_name, sanitized_label, callback);
383
 
  
384
 
  UNITY_WEBAPPS_NOTE (LAUNCHER, "Adding action: %s", label);
385
 
}
386