From 431081eb0278f0cb2c8cc3badd1b7185dfe89f64 Mon Sep 17 00:00:00 2001 From: Travis Reitter Date: Sun, 22 Nov 2009 20:40:25 -0800 Subject: [PATCH] Prompt the user to log into Milk via the web page upon launch. --- src/Makefile.am | 2 + src/milk-auth.c | 129 +++++++++++++++++++++++------------------------- src/milk-dialogs.c | 96 +++++++++++++++++++++++++++++++++++ src/milk-dialogs.h | 25 ++++++++++ src/milk-main-window.c | 41 ++++++++++----- src/milk-main-window.h | 2 +- src/milk-main.c | 5 +- 7 files changed, 220 insertions(+), 80 deletions(-) create mode 100644 src/milk-dialogs.c create mode 100644 src/milk-dialogs.h diff --git a/src/Makefile.am b/src/Makefile.am index 4f5305b..0c45bb2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,6 +10,8 @@ bin_PROGRAMS = milk milk_SOURCES = \ milk-auth.c \ milk-auth.h \ + milk-dialogs.c \ + milk-dialogs.h \ milk-main.c \ milk-main.h \ milk-main-window.c \ diff --git a/src/milk-auth.c b/src/milk-auth.c index 4d51857..8f81131 100644 --- a/src/milk-auth.c +++ b/src/milk-auth.c @@ -28,6 +28,7 @@ #include #include "milk-auth.h" +#include "milk-dialogs.h" G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT); @@ -42,6 +43,7 @@ struct _MilkAuthPrivate RtmGlib *rtm_glib; char *api_key; char *shared_secret; + char *frob; }; enum { @@ -104,84 +106,25 @@ milk_auth_set_property (GObject *object, } } -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; - */ 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"); - } - if (error != NULL) { - g_error ("%s", rtm_error_get_message (error)); - } - - /* 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)); - } - 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)); - } - g_print ("Time: %s\n", time); - g_free (time); - - 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); - */ - - frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error); - if (error != NULL) { - g_error ("%s", rtm_error_get_message (error)); - } - g_print ("Frob: %s\n", frob); - - url = rtm_glib_auth_get_login_url (priv->rtm_glib, frob, "delete"); - g_print ("URL: %s\n", url); - - getchar (); - - auth_token = rtm_glib_auth_get_token (priv->rtm_glib, frob, &error); + 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_OUT; } if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) { @@ -190,6 +133,7 @@ milk_auth_run_demo (MilkAuth *auth) } if (error != NULL) { g_error ("%s", rtm_error_get_message (error)); + goto auth_response_cb_OUT; } username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error); @@ -197,6 +141,7 @@ milk_auth_run_demo (MilkAuth *auth) if (error != NULL) { g_error ("%s", rtm_error_get_message (error)); + goto auth_response_cb_OUT; } /* FIXME: work this in where appropriate */ @@ -214,6 +159,7 @@ milk_auth_run_demo (MilkAuth *auth) glist = rtm_glib_lists_get_list (priv->rtm_glib, &error); if (error != NULL) { g_error ("%s", rtm_error_get_message (error)); + goto auth_response_cb_OUT; } for (item = glist; item; item = g_list_next (item)) { rtm_list = (RtmList *) item->data; @@ -233,14 +179,62 @@ milk_auth_run_demo (MilkAuth *auth) task = rtm_glib_tasks_add (priv->rtm_glib, timeline, "test-rtm-glib", NULL, FALSE, &error); if (error != NULL) { g_error ("%s", rtm_error_get_message (error)); + goto auth_response_cb_OUT; } 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"); + goto auth_response_cb_OUT; } #endif + +auth_response_cb_OUT: + gtk_widget_destroy (dialog); +} + +void +milk_auth_run_demo (MilkAuth *auth) +{ + MilkAuthPrivate *priv; + + GError *error = NULL; + gchar *url; + GtkDialog *dialog; + + 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); } @@ -261,6 +255,7 @@ milk_auth_finalize (GObject *object) g_free (priv->api_key); g_free (priv->shared_secret); + g_free (priv->frob); } static void diff --git a/src/milk-dialogs.c b/src/milk-dialogs.c new file mode 100644 index 0000000..c1a3305 --- /dev/null +++ b/src/milk-dialogs.c @@ -0,0 +1,96 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: Travis Reitter + */ + +#include + +#include +#include +#include +#include +#include + +#include "milk-dialogs.h" +#include "milk-main-window.h" + +typedef struct { + const char *uri; + GtkWidget *finish_button; +} LinkClickedClosure; + +static void +link_clicked_cb (GtkButton *link, + LinkClickedClosure *closure) +{ + gtk_widget_set_sensitive (closure->finish_button, TRUE); + + hildon_uri_open (closure->uri, NULL, NULL); + + g_free (closure); +} + +static void +finish_button_clicked_cb (GtkButton *finish_button, + GtkDialog *dialog) +{ + gtk_dialog_response (dialog, GTK_RESPONSE_OK); +} + +GtkDialog* +milk_dialogs_auth_prompt (GtkWindow *parent, + const char *uri) +{ + HildonDialog *dialog; + GtkWidget *label; + GtkWidget *link; + GtkWidget *finish_button; + LinkClickedClosure *closure; + + if (!parent) + parent = GTK_WINDOW (milk_main_window_get_default ()); + + dialog = HILDON_DIALOG (hildon_dialog_new ()); + gtk_window_set_title (GTK_WINDOW (dialog), + _("Log in to Remember The Milk")); + gtk_window_set_transient_for (GTK_WINDOW (dialog), parent); + + label = gtk_label_new (_("Log in, then tap Finish")); + gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox), label); + + finish_button = hildon_button_new_with_text (HILDON_SIZE_FINGER_HEIGHT, + HILDON_BUTTON_ARRANGEMENT_VERTICAL, _("Finish"), NULL); + gtk_widget_set_sensitive (finish_button, FALSE); + g_signal_connect (finish_button, "clicked", + G_CALLBACK (finish_button_clicked_cb), dialog); + + link = gtk_link_button_new_with_label (uri, _("Log in")); + closure = g_new0 (LinkClickedClosure, 1); + closure->uri = uri; + closure->finish_button = finish_button; + g_signal_connect (link, "clicked", G_CALLBACK (link_clicked_cb), + closure); + + gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), + link); + gtk_container_add (GTK_CONTAINER (GTK_DIALOG (dialog)->action_area), + finish_button); + + gtk_widget_show_all (GTK_WIDGET (dialog)); + + return GTK_DIALOG (dialog); +} diff --git a/src/milk-dialogs.h b/src/milk-dialogs.h new file mode 100644 index 0000000..3df55df --- /dev/null +++ b/src/milk-dialogs.h @@ -0,0 +1,25 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301 USA + * + * Authors: Travis Reitter + */ + +#ifndef _MILK_DIALOGS_H +#define _MILK_DIALOGS_H + +GtkDialog* milk_dialogs_auth_prompt (GtkWindow *parent, const char *uri); + +#endif /* _MILK_DIALOGS_H */ diff --git a/src/milk-main-window.c b/src/milk-main-window.c index d72b983..06d435e 100644 --- a/src/milk-main-window.c +++ b/src/milk-main-window.c @@ -34,6 +34,8 @@ G_DEFINE_TYPE (MilkMainWindow, milk_main_window, HILDON_TYPE_WINDOW) /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */ #define MILK_MAIN_WINDOW_PRIVATE(o) ((MILK_MAIN_WINDOW ((o)))->priv) +static GtkWidget *default_window = NULL; + struct _MilkMainWindowPrivate { MilkAuth *auth; @@ -238,6 +240,25 @@ contact_column_render_func (GtkCellLayout *cell_layout, g_object_unref (task); } +static gboolean +begin_auth_idle (MilkMainWindow *window) +{ + MilkMainWindowPrivate *priv; + + priv = MILK_MAIN_WINDOW_PRIVATE (window); + + /* FIXME: is there a better place for this? */ + priv->auth = milk_auth_get_default (); + /* FIXME: plug this into the task model */ + + /* FIXME: cut this */ + g_debug ("trying to run the milk auth demo"); + /* FIXME: cut this */ + milk_auth_run_demo (priv->auth); + + return FALSE; +} + static void milk_main_window_constructed (GObject* object) { @@ -306,14 +327,9 @@ milk_main_window_constructed (GObject* object) hildon_window_set_app_menu ( HILDON_WINDOW (self), HILDON_APP_MENU (priv->app_menu)); - /* FIXME: is there a better place for this? */ - priv->auth = milk_auth_get_default (); - /* FIXME: plug this into the task model */ - - /* FIXME: cut this */ - g_debug ("trying to run the milk auth demo"); - /* FIXME: cut this */ - milk_auth_run_demo (priv->auth); + /* break a cyclical dependency by doing this after the window is + * constructed */ + g_idle_add ((GSourceFunc) begin_auth_idle, self); } static void @@ -337,8 +353,11 @@ milk_main_window_init (MilkMainWindow *self) } GtkWidget* -milk_main_window_new () +milk_main_window_get_default () { - return g_object_new (MILK_TYPE_MAIN_WINDOW, - NULL); + if (!default_window) { + default_window = g_object_new (MILK_TYPE_MAIN_WINDOW, NULL); + } + + return default_window; } diff --git a/src/milk-main-window.h b/src/milk-main-window.h index 6af5328..34439ff 100644 --- a/src/milk-main-window.h +++ b/src/milk-main-window.h @@ -64,6 +64,6 @@ struct _MilkMainWindowClass GType milk_main_window_get_type (void); -GtkWidget* milk_main_window_new (void); +GtkWidget* milk_main_window_get_default (void); #endif /* _MILK_MAIN_WINDOW_H */ diff --git a/src/milk-main.c b/src/milk-main.c index 9be1d68..2de05ee 100644 --- a/src/milk-main.c +++ b/src/milk-main.c @@ -37,7 +37,10 @@ main (int argc, char *argv[]) hildon_gtk_init (&argc, &argv); memset (&app, 0, sizeof (app)); - app.main_window = milk_main_window_new (); + app.main_window = milk_main_window_get_default (); + + /* FIXME: cut this */ + g_debug ("in main(), main window: %p", app.main_window); gtk_widget_show_all (app.main_window); -- 1.7.9.5