1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3
* unity-webapps-runner.c
4
* Copyright (C) Canonical LTD 2011
6
* Author: Robert Carr <racarr@canonical.com>
8
unity-webapps is free software: you can redistribute it and/or modify it
9
* under the terms of the GNU Lesser General Public License as published
10
* by the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* unity-webapps is distributed in the hope that it will be useful, but
14
* WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
* See the GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
24
#include "unity-webapps-service.h"
25
#include "unity-webapps-context.h"
26
#include "unity-webapps-runner-amazon.h"
28
static UnityWebappsService *service = NULL;
30
static gchar *name = NULL;
31
static gchar *domain = NULL;
32
static gboolean amazon = FALSE;
33
GMainLoop *loop = NULL;
35
static GOptionEntry option_entries[] =
37
{ "name", 'n',0,G_OPTION_ARG_STRING, &name, "Application name", NULL },
38
{ "domain", 'd',0, G_OPTION_ARG_STRING, &domain, "Application domain", NULL},
39
{ "amazon", 'a',0, G_OPTION_ARG_NONE, &amazon, "Launch amazon (with geoclue store selection)", NULL}
43
has_context (const gchar *name, const gchar *domain)
47
gchar** contexts = unity_webapps_service_list_contexts (service);
49
for (it = contexts; it && *it; it++)
51
UnityWebappsContext *context = unity_webapps_context_new_for_context_name (service, *it);
53
res = res || ((g_strcmp0 (unity_webapps_context_get_name (context), name) == 0)
54
&& (g_strcmp0 (unity_webapps_context_get_domain (context), domain) == 0));
56
g_object_unref (context);
64
on_context_appeared (UnityWebappsService *service, const gchar *context_name, gpointer user_data)
66
UnityWebappsContext *context = unity_webapps_context_new_for_context_name (service, context_name);
68
if ((g_strcmp0 (unity_webapps_context_get_name (context), name) == 0)
69
&& (g_strcmp0 (unity_webapps_context_get_domain (context), domain) == 0))
71
g_usleep (G_USEC_PER_SEC * 2);
72
g_main_loop_quit (loop);
75
g_object_unref (context);
79
main (gint argc, gchar **argv)
83
GOptionContext *context;
90
service = unity_webapps_service_new ();
94
context = g_option_context_new ("- Activate Unity WebApps");
97
g_option_context_add_main_entries (context, option_entries, NULL);
99
if (!g_option_context_parse (context, &argc, &argv, &error))
101
printf("Failed to parse arguments: %s\n", error->message);
114
country = unity_webapps_runner_amazon_get_country ();
116
url = unity_webapps_runner_amazon_get_homepage_for_country (country);
119
g_error("Could not retrieve the url associated with the current country %s", country);
122
command_line = g_strdup_printf("xdg-open '%s'", url);
124
g_spawn_command_line_async (command_line, &spawn_error);
125
if (spawn_error != NULL)
127
g_error ("Error spawning browser: %s", spawn_error->message);
128
g_error_free (spawn_error);
133
// We should delay this until the amazon context appears.
134
// Even then it's questionable! Bug fix ;)
135
unity_webapps_runner_amazon_unpin_default_launcher ();
137
g_free (command_line);
142
g_base64_decode_inplace (name, &len);
144
for (i = 1; i < argc; i++)
145
files[i - 1] = argv[i];
147
files[argc - 1] = NULL;
149
has_files = files[0] != NULL;
151
if (has_files && !has_context (name, domain))
153
loop = g_main_loop_new (NULL, FALSE);
154
unity_webapps_service_on_context_appeared (service, on_context_appeared, NULL);
156
unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);
158
g_main_loop_run (loop);
160
unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);
164
unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);