Update the task model with tasks from the server.
[milk] / src / milk-auth.c
index 4d51857..aa075fe 100644 (file)
@@ -28,6 +28,7 @@
 #include <rtm-glib/rtm-glib.h>
 
 #include "milk-auth.h"
+#include "milk-dialogs.h"
 
 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
 
@@ -42,16 +43,29 @@ struct _MilkAuthPrivate
         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,
@@ -70,6 +84,10 @@ milk_auth_get_property (GObject    *object,
                         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);
@@ -98,151 +116,138 @@ milk_auth_set_property (GObject      *object,
                         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);
         }
 }
 
-void
-milk_auth_run_demo (MilkAuth *auth)
+static void
+auth_response_cb (GtkWidget *dialog,
+                  int        response,
+                  MilkAuth  *auth)
+
 {
         MilkAuthPrivate *priv;
-
-        /* FIXME: clean this up */
         GError *error = NULL;
-        /* FIXME: cut this
-        RtmGlib *rtm;
-        */
-        gchar *frob;
-        gchar *url;
         gchar *auth_token;
         gchar *username;
-        /* FIXME: cut this
-        GList *glist;
-        GList *item;
-        RtmTask *task;
-        RtmList *rtm_list;
-        gchar *timeline;
-        gchar *transaction_id;
-        RtmLocation *location;
-        gchar *list_id_sent = NULL;
-        RtmTimeZone *time_zone;
-        gchar *time;
-        */
+        MilkAuthState previous_state;
 
         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");
-        }
+        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;
         }
 
-        /* FIXME: cut this
-        glist = rtm_glib_time_zones_get_list (priv->rtm_glib, &error);
-        if (error != NULL) {
-                g_error ("%s", rtm_error_get_message (error));
-        }
-        for (item = glist; item; item = g_list_next (item)) {
-                time_zone = (RtmTimeZone *) item->data;
-                g_print ("%s", rtm_time_zone_to_string (time_zone));
+        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;
         }
-        g_list_free (glist);
-
-        time = rtm_glib_time_parse (priv->rtm_glib, "02/10/2009 10:25", NULL, FALSE, &error);
         if (error != NULL) {
                 g_error ("%s", rtm_error_get_message (error));
+                goto auth_response_cb_error_OUT;
         }
-        g_print ("Time: %s\n", time);
-        g_free (time);
+        username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
 
-        time = rtm_glib_time_convert (priv->rtm_glib, "Europe/Madrid", NULL, NULL, &error);
-        if (error != NULL) {
-                g_error ("%s", rtm_error_get_message (error));
-        }
-        g_print ("Time: %s\n", time);
-        g_free (time);
-        */
+        g_free (auth_token);
 
-        frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
         if (error != NULL) {
                 g_error ("%s", rtm_error_get_message (error));
+                goto auth_response_cb_error_OUT;
         }
-        g_print ("Frob: %s\n", frob);
 
-        url = rtm_glib_auth_get_login_url (priv->rtm_glib, frob, "delete");
-        g_print ("URL: %s\n", url);
+        priv->state = MILK_AUTH_STATE_CONNECTED;
+        goto auth_response_cb_OUT;
 
-        getchar ();
+auth_response_cb_error_OUT:
+        priv->state = MILK_AUTH_STATE_DISCONNECTED;
 
-        auth_token = rtm_glib_auth_get_token (priv->rtm_glib, frob, &error);
-        if (error != NULL) {
-                g_error ("%s", rtm_error_get_message (error));
-        }
+auth_response_cb_OUT:
+        if (priv->state != previous_state)
+                g_object_notify (G_OBJECT (auth), "state");
 
-        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));
-        }
-        username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
+        gtk_widget_destroy (dialog);
+}
 
-        g_free (auth_token);
+GList *
+milk_auth_get_tasks (MilkAuth *auth)
+{
+        MilkAuthPrivate *priv;
+        GError *error = NULL;
+        GList *rtm_tasks;
 
-        if (error != NULL) {
-                g_error ("%s", rtm_error_get_message (error));
-        }
+        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);
 
-        /* FIXME: work this in where appropriate */
-#if 0
-        glist = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL, NULL, &error);
-        if (error != NULL) {
-                g_error ("%s", rtm_error_get_message (error));
-        }
-        for (item = glist; item; item = g_list_next (item)) {
-                task = (RtmTask *) item->data;
-                g_print ("%s", rtm_task_to_string (task));
-        }
-        g_list_free (glist);
+        priv = MILK_AUTH_PRIVATE (auth);
 
-        glist = rtm_glib_lists_get_list (priv->rtm_glib, &error);
+        /* 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;
         }
-        for (item = glist; item; item = g_list_next (item)) {
-                rtm_list = (RtmList *) item->data;
-                if (g_strcmp0 (rtm_list_get_name (rtm_list), "Sent") == 0) {
-                        list_id_sent = rtm_list_get_id (rtm_list);
-                }
-                g_print ("%s", rtm_list_to_string (rtm_list));
-        }
-        g_list_free (glist);
+        
+        return rtm_tasks;
+}
 
-        timeline = rtm_glib_timelines_create (priv->rtm_glib, &error);
+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;
         }
-        g_print ("timeline: %s", timeline);
 
-        task = rtm_glib_tasks_add (priv->rtm_glib, timeline, "test-rtm-glib", NULL, FALSE, &error);
+        /* 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;
         }
-        if (task != NULL) {
-                g_print ("First task added! task_id: %s\n", rtm_task_get_id (task));
-        } else {
-                g_print ("First task NOT added!\n");
-        }
+        g_print ("Frob: %s\n", priv->frob);
 
-#endif
-}
+        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)
@@ -261,6 +266,7 @@ milk_auth_finalize (GObject *object)
 
         g_free (priv->api_key);
         g_free (priv->shared_secret);
+        g_free (priv->frob);
 }
 
 static void
@@ -297,6 +303,18 @@ milk_auth_class_init (MilkAuthClass *klass)
                           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