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"
32 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
34 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
35 #define MILK_AUTH_PRIVATE(o) ((MILK_AUTH ((o)))->priv)
37 #define RTM_API_KEY "81f5c6c904aeafbbc914d9845d250ea8"
38 #define RTM_SHARED_SECRET "b08b15419378f913"
40 struct _MilkAuthPrivate
52 static MilkAuth *default_auth = NULL;
56 milk_auth_get_property (GObject *object,
61 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
66 g_value_set_string (value, priv->api_key);
69 case PROP_SHARED_SECRET:
70 g_value_set_string (value, priv->shared_secret);
74 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
80 milk_auth_set_property (GObject *object,
85 MilkAuthPrivate *priv;
88 auth = MILK_AUTH (object);
89 priv = MILK_AUTH_PRIVATE (auth);
94 priv->api_key = g_value_dup_string (value);
97 case PROP_SHARED_SECRET:
98 priv->shared_secret = g_value_dup_string (value);
102 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
108 milk_auth_run_demo (MilkAuth *auth)
110 MilkAuthPrivate *priv;
112 /* FIXME: clean this up */
113 GError *error = NULL;
127 gchar *transaction_id;
128 RtmLocation *location;
129 gchar *list_id_sent = NULL;
130 RtmTimeZone *time_zone;
134 priv = MILK_AUTH_PRIVATE (auth);
136 if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
137 g_print ("Test echo OK!\n");
139 g_print ("Test echo FAIL!\n");
142 g_error ("%s", rtm_error_get_message (error));
146 glist = rtm_glib_time_zones_get_list (priv->rtm_glib, &error);
148 g_error ("%s", rtm_error_get_message (error));
150 for (item = glist; item; item = g_list_next (item)) {
151 time_zone = (RtmTimeZone *) item->data;
152 g_print ("%s", rtm_time_zone_to_string (time_zone));
156 time = rtm_glib_time_parse (priv->rtm_glib, "02/10/2009 10:25", NULL, FALSE, &error);
158 g_error ("%s", rtm_error_get_message (error));
160 g_print ("Time: %s\n", time);
163 time = rtm_glib_time_convert (priv->rtm_glib, "Europe/Madrid", NULL, NULL, &error);
165 g_error ("%s", rtm_error_get_message (error));
167 g_print ("Time: %s\n", time);
171 frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
173 g_error ("%s", rtm_error_get_message (error));
175 g_print ("Frob: %s\n", frob);
177 url = rtm_glib_auth_get_login_url (priv->rtm_glib, frob, "delete");
178 g_print ("URL: %s\n", url);
182 auth_token = rtm_glib_auth_get_token (priv->rtm_glib, frob, &error);
184 g_error ("%s", rtm_error_get_message (error));
187 if (!rtm_glib_auth_check_token (priv->rtm_glib, auth_token, NULL)) {
188 g_error ("auth_token not valid!\n");
189 goto auth_response_cb_error_OUT;
192 g_error ("%s", rtm_error_get_message (error));
194 username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
199 g_error ("%s", rtm_error_get_message (error));
202 /* FIXME: work this in where appropriate */
204 glist = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL, NULL, &error);
206 g_error ("%s", rtm_error_get_message (error));
208 for (item = glist; item; item = g_list_next (item)) {
209 task = (RtmTask *) item->data;
210 g_print ("%s", rtm_task_to_string (task));
214 glist = rtm_glib_lists_get_list (priv->rtm_glib, &error);
216 g_error ("%s", rtm_error_get_message (error));
218 for (item = glist; item; item = g_list_next (item)) {
219 rtm_list = (RtmList *) item->data;
220 if (g_strcmp0 (rtm_list_get_name (rtm_list), "Sent") == 0) {
221 list_id_sent = rtm_list_get_id (rtm_list);
223 g_print ("%s", rtm_list_to_string (rtm_list));
227 timeline = rtm_glib_timelines_create (priv->rtm_glib, &error);
229 g_error ("%s", rtm_error_get_message (error));
231 g_print ("timeline: %s", timeline);
233 task = rtm_glib_tasks_add (priv->rtm_glib, timeline, "test-rtm-glib", NULL, FALSE, &error);
235 g_error ("%s", rtm_error_get_message (error));
238 g_print ("First task added! task_id: %s\n", rtm_task_get_id (task));
240 g_print ("First task NOT added!\n");
248 milk_auth_constructed (GObject *object)
250 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
252 priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
256 milk_auth_finalize (GObject *object)
258 MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
260 g_object_unref (priv->rtm_glib);
262 g_free (priv->api_key);
263 g_free (priv->shared_secret);
267 milk_auth_class_init (MilkAuthClass *klass)
269 GObjectClass *object_class = G_OBJECT_CLASS (klass);
271 g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
273 object_class->get_property = milk_auth_get_property;
274 object_class->set_property = milk_auth_set_property;
275 object_class->constructed = milk_auth_constructed;
276 object_class->finalize = milk_auth_finalize;
278 /* FIXME: make these read-only */
279 g_object_class_install_property
284 "API authentication key",
285 "Milk's API authentication key.",
287 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
288 G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property
296 "Milk's shared secret with the server.",
298 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
299 G_PARAM_STATIC_STRINGS));
303 milk_auth_init (MilkAuth *self)
305 MilkAuthPrivate *priv;
307 self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
308 self, MILK_TYPE_AUTH, MilkAuthPrivate);
312 milk_auth_get_default ()
315 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);