2
* unity-webapps-launcher-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-launcher-context.h"
24
#include "unity-webapps-context-private.h"
26
#include "unity-webapps-sanitizer.h"
27
#include "unity-webapps-debug.h"
30
_launcher_context_action_invoked (UnityWebappsGenLauncher *launcher,
34
UnityWebappsLauncherContext *launcher_context;
35
UnityWebappsLauncherCallback callback;
37
launcher_context = (UnityWebappsLauncherContext *)user_data;
39
callback = g_hash_table_lookup (launcher_context->quicklist_callbacks_by_name, label);
43
UNITY_WEBAPPS_NOTE (LAUNCHER, "Quicklist action invoked: %s", label);
49
UNITY_WEBAPPS_NOTE (LAUNCHER, "Quicklist action invoked, but we do not have a handler: %s", label);
52
UnityWebappsLauncherContext *
53
unity_webapps_launcher_context_new (GDBusConnection *connection, const gchar *name, GError **error)
55
UnityWebappsLauncherContext *context;
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);
61
context = g_malloc0 (sizeof (UnityWebappsLauncherContext));
62
context->launcher_rate = 0;
64
context->launcher_proxy =
65
unity_webapps_gen_launcher_proxy_new_sync (connection ,
66
G_DBUS_PROXY_FLAGS_NONE,
68
UNITY_WEBAPPS_LAUNCHER_PATH,
69
NULL /* Cancellable */,
72
if (error && (*error != NULL))
74
g_critical ("Error creating launcher context proxy object for %s: %s", name, (*error)->message);
79
context->quicklist_callbacks_by_name = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
81
g_signal_connect (context->launcher_proxy, "action-invoked", G_CALLBACK (_launcher_context_action_invoked), context);
87
unity_webapps_launcher_context_free (UnityWebappsLauncherContext *context)
89
g_return_if_fail (context != NULL);
90
g_object_unref (G_OBJECT (context->launcher_proxy));
92
g_hash_table_destroy (context->quicklist_callbacks_by_name);
98
set_count_complete_callback (GObject *source_object,
102
UnityWebappsGenLauncher *proxy;
105
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
109
unity_webapps_gen_launcher_call_set_count_finish (proxy, res, &error);
113
g_warning ("Error calling SetCount method of Launcher context: %s", error->message);
115
g_error_free (error);
120
UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, count succesfully set");
124
unity_webapps_launcher_set_count (UnityWebappsContext *context,
127
g_return_if_fail (context != NULL);
128
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
130
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
135
unity_webapps_gen_launcher_call_set_count (context->priv->launcher_context->launcher_proxy,
137
NULL /* Cancellable */,
138
set_count_complete_callback,
140
UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting count: %d", count);
144
set_progress_complete_callback (GObject *source_object,
148
UnityWebappsGenLauncher *proxy;
151
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
155
unity_webapps_gen_launcher_call_set_progress_finish (proxy, res, &error);
159
g_warning ("Error calling SetProgress of Launcher context: %s", error->message);
161
g_error_free (error);
166
UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, progress succesfully set");
170
unity_webapps_launcher_set_progress (UnityWebappsContext *context,
173
g_return_if_fail (context != NULL);
174
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
176
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
181
unity_webapps_gen_launcher_call_set_progress (context->priv->launcher_context->launcher_proxy,
183
NULL /* Cancellable */,
184
set_progress_complete_callback,
187
UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting progress: %f", progress);
191
clear_count_complete_callback (GObject *source_object,
195
UnityWebappsGenLauncher *proxy;
198
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
202
unity_webapps_gen_launcher_call_clear_count_finish (proxy, res, &error);
206
g_warning ("Error calling ClearCount method of Launcher context: %s", error->message);
208
g_error_free (error);
213
UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, count succesfully cleared");
217
unity_webapps_launcher_clear_count (UnityWebappsContext *context)
220
g_return_if_fail (context != NULL);
221
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
223
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
228
unity_webapps_gen_launcher_call_clear_count (context->priv->launcher_context->launcher_proxy,
229
NULL /* Cancellable */,
230
clear_count_complete_callback,
233
UNITY_WEBAPPS_NOTE (LAUNCHER, "Clearing count");
237
clear_progress_complete_callback (GObject *source_object,
241
UnityWebappsGenLauncher *proxy;
244
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
248
unity_webapps_gen_launcher_call_clear_progress_finish (proxy, res, &error);
252
g_warning ("Error calling ClearProgress method of Launcher context: %s", error->message);
254
g_error_free (error);
259
UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, progress succesfully cleared");
263
unity_webapps_launcher_clear_progress (UnityWebappsContext *context)
266
g_return_if_fail (context != NULL);
267
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
269
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
274
unity_webapps_gen_launcher_call_clear_progress (context->priv->launcher_context->launcher_proxy,
275
NULL /* Cancellable */,
276
clear_progress_complete_callback,
279
UNITY_WEBAPPS_NOTE (LAUNCHER, "Clearing progress");
283
set_urgent_complete_callback (GObject *source_object,
287
UnityWebappsGenLauncher *proxy;
290
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
294
unity_webapps_gen_launcher_call_set_urgent_finish (proxy, res, &error);
298
g_warning ("Error calling SetUrgent of Launcher context: %s", error->message);
300
g_error_free (error);
305
UNITY_WEBAPPS_NOTE (LAUNCHER, "Received response, urgent succesfully set");
309
unity_webapps_launcher_set_urgent (UnityWebappsContext *context)
312
g_return_if_fail (context != NULL);
313
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
315
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
320
unity_webapps_gen_launcher_call_set_urgent (context->priv->launcher_context->launcher_proxy,
321
NULL /* Cancellable */,
322
set_urgent_complete_callback,
325
UNITY_WEBAPPS_NOTE (LAUNCHER, "Setting urgent state");
329
add_action_complete_callback (GObject *source_object,
333
UnityWebappsGenLauncher *proxy;
336
proxy = UNITY_WEBAPPS_GEN_LAUNCHER (source_object);
340
unity_webapps_gen_launcher_call_add_action_finish (proxy, res, &error);
344
g_warning ("Error calling AddAction method of Launcher context: %s", error->message);
346
g_error_free (error);
351
UNITY_WEBAPPS_NOTE (INDICATOR, "Received response, action succesfully added");
355
#define MAXIMUM_LABEL_LENGTH 60
358
unity_webapps_launcher_add_action (UnityWebappsContext *context, const gchar *label, UnityWebappsLauncherCallback callback)
361
gchar *sanitized_label;
363
g_return_if_fail (context != NULL);
364
g_return_if_fail (UNITY_WEBAPPS_IS_CONTEXT (context));
365
g_return_if_fail (callback != NULL);
368
if (unity_webapps_rate_check_launcher_rate_limit (context) == FALSE)
373
// The Hash Table takes ownership of this.
374
sanitized_label = unity_webapps_sanitizer_limit_string_argument (label, MAXIMUM_LABEL_LENGTH);
376
unity_webapps_gen_launcher_call_add_action (context->priv->launcher_context->launcher_proxy,
378
NULL /* Cancellable */,
379
add_action_complete_callback,
382
g_hash_table_insert (context->priv->launcher_context->quicklist_callbacks_by_name, sanitized_label, callback);
384
UNITY_WEBAPPS_NOTE (LAUNCHER, "Adding action: %s", label);