~ubuntu-branches/ubuntu/vivid/libunity-webapps/vivid

« back to all changes in this revision

Viewing changes to .pc/fix-race.patch/src/runner/unity-webapps-runner.c

  • Committer: Ken VanDine
  • Author(s): Michael Terry
  • Date: 2012-09-21 23:41:19 UTC
  • mfrom: (260.241.90)
  • Revision ID: ken.vandine@canonical.com-20120921234119-nswef23t15c2hol6
Tags: 2.3.8-0ubuntu2
* debian/patches/fix-race.patch:
  - Fix race condition, preventing the amazon icon from hiding
    immediately sometimes (hiding and popping up is a separate issue)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
 
2
/*
 
3
 * unity-webapps-runner.c
 
4
 * Copyright (C) Canonical LTD 2011
 
5
 *
 
6
 * Author: Robert Carr <racarr@canonical.com>
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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/>.";
 
20
 */
 
21
#include <stdlib.h>
 
22
#include <stdio.h>
 
23
 
 
24
#include "unity-webapps-service.h"
 
25
#include "unity-webapps-context.h"
 
26
#include "unity-webapps-runner-amazon.h"
 
27
 
 
28
static UnityWebappsService *service = NULL;
 
29
 
 
30
static gchar *name = NULL;
 
31
static gchar *domain = NULL;
 
32
static gboolean amazon = FALSE;
 
33
GMainLoop *loop = NULL;
 
34
 
 
35
static GOptionEntry option_entries[] =
 
36
        {
 
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}
 
40
        };
 
41
 
 
42
gboolean
 
43
has_context (const gchar *name, const gchar *domain)
 
44
{
 
45
        gboolean res = FALSE;
 
46
        gchar** it;
 
47
        gchar** contexts = unity_webapps_service_list_contexts (service);
 
48
 
 
49
        for (it = contexts; it && *it; it++)
 
50
                {
 
51
                        UnityWebappsContext *context = unity_webapps_context_new_for_context_name (service, *it);
 
52
 
 
53
                        res = res || ((g_strcmp0 (unity_webapps_context_get_name (context), name) == 0)
 
54
                                                  && (g_strcmp0 (unity_webapps_context_get_domain (context), domain) == 0));
 
55
 
 
56
                        g_object_unref (context);
 
57
                }
 
58
 
 
59
        return res;
 
60
}
 
61
 
 
62
 
 
63
static void
 
64
on_context_appeared (UnityWebappsService *service, const gchar *context_name, gpointer user_data)
 
65
{
 
66
        UnityWebappsContext *context = unity_webapps_context_new_for_context_name (service, context_name);
 
67
 
 
68
        if ((g_strcmp0 (unity_webapps_context_get_name (context), name) == 0)
 
69
                && (g_strcmp0 (unity_webapps_context_get_domain (context), domain) == 0))
 
70
                {
 
71
                        g_usleep (G_USEC_PER_SEC * 2);
 
72
                        g_main_loop_quit (loop);
 
73
                }
 
74
 
 
75
        g_object_unref (context);
 
76
}
 
77
 
 
78
gint
 
79
main (gint argc, gchar **argv)
 
80
{
 
81
        gint i;
 
82
        gsize len;
 
83
        GOptionContext *context;
 
84
        GError *error;
 
85
        gboolean has_files;
 
86
        gchar *files[argc];
 
87
 
 
88
        g_type_init ();
 
89
 
 
90
        service = unity_webapps_service_new ();
 
91
 
 
92
        error = NULL;
 
93
 
 
94
        context = g_option_context_new ("- Activate Unity WebApps");
 
95
 
 
96
        // TODO: GETTEXT
 
97
        g_option_context_add_main_entries (context, option_entries, NULL);
 
98
 
 
99
        if (!g_option_context_parse (context, &argc, &argv, &error))
 
100
                {
 
101
                        printf("Failed to parse arguments: %s\n", error->message);
 
102
                        exit(1);
 
103
                }
 
104
        
 
105
        if (amazon == TRUE)
 
106
          {
 
107
                GError *spawn_error;
 
108
                gchar *command_line;
 
109
                gchar *country;
 
110
                const gchar *url;
 
111
                
 
112
                spawn_error = NULL;
 
113
                
 
114
                country = unity_webapps_runner_amazon_get_country ();
 
115
 
 
116
                url = unity_webapps_runner_amazon_get_homepage_for_country (country);
 
117
                if (NULL == url)
 
118
                  {
 
119
                        g_error("Could not retrieve the url associated with the current country %s", country);
 
120
                        return 1;
 
121
                  }
 
122
                command_line = g_strdup_printf("xdg-open '%s'", url);
 
123
                
 
124
                g_spawn_command_line_async (command_line, &spawn_error);
 
125
                if (spawn_error != NULL)
 
126
                  {
 
127
                        g_error ("Error spawning browser: %s", spawn_error->message);
 
128
                        g_error_free (spawn_error);
 
129
                        g_free (country);
 
130
                        return 1;
 
131
                  }
 
132
                
 
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 ();
 
136
                
 
137
                g_free (command_line);
 
138
                g_free (country);
 
139
                return 0;
 
140
          }
 
141
 
 
142
        g_base64_decode_inplace (name, &len);
 
143
 
 
144
        for (i = 1; i < argc; i++)
 
145
                files[i - 1] = argv[i];
 
146
 
 
147
        files[argc - 1] = NULL;
 
148
 
 
149
        has_files = files[0] != NULL;
 
150
 
 
151
        if (has_files && !has_context (name, domain))
 
152
                {
 
153
                        loop = g_main_loop_new (NULL, FALSE);
 
154
                        unity_webapps_service_on_context_appeared (service, on_context_appeared, NULL);
 
155
 
 
156
                        unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);
 
157
 
 
158
                        g_main_loop_run (loop);
 
159
 
 
160
                        unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);
 
161
                }
 
162
        else
 
163
                {
 
164
                        unity_webapps_service_activate_application (service, name, domain, (const gchar * const*)files);
 
165
                }
 
166
 
 
167
        exit (0);
 
168
}