From 4a6af28d4a7476ca3adebb924efff963844bb25e Mon Sep 17 00:00:00 2001 From: Daniele Ricci Date: Fri, 12 Nov 2010 00:36:14 +0100 Subject: [PATCH] Begin OAuth window Signed-off-by: Daniele Ricci --- Makefile.am | 2 +- autogen.sh | 2 +- configure.ac | 16 +++++- src/Makefile.am | 6 ++- src/auth.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++ src/auth.h | 28 ++++++++++ src/globals.h | 3 ++ src/main.c | 50 +++++++----------- src/timeline.c | 2 +- src/timeline.h | 2 +- 10 files changed, 209 insertions(+), 39 deletions(-) create mode 100644 src/auth.c create mode 100644 src/auth.h diff --git a/Makefile.am b/Makefile.am index 3902694..6706414 100644 --- a/Makefile.am +++ b/Makefile.am @@ -21,6 +21,6 @@ MAINTAINERCLEANFILES = \ po/en@boldquot.header \ po/en@quot.header \ po/insert-header.sin \ - po/microtwitter.pot \ + po/micromoko.pot \ po/quot.sed \ po/remove-potcdate.sin diff --git a/autogen.sh b/autogen.sh index 47ffb16..b40bdf1 100755 --- a/autogen.sh +++ b/autogen.sh @@ -7,7 +7,7 @@ ORIGDIR=`pwd` cd $srcdir autoreconf -v --install || exit 1 -#intltoolize -c --automake --force || exit 1 +intltoolize -c --automake --force || exit 1 cd $ORIGDIR || exit $? $srcdir/configure --enable-debug "$@" diff --git a/configure.ac b/configure.ac index 35c1c25..d39749b 100644 --- a/configure.ac +++ b/configure.ac @@ -10,8 +10,20 @@ AC_GNU_SOURCE AC_PROG_CC AC_PROG_LIBTOOL -AM_GNU_GETTEXT(external) -AM_GNU_GETTEXT_VERSION([0.12.1]) +dnl Intl for gettext +IT_PROG_INTLTOOL([0.35.0]) + +if test "x$localedir" == x +then + localedir='${datadir}/locale' + AC_SUBST(localedir) + AM_GLIB_DEFINE_LOCALEDIR([localedir]) +fi + +AM_GLIB_GNU_GETTEXT +GETTEXT_PACKAGE="micromoko" +AC_SUBST(GETTEXT_PACKAGE) +AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, ["$GETTEXT_PACKAGE"], [Name of the gettext message domain]) dnl Dependencies ELEMENTARY_REQUIRED=0.0.0 diff --git a/src/Makefile.am b/src/Makefile.am index eaecf81..89a5ed2 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -15,7 +15,7 @@ AM_LDFLAGS = \ @ELEMENTARY_LIBS@ \ @GLIB_LIBS@ \ @MOKOSUITE_LIBS@ \ - -lreadline -Ltwitter -ltwitter + -Ltwitter -ltwitter bin_PROGRAMS = micromoko @@ -24,4 +24,6 @@ micromoko_SOURCES = \ globals.h \ main.c \ timeline.c \ - timeline.h + timeline.h \ + auth.c \ + auth.h diff --git a/src/auth.c b/src/auth.c new file mode 100644 index 0000000..e88d366 --- /dev/null +++ b/src/auth.c @@ -0,0 +1,137 @@ +/* + * Micromoko + * Authorization window + * Copyright (C) 2009-2010 Daniele Ricci + * + * 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 3 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +#include "globals.h" +#include "twitter/twitter.h" +#include "auth.h" + +#include + +static MokoWin* win = NULL; +static Evas_Object* btn_auth = NULL; +static Evas_Object* msg_status = NULL; +static Evas_Object* pin_entry = NULL; + +static void _delete(void* mokowin, Evas_Object* obj, void* event_info) +{ + // TODO che famo? + mokowin_destroy((MokoWin *)mokowin); +} + +static void update_status(const char* status) +{ + elm_label_label_set(msg_status, status); +} + +static void _access_token(twitter_session* session, void* userdata) +{ + // TODO now what? + remote_config_service_set_string(home_config, "auth", "access_token", session->access.key); + remote_config_service_set_string(home_config, "auth", "access_token_secret", session->access.secret); +} + +static bool _pin_response(void* userdata) +{ + // remove auth button + evas_object_del(btn_auth); + btn_auth = NULL; + + pin_entry = elm_entry_add(win->win); + evas_object_size_hint_weight_set(pin_entry, EVAS_HINT_EXPAND, 0.0); + evas_object_size_hint_align_set(pin_entry, EVAS_HINT_FILL, 0.5); + + elm_box_pack_before(win->win, pin_entry, msg_status); + evas_object_show(pin_entry); + + update_status(_("Paste PIN code above.")); + return FALSE; +} + +static void _request_token(twitter_session* session, void* userdata) +{ + update_status(_("Starting browser...")); + + // access token for pin :) + char* cmd = g_strdup_printf("xdg-open \"%s%s?oauth_token=%s\"", + TWITTER_BASE_URI, TWITTER_AUTHORIZE_FUNC, session->request.key); + system(cmd); + g_free(cmd); + + ecore_timer_add(5, _pin_response, NULL); + //twitter_session_oauth_access_token(session, pin, _access_token, userdata); + // TODO swich UI to pin request :) +} + +static void _authorize(void* data, Evas_Object* obj, void* event_info) +{ + // start authentication + elm_object_disabled_set(obj, TRUE); + update_status(_("Requesting token...")); + twitter_session_oauth_request_token(global_session, _request_token, NULL); +} + +void auth_win(void) +{ + if (!win) { + win = mokowin_new("micromoko_auth", TRUE); + if (win == NULL) { + EINA_LOG_ERR("cannot create authorization window."); + return; + } + + win->delete_callback = _delete; + + elm_win_title_set(win->win, _("Authorization")); + + mokowin_create_vbox(win, FALSE); + mokowin_set_title(win, _("Authorization")); + + //mokowin_menu_enable(win); + //mokowin_menu_set(win, make_menu()); + + // big auth button :D + btn_auth = elm_button_add(win->win); + elm_button_label_set(btn_auth, _("Authorize to Twitter")); + evas_object_size_hint_weight_set(btn_auth, EVAS_HINT_EXPAND, 0.0); + evas_object_size_hint_align_set(btn_auth, EVAS_HINT_FILL, 0.5); + evas_object_smart_callback_add(btn_auth, "clicked", _authorize, NULL); + + mokowin_pack_end(win, btn_auth, FALSE); + evas_object_show(btn_auth); + + // label/status + msg_status = elm_label_add(win->win); + elm_label_label_set(msg_status, _("Click the button to authorize.")); + evas_object_size_hint_weight_set(msg_status, EVAS_HINT_EXPAND, 0.0); + evas_object_size_hint_align_set(msg_status, 0.5, 0.5); + + mokowin_pack_end(win, msg_status, FALSE); + evas_object_show(msg_status); + + // TEST + evas_object_resize(win->win, 480, 640); + } + + mokowin_activate(win); +} diff --git a/src/auth.h b/src/auth.h new file mode 100644 index 0000000..44ece36 --- /dev/null +++ b/src/auth.h @@ -0,0 +1,28 @@ +/* + * Micromoko + * Authorization window + * Copyright (C) 2009-2010 Daniele Ricci + * + * 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 3 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 Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __AUTH_H +#define __AUTH_H + + +void auth_win(void); + + +#endif /* __AUTH_H */ diff --git a/src/globals.h b/src/globals.h index 52e9a92..6ff135f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -31,6 +31,8 @@ #include #include +#include "twitter/twitter.h" + // default log domain #undef EINA_LOG_DOMAIN_DEFAULT #define EINA_LOG_DOMAIN_DEFAULT _log_dom @@ -45,6 +47,7 @@ extern int _log_dom; #define MICROMOKO_DATADIR DATADIR "/micromoko" extern RemoteConfigService* home_config; +extern twitter_session* global_session; void get_screen_size(int *w, int *h); diff --git a/src/main.c b/src/main.c index 8b0716b..b8679e2 100644 --- a/src/main.c +++ b/src/main.c @@ -25,12 +25,9 @@ #include #include -// TEST -#include -#include - #include "globals.h" #include "twitter/twitter.h" +#include "auth.h" #define MICROMOKO_NAME "org.mokosuite.micromoko" #define MICROMOKO_CONFIG_PATH MOKOSUITE_DBUS_PATH "/Micromoko/Config" @@ -40,25 +37,8 @@ int _log_dom = -1; RemoteConfigService* home_config = NULL; +twitter_session* global_session = NULL; -static void _access_token(twitter_session* session, void* userdata) -{ - // TODO now what? - remote_config_service_set_string(home_config, "auth", "access_token", session->access.key); - remote_config_service_set_string(home_config, "auth", "access_token_secret", session->access.secret); -} - -static void _request_token(twitter_session* session, void* userdata) -{ - // TODO access token for pin :) - char* cmd = g_strdup_printf("xdg-open \"%s%s?oauth_token=%s\"", - TWITTER_BASE_URI, TWITTER_AUTHORIZE_FUNC, session->request.key); - system(cmd); - g_free(cmd); - - char* pin = readline("PIN: "); - twitter_session_oauth_access_token(session, pin, _access_token, userdata); -} int main(int argc, char* argv[]) { @@ -98,25 +78,33 @@ int main(int argc, char* argv[]) elm_theme_extension_add(NULL, MICROMOKO_DATADIR "/theme.edj"); // TODO additional themes - // TODO public timeline or last open window? - - // TEST twitter test :) + // twitter session + // TODO read consumer token from configuration? oauth_token consumer = { "dY49wvIY7386ET15vCRVQ", "D8wKnvbTQqxJmxBC0JyHVY6LpHdpTBbJwyISlrUg8" }; - twitter_session* sess = twitter_session_new(&consumer); + global_session = twitter_session_new(&consumer); + // if not yet authenticated, open twitter authorization window oauth_token access_token = {NULL, NULL}; remote_config_service_get_string(home_config, "auth", "access_token", &access_token.key); remote_config_service_get_string(home_config, "auth", "access_token_secret", &access_token.secret); - if (access_token.key != NULL) - twitter_session_set_access_token(sess, &access_token); - else - twitter_session_oauth_request_token(sess, _request_token, NULL); + // access token is present, go to last open window + if (access_token.key != NULL && strlen(access_token.key) && access_token.secret == NULL && strlen(access_token.secret)) { + // TODO last open window + EINA_LOG_DBG("open last window"); + } - // TEST twitter test + // no access token, authorization needed + else { + // auth window + EINA_LOG_DBG("open auth window"); + auth_win(); + } + + // TODO public timeline or last open window? elm_run(); elm_shutdown(); diff --git a/src/timeline.c b/src/timeline.c index 5e56c4c..372ee02 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -1,5 +1,5 @@ /* - * MicroTwitter + * Micromoko * Generic timeline window * Copyright (C) 2009-2010 Daniele Ricci * diff --git a/src/timeline.h b/src/timeline.h index 5e56c4c..372ee02 100644 --- a/src/timeline.h +++ b/src/timeline.h @@ -1,5 +1,5 @@ /* - * MicroTwitter + * Micromoko * Generic timeline window * Copyright (C) 2009-2010 Daniele Ricci *