From c67fe4f5a56040d8fa4496e4bdc38340117225c6 Mon Sep 17 00:00:00 2001 From: Vincent Untz Date: Wed, 17 Feb 2010 13:44:13 +0100 Subject: [PATCH] glib2: add pk_control_suggest_daemon_quit() (and async version) --- lib/packagekit-glib2/pk-control-sync.c | 52 +++++++++++ lib/packagekit-glib2/pk-control-sync.h | 3 + lib/packagekit-glib2/pk-control.c | 145 ++++++++++++++++++++++++++++++++ lib/packagekit-glib2/pk-control.h | 7 ++ 4 files changed, 207 insertions(+), 0 deletions(-) diff --git a/lib/packagekit-glib2/pk-control-sync.c b/lib/packagekit-glib2/pk-control-sync.c index feff991..7fd3dfe 100644 --- a/lib/packagekit-glib2/pk-control-sync.c +++ b/lib/packagekit-glib2/pk-control-sync.c @@ -144,3 +144,55 @@ pk_control_get_transaction_list (PkControl *control, GCancellable *cancellable, return transaction_list; } +/** + * pk_control_suggest_daemon_quit_cb: + **/ +static void +pk_control_suggest_daemon_quit_cb (PkControl *control, GAsyncResult *res, PkControlHelper *helper) +{ + /* get the result */ + helper->ret = pk_control_suggest_daemon_quit_finish (control, res, helper->error); + g_main_loop_quit (helper->loop); +} + +/** + * pk_control_suggest_daemon_quit: + * @control: a valid #PkControl instance + * @cancellable: a #GCancellable or %NULL + * @error: A #GError or %NULL + * + * Suggests to the daemon that it should quit as soon as possible. + * Warning: this function is synchronous, and may block. Do not use it in GUI + * applications. + * + * Return value: %TRUE if the suggestion was sent + * + * Since: 0.6.2 + **/ +gboolean +pk_control_suggest_daemon_quit (PkControl *control, GCancellable *cancellable, GError **error) +{ + gboolean ret; + PkControlHelper *helper; + + g_return_val_if_fail (PK_IS_CONTROL (control), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + /* create temp object */ + helper = g_new0 (PkControlHelper, 1); + helper->loop = g_main_loop_new (NULL, FALSE); + helper->error = error; + + /* run async method */ + pk_control_suggest_daemon_quit_async (control, cancellable, (GAsyncReadyCallback) pk_control_suggest_daemon_quit_cb, helper); + g_main_loop_run (helper->loop); + + ret = helper->ret; + + /* free temp object */ + g_main_loop_unref (helper->loop); + g_free (helper); + + return ret; +} + diff --git a/lib/packagekit-glib2/pk-control-sync.h b/lib/packagekit-glib2/pk-control-sync.h index 3b507d3..cb8b677 100644 --- a/lib/packagekit-glib2/pk-control-sync.h +++ b/lib/packagekit-glib2/pk-control-sync.h @@ -31,6 +31,9 @@ gboolean pk_control_get_properties (PkControl *control, gchar **pk_control_get_transaction_list (PkControl *control, GCancellable *cancellable, GError **error); +gboolean pk_control_suggest_daemon_quit (PkControl *control, + GCancellable *cancellable, + GError **error); #endif /* __PK_CONTROL_SYNC_H */ diff --git a/lib/packagekit-glib2/pk-control.c b/lib/packagekit-glib2/pk-control.c index 1d3c288..682d9c7 100644 --- a/lib/packagekit-glib2/pk-control.c +++ b/lib/packagekit-glib2/pk-control.c @@ -339,6 +339,151 @@ pk_control_get_tid_finish (PkControl *control, GAsyncResult *res, GError **error /***************************************************************************************************/ /** + * pk_control_suggest_daemon_quit_state_finish: + **/ +static void +pk_control_suggest_daemon_quit_state_finish (PkControlState *state, const GError *error) +{ + /* get result */ + if (state->ret) { + g_simple_async_result_set_op_res_gboolean (state->res, state->ret); + } else { + g_simple_async_result_set_from_error (state->res, error); + } + + /* remove from list */ + g_ptr_array_remove (state->control->priv->calls, state); + if (state->call != NULL) + egg_warning ("state array remove %p (%p)", state, state->call); + else + egg_debug ("state array remove %p", state); + + /* complete */ + g_simple_async_result_complete_in_idle (state->res); + + /* deallocate */ + if (state->cancellable != NULL) { + g_cancellable_disconnect (state->cancellable, state->cancellable_id); + g_object_unref (state->cancellable); + } + g_object_unref (state->res); + g_object_unref (state->control); + g_slice_free (PkControlState, state); +} + +/** + * pk_control_suggest_daemon_quit_cb: + **/ +static void +pk_control_suggest_daemon_quit_cb (DBusGProxy *proxy, DBusGProxyCall *call, PkControlState *state) +{ + GError *error = NULL; + gchar *tid = NULL; + gboolean ret; + + /* finished this call */ + state->call = NULL; + + /* get the result */ + ret = dbus_g_proxy_end_call (proxy, call, &error, + G_TYPE_INVALID); + if (!ret) { + egg_warning ("failed to suggest quit: %s", error->message); + pk_control_suggest_daemon_quit_state_finish (state, error); + g_error_free (error); + goto out; + } + + /* save data */ + state->ret = TRUE; + + /* we're done */ + pk_control_suggest_daemon_quit_state_finish (state, NULL); +out: + g_free (tid); +} + +/** + * pk_control_suggest_daemon_quit_async: + * @control: a valid #PkControl instance + * @cancellable: a #GCancellable or %NULL + * @callback: the function to run on completion + * @user_data: the data to pass to @callback + * + * Suggests to the daemon that it should quit as soon as possible. + * + * Since: 0.6.2 + **/ +void +pk_control_suggest_daemon_quit_async (PkControl *control, GCancellable *cancellable, GAsyncReadyCallback callback, gpointer user_data) +{ + GSimpleAsyncResult *res; + PkControlState *state; + + g_return_if_fail (PK_IS_CONTROL (control)); + g_return_if_fail (callback != NULL); + + res = g_simple_async_result_new (G_OBJECT (control), callback, user_data, pk_control_suggest_daemon_quit_async); + + /* save state */ + state = g_slice_new0 (PkControlState); + state->res = g_object_ref (res); + state->control = g_object_ref (control); + if (cancellable != NULL) { + state->cancellable = g_object_ref (cancellable); + state->cancellable_id = g_cancellable_connect (cancellable, G_CALLBACK (pk_control_cancellable_cancel_cb), state, NULL); + } + + /* call D-Bus method async */ + state->call = dbus_g_proxy_begin_call (control->priv->proxy, "SuggestDaemonQuit", + (DBusGProxyCallNotify) pk_control_suggest_daemon_quit_cb, state, + NULL, G_TYPE_INVALID); + if (state->call == NULL) + egg_error ("failed to setup call, maybe OOM or no connection"); + + /* track state */ + g_ptr_array_add (control->priv->calls, state); + egg_debug ("state array add %p (%p)", state, state->call); + + g_object_unref (res); +} + +/** + * pk_control_suggest_daemon_quit_finish: + * @control: a valid #PkControl instance + * @res: the #GAsyncResult + * @error: A #GError or %NULL + * + * Gets the result from the asynchronous function. + * + * Return value: %TRUE if the suggestion was sent + * + * Since: 0.6.2 + **/ +gboolean +pk_control_suggest_daemon_quit_finish (PkControl *control, GAsyncResult *res, GError **error) +{ + GSimpleAsyncResult *simple; + gpointer source_tag; + + g_return_val_if_fail (PK_IS_CONTROL (control), FALSE); + g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE); + g_return_val_if_fail (error == NULL || *error == NULL, FALSE); + + simple = G_SIMPLE_ASYNC_RESULT (res); + source_tag = g_simple_async_result_get_source_tag (simple); + + g_return_val_if_fail (source_tag == pk_control_suggest_daemon_quit_async, FALSE); + + if (g_simple_async_result_propagate_error (simple, error)) + return FALSE; + + return g_simple_async_result_get_op_res_gboolean (simple); +} + +/***************************************************************************************************/ + +/** * pk_control_get_daemon_state_state_finish: **/ static void diff --git a/lib/packagekit-glib2/pk-control.h b/lib/packagekit-glib2/pk-control.h index 562477d..d3d14d1 100644 --- a/lib/packagekit-glib2/pk-control.h +++ b/lib/packagekit-glib2/pk-control.h @@ -99,6 +99,13 @@ void pk_control_get_tid_async (PkControl *control, gchar *pk_control_get_tid_finish (PkControl *control, GAsyncResult *res, GError **error); +void pk_control_suggest_daemon_quit_async (PkControl *control, + GCancellable *cancellable, + GAsyncReadyCallback callback, + gpointer user_data); +gboolean pk_control_suggest_daemon_quit_finish (PkControl *control, + GAsyncResult *res, + GError **error); void pk_control_get_daemon_state_async (PkControl *control, GCancellable *cancellable, GAsyncReadyCallback callback, -- 1.6.6.1