lib: Add XdgAppBundleRef

tingping/wmclass
Alexander Larsson 2016-02-24 14:44:05 +01:00
parent cbf3d25440
commit 69052c7d9a
6 changed files with 285 additions and 0 deletions

View File

@ -7,6 +7,7 @@ public_headers = \
lib/xdg-app-error.h \
lib/xdg-app-installed-ref.h \
lib/xdg-app-remote-ref.h \
lib/xdg-app-bundle-ref.h \
lib/xdg-app-installation.h \
lib/xdg-app-remote.h \
lib/xdg-app-version-macros.h \
@ -47,6 +48,7 @@ sources = \
lib/xdg-app-installed-ref.c \
lib/xdg-app-installed-ref-private.h \
lib/xdg-app-remote-ref.c \
lib/xdg-app-bundle-ref.c \
lib/xdg-app-remote-ref-private.h \
lib/xdg-app-remote-private.h \
lib/xdg-app-remote.c \

View File

@ -88,6 +88,30 @@ main (int argc, char *argv[])
return 0;
}
g_print ("\n**** Loading bundle\n");
{
g_autoptr(GFile) f = g_file_new_for_commandline_arg ("tests/hello.xdgapp");
g_autoptr(XdgAppBundleRef) bundle = xdg_app_bundle_ref_new (f, &error);
if (bundle == NULL)
{
g_print ("Error loading bundle: %s\n", error->message);
g_clear_error (&error);
}
else
{
g_autofree char *path = g_file_get_path (xdg_app_bundle_ref_get_file (bundle));
g_autoptr(GBytes) metadata = xdg_app_bundle_ref_get_metadata (bundle);
g_print ("%d %s %s %s %s %s\n%s\n",
xdg_app_ref_get_kind (XDG_APP_REF(bundle)),
xdg_app_ref_get_name (XDG_APP_REF(bundle)),
xdg_app_ref_get_arch (XDG_APP_REF(bundle)),
xdg_app_ref_get_branch (XDG_APP_REF(bundle)),
xdg_app_ref_get_commit (XDG_APP_REF(bundle)),
path,
(char *)g_bytes_get_data (metadata, NULL));
}
}
g_print ("\n**** Checking for updates\n");
{
g_autoptr(GPtrArray) updates =

View File

@ -0,0 +1,201 @@
/*
* Copyright © 2015 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <alexl@redhat.com>
*/
#include "config.h"
#include <string.h>
#include "xdg-app-utils.h"
#include "xdg-app-bundle-ref.h"
#include "xdg-app-enum-types.h"
typedef struct _XdgAppBundleRefPrivate XdgAppBundleRefPrivate;
struct _XdgAppBundleRefPrivate
{
GFile *file;
GBytes *metadata;
};
G_DEFINE_TYPE_WITH_PRIVATE (XdgAppBundleRef, xdg_app_bundle_ref, XDG_APP_TYPE_REF)
enum {
PROP_0,
PROP_FILE,
};
static void
xdg_app_bundle_ref_finalize (GObject *object)
{
XdgAppBundleRef *self = XDG_APP_BUNDLE_REF (object);
XdgAppBundleRefPrivate *priv = xdg_app_bundle_ref_get_instance_private (self);
g_clear_object (&priv->file);
G_OBJECT_CLASS (xdg_app_bundle_ref_parent_class)->finalize (object);
}
static void
xdg_app_bundle_ref_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
XdgAppBundleRef *self = XDG_APP_BUNDLE_REF (object);
XdgAppBundleRefPrivate *priv = xdg_app_bundle_ref_get_instance_private (self);
switch (prop_id)
{
case PROP_FILE:
g_set_object (&priv->file, g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
xdg_app_bundle_ref_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
XdgAppBundleRef *self = XDG_APP_BUNDLE_REF (object);
XdgAppBundleRefPrivate *priv = xdg_app_bundle_ref_get_instance_private (self);
switch (prop_id)
{
case PROP_FILE:
g_value_set_object (value, priv->file);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
xdg_app_bundle_ref_class_init (XdgAppBundleRefClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = xdg_app_bundle_ref_get_property;
object_class->set_property = xdg_app_bundle_ref_set_property;
object_class->finalize = xdg_app_bundle_ref_finalize;
g_object_class_install_property (object_class,
PROP_FILE,
g_param_spec_object ("file",
"",
"",
G_TYPE_FILE,
G_PARAM_READWRITE));
}
static void
xdg_app_bundle_ref_init (XdgAppBundleRef *self)
{
}
/**
* xdg_app_bundle_ref_get_file:
* @self: a #XdgAppInstallation
*
* Get the file this bundle is stored in.
*
* Returns: (transfer full) : an #GFile
*/
GFile *
xdg_app_bundle_ref_get_file (XdgAppBundleRef *self)
{
XdgAppBundleRefPrivate *priv = xdg_app_bundle_ref_get_instance_private (self);
return g_object_ref (priv->file);
}
/**
* xdg_app_bundle_ref_get_metadata:
* @self: a #XdgAppInstallation
*
* Get the metadata for the app/runtime
*
* Returns: (transfer full) : an #GBytes with the metadata contents
*/
GBytes *
xdg_app_bundle_ref_get_metadata (XdgAppBundleRef *self)
{
XdgAppBundleRefPrivate *priv = xdg_app_bundle_ref_get_instance_private (self);
return g_bytes_ref (priv->metadata);
}
XdgAppBundleRef *
xdg_app_bundle_ref_new (GFile *file,
GError **error)
{
XdgAppRefKind kind = XDG_APP_REF_KIND_APP;
XdgAppBundleRefPrivate *priv;
g_auto(GStrv) parts = NULL;
XdgAppBundleRef *ref;
g_autoptr(GVariant) metadata = NULL;
g_autofree char *commit = NULL;
g_autofree char *full_ref = NULL;
g_autofree char *metadata_contents = NULL;
metadata = xdg_app_bundle_load (file, &commit, error);
if (metadata == NULL)
return NULL;
if (!g_variant_lookup (metadata, "ref", "s", &full_ref))
{
xdg_app_fail (error, "Invalid bundle, no ref in metadata");
return NULL;
}
parts = xdg_app_decompose_ref (full_ref, error);
if (parts == NULL)
return NULL;
if (!g_variant_lookup (metadata, "metadata", "s", &metadata_contents))
metadata_contents = g_strdup ("");
if (strcmp (parts[0], "app") != 0)
kind = XDG_APP_REF_KIND_RUNTIME;
ref = g_object_new (XDG_APP_TYPE_BUNDLE_REF,
"kind", kind,
"name", parts[1],
"arch", parts[2],
"branch", parts[3],
"commit", commit,
"file", file,
NULL);
priv = xdg_app_bundle_ref_get_instance_private (ref);
priv->metadata = g_bytes_new_take (metadata_contents,
strlen (metadata_contents));
metadata_contents = NULL; /* Stolen */
return ref;
}

View File

@ -0,0 +1,57 @@
/*
* Copyright © 2015 Red Hat, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Alexander Larsson <alexl@redhat.com>
*/
#if !defined (__XDG_APP_H_INSIDE__) && !defined (XDG_APP_COMPILATION)
#error "Only <xdg-app.h> can be included directly."
#endif
#ifndef __XDG_APP_BUNDLE_REF_H__
#define __XDG_APP_BUNDLE_REF_H__
typedef struct _XdgAppBundleRef XdgAppBundleRef;
#include <gio/gio.h>
#include <xdg-app-ref.h>
#define XDG_APP_TYPE_BUNDLE_REF xdg_app_bundle_ref_get_type()
#define XDG_APP_BUNDLE_REF(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), XDG_APP_TYPE_BUNDLE_REF, XdgAppBundleRef))
#define XDG_APP_IS_BUNDLE_REF(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), XDG_APP_TYPE_BUNDLE_REF))
XDG_APP_EXTERN GType xdg_app_bundle_ref_get_type (void);
struct _XdgAppBundleRef {
XdgAppRef parent;
};
typedef struct {
XdgAppRefClass parent_class;
} XdgAppBundleRefClass;
XDG_APP_EXTERN XdgAppBundleRef *xdg_app_bundle_ref_new (GFile *file,
GError **error);
XDG_APP_EXTERN GFile *xdg_app_bundle_ref_get_file (XdgAppBundleRef *self);
XDG_APP_EXTERN GBytes *xdg_app_bundle_ref_get_metadata (XdgAppBundleRef *self);
#ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC
G_DEFINE_AUTOPTR_CLEANUP_FUNC(XdgAppBundleRef, g_object_unref)
#endif
#endif /* __XDG_APP_BUNDLE_REF_H__ */

View File

@ -31,6 +31,7 @@
#include <xdg-app-ref.h>
#include <xdg-app-installed-ref.h>
#include <xdg-app-remote-ref.h>
#include <xdg-app-bundle-ref.h>
#include <xdg-app-remote.h>
#include <xdg-app-installation.h>

BIN
tests/hello.xdgapp 100644

Binary file not shown.