summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonny Lamb <jonny.lamb@collabora.co.uk>2011-05-02 13:23:01 +0100
committerJonny Lamb <jonny.lamb@collabora.co.uk>2011-05-02 13:23:01 +0100
commit16e2a1a444252f26e55b85aeaf73b0b7fb5ea9e3 (patch)
treeb4483809378025f30bb08115656a55f704fc8f73
parent342d7fa70b8f9cbf8f0ab9078060e17daf4316e2 (diff)
status: take a TpAccount and wait for a connection instead of taking a connection directly
Signed-off-by: Jonny Lamb <jonny.lamb@collabora.co.uk>
-rw-r--r--docs/reference/telepathy-ytstenut-glib-sections.txt4
-rw-r--r--telepathy-ytstenut-glib/status.c146
-rw-r--r--telepathy-ytstenut-glib/status.h8
-rw-r--r--telepathy-ytstenut-glib/tests/nosey-status.c71
-rw-r--r--telepathy-ytstenut-glib/tests/passing-status.c64
5 files changed, 145 insertions, 148 deletions
diff --git a/docs/reference/telepathy-ytstenut-glib-sections.txt b/docs/reference/telepathy-ytstenut-glib-sections.txt
index 0a7b7e7..aedf356 100644
--- a/docs/reference/telepathy-ytstenut-glib-sections.txt
+++ b/docs/reference/telepathy-ytstenut-glib-sections.txt
@@ -48,8 +48,8 @@ TP_YTS_SVC_ACCOUNT_MANAGER_GET_CLASS
<FILE>status</FILE>
TpYtsStatus
TpYtsStatusClass
-tp_yts_status_ensure_for_connection_async
-tp_yts_status_ensure_for_connection_finish
+tp_yts_status_ensure_async
+tp_yts_status_ensure_finish
tp_yts_status_get_discovered_services
tp_yts_status_get_discovered_statuses
tp_yts_status_advertise_status_async
diff --git a/telepathy-ytstenut-glib/status.c b/telepathy-ytstenut-glib/status.c
index 68c9b67..02d1fbf 100644
--- a/telepathy-ytstenut-glib/status.c
+++ b/telepathy-ytstenut-glib/status.c
@@ -53,9 +53,9 @@
* Ytstenut Status service.
*
* Each Ytstenut Status object is associated with a relevant Ytstenut-enabled
- * telepathy connection, represented by a #TpConnection object. To create a new
- * TpYtsStatus object for a given connection use the
- * use tp_yts_status_ensure_for_connection_async() function.
+ * telepathy account, represented by a #TpAccount object. To create a new
+ * #TpYtsStatus object for a given account use the
+ * use tp_yts_status_ensure_async() function.
*
* This object automatically keeps track of all the discovered Ytstenut
* services and their statuses. Use tp_yts_status_get_discovered_statuses()
@@ -520,9 +520,88 @@ on_connection_future_ensure_sidecar_returned (GObject *source_object,
g_free (object_path);
}
+static void
+on_account_notify_connection (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data)
+{
+ TpAccount *account = TP_ACCOUNT (object);
+ GSimpleAsyncResult *res = G_SIMPLE_ASYNC_RESULT (user_data);
+ TpConnection *connection;
+
+ connection = tp_account_get_connection (account);
+
+ if (connection != NULL)
+ {
+ /* TODO: this should be given the cancellable when they're used. */
+ _tp_yts_connection_future_ensure_sidecar_async (connection,
+ TP_YTS_IFACE_STATUS, NULL,
+ on_connection_future_ensure_sidecar_returned, res);
+ }
+}
+
+typedef struct
+{
+ TpAccount *account;
+ guint source_id;
+ guint handler_id;
+ GSimpleAsyncResult *res;
+} ChangingPresenceData;
+
+static void
+on_account_notify_changing_presence (GObject *object,
+ GParamSpec *pspec,
+ gpointer user_data)
+{
+ TpAccount *account = TP_ACCOUNT (object);
+ ChangingPresenceData *data = user_data;
+ TpConnection *connection;
+
+ if (tp_account_get_changing_presence (account))
+ {
+ g_source_remove (data->source_id);
+ g_signal_handler_disconnect (data->account, data->handler_id);
+
+ connection = tp_account_get_connection (account);
+
+ if (connection == NULL)
+ {
+ /* now just wait for the connection */
+ tp_g_signal_connect_object (account, "notify::connection",
+ G_CALLBACK (on_account_notify_connection), data->res, 0);
+ }
+ else
+ {
+ /* TODO: this should be given the cancellable when they're used. */
+ _tp_yts_connection_future_ensure_sidecar_async (connection,
+ TP_YTS_IFACE_STATUS, NULL,
+ on_connection_future_ensure_sidecar_returned, data->res);
+ }
+
+ g_slice_free (ChangingPresenceData, data);
+ }
+}
+
+static gboolean
+on_account_timeout (gpointer user_data)
+{
+ ChangingPresenceData *data = user_data;
+
+ g_signal_handler_disconnect (data->account, data->handler_id);
+
+ g_simple_async_result_set_error (data->res, TP_ERRORS,
+ TP_ERROR_DISCONNECTED, "The account is not connected");
+ g_simple_async_result_complete (data->res);
+ g_object_unref (data->res);
+
+ g_slice_free (ChangingPresenceData, data);
+
+ return FALSE;
+}
+
/**
- * tp_yts_status_ensure_for_connection_async:
- * @connection: The Ytstenut enabled connection
+ * tp_yts_status_ensure_async:
+ * @account: The Ytstenut enabled account
* @cancellable: Not used
* @callback: A callback which should be called when the result is ready
* @user_data: Data to pass to the callback
@@ -530,26 +609,55 @@ on_connection_future_ensure_sidecar_returned (GObject *source_object,
* Create a #TpYtsStatus object for a Ytstenut enabled connection.
*/
void
-tp_yts_status_ensure_for_connection_async (TpConnection *connection,
+tp_yts_status_ensure_async (TpAccount *account,
GCancellable *cancellable,
GAsyncReadyCallback callback,
gpointer user_data)
{
GSimpleAsyncResult *res;
+ TpConnection *connection;
+
+ g_return_if_fail (TP_IS_ACCOUNT (account));
- g_return_if_fail (TP_IS_CONNECTION (connection));
+ connection = tp_account_get_connection (account);
- res = g_simple_async_result_new (G_OBJECT (connection), callback, user_data,
- tp_yts_status_ensure_for_connection_async);
+ res = g_simple_async_result_new (G_OBJECT (account), callback, user_data,
+ tp_yts_status_ensure_async);
- _tp_yts_connection_future_ensure_sidecar_async (connection,
- TP_YTS_IFACE_STATUS, cancellable,
- on_connection_future_ensure_sidecar_returned, res);
+ if (connection == NULL)
+ {
+ if (tp_account_get_changing_presence (account))
+ {
+ /* just wait for the connection */
+ tp_g_signal_connect_object (account, "notify::connection",
+ G_CALLBACK (on_account_notify_connection), res, 0);
+ }
+ else
+ {
+ /* wait to see if the connection appears */
+ ChangingPresenceData *data = g_slice_new0 (ChangingPresenceData);
+ data->res = res;
+ data->account = account;
+
+ data->source_id = g_timeout_add_seconds (1, on_account_timeout, data);
+
+ data->handler_id = g_signal_connect (account,
+ "notify::changing-presence",
+ G_CALLBACK (on_account_notify_changing_presence),
+ data);
+ }
+ }
+ else
+ {
+ _tp_yts_connection_future_ensure_sidecar_async (connection,
+ TP_YTS_IFACE_STATUS, cancellable,
+ on_connection_future_ensure_sidecar_returned, res);
+ }
}
/**
- * tp_yts_status_ensure_for_connection_finish:
- * @connection: The Ytstenut enabled connection
+ * tp_yts_status_ensure_finish:
+ * @account: The Ytstenut enabled account
* @result: The result object passed to the callback
* @error: If an error occurred, this will be set
*
@@ -559,21 +667,21 @@ tp_yts_status_ensure_for_connection_async (TpConnection *connection,
* the Ytstenut Status service. If the operation failed, %NULL will be returned.
*/
TpYtsStatus *
-tp_yts_status_ensure_for_connection_finish (TpConnection *connection,
+tp_yts_status_ensure_finish (TpAccount *account,
GAsyncResult *result,
GError **error)
{
GSimpleAsyncResult *res;
- g_return_val_if_fail (TP_IS_CONNECTION (connection), NULL);
- g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), FALSE);
+ g_return_val_if_fail (TP_IS_ACCOUNT (account), NULL);
+ g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (result), NULL);
res = G_SIMPLE_ASYNC_RESULT (result);
g_return_val_if_fail (g_simple_async_result_is_valid (result,
- G_OBJECT (connection), tp_yts_status_ensure_for_connection_async), FALSE);
+ G_OBJECT (account), tp_yts_status_ensure_async), NULL);
if (g_simple_async_result_propagate_error (res, error))
- return FALSE;
+ return NULL;
return g_object_ref (g_simple_async_result_get_op_res_gpointer (res));
}
diff --git a/telepathy-ytstenut-glib/status.h b/telepathy-ytstenut-glib/status.h
index 6aaad42..0e24a62 100644
--- a/telepathy-ytstenut-glib/status.h
+++ b/telepathy-ytstenut-glib/status.h
@@ -21,7 +21,7 @@
#ifndef TP_YTS_STATUS_H
#define TP_YTS_STATUS_H
-#include <telepathy-glib/connection.h>
+#include <telepathy-glib/account.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/defs.h>
#include <telepathy-glib/proxy.h>
@@ -58,12 +58,12 @@ GType tp_yts_status_get_type (void);
#define TP_YTS_STATUS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), \
TP_TYPE_YTS_STATUS, TpYtsStatusClass))
-void tp_yts_status_ensure_for_connection_async (TpConnection *connection,
+void tp_yts_status_ensure_async (TpAccount *account,
GCancellable *cancellable, GAsyncReadyCallback callback,
gpointer user_data);
-TpYtsStatus *tp_yts_status_ensure_for_connection_finish (
- TpConnection *connection, GAsyncResult *result, GError **error);
+TpYtsStatus *tp_yts_status_ensure_finish (
+ TpAccount *account, GAsyncResult *result, GError **error);
void tp_yts_status_advertise_status_async (TpYtsStatus *self,
const gchar *capability, const gchar *service_name, const gchar *status_xml,
diff --git a/telepathy-ytstenut-glib/tests/nosey-status.c b/telepathy-ytstenut-glib/tests/nosey-status.c
index 2b7f97f..e5b3264 100644
--- a/telepathy-ytstenut-glib/tests/nosey-status.c
+++ b/telepathy-ytstenut-glib/tests/nosey-status.c
@@ -168,8 +168,8 @@ status_ensured_cb (GObject *source_object,
TpYtsStatus *status;
GError *error = NULL;
- status = tp_yts_status_ensure_for_connection_finish (
- TP_CONNECTION (source_object), result, &error);
+ status = tp_yts_status_ensure_finish (
+ TP_ACCOUNT (source_object), result, &error);
if (status == NULL)
{
@@ -231,17 +231,17 @@ status_ensured_cb (GObject *source_object,
}
static void
-connection_prepared_cb (GObject *source_object,
+am_get_account_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
{
- TpConnection *connection = TP_CONNECTION (source_object);
- TpAccount *account = user_data;
GError *error = NULL;
+ TpAccount *account = tp_yts_account_manager_get_account_finish (
+ TP_YTS_ACCOUNT_MANAGER (source_object), result, &error);
- if (!tp_proxy_prepare_finish (source_object, result, &error))
+ if (account == NULL)
{
- g_printerr ("Failed to prepare connection: %s\n", error->message);
+ g_printerr ("Failed to get automatic account: %s\n", error->message);
getoutofhere ();
}
else
@@ -254,7 +254,7 @@ connection_prepared_cb (GObject *source_object,
tp_yts_client_register (client, NULL);
- tp_yts_status_ensure_for_connection_async (connection,
+ tp_yts_status_ensure_async (account,
NULL, status_ensured_cb, NULL);
}
@@ -263,61 +263,6 @@ connection_prepared_cb (GObject *source_object,
}
static void
-notify_connection_cb (GObject *gobject,
- GParamSpec *pspec,
- gpointer user_data)
-{
- TpAccount *account = TP_ACCOUNT (gobject);
- GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
- TpConnection *connection = tp_account_get_connection (account);
-
- if (connection == NULL)
- return;
-
- g_print ("Trying to prepare account, this will only continue "
- "if the account is connected...\n");
-
- /* account already reffed */
- tp_proxy_prepare_async (connection, features, connection_prepared_cb,
- account);
-}
-
-static void
-am_get_account_cb (GObject *source_object,
- GAsyncResult *result,
- gpointer user_data)
-{
- GError *error = NULL;
- TpAccount *account = tp_yts_account_manager_get_account_finish (
- TP_YTS_ACCOUNT_MANAGER (source_object), result, &error);
-
- if (account == NULL)
- {
- g_printerr ("Failed to get automatic account: %s\n", error->message);
- getoutofhere ();
- }
- else
- {
- /* We got the account fine, but we need to ensure some features
- * on it so we have the :self-contact property set. */
- TpConnection *connection = tp_account_get_connection (account);
-
- if (connection != NULL)
- {
- notify_connection_cb (g_object_ref (account), NULL, NULL);
- }
- else
- {
- g_signal_connect (account, "notify::connection",
- G_CALLBACK (notify_connection_cb), g_object_ref (account));
- }
- }
-
- g_object_unref (account);
- g_clear_error (&error);
-}
-
-static void
sigint_handler (int s)
{
g_print ("Cleaning up...\n");
diff --git a/telepathy-ytstenut-glib/tests/passing-status.c b/telepathy-ytstenut-glib/tests/passing-status.c
index e9f0db6..64ed7ff 100644
--- a/telepathy-ytstenut-glib/tests/passing-status.c
+++ b/telepathy-ytstenut-glib/tests/passing-status.c
@@ -114,8 +114,8 @@ status_ensured_cb (GObject *source_object,
TpYtsStatus *status;
GError *error = NULL;
- status = tp_yts_status_ensure_for_connection_finish (
- TP_CONNECTION (source_object), result, &error);
+ status = tp_yts_status_ensure_finish (
+ TP_ACCOUNT (source_object), result, &error);
if (status == NULL)
{
@@ -140,55 +140,10 @@ status_ensured_cb (GObject *source_object,
NULL, advertise_status_cb, NULL);
}
- g_object_unref (account);
g_clear_error (&error);
}
static void
-connection_prepared_cb (GObject *source_object,
- GAsyncResult *result,
- gpointer user_data)
-{
- TpConnection *connection = TP_CONNECTION (source_object);
- TpAccount *account = user_data;
- GError *error = NULL;
-
- if (!tp_proxy_prepare_finish (source_object, result, &error))
- {
- g_printerr ("Failed to prepare connection: %s\n", error->message);
- getoutofhere ();
- }
- else
- {
- tp_yts_status_ensure_for_connection_async (connection,
- NULL, status_ensured_cb, g_object_ref (account));
- }
-
- g_object_unref (account);
- g_clear_error (&error);
-}
-
-static void
-notify_connection_cb (GObject *gobject,
- GParamSpec *pspec,
- gpointer user_data)
-{
- TpAccount *account = TP_ACCOUNT (gobject);
- GQuark features[] = { TP_CONNECTION_FEATURE_CONNECTED, 0 };
- TpConnection *connection = tp_account_get_connection (account);
-
- if (connection == NULL)
- return;
-
- g_print ("Trying to prepare account, this will only continue "
- "if the account is connected...\n");
-
- /* account already reffed */
- tp_proxy_prepare_async (connection, features, connection_prepared_cb,
- account);
-}
-
-static void
am_get_account_cb (GObject *source_object,
GAsyncResult *result,
gpointer user_data)
@@ -204,19 +159,8 @@ am_get_account_cb (GObject *source_object,
}
else
{
- /* We got the account fine, but we need to ensure some features
- * on it so we have the :self-contact property set. */
- TpConnection *connection = tp_account_get_connection (account);
-
- if (connection != NULL)
- {
- notify_connection_cb (g_object_ref (account), NULL, NULL);
- }
- else
- {
- g_signal_connect (account, "notify::connection",
- G_CALLBACK (notify_connection_cb), g_object_ref (account));
- }
+ tp_yts_status_ensure_async (account,
+ NULL, status_ensured_cb, NULL);
}
g_object_unref (account);