~ted/ubuntu-app-launch/snap-icon-unbreak

« back to all changes in this revision

Viewing changes to application-job.c

Updated to failure-is-an-option

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This program is free software: you can redistribute it and/or modify it
 
5
 * under the terms of the GNU General Public License version 3, as published
 
6
 * by the Free Software Foundation.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful, but
 
9
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 
10
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 
11
 * PURPOSE.  See the GNU General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU General Public License along
 
14
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 *     Ted Gould <ted.gould@canonical.com>
 
18
 */
 
19
 
 
20
#include <gio/gio.h>
 
21
#include "libupstart-app-launch/upstart-app-launch.h"
 
22
 
 
23
int retval = 0;
 
24
const gchar * global_appid;
 
25
 
 
26
static void
 
27
app_started (const gchar * appid, gpointer user_data)
 
28
{
 
29
        if (g_strcmp0(appid, global_appid) != 0)
 
30
                return;
 
31
        g_debug("Application Started: %s", appid);
 
32
        g_main_loop_quit((GMainLoop *)user_data);
 
33
}
 
34
 
 
35
static void
 
36
app_focus (const gchar * appid, gpointer user_data)
 
37
{
 
38
        if (g_strcmp0(appid, global_appid) != 0)
 
39
                return;
 
40
        g_debug("Application Focused");
 
41
        g_main_loop_quit((GMainLoop *)user_data);
 
42
}
 
43
 
 
44
static void
 
45
app_failed (const gchar * appid, upstart_app_launch_app_failed_t failure_type, gpointer user_data)
 
46
{
 
47
        if (g_strcmp0(appid, global_appid) != 0)
 
48
                return;
 
49
        g_warning("Application Startup Failed");
 
50
        retval = 1;
 
51
        g_main_loop_quit((GMainLoop *)user_data);
 
52
}
 
53
 
 
54
int
 
55
main (int argc, char * argv[])
 
56
{
 
57
        GDBusConnection * con = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, NULL);
 
58
        g_return_val_if_fail(con != NULL, 1);
 
59
 
 
60
        global_appid = g_getenv("APP_ID");
 
61
        g_return_val_if_fail(global_appid != NULL, 1);
 
62
 
 
63
        const gchar * uris_str = g_getenv("APP_URIS");
 
64
        gchar ** uris = NULL;
 
65
        if (uris_str != NULL) {
 
66
                GError * error = NULL;
 
67
                gint uri_count = 0;
 
68
                g_shell_parse_argv(uris_str, &uri_count, &uris, &error);
 
69
 
 
70
                if (error != NULL) {
 
71
                        g_warning("Unable to parse uris '%s': %s", uris_str, error->message);
 
72
                        g_error_free(error);
 
73
                } else {
 
74
                        g_debug("Got %d URIs", uri_count);
 
75
                }
 
76
        }
 
77
 
 
78
        GMainLoop * mainloop = g_main_loop_new(NULL, FALSE);
 
79
 
 
80
        upstart_app_launch_observer_add_app_started(app_started, mainloop);
 
81
        upstart_app_launch_observer_add_app_focus(app_focus, mainloop);
 
82
        upstart_app_launch_observer_add_app_failed(app_failed, mainloop);
 
83
 
 
84
        g_debug("Start Application: %s", global_appid);
 
85
        g_return_val_if_fail(upstart_app_launch_start_application(global_appid, (const gchar * const *)uris), -1);
 
86
        g_strfreev(uris);
 
87
 
 
88
        g_debug("Wait for results");
 
89
        g_main_loop_run(mainloop);
 
90
 
 
91
        upstart_app_launch_observer_delete_app_started(app_started, mainloop);
 
92
        upstart_app_launch_observer_delete_app_focus(app_focus, mainloop);
 
93
        upstart_app_launch_observer_delete_app_failed(app_failed, mainloop);
 
94
 
 
95
        g_main_loop_unref(mainloop);
 
96
        g_object_unref(con);
 
97
 
 
98
        return retval;
 
99
}