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

« back to all changes in this revision

Viewing changes to libupstart-app-launch/upstart-app-launch.c

Merge trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 */
19
19
 
20
20
#include "upstart-app-launch.h"
 
21
#include <json-glib/json-glib.h>
21
22
#include <upstart.h>
22
23
#include <gio/gio.h>
23
24
#include <string.h>
984
985
 
985
986
        return primary == pid;
986
987
}
 
988
 
 
989
/* Try and get a manifest file and do a couple sanity checks on it */
 
990
JsonParser *
 
991
get_manifest_file (const gchar * pkg)
 
992
{
 
993
        /* Get the directory from click */
 
994
        GError * error = NULL;
 
995
        const gchar * click_exec = NULL;
 
996
 
 
997
        if (g_getenv("UAL_CLICK_EXEC") != NULL) {
 
998
                click_exec = g_getenv("UAL_CLICK_EXEC");
 
999
        } else {
 
1000
                click_exec = "click";
 
1001
        }
 
1002
 
 
1003
        gchar * cmdline = g_strdup_printf("%s info \"%s\"",
 
1004
                click_exec, pkg);
 
1005
 
 
1006
        gchar * output = NULL;
 
1007
        g_spawn_command_line_sync(cmdline, &output, NULL, NULL, &error);
 
1008
        g_free(cmdline);
 
1009
 
 
1010
        if (error != NULL) {
 
1011
                g_warning("Unable to get manifest for '%s' package: %s", pkg, error->message);
 
1012
                g_error_free(error);
 
1013
                g_free(output);
 
1014
                return NULL;
 
1015
        }
 
1016
 
 
1017
        /* Let's look at that manifest file */
 
1018
        JsonParser * parser = json_parser_new();
 
1019
        json_parser_load_from_data(parser, output, -1, &error);
 
1020
        g_free(output);
 
1021
 
 
1022
        if (error != NULL) {
 
1023
                g_warning("Unable to load manifest for '%s': %s", pkg, error->message);
 
1024
                g_error_free(error);
 
1025
                g_object_unref(parser);
 
1026
                return NULL;
 
1027
        }
 
1028
        JsonNode * root = json_parser_get_root(parser);
 
1029
        if (json_node_get_node_type(root) != JSON_NODE_OBJECT) {
 
1030
                g_warning("Manifest file for package '%s' does not have an object as its root node", pkg);
 
1031
                g_object_unref(parser);
 
1032
                return NULL;
 
1033
        }
 
1034
 
 
1035
        JsonObject * rootobj = json_node_get_object(root);
 
1036
        if (!json_object_has_member(rootobj, "version")) {
 
1037
                g_warning("Manifest file for package '%s' does not have a version", pkg);
 
1038
                g_object_unref(parser);
 
1039
                return NULL;
 
1040
        }
 
1041
 
 
1042
        return parser;
 
1043
}
 
1044
 
 
1045
/* Figure out the app version using the manifest */
 
1046
gchar *
 
1047
manifest_version (const gchar * pkg, const gchar * original_ver)
 
1048
{
 
1049
        if (g_strcmp0(original_ver, "current-user-version") != 0) {
 
1050
                return g_strdup(original_ver);
 
1051
        } else  {
 
1052
                JsonParser * manifest = get_manifest_file(pkg);
 
1053
                g_return_val_if_fail(manifest != NULL, NULL);
 
1054
                JsonNode * node = json_parser_get_root(manifest);
 
1055
                JsonObject * obj = json_node_get_object(node);
 
1056
                gchar * ret = g_strdup(json_object_get_string_member(obj, "version"));
 
1057
                g_object_unref(manifest);
 
1058
                return ret;
 
1059
        }
 
1060
 
 
1061
        return NULL;
 
1062
}
 
1063
 
 
1064
gchar *
 
1065
upstart_app_launch_triplet_to_app_id (const gchar * pkg, const gchar * app, const gchar * ver)
 
1066
{
 
1067
        gchar * version = NULL;
 
1068
        version = manifest_version(pkg, ver);
 
1069
        g_return_val_if_fail(version != NULL, NULL);
 
1070
        return g_strdup_printf("%s_%s_%s", pkg, app, version);
 
1071
}