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
54 static MilkAuth *default_auth = NULL;
58 milk_auth_get_property (GObject *object,
63 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
68 g_value_set_string (value, priv->api_key);
71 case PROP_SHARED_SECRET:
72 g_value_set_string (value, priv->shared_secret);
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
82 milk_auth_set_property (GObject *object,
87 MilkAuthPrivate *priv;
90 auth = MILK_AUTH (object);
91 priv = MILK_AUTH_PRIVATE (auth);
96 priv->api_key = g_value_dup_string (value);
99 case PROP_SHARED_SECRET:
100 priv->shared_secret = g_value_dup_string (value);
104 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
110 auth_response_cb (GtkWidget *dialog,
115 MilkAuthPrivate *priv;
117 GError *error = NULL;
121 priv = MILK_AUTH_PRIVATE (auth);
123 auth_token = rtm_glib_auth_get_token (priv->rtm_glib, priv->frob,
126 g_error ("%s", rtm_error_get_message (error));
127 goto auth_response_cb_OUT;
130 if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) {
131 g_error ("auth_token not valid!\n");
132 goto auth_response_cb_error_OUT;
135 g_error ("%s", rtm_error_get_message (error));
136 goto auth_response_cb_OUT;
138 username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
143 g_error ("%s", rtm_error_get_message (error));
144 goto auth_response_cb_OUT;
147 /* FIXME: work this in where appropriate */
149 glist = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL, NULL, &error);
151 g_error ("%s", rtm_error_get_message (error));
153 for (item = glist; item; item = g_list_next (item)) {
154 task = (RtmTask *) item->data;
155 g_print ("%s", rtm_task_to_string (task));
159 glist = rtm_glib_lists_get_list (priv->rtm_glib, &error);
161 g_error ("%s", rtm_error_get_message (error));
162 goto auth_response_cb_OUT;
164 for (item = glist; item; item = g_list_next (item)) {
165 rtm_list = (RtmList *) item->data;
166 if (g_strcmp0 (rtm_list_get_name (rtm_list), "Sent") == 0) {
167 list_id_sent = rtm_list_get_id (rtm_list);
169 g_print ("%s", rtm_list_to_string (rtm_list));
173 timeline = rtm_glib_timelines_create (priv->rtm_glib, &error);
175 g_error ("%s", rtm_error_get_message (error));
177 g_print ("timeline: %s", timeline);
179 task = rtm_glib_tasks_add (priv->rtm_glib, timeline, "test-rtm-glib", NULL, FALSE, &error);
181 g_error ("%s", rtm_error_get_message (error));
182 goto auth_response_cb_OUT;
185 g_print ("First task added! task_id: %s\n", rtm_task_get_id (task));
187 g_print ("First task NOT added!\n");
188 goto auth_response_cb_OUT;
193 auth_response_cb_OUT:
194 gtk_widget_destroy (dialog);
198 milk_auth_log_in (MilkAuth *auth)
200 MilkAuthPrivate *priv;
202 GError *error = NULL;
206 priv = MILK_AUTH_PRIVATE (auth);
208 if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
209 g_print ("Test echo OK!\n");
211 g_print ("Test echo FAIL!\n");
215 g_error ("%s", rtm_error_get_message (error));
219 /* FIXME: relocate this */
223 priv->frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
225 g_error ("%s", rtm_error_get_message (error));
228 g_print ("Frob: %s\n", priv->frob);
230 url = rtm_glib_auth_get_login_url (priv->rtm_glib, priv->frob,
232 g_print ("URL: %s\n", url);
234 dialog = milk_dialogs_auth_prompt (NULL, url);
236 g_signal_connect (dialog, "response", G_CALLBACK (auth_response_cb),
242 milk_auth_constructed (GObject *object)
244 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
246 priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
250 milk_auth_finalize (GObject *object)
252 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
254 g_object_unref (priv->rtm_glib);
256 g_free (priv->api_key);
257 g_free (priv->shared_secret);
262 milk_auth_class_init (MilkAuthClass *klass)
264 GObjectClass *object_class = G_OBJECT_CLASS (klass);
266 g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
268 object_class->get_property = milk_auth_get_property;
269 object_class->set_property = milk_auth_set_property;
270 object_class->constructed = milk_auth_constructed;
271 object_class->finalize = milk_auth_finalize;
273 /* FIXME: make these read-only */
274 g_object_class_install_property
279 "API authentication key",
280 "Milk's API authentication key.",
282 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
283 G_PARAM_STATIC_STRINGS));
285 g_object_class_install_property
291 "Milk's shared secret with the server.",
293 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
294 G_PARAM_STATIC_STRINGS));
298 milk_auth_init (MilkAuth *self)
300 MilkAuthPrivate *priv;
302 self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
303 self, MILK_TYPE_AUTH, MilkAuthPrivate);
307 milk_auth_get_default ()
310 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);