80c72d82cae633c9f30740fb6b79a40f22b3387e
[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-glib.h>
28
29 #include "milk-auth.h"
30
31 G_DEFINE_TYPE (MilkAuth, milk_auth, G_TYPE_OBJECT);
32
33 /* less expensive than G_TYPE_INSTANCE_GET_PRIVATE */
34 #define MILK_AUTH_PRIVATE(o) ((MILK_AUTH ((o)))->priv)
35
36 #define RTM_API_KEY "81f5c6c904aeafbbc914d9845d250ea8"
37 #define RTM_SHARED_SECRET "b08b15419378f913"
38
39 struct _MilkAuthPrivate
40 {
41         RtmGlib *rtm_glib;
42         char *api_key;
43         char *shared_secret;
44 };
45
46 enum {
47         PROP_API_KEY = 1,
48         PROP_SHARED_SECRET,
49 };
50
51 static MilkAuth *default_auth = NULL;
52
53
54 static void
55 milk_auth_get_property (GObject    *object,
56                         guint       property_id,
57                         GValue     *value,
58                         GParamSpec *pspec)
59 {
60         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
61
62         switch (property_id)
63         {
64                 case PROP_API_KEY:
65                         g_value_set_string (value, priv->api_key);
66                 break;
67
68                 case PROP_SHARED_SECRET:
69                         g_value_set_string (value, priv->shared_secret);
70                 break;
71
72                 default:
73                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
74                                         pspec);
75         }
76 }
77
78 static void
79 milk_auth_set_property (GObject      *object,
80                         guint         property_id,
81                         const GValue *value,
82                         GParamSpec   *pspec)
83 {
84         MilkAuthPrivate *priv;
85         MilkAuth *auth;
86
87         auth = MILK_AUTH (object);
88         priv = MILK_AUTH_PRIVATE (auth);
89
90         switch (property_id)
91         {
92                 case PROP_API_KEY:
93                         priv->api_key = g_value_dup_string (value);
94                 break;
95
96                 case PROP_SHARED_SECRET:
97                         priv->shared_secret = g_value_dup_string (value);
98                 break;
99
100                 default:
101                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id,
102                                         pspec);
103         }
104 }
105
106 static void
107 milk_auth_constructed (GObject *object)
108 {
109         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
110
111         priv->rtm_glib = rtm_glib_new (priv->api_key, priv->shared_secret);
112 }
113
114 static void
115 milk_auth_finalize (GObject *object)
116 {
117         MilkAuthPrivate *priv = MILK_AUTH_PRIVATE (object);
118
119         g_object_unref (priv->rtm_glib);
120
121         g_free (priv->api_key);
122         g_free (priv->shared_secret);
123 }
124
125 static void
126 milk_auth_class_init (MilkAuthClass *klass)
127 {
128         GObjectClass *object_class = G_OBJECT_CLASS (klass);
129
130         g_type_class_add_private (klass, sizeof (MilkAuthPrivate));
131
132         object_class->get_property = milk_auth_get_property;
133         object_class->set_property = milk_auth_set_property;
134         object_class->constructed = milk_auth_constructed;
135         object_class->finalize = milk_auth_finalize;
136
137         /* FIXME: make these read-only */
138         g_object_class_install_property
139                 (object_class,
140                  PROP_API_KEY,
141                  g_param_spec_string
142                          ("api-key",
143                           "API authentication key",
144                           "Milk's API authentication key.",
145                           RTM_API_KEY,
146                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
147                           G_PARAM_STATIC_STRINGS));
148
149         g_object_class_install_property
150                 (object_class,
151                  PROP_SHARED_SECRET,
152                  g_param_spec_string
153                          ("shared-secret",
154                           "Shared secret",
155                           "Milk's shared secret with the server.",
156                           RTM_SHARED_SECRET,
157                           G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
158                           G_PARAM_STATIC_STRINGS));
159 }
160
161 static void
162 milk_auth_init (MilkAuth *self)
163 {
164         MilkAuthPrivate *priv;
165
166         self->priv = priv = G_TYPE_INSTANCE_GET_PRIVATE (
167                         self, MILK_TYPE_AUTH, MilkAuthPrivate);
168 }
169
170 MilkAuth*
171 milk_auth_get_default ()
172 {
173         if (!default_auth)
174                 default_auth = g_object_new (MILK_TYPE_AUTH, NULL);
175
176         return default_auth;
177 }