diff options
author | Richard Hughes <richard@hughsie.com> | 2016-10-31 12:16:42 +0000 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2016-10-31 12:16:42 +0000 |
commit | 3057feffddafd1295687b9d7501db89e645a0b34 (patch) | |
tree | 33f6e3f91e7250457a3a7a4c7e58facd8e5d610f | |
parent | de08b0d844e8b6f30eb62857999d436a389a8e66 (diff) | |
download | appstream-glib-3057feffddafd1295687b9d7501db89e645a0b34.tar.gz |
Add app-removed, app-added and app-changed signals to AsStore
This allows us to invalidate things built from the AsApp objects.
-rw-r--r-- | client/as-util.c | 27 | ||||
-rw-r--r-- | libappstream-glib/as-self-test.c | 24 | ||||
-rw-r--r-- | libappstream-glib/as-store.c | 68 | ||||
-rw-r--r-- | libappstream-glib/as-store.h | 9 |
4 files changed, 124 insertions, 4 deletions
diff --git a/client/as-util.c b/client/as-util.c index 6fba2f6..4be1f1d 100644 --- a/client/as-util.c +++ b/client/as-util.c @@ -1202,6 +1202,27 @@ as_util_watch_store_changed_cb (AsStore *store, AsUtilPrivate *priv) as_store_get_size (store)); } +static void +as_util_watch_store_app_added_cb (AsStore *store, AsApp *app, AsUtilPrivate *priv) +{ + g_print ("Component added to store: %s\n", + as_app_get_unique_id (app)); +} + +static void +as_util_watch_store_app_removed_cb (AsStore *store, AsApp *app, AsUtilPrivate *priv) +{ + g_print ("Component removed from store: %s\n", + as_app_get_unique_id (app)); +} + +static void +as_util_watch_store_app_changed_cb (AsStore *store, AsApp *app, AsUtilPrivate *priv) +{ + g_print ("Component changed in store: %s\n", + as_app_get_unique_id (app)); +} + static gboolean as_util_watch (AsUtilPrivate *priv, gchar **values, GError **error) { @@ -1225,6 +1246,12 @@ as_util_watch (AsUtilPrivate *priv, gchar **values, GError **error) } g_signal_connect (store, "changed", G_CALLBACK (as_util_watch_store_changed_cb), priv); + g_signal_connect (store, "app-added", + G_CALLBACK (as_util_watch_store_app_added_cb), priv); + g_signal_connect (store, "app-removed", + G_CALLBACK (as_util_watch_store_app_removed_cb), priv); + g_signal_connect (store, "app-changed", + G_CALLBACK (as_util_watch_store_app_changed_cb), priv); /* wait */ g_main_loop_run (priv->loop); diff --git a/libappstream-glib/as-self-test.c b/libappstream-glib/as-self-test.c index 06801e6..df119f0 100644 --- a/libappstream-glib/as-self-test.c +++ b/libappstream-glib/as-self-test.c @@ -2843,6 +2843,12 @@ store_changed_cb (AsStore *store, guint *cnt) g_debug ("changed callback, now #%u", *cnt); } +static void +store_app_changed_cb (AsStore *store, AsApp *app, guint *cnt) +{ + (*cnt)++; +} + /* automatically reload changed directories */ static void as_test_store_auto_reload_dir_func (void) @@ -2850,6 +2856,8 @@ as_test_store_auto_reload_dir_func (void) AsApp *app; gboolean ret; guint cnt = 0; + guint cnt_added = 0; + guint cnt_removed = 0; g_autoptr(GError) error = NULL; g_autoptr(AsStore) store = NULL; @@ -2857,6 +2865,10 @@ as_test_store_auto_reload_dir_func (void) store = as_store_new (); g_signal_connect (store, "changed", G_CALLBACK (store_changed_cb), &cnt); + g_signal_connect (store, "app-added", + G_CALLBACK (store_app_changed_cb), &cnt_added); + g_signal_connect (store, "app-removed", + G_CALLBACK (store_app_changed_cb), &cnt_removed); as_store_set_watch_flags (store, AS_STORE_WATCH_FLAG_ADDED | AS_STORE_WATCH_FLAG_REMOVED); @@ -2869,6 +2881,8 @@ as_test_store_auto_reload_dir_func (void) g_assert_no_error (error); g_assert (ret); g_assert_cmpint (cnt, ==, 1); + g_assert_cmpint (cnt_added, ==, 0); + g_assert_cmpint (cnt_removed, ==, 0); /* create file */ ret = g_file_set_contents ("/tmp/repo-tmp/usr/share/app-info/xmls/foo.xml", @@ -2883,6 +2897,8 @@ as_test_store_auto_reload_dir_func (void) as_test_loop_run_with_timeout (2000); g_assert_cmpint (cnt, ==, 2); + g_assert_cmpint (cnt_added, ==, 1); + g_assert_cmpint (cnt_removed, ==, 0); /* verify */ app = as_store_get_app_by_id (store, "test.desktop"); @@ -2892,6 +2908,8 @@ as_test_store_auto_reload_dir_func (void) g_unlink ("/tmp/repo-tmp/usr/share/app-info/xmls/foo.xml"); as_test_loop_run_with_timeout (2000); g_assert_cmpint (cnt, ==, 3); + g_assert_cmpint (cnt_added, ==, 1); + g_assert_cmpint (cnt_removed, ==, 1); app = as_store_get_app_by_id (store, "test.desktop"); g_assert (app == NULL); } @@ -2904,6 +2922,8 @@ as_test_store_auto_reload_file_func (void) AsRelease *rel; gboolean ret; guint cnt = 0; + guint cnt_added = 0; + guint cnt_removed = 0; g_autoptr(GError) error = NULL; g_autoptr(AsStore) store = NULL; g_autoptr(GFile) file = NULL; @@ -2927,6 +2947,10 @@ as_test_store_auto_reload_file_func (void) store = as_store_new (); g_signal_connect (store, "changed", G_CALLBACK (store_changed_cb), &cnt); + g_signal_connect (store, "app-added", + G_CALLBACK (store_app_changed_cb), &cnt_added); + g_signal_connect (store, "app-removed", + G_CALLBACK (store_app_changed_cb), &cnt_added); as_store_set_watch_flags (store, AS_STORE_WATCH_FLAG_ADDED | AS_STORE_WATCH_FLAG_REMOVED); file = g_file_new_for_path ("/tmp/foo.xml"); diff --git a/libappstream-glib/as-store.c b/libappstream-glib/as-store.c index 0c85da6..c63aaf7 100644 --- a/libappstream-glib/as-store.c +++ b/libappstream-glib/as-store.c @@ -89,6 +89,9 @@ G_DEFINE_TYPE_WITH_PRIVATE (AsStore, as_store, G_TYPE_OBJECT) enum { SIGNAL_CHANGED, + SIGNAL_APP_ADDED, + SIGNAL_APP_REMOVED, + SIGNAL_APP_CHANGED, SIGNAL_LAST }; @@ -143,7 +146,7 @@ as_store_class_init (AsStoreClass *klass) /** * AsStore::changed: - * @device: the #AsStore instance that emitted the signal + * @store: the #AsStore instance that emitted the signal * * The ::changed signal is emitted when components have been added * or removed from the store. @@ -157,6 +160,57 @@ as_store_class_init (AsStoreClass *klass) NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0); + /** + * AsStore::app-added: + * @store: the #AsStore instance that emitted the signal + * @app: the #AsApp instance + * + * The ::app-added signal is emitted when a component has been added to + * the store. + * + * Since: 0.6.5 + **/ + signals [SIGNAL_APP_ADDED] = + g_signal_new ("app-added", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (AsStoreClass, app_added), + NULL, NULL, g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, AS_TYPE_APP); + + /** + * AsStore::app-removed: + * @store: the #AsStore instance that emitted the signal + * @app: the #AsApp instance + * + * The ::app-removed signal is emitted when a component has been removed + * from the store. + * + * Since: 0.6.5 + **/ + signals [SIGNAL_APP_REMOVED] = + g_signal_new ("app-removed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (AsStoreClass, app_removed), + NULL, NULL, g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, AS_TYPE_APP); + + /** + * AsStore::app-changed: + * @store: the #AsStore instance that emitted the signal + * @app: the #AsApp instance + * + * The ::app-changed signal is emitted when a component has been changed + * in the store. + * + * Since: 0.6.5 + **/ + signals [SIGNAL_APP_CHANGED] = + g_signal_new ("app-changed", + G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST, + G_STRUCT_OFFSET (AsStoreClass, app_changed), + NULL, NULL, g_cclosure_marshal_VOID__OBJECT, + G_TYPE_NONE, 1, AS_TYPE_APP); + object_class->finalize = as_store_finalize; } @@ -834,6 +888,9 @@ as_store_remove_app (AsStore *store, AsApp *app) AsStorePrivate *priv = GET_PRIVATE (store); GPtrArray *apps; + /* emit before removal */ + g_signal_emit (store, signals[SIGNAL_APP_REMOVED], 0, app); + /* only remove this specific unique app */ apps = g_hash_table_lookup (priv->hash_id, as_app_get_id (app)); if (apps != NULL) @@ -869,6 +926,10 @@ as_store_remove_app_by_id (AsStore *store, const gchar *id) app = g_ptr_array_index (priv->array, i); if (g_strcmp0 (id, as_app_get_id (app)) != 0) continue; + + /* emit before removal */ + g_signal_emit (store, signals[SIGNAL_APP_REMOVED], 0, app); + g_ptr_array_remove (priv->array, app); g_hash_table_remove (priv->hash_unique_id, as_app_get_unique_id (app)); @@ -994,6 +1055,10 @@ as_store_add_app (AsStore *store, AsApp *app) as_app_merge_kind_to_string (merge_kind), id, as_app_get_unique_id (app_tmp)); as_app_subsume_full (app_tmp, app, flags); + + /* emit after changes have been made */ + g_signal_emit (store, signals[SIGNAL_APP_CHANGED], + 0, app_tmp); } return; } @@ -1151,6 +1216,7 @@ as_store_add_app (AsStore *store, AsApp *app) } /* added */ + g_signal_emit (store, signals[SIGNAL_APP_ADDED], 0, app); as_store_perhaps_emit_changed (store, "add-app"); } diff --git a/libappstream-glib/as-store.h b/libappstream-glib/as-store.h index 55338e4..fd41a00 100644 --- a/libappstream-glib/as-store.h +++ b/libappstream-glib/as-store.h @@ -41,14 +41,17 @@ struct _AsStoreClass { GObjectClass parent_class; void (*changed) (AsStore *store); + void (*app_added) (AsStore *store, + AsApp *app); + void (*app_removed) (AsStore *store, + AsApp *app); + void (*app_changed) (AsStore *store, + AsApp *app); /*< private >*/ void (*_as_reserved1) (void); void (*_as_reserved2) (void); void (*_as_reserved3) (void); void (*_as_reserved4) (void); - void (*_as_reserved5) (void); - void (*_as_reserved6) (void); - void (*_as_reserved7) (void); }; /** |