Support marking tasks as complete
[milk] / src / milk-auth.c
1 /*
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.
6  *
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.
11  *
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
16  *
17  * Authors: Travis Reitter <treitter@gmail.com>
18  */
19
20 #include <config.h>
21
22 #include <glib.h>
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 #include <hildon/hildon.h>
26
27 #include <rtm-glib/rtm-error.h>
28 #include <rtm-glib/rtm-glib.h>
29
30 #include "milk-auth.h"
31 #include "milk-dialogs.h"
32
33 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
34
35 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
36 #define MILK_AUTH_PRIVATE(o) ((MILK_AUTH ((o)))->priv)
37
38 #define RTM_API_KEY "81f5c6c904aeafbbc914d9845d250ea8"
39 #define RTM_SHARED_SECRET "b08b15419378f913"
40
41 struct _MilkAuthPrivate
42 {
43         RtmGlib *rtm_glib;
44         char *api_key;
45         char *shared_secret;
46         char *frob;
47         MilkAuthState state;
48 };
49
50 enum {
51         PROP_API_KEY = 1,
52         PROP_SHARED_SECRET,
53         PROP_STATE,
54 };
55
56 static MilkAuth *default_auth = NULL;
57
58
59 MilkAuthState
60 milk_auth_get_state (MilkAuth *auth)
61 {
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);
65
66         return MILK_AUTH_PRIVATE (auth)->state;
67 }
68
69 static void
70 milk_auth_get_property (GObject    *object,
71                         guint       property_id,
72                         GValue     *value,
73                         GParamSpec *pspec)
74 {
75         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
76
77         switch (property_id)
78         {
79                 case PROP_API_KEY:
80                         g_value_set_string (value, priv->api_key);
81                 break;
82
83                 case PROP_SHARED_SECRET:
84                         g_value_set_string (value, priv->shared_secret);
85                 break;
86
87                 case PROP_STATE:
88                         g_value_set_int (value, priv->state);
89                 break;
90
91                 default:
92                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
93                                         pspec);
94         }
95 }
96
97 static void
98 milk_auth_set_property (GObject      *object,
99                         guint         property_id,
100                         const GValue *value,
101                         GParamSpec   *pspec)
102 {
103         MilkAuthPrivate *priv;
104         MilkAuth *auth;
105
106         auth = MILK_AUTH (object);
107         priv = MILK_AUTH_PRIVATE (auth);
108
109         switch (property_id)
110         {
111                 case PROP_API_KEY:
112                         priv->api_key = g_value_dup_string (value);
113                 break;
114
115                 case PROP_SHARED_SECRET:
116                         priv->shared_secret = g_value_dup_string (value);
117                 break;
118
119                 case PROP_STATE:
120                         priv->state = g_value_get_int (value);
121                 break;
122
123                 default:
124                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
125                                         pspec);
126         }
127 }
128
129 static void
130 auth_response_cb (GtkWidget *dialog,
131                   int        response,
132                   MilkAuth  *auth)
133
134 {
135         MilkAuthPrivate *priv;
136         GError *error = NULL;
137         gchar *auth_token;
138         gchar *username;
139         MilkAuthState previous_state;
140
141         priv = MILK_AUTH_PRIVATE (auth);
142
143         previous_state = priv->state;
144         auth_token = rtm_glib_auth_get_token (priv->rtm_glib, priv->frob,
145                         &error);
146         if (error != NULL) {
147                 g_warning ("%s", rtm_error_get_message (error));
148                 goto auth_response_cb_error_OUT;
149         }
150
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;
154         }
155         if (error != NULL) {
156                 g_warning ("%s", rtm_error_get_message (error));
157                 goto auth_response_cb_error_OUT;
158         }
159         username = rtm_glib_test_login (priv->rtm_glib, auth_token, &error);
160
161         g_free (auth_token);
162
163         if (error != NULL) {
164                 g_warning ("%s", rtm_error_get_message (error));
165                 goto auth_response_cb_error_OUT;
166         }
167
168         priv->state = MILK_AUTH_STATE_CONNECTED;
169         goto auth_response_cb_OUT;
170
171 auth_response_cb_error_OUT:
172         priv->state = MILK_AUTH_STATE_DISCONNECTED;
173         /* FIXME: make it possible to re-try, etc. */
174         gtk_main_quit ();
175
176 auth_response_cb_OUT:
177         if (priv->state != previous_state)
178                 g_object_notify (G_OBJECT (auth), "state");
179
180         gtk_widget_destroy (dialog);
181 }
182
183 GList *
184 milk_auth_get_tasks (MilkAuth    *auth,
185                      const char  *last_sync,
186                      GError     **error)
187 {
188         MilkAuthPrivate *priv;
189         GList *rtm_tasks;
190
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,
195                         NULL);
196
197         priv = MILK_AUTH_PRIVATE (auth);
198
199         /* FIXME: cache this */
200         rtm_tasks = rtm_glib_tasks_get_list (priv->rtm_glib, NULL, NULL,
201                         (char*) last_sync, error);
202
203         return rtm_tasks;
204 }
205
206 char*
207 milk_auth_timeline_create (MilkAuth  *auth,
208                            GError   **error)
209 {
210         MilkAuthPrivate *priv;
211
212         g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
213
214         priv = MILK_AUTH_PRIVATE (auth);
215
216         return rtm_glib_timelines_create (priv->rtm_glib, error);
217 }
218
219 char*
220 milk_auth_task_complete (MilkAuth  *auth,
221                          char      *timeline,
222                          RtmTask   *task,
223                          GError   **error)
224 {
225         MilkAuthPrivate *priv;
226
227         g_return_val_if_fail (MILK_IS_AUTH (auth), NULL);
228
229         priv = MILK_AUTH_PRIVATE (auth);
230
231         return rtm_glib_tasks_complete (priv->rtm_glib, timeline, task, error);
232 }
233
234 void
235 milk_auth_log_in (MilkAuth *auth)
236 {
237         MilkAuthPrivate *priv;
238         GError *error = NULL;
239         gchar *url;
240         GtkDialog *dialog;
241
242         g_return_if_fail (auth);
243         g_return_if_fail (MILK_IS_AUTH (auth));
244
245         priv = MILK_AUTH_PRIVATE (auth);
246
247         if (rtm_glib_test_echo (priv->rtm_glib, &error)) {
248                 g_print ("Test echo OK!\n");
249         } else {
250                 g_print ("Test echo FAIL!\n");
251                 return;
252         }
253         if (error != NULL) {
254                 g_error ("%s", rtm_error_get_message (error));
255                 return;
256         }
257
258         /* FIXME: relocate this */
259         if (priv->frob)
260                 g_free (priv->frob);
261
262         priv->frob = rtm_glib_auth_get_frob (priv->rtm_glib, &error);
263         if (error != NULL) {
264                 g_error ("%s", rtm_error_get_message (error));
265                 return;
266         }
267         g_print ("Frob: %s\n", priv->frob);
268
269         url = rtm_glib_auth_get_login_url (priv->rtm_glib, priv->frob,
270                         "delete");
271         g_print ("URL: %s\n", url);
272
273         dialog = milk_dialogs_auth_prompt (NULL, url);
274
275         g_signal_connect (dialog, "response", G_CALLBACK (auth_response_cb),
276                         auth);
277 }
278
279 static void
280 milk_auth_constructed (GObject *object)
281 {
282         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
283
284         priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
285 }
286
287 static void
288 milk_auth_finalize (GObject *object)
289 {
290         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
291
292         g_object_unref (priv->rtm_glib);
293
294         g_free (priv->api_key);
295         g_free (priv->shared_secret);
296         g_free (priv->frob);
297 }
298
299 static void
300 milk_auth_class_init (MilkAuthClass *klass)
301 {
302         GObjectClass *object_class = G_OBJECT_CLASS (klass);
303
304         g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
305
306         object_class->get_property = milk_auth_get_property;
307         object_class->set_property = milk_auth_set_property;
308         object_class->constructed = milk_auth_constructed;
309         object_class->finalize = milk_auth_finalize;
310
311         /* FIXME: make these read-only */
312         g_object_class_install_property
313                 (object_class,
314                  PROP_API_KEY,
315                  g_param_spec_string
316                          ("api-key",
317                           "API authentication key",
318                           "Milk's API authentication key.",
319                           RTM_API_KEY,
320                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
321                           G_PARAM_STATIC_STRINGS));
322
323         g_object_class_install_property
324                 (object_class,
325                  PROP_SHARED_SECRET,
326                  g_param_spec_string
327                          ("shared-secret",
328                           "Shared secret",
329                           "Milk's shared secret with the server.",
330                           RTM_SHARED_SECRET,
331                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
332                           G_PARAM_STATIC_STRINGS));
333
334         g_object_class_install_property
335                 (object_class,
336                  PROP_STATE,
337                  g_param_spec_int
338                          ("state",
339                           "Authentication state",
340                           "Authentication/connection state with the server.",
341                           0, (NUM_MILK_AUTH_STATES-1),
342                           MILK_AUTH_STATE_DISCONNECTED,
343                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
344                           G_PARAM_STATIC_STRINGS));
345 }
346
347 static void
348 milk_auth_init (MilkAuth *self)
349 {
350         MilkAuthPrivate *priv;
351
352         self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
353                         self, MILK_TYPE_AUTH, MilkAuthPrivate);
354 }
355
356 MilkAuth*
357 milk_auth_get_default ()
358 {
359         if (!default_auth)
360                 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);
361
362         return default_auth;
363 }