summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-06-26 10:50:28 +0100
committerRichard Hughes <richard@hughsie.com>2014-06-26 11:56:36 +0100
commit17ec03eb373ae0d4901aa0f61b8969edca326092 (patch)
tree99d70064f267932fe64f4c69845c01a141977fc6
parente9fce47136ba0b361ebbd94e997924929bff0c9e (diff)
downloadappstream-glib-17ec03eb373ae0d4901aa0f61b8969edca326092.tar.gz
Allow AsStore to load directories of AppData and desktop files
-rw-r--r--client/as-util.c30
-rw-r--r--libappstream-glib/as-store.c80
-rw-r--r--libappstream-glib/as-store.h4
3 files changed, 107 insertions, 7 deletions
diff --git a/client/as-util.c b/client/as-util.c
index ab57ddf..0a26b4e 100644
--- a/client/as-util.c
+++ b/client/as-util.c
@@ -723,6 +723,31 @@ as_util_dump_filename (AsUtilPrivate *priv, const gchar *filename, GError **erro
}
/**
+ * as_util_dump_installed:
+ **/
+static gboolean
+as_util_dump_installed (AsUtilPrivate *priv, GError **error)
+{
+ _cleanup_object_unref_ AsStore *store = NULL;
+ _cleanup_string_free_ GString *xml = NULL;
+
+ /* dump to screen */
+ store = as_store_new ();
+ if (!as_store_load (store,
+ AS_STORE_LOAD_FLAG_APPDATA |
+ AS_STORE_LOAD_FLAG_DESKTOP,
+ NULL, error))
+ return FALSE;
+ as_store_set_api_version (store, 1.0);
+ xml = as_store_to_xml (store,
+ AS_NODE_TO_XML_FLAG_FORMAT_MULTILINE |
+ AS_NODE_TO_XML_FLAG_FORMAT_INDENT |
+ AS_NODE_TO_XML_FLAG_ADD_HEADER);
+ g_print ("%s", xml->str);
+ return TRUE;
+}
+
+/**
* as_util_dump:
**/
static gboolean
@@ -739,6 +764,11 @@ as_util_dump (AsUtilPrivate *priv, gchar **values, GError **error)
"expected data.xml");
return FALSE;
}
+
+ /* magic value */
+ if (g_strcmp0 (values[0], "installed") == 0)
+ return as_util_dump_installed (priv, error);
+
for (i = 0; values[i] != NULL; i++) {
if (!as_util_dump_filename (priv, values[0], error))
return FALSE;
diff --git a/libappstream-glib/as-store.c b/libappstream-glib/as-store.c
index c7544fd..1f8fa83 100644
--- a/libappstream-glib/as-store.c
+++ b/libappstream-glib/as-store.c
@@ -1010,6 +1010,45 @@ as_store_load_app_install (AsStore *store,
}
/**
+ * as_store_load_installed:
+ **/
+static gboolean
+as_store_load_installed (AsStore *store, const gchar *path,
+ GCancellable *cancellable, GError **error)
+{
+ const gchar *tmp;
+ _cleanup_dir_close_ GDir *dir = NULL;
+ GError *error_local = NULL;
+
+ dir = g_dir_open (path, 0, error);
+ if (dir == NULL)
+ return FALSE;
+
+ while ((tmp = g_dir_read_name (dir)) != NULL) {
+ _cleanup_free_ gchar *filename = NULL;
+ _cleanup_object_unref_ AsApp *app = NULL;
+ filename = g_build_filename (path, tmp, NULL);
+ app = as_app_new ();
+ if (!as_app_parse_file (app, filename,
+ AS_APP_PARSE_FLAG_USE_HEURISTICS,
+ &error_local)) {
+ if (g_error_matches (error_local,
+ AS_APP_ERROR,
+ AS_APP_ERROR_INVALID_TYPE)) {
+ g_clear_error (&error_local);
+ continue;
+ }
+ g_propagate_error (error, error_local);
+ return FALSE;
+ }
+ /* set lower priority than AppStream entries */
+ as_app_set_priority (app, -1);
+ as_store_add_app (store, app);
+ }
+ return TRUE;
+}
+
+/**
* as_store_load:
* @store: a #AsStore instance.
* @flags: #AsStoreLoadFlags, e.g. %AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM
@@ -1033,19 +1072,29 @@ as_store_load (AsStore *store,
gchar *path;
guint i;
_cleanup_ptrarray_unref_ GPtrArray *app_info = NULL;
+ _cleanup_ptrarray_unref_ GPtrArray *installed = NULL;
/* system locations */
app_info = g_ptr_array_new_with_free_func (g_free);
- if ((flags & AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM) > 0) {
- data_dirs = g_get_system_data_dirs ();
- for (i = 0; data_dirs[i] != NULL; i++) {
+ installed = g_ptr_array_new_with_free_func (g_free);
+ data_dirs = g_get_system_data_dirs ();
+ for (i = 0; data_dirs[i] != NULL; i++) {
+ if ((flags & AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM) > 0) {
path = g_build_filename (data_dirs[i], "app-info", NULL);
g_ptr_array_add (app_info, path);
+ path = g_build_filename (LOCALSTATEDIR, "lib", "app-info", NULL);
+ g_ptr_array_add (app_info, path);
+ path = g_build_filename (LOCALSTATEDIR, "cache", "app-info", NULL);
+ g_ptr_array_add (app_info, path);
+ }
+ if ((flags & AS_STORE_LOAD_FLAG_APPDATA) > 0) {
+ path = g_build_filename (data_dirs[i], "appdata", NULL);
+ g_ptr_array_add (installed, path);
+ }
+ if ((flags & AS_STORE_LOAD_FLAG_DESKTOP) > 0) {
+ path = g_build_filename (data_dirs[i], "applications", NULL);
+ g_ptr_array_add (installed, path);
}
- path = g_build_filename (LOCALSTATEDIR, "lib", "app-info", NULL);
- g_ptr_array_add (app_info, path);
- path = g_build_filename (LOCALSTATEDIR, "cache", "app-info", NULL);
- g_ptr_array_add (app_info, path);
}
/* per-user locations */
@@ -1053,6 +1102,14 @@ as_store_load (AsStore *store,
path = g_build_filename (g_get_user_data_dir (), "app-info", NULL);
g_ptr_array_add (app_info, path);
}
+ if ((flags & AS_STORE_LOAD_FLAG_APPDATA) > 0) {
+ path = g_build_filename (g_get_user_data_dir (), "appdata", NULL);
+ g_ptr_array_add (installed, path);
+ }
+ if ((flags & AS_STORE_LOAD_FLAG_DESKTOP) > 0) {
+ path = g_build_filename (g_get_user_data_dir (), "applications", NULL);
+ g_ptr_array_add (installed, path);
+ }
/* load each app-info path if it exists */
for (i = 0; i < app_info->len; i++) {
@@ -1063,6 +1120,15 @@ as_store_load (AsStore *store,
return FALSE;
}
+ /* load each appdata and desktop path if it exists */
+ for (i = 0; i < installed->len; i++) {
+ tmp = g_ptr_array_index (installed, i);
+ if (!g_file_test (tmp, G_FILE_TEST_EXISTS))
+ continue;
+ if (!as_store_load_installed (store, tmp, cancellable, error))
+ return FALSE;
+ }
+
/* ubuntu specific */
if ((flags & AS_STORE_LOAD_FLAG_APP_INSTALL) > 0) {
if (!as_store_load_app_install (store,
diff --git a/libappstream-glib/as-store.h b/libappstream-glib/as-store.h
index 9aad7ee..6231675 100644
--- a/libappstream-glib/as-store.h
+++ b/libappstream-glib/as-store.h
@@ -69,6 +69,8 @@ struct _AsStoreClass
* @AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM: The system app-info AppStream data
* @AS_STORE_LOAD_FLAG_APP_INFO_USER: The per-user app-info AppStream data
* @AS_STORE_LOAD_FLAG_APP_INSTALL: The ubuntu-specific app-install data
+ * @AS_STORE_LOAD_FLAG_APPDATA: The installed AppData files
+ * @AS_STORE_LOAD_FLAG_DESKTOP: The installed desktop files
*
* The flags to use when loading the store.
**/
@@ -77,6 +79,8 @@ typedef enum {
AS_STORE_LOAD_FLAG_APP_INFO_SYSTEM = 1, /* Since: 0.1.2 */
AS_STORE_LOAD_FLAG_APP_INFO_USER = 2, /* Since: 0.1.2 */
AS_STORE_LOAD_FLAG_APP_INSTALL = 4, /* Since: 0.1.2 */
+ AS_STORE_LOAD_FLAG_APPDATA = 8, /* Since: 0.2.2 */
+ AS_STORE_LOAD_FLAG_DESKTOP = 16, /* Since: 0.2.2 */
/*< private >*/
AS_STORE_LOAD_FLAG_LAST
} AsStoreLoadFlags;