#include <gtk/gtk.h>
#include <hildon/hildon.h>
+#include <rtm-glib/rtm-error.h>
#include <rtm-glib/rtm-glib.h>
#include "milk-auth.h"
+#include "milk-dialogs.h"
G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
RtmGlib *rtm_glib;
char *api_key;
char *shared_secret;
+ char *frob;
+ MilkAuthState state;
};
enum {
PROP_API_KEY = 1,
PROP_SHARED_SECRET,
+ PROP_STATE,
};
static MilkAuth *default_auth = NULL;
+MilkAuthState
+milk_auth_get_state (MilkAuth *auth)
+{
+ g_return_val_if_fail (auth, MILK_AUTH_STATE_DISCONNECTED);
+ g_return_val_if_fail (MILK_IS_AUTH (auth),
+ MILK_AUTH_STATE_DISCONNECTED);
+
+ return MILK_AUTH_PRIVATE (auth)->state;
+}
+
static void
milk_auth_get_property (GObject *object,
guint property_id,
g_value_set_string (value, priv->shared_secret);
break;
+ case PROP_STATE:
+ g_value_set_int (value, priv->state);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
pspec);
priv->shared_secret = g_value_dup_string (value);
break;
+ case PROP_STATE:
+ priv->state = g_value_get_int (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
pspec);
}
static void
+auth_response_cb (GtkWidget *dialog,
+ int response,
+ MilkAuth *auth)
+
+{
+ MilkAuthPrivate *priv;
+ GError *error = NULL;
+ gchar *auth_token;
+ gchar *username;
+ MilkAuthState previous_state;
+
+ priv = MILK_AUTH_PRIVATE (auth);
+
+ previous_state = priv->state;
+ auth_token = rtm_glib_auth_get_token (priv->rtm_glib, priv->frob,
+ &error);
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ goto auth_response_cb_error_OUT;
+ }
+
+ if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) {
+ g_error ("auth_token not valid!\n");
+ goto auth_response_cb_error_OUT;
+ }
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ goto auth_response_cb_error_OUT;
+ }
+ username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
+
+ g_free (auth_token);
+
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ goto auth_response_cb_error_OUT;
+ }
+
+ priv->state = MILK_AUTH_STATE_CONNECTED;
+ goto auth_response_cb_OUT;
+
+auth_response_cb_error_OUT:
+ priv->state = MILK_AUTH_STATE_DISCONNECTED;
+
+auth_response_cb_OUT:
+ if (priv->state != previous_state)
+ g_object_notify (G_OBJECT (auth), "state");
+
+ gtk_widget_destroy (dialog);
+}
+
+GList *
+milk_auth_get_tasks (MilkAuth *auth)
+{
+ MilkAuthPrivate *priv;
+ GError *error = NULL;
+ GList *rtm_tasks;
+
+ g_return_val_if_fail (auth, NULL);
+ g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
+ g_return_val_if_fail (
+ milk_auth_get_state (auth) == MILK_AUTH_STATE_CONNECTED,
+ NULL);
+
+ priv = MILK_AUTH_PRIVATE (auth);
+
+ /* FIXME: cache this */
+ rtm_tasks = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL, NULL,
+ &error);
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ return NULL;
+ }
+
+ return rtm_tasks;
+}
+
+void
+milk_auth_log_in (MilkAuth *auth)
+{
+ MilkAuthPrivate *priv;
+ GError *error = NULL;
+ gchar *url;
+ GtkDialog *dialog;
+
+ g_return_if_fail (auth);
+ g_return_if_fail (MILK_IS_AUTH (auth));
+
+ priv = MILK_AUTH_PRIVATE (auth);
+
+ if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
+ g_print ("Test echo OK!\n");
+ } else {
+ g_print ("Test echo FAIL!\n");
+ return;
+ }
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ return;
+ }
+
+ /* FIXME: relocate this */
+ if (priv->frob)
+ g_free (priv->frob);
+
+ priv->frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
+ if (error != NULL) {
+ g_error ("%s", rtm_error_get_message (error));
+ return;
+ }
+ g_print ("Frob: %s\n", priv->frob);
+
+ url = rtm_glib_auth_get_login_url (priv->rtm_glib, priv->frob,
+ "delete");
+ g_print ("URL: %s\n", url);
+
+ dialog = milk_dialogs_auth_prompt (NULL, url);
+
+ g_signal_connect (dialog, "response", G_CALLBACK (auth_response_cb),
+ auth);
+}
+
+static void
milk_auth_constructed (GObject *object)
{
MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
g_free (priv->api_key);
g_free (priv->shared_secret);
+ g_free (priv->frob);
}
static void
RTM_SHARED_SECRET,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
+
+ g_object_class_install_property
+ (object_class,
+ PROP_STATE,
+ g_param_spec_int
+ ("state",
+ "Authentication state",
+ "Authentication/connection state with the server.",
+ 0, (NUM_MILK_AUTH_STATES-1),
+ MILK_AUTH_STATE_DISCONNECTED,
+ G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
+ G_PARAM_STATIC_STRINGS));
}
static void