2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public
13 * License along with this program; if not, write to the
14 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15 * Boston, MA 02110-1301 USA
17 * Authors: Travis Reitter <treitter@gmail.com>
23 #include <glib/gi18n.h>
25 #include <hildon/hildon.h>
27 #include <rtm-glib/rtm-error.h>
28 #include <rtm-glib/rtm-glib.h>
30 #include "milk-auth.h"
31 #include "milk-dialogs.h"
33 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
35 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
36 #define MILK_AUTH_PRIVATE(o) ((MILK_AUTH ((o)))->priv)
38 #define RTM_API_KEY "81f5c6c904aeafbbc914d9845d250ea8"
39 #define RTM_SHARED_SECRET "b08b15419378f913"
41 struct _MilkAuthPrivate
56 static MilkAuth *default_auth = NULL;
60 milk_auth_get_state (MilkAuth *auth)
62 g_return_val_if_fail (auth, MILK_AUTH_STATE_DISCONNECTED);
63 g_return_val_if_fail (MILK_IS_AUTH (auth),
64 MILK_AUTH_STATE_DISCONNECTED);
66 return MILK_AUTH_PRIVATE (auth)->state;
70 milk_auth_get_property (GObject *object,
75 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
80 g_value_set_string (value, priv->api_key);
83 case PROP_SHARED_SECRET:
84 g_value_set_string (value, priv->shared_secret);
88 g_value_set_int (value, priv->state);
92 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
98 milk_auth_set_property (GObject *object,
103 MilkAuthPrivate *priv;
106 auth = MILK_AUTH (object);
107 priv = MILK_AUTH_PRIVATE (auth);
112 priv->api_key = g_value_dup_string (value);
115 case PROP_SHARED_SECRET:
116 priv->shared_secret = g_value_dup_string (value);
120 priv->state = g_value_get_int (value);
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
130 auth_response_cb (GtkWidget *dialog,
135 MilkAuthPrivate *priv;
136 GError *error = NULL;
139 MilkAuthState previous_state;
141 priv = MILK_AUTH_PRIVATE (auth);
143 previous_state = priv->state;
144 auth_token = rtm_glib_auth_get_token (priv->rtm_glib, priv->frob,
147 g_warning ("%s", rtm_error_get_message (error));
148 goto auth_response_cb_error_OUT;
151 if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) {
152 g_warning ("auth_token not valid!\n");
153 goto auth_response_cb_error_OUT;
156 g_warning ("%s", rtm_error_get_message (error));
157 goto auth_response_cb_error_OUT;
159 username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
164 g_warning ("%s", rtm_error_get_message (error));
165 goto auth_response_cb_error_OUT;
168 priv->state = MILK_AUTH_STATE_CONNECTED;
169 goto auth_response_cb_OUT;
171 auth_response_cb_error_OUT:
172 priv->state = MILK_AUTH_STATE_DISCONNECTED;
173 /* FIXME: make it possible to re-try, etc. */
176 auth_response_cb_OUT:
177 if (priv->state != previous_state)
178 g_object_notify (G_OBJECT (auth), "state");
180 gtk_widget_destroy (dialog);
184 milk_auth_get_tasks (MilkAuth *auth,
185 const char *last_sync,
188 MilkAuthPrivate *priv;
191 g_return_val_if_fail (auth, NULL);
192 g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
193 g_return_val_if_fail (
194 milk_auth_get_state (auth) == MILK_AUTH_STATE_CONNECTED,
197 priv = MILK_AUTH_PRIVATE (auth);
199 /* FIXME: cache this */
200 rtm_tasks = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL,
201 (char*) last_sync, error);
207 milk_auth_timeline_create (MilkAuth *auth,
210 MilkAuthPrivate *priv;
212 g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
214 priv = MILK_AUTH_PRIVATE (auth);
216 return rtm_glib_timelines_create (priv->rtm_glib, error);
220 milk_auth_task_add (MilkAuth *auth,
225 MilkAuthPrivate *priv;
227 g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
229 priv = MILK_AUTH_PRIVATE (auth);
231 /* XXX: this uses Smart Add parsing; make this user-settable? */
232 /* FIXME: the cast to char* is actually a bug in the rtm-glib API */
233 return rtm_glib_tasks_add (priv->rtm_glib, timeline, (char*) name, NULL,
238 milk_auth_task_complete (MilkAuth *auth,
243 MilkAuthPrivate *priv;
245 g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
247 priv = MILK_AUTH_PRIVATE (auth);
249 return rtm_glib_tasks_complete (priv->rtm_glib, timeline, task, error);
253 milk_auth_task_delete (MilkAuth *auth,
258 MilkAuthPrivate *priv;
260 g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
262 priv = MILK_AUTH_PRIVATE (auth);
264 return rtm_glib_tasks_delete (priv->rtm_glib, timeline, task, error);
268 milk_auth_log_in (MilkAuth *auth)
270 MilkAuthPrivate *priv;
271 GError *error = NULL;
275 g_return_if_fail (auth);
276 g_return_if_fail (MILK_IS_AUTH (auth));
278 priv = MILK_AUTH_PRIVATE (auth);
280 if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
281 g_print ("Test echo OK!\n");
283 g_print ("Test echo FAIL!\n");
287 g_error ("%s", rtm_error_get_message (error));
291 /* FIXME: relocate this */
295 priv->frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
297 g_error ("%s", rtm_error_get_message (error));
300 g_print ("Frob: %s\n", priv->frob);
302 url = rtm_glib_auth_get_login_url (priv->rtm_glib, priv->frob,
304 g_print ("URL: %s\n", url);
306 dialog = milk_dialogs_auth_prompt (NULL, url);
308 g_signal_connect (dialog, "response", G_CALLBACK (auth_response_cb),
313 milk_auth_constructed (GObject *object)
315 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
317 priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
321 milk_auth_finalize (GObject *object)
323 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
325 g_object_unref (priv->rtm_glib);
327 g_free (priv->api_key);
328 g_free (priv->shared_secret);
333 milk_auth_class_init (MilkAuthClass *klass)
335 GObjectClass *object_class = G_OBJECT_CLASS (klass);
337 g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
339 object_class->get_property = milk_auth_get_property;
340 object_class->set_property = milk_auth_set_property;
341 object_class->constructed = milk_auth_constructed;
342 object_class->finalize = milk_auth_finalize;
344 /* FIXME: make these read-only */
345 g_object_class_install_property
350 "API authentication key",
351 "Milk's API authentication key.",
353 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
354 G_PARAM_STATIC_STRINGS));
356 g_object_class_install_property
362 "Milk's shared secret with the server.",
364 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
365 G_PARAM_STATIC_STRINGS));
367 g_object_class_install_property
372 "Authentication state",
373 "Authentication/connection state with the server.",
374 0, (NUM_MILK_AUTH_STATES-1),
375 MILK_AUTH_STATE_DISCONNECTED,
376 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
377 G_PARAM_STATIC_STRINGS));
381 milk_auth_init (MilkAuth *self)
383 MilkAuthPrivate *priv;
385 self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
386 self, MILK_TYPE_AUTH, MilkAuthPrivate);
390 milk_auth_get_default ()
393 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);