bc5a485216f368d46b4720e50fc4b8ca62c87728
[hildon] / src / hildon-time-selector.c
1 /*
2  * This file is a part of hildon
3  *
4  * Copyright (C) 2005, 2008 Nokia Corporation.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version. or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20
21 /**
22  * SECTION:hildon-time-selector
23  * @short_description: A widget to select the current time.
24  *
25  * HildonTimeSelector is a time widget, equivalent to hildon-calendar, but with a multi-column
26  * approach
27  *
28  */
29
30 #define _GNU_SOURCE     /* needed for GNU nl_langinfo_l */
31 #define __USE_GNU       /* needed for locale */
32
33 #ifdef HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif
36
37 #include <string.h>
38 #include <stdlib.h>
39 #include <libintl.h>
40 #include <time.h>
41 #include <langinfo.h>
42 #include <locale.h>
43 #include <gconf/gconf-client.h>
44
45 #include "hildon-time-selector.h"
46
47 #define HILDON_TIME_SELECTOR_GET_PRIVATE(obj)                           \
48   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), HILDON_TYPE_TIME_SELECTOR, HildonTimeSelectorPrivate))
49
50 G_DEFINE_TYPE (HildonTimeSelector, hildon_time_selector, HILDON_TYPE_TOUCH_SELECTOR)
51
52 #define INIT_YEAR 100
53 #define LAST_YEAR 50    /* since current year */
54
55 #define _(String)  dgettext("hildon-libs", String)
56 #define N_(String) String
57
58
59 /* FIXME: we should get this two props from the clock ui headers */
60 #define CLOCK_GCONF_PATH "/apps/clock"
61 #define CLOCK_GCONF_IS_24H_FORMAT CLOCK_GCONF_PATH  "/time-format"
62
63 enum {
64   COLUMN_STRING,
65   COLUMN_INT,
66   TOTAL_MODEL_COLUMNS
67 };
68
69 enum {
70   COLUMN_HOURS = 0,
71   COLUMN_MINUTES,
72   COLUMN_AMPM,
73   TOTAL_TIME_COLUMNS
74 };
75
76 struct _HildonTimeSelectorPrivate
77 {
78   GtkTreeModel *hours_model;
79   GtkTreeModel *minutes_model;
80   GtkTreeModel *ampm_model;
81
82   gboolean ampm_format;         /* if using am/pm format or 24 h one */
83
84   gboolean pm;                  /* if we are on pm (only useful if ampm_format == TRUE) */
85
86   gint creation_hours;
87   gint creation_minutes;
88 };
89
90 static void hildon_time_selector_finalize (GObject * object);
91
92 /* private functions */
93 static GtkTreeModel *_create_hours_model (HildonTimeSelector * selector);
94 static GtkTreeModel *_create_minutes_model (HildonTimeSelector * selector);
95 static GtkTreeModel *_create_ampm_model (HildonTimeSelector * selector);
96
97 static void _get_real_time (gint * hours, gint * minutes);
98 static void _manage_ampm_selection_cb (HildonTouchSelector * selector,
99                                        gint num_column, gpointer data);
100 static void _check_ampm_format (HildonTimeSelector * selector);
101 static void _set_pm (HildonTimeSelector * selector, gboolean pm);
102
103 static gchar *_custom_print_func (HildonTouchSelector * selector);
104
105 static void
106 hildon_time_selector_class_init (HildonTimeSelectorClass * class)
107 {
108   GObjectClass *gobject_class;
109   GtkObjectClass *object_class;
110   GtkWidgetClass *widget_class;
111   GtkContainerClass *container_class;
112
113   gobject_class = (GObjectClass *) class;
114   object_class = (GtkObjectClass *) class;
115   widget_class = (GtkWidgetClass *) class;
116   container_class = (GtkContainerClass *) class;
117
118   /* GObject */
119   gobject_class->finalize = hildon_time_selector_finalize;
120
121   /* GtkWidget */
122
123   /* GtkContainer */
124
125   /* signals */
126
127   g_type_class_add_private (object_class, sizeof (HildonTimeSelectorPrivate));
128 }
129
130
131 static void
132 hildon_time_selector_init (HildonTimeSelector * selector)
133 {
134   selector->priv = HILDON_TIME_SELECTOR_GET_PRIVATE (selector);
135
136   GTK_WIDGET_SET_FLAGS (GTK_WIDGET (selector), GTK_NO_WINDOW);
137   gtk_widget_set_redraw_on_allocate (GTK_WIDGET (selector), FALSE);
138
139   hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector),
140                                         _custom_print_func);
141
142   _get_real_time (&selector->priv->creation_hours,
143                   &selector->priv->creation_minutes);
144
145   _check_ampm_format (selector);
146
147   selector->priv->hours_model = _create_hours_model (selector);
148   selector->priv->minutes_model = _create_minutes_model (selector);
149
150   hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
151                                             selector->priv->hours_model, TRUE);
152
153   hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
154                                             selector->priv->minutes_model, TRUE);
155
156   if (selector->priv->ampm_format) {
157     selector->priv->ampm_model = _create_ampm_model (selector);
158
159     hildon_touch_selector_append_text_column (HILDON_TOUCH_SELECTOR (selector),
160                                               selector->priv->ampm_model, TRUE);
161
162     g_signal_connect (G_OBJECT (selector),
163                       "changed", G_CALLBACK (_manage_ampm_selection_cb),
164                       NULL);
165   }
166
167   /* By default we should select the current day */
168   hildon_time_selector_set_time (selector,
169                                  selector->priv->creation_hours,
170                                  selector->priv->creation_minutes);
171
172 }
173
174 static void
175 hildon_time_selector_finalize (GObject * object)
176 {
177   HildonTimeSelector *selector = NULL;
178
179   selector = HILDON_TIME_SELECTOR (object);
180
181   /* FIXME: FILL ME !! */
182
183   (*G_OBJECT_CLASS (hildon_time_selector_parent_class)->finalize) (object);
184 }
185
186 /* ------------------------------ PRIVATE METHODS ---------------------------- */
187
188 static gchar *
189 _custom_print_func (HildonTouchSelector * touch_selector)
190 {
191   gchar *result = NULL;
192   struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
193   HildonTimeSelector *selector = NULL;
194   static gchar string[255];
195   guint hours = 0;
196   guint minutes = 0;
197
198   selector = HILDON_TIME_SELECTOR (touch_selector);
199
200   hildon_time_selector_get_time (selector, &hours, &minutes);
201
202   tm.tm_min = minutes;
203   tm.tm_hour = hours;
204
205   if (selector->priv->ampm_format) {
206     strftime (string, 255, _("wdgt_va_12h_time"), &tm);
207   } else {
208     strftime (string, 255, _("wdgt_va_24h_time"), &tm);
209   }
210
211
212   result = g_strdup (string);
213
214   return result;
215 }
216
217 static GtkTreeModel *
218 _create_minutes_model (HildonTimeSelector * selector)
219 {
220   GtkListStore *store_minutes = NULL;
221   gint i = 0;
222   static gchar label[255];
223   struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
224   GtkTreeIter iter;
225
226   store_minutes = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
227   for (i = 0; i <= 59; i++) {
228     tm.tm_min = i;
229     strftime (label, 255, _("wdgt_va_minutes"), &tm);
230
231     gtk_list_store_append (store_minutes, &iter);
232     gtk_list_store_set (store_minutes, &iter,
233                         COLUMN_STRING, label, COLUMN_INT, i, -1);
234   }
235
236   return GTK_TREE_MODEL (store_minutes);
237 }
238
239 static GtkTreeModel *
240 _create_hours_model (HildonTimeSelector * selector)
241 {
242   GtkListStore *store_hours = NULL;
243   gint i = 0;
244   GtkTreeIter iter;
245   struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
246   static gchar label[255];
247   static gint range_12h[12] = {12, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11};
248   static gint range_24h[24] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
249                                12,13,14,15,16,17,18,19,20,21,22,23};
250   gint *range = NULL;
251   gint num_elements = 0;
252   gchar *format_string = NULL;
253
254   if (selector->priv->ampm_format) {
255     range = range_12h;
256     num_elements = 12;
257     format_string = N_("wdgt_va_12h_hours");
258   } else {
259     range = range_24h;
260     num_elements = 24;
261     format_string = N_("wdgt_va_24h_hours");
262   }
263
264   store_hours = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
265   for (i = 0; i < num_elements; i++) {
266     tm.tm_hour = range[i];
267     strftime (label, 255, _(format_string), &tm);
268
269     gtk_list_store_append (store_hours, &iter);
270     gtk_list_store_set (store_hours, &iter,
271                         COLUMN_STRING, label, COLUMN_INT, range[i], -1);
272   }
273
274   return GTK_TREE_MODEL (store_hours);
275 }
276
277 static GtkTreeModel *
278 _create_ampm_model (HildonTimeSelector * selector)
279 {
280   GtkListStore *store_ampm = NULL;
281   GtkTreeIter iter;
282   static gchar label[255];
283   struct tm tm = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
284
285   store_ampm = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
286
287   tm.tm_hour = 0;
288   strftime (label, 255, _("wdgt_va_am_pm"), &tm);
289
290   gtk_list_store_append (store_ampm, &iter);
291   gtk_list_store_set (store_ampm, &iter,
292                       COLUMN_STRING, label,
293                       COLUMN_INT, 0, -1);
294
295   tm.tm_hour = 12;
296   strftime (label, 255, _("wdgt_va_am_pm"), &tm);
297
298   gtk_list_store_append (store_ampm, &iter);
299   gtk_list_store_set (store_ampm, &iter,
300                       COLUMN_STRING, label,
301                       COLUMN_INT, 1, -1);
302
303   return GTK_TREE_MODEL (store_ampm);
304 }
305
306 static void
307 _get_real_time (gint * hours, gint * minutes)
308 {
309   time_t secs;
310   struct tm *tm = NULL;
311
312   secs = time (NULL);
313   tm = localtime (&secs);
314
315   if (hours != NULL) {
316     *hours = tm->tm_hour;
317   }
318
319   if (minutes != NULL) {
320     *minutes = tm->tm_min;
321   }
322 }
323
324 static void
325 _manage_ampm_selection_cb (HildonTouchSelector * touch_selector,
326                            gint num_column, gpointer data)
327 {
328   HildonTimeSelector *selector = NULL;
329   gint pm;
330   GtkTreeIter iter;
331
332   g_return_if_fail (HILDON_IS_TIME_SELECTOR (touch_selector));
333   selector = HILDON_TIME_SELECTOR (touch_selector);
334
335   if (num_column == COLUMN_AMPM) {
336     hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
337                                         COLUMN_AMPM, &iter);
338     gtk_tree_model_get (selector->priv->ampm_model, &iter, COLUMN_INT, &pm, -1);
339
340     selector->priv->pm = pm;
341   }
342 }
343
344 static void
345 _check_ampm_format (HildonTimeSelector * selector)
346 {
347   GConfClient *client = NULL;
348   gboolean value = FALSE;
349   GError *error = NULL;
350
351   client = gconf_client_get_default ();
352   value = gconf_client_get_bool (client, CLOCK_GCONF_IS_24H_FORMAT, &error);
353   if (error != NULL) {
354     g_warning
355       ("Error trying to get gconf variable %s, using 24h format by default",
356        CLOCK_GCONF_IS_24H_FORMAT);
357     g_error_free (error);
358   }
359
360   selector->priv->ampm_format = value;
361   selector->priv->pm = TRUE;
362 }
363
364 static void
365 _set_pm (HildonTimeSelector * selector, gboolean pm)
366 {
367   GtkTreeIter iter;
368
369   selector->priv->pm = pm;
370
371   gtk_tree_model_iter_nth_child (selector->priv->ampm_model, &iter, NULL, pm);
372
373   hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
374                                      COLUMN_AMPM, &iter, FALSE);
375 }
376
377 /* ------------------------------ PUBLIC METHODS ---------------------------- */
378
379 /**
380  * hildon_time_selector_new:
381  *
382  * Creates a new #HildonTimeSelector
383  *
384  * Returns: a new #HildonTimeSelector
385  **/
386 GtkWidget *
387 hildon_time_selector_new ()
388 {
389   return g_object_new (HILDON_TYPE_TIME_SELECTOR, NULL);
390 }
391
392 /*
393  * hours (0-23)
394  * minutes (0-59)
395  */
396 gboolean
397 hildon_time_selector_set_time (HildonTimeSelector * selector,
398                                guint hours, guint minutes)
399 {
400   GtkTreeIter iter;
401   gint hours_item = 0;
402
403   g_return_val_if_fail (hours >= 0 && hours <= 23, FALSE);
404   g_return_val_if_fail (minutes >= 0 && minutes <= 59, FALSE);
405
406   if (selector->priv->ampm_format) {
407     _set_pm (selector, hours >= 12);
408
409     hours_item = hours - selector->priv->pm * 12;
410   } else {
411     hours_item = hours;
412   }
413
414   gtk_tree_model_iter_nth_child (selector->priv->hours_model, &iter, NULL,
415                                  hours_item);
416   hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
417                                      COLUMN_HOURS, &iter, FALSE);
418
419   gtk_tree_model_iter_nth_child (selector->priv->minutes_model, &iter, NULL,
420                                  minutes);
421   hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector),
422                                      COLUMN_MINUTES, &iter, FALSE);
423
424   return TRUE;
425 }
426
427 void
428 hildon_time_selector_get_time (HildonTimeSelector * selector,
429                                guint * hours, guint * minutes)
430 {
431   GtkTreeIter iter;
432
433   if (hours != NULL) {
434     hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
435                                         COLUMN_HOURS, &iter);
436     gtk_tree_model_get (selector->priv->hours_model,
437                         &iter, COLUMN_INT, hours, -1);
438     if (selector->priv->ampm_format) {
439       *hours %= 12;
440       *hours += selector->priv->pm * 12;
441     }
442   }
443
444   if (minutes != NULL) {
445     hildon_touch_selector_get_selected (HILDON_TOUCH_SELECTOR (selector),
446                                         COLUMN_MINUTES, &iter);
447     gtk_tree_model_get (selector->priv->minutes_model,
448                         &iter, COLUMN_INT, minutes, -1);
449   }
450 }