1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Nokia Corporation nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <modest-datetime-formatter.h>
32 #ifdef MODEST_TOOLKIT_HILDON2
33 #include <gconf/gconf-client.h>
34 #include <gtk/gtkmarshal.h>
36 #include <glib/gi18n.h>
37 #include "modest-text-utils.h"
42 DATETIME_FORMAT_LOCALE,
45 #define HILDON2_GCONF_FORMAT_DIR "/apps/clock"
46 #define HILDON2_GCONF_FORMAT_KEY HILDON2_GCONF_FORMAT_DIR "/time-format"
48 /* 'private'/'protected' functions */
49 static void modest_datetime_formatter_class_init (ModestDatetimeFormatterClass *klass);
50 static void modest_datetime_formatter_finalize (GObject *obj);
51 static void modest_datetime_formatter_instance_init (ModestDatetimeFormatter *obj);
53 typedef struct _ModestDatetimeFormatterPrivate ModestDatetimeFormatterPrivate;
54 struct _ModestDatetimeFormatterPrivate {
55 DatetimeFormat current_format;
56 #ifdef MODEST_TOOLKIT_HILDON2
61 #define MODEST_DATETIME_FORMATTER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
62 MODEST_TYPE_DATETIME_FORMATTER, \
63 ModestDatetimeFormatterPrivate))
66 FORMAT_CHANGED_SIGNAL,
71 static GObjectClass *parent_class = NULL;
73 static guint signals[LAST_SIGNAL] = {0};
76 modest_datetime_formatter_get_type (void)
78 static GType my_type = 0;
81 static const GTypeInfo my_info = {
82 sizeof(ModestDatetimeFormatterClass),
84 NULL, /* base finalize */
85 (GClassInitFunc) modest_datetime_formatter_class_init,
86 NULL, /* class finalize */
87 NULL, /* class data */
88 sizeof(ModestDatetimeFormatter),
90 (GInstanceInitFunc) modest_datetime_formatter_instance_init,
94 my_type = g_type_register_static (G_TYPE_OBJECT,
95 "ModestDatetimeFormatter",
102 modest_datetime_formatter_class_init (ModestDatetimeFormatterClass *klass)
104 GObjectClass *gobject_class;
105 gobject_class = (GObjectClass *) klass;
107 parent_class = g_type_class_peek_parent (klass);
108 gobject_class->finalize = modest_datetime_formatter_finalize;
110 g_type_class_add_private (gobject_class,
111 sizeof(ModestDatetimeFormatterPrivate));
113 signals[FORMAT_CHANGED_SIGNAL] =
114 g_signal_new ("format_changed",
115 G_TYPE_FROM_CLASS (gobject_class),
117 G_STRUCT_OFFSET (ModestDatetimeFormatterClass, format_changed),
119 g_cclosure_marshal_VOID__VOID,
123 #ifdef MODEST_TOOLKIT_HILDON2
125 update_format (ModestDatetimeFormatter *obj)
129 gboolean gconf_value;
130 ModestDatetimeFormatterPrivate *priv;
132 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (obj);
134 gconf = gconf_client_get_default ();
135 gconf_value = gconf_client_get_bool (gconf, HILDON2_GCONF_FORMAT_KEY,
139 g_warning ("Error reading time format in gconf %s", err->message);
142 priv->current_format = gconf_value?DATETIME_FORMAT_24H:DATETIME_FORMAT_12H;
147 clock_format_changed (GConfClient *gconf,
152 ModestDatetimeFormatter *self = (ModestDatetimeFormatter *) userdata;
154 update_format (self);
155 g_signal_emit (G_OBJECT (self), signals[FORMAT_CHANGED_SIGNAL], 0);
160 init_format (ModestDatetimeFormatter *obj)
162 ModestDatetimeFormatterPrivate *priv;
164 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (obj);
166 priv->current_format = DATETIME_FORMAT_LOCALE;
168 #ifdef MODEST_TOOLKIT_HILDON2
172 gconf = gconf_client_get_default ();
173 gconf_client_add_dir (gconf, HILDON2_GCONF_FORMAT_DIR,
174 GCONF_CLIENT_PRELOAD_ONELEVEL,
176 priv->gconf_handler = gconf_client_notify_add (gconf, HILDON2_GCONF_FORMAT_KEY,
177 clock_format_changed, (gpointer) obj,
181 g_warning ("Error listening to time format in gconf %s", err->message);
189 modest_datetime_formatter_instance_init (ModestDatetimeFormatter *obj)
191 ModestDatetimeFormatterPrivate *priv;
193 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (obj);
199 modest_datetime_formatter_finalize (GObject *obj)
201 #ifdef MODEST_TOOLKIT_HILDON2
202 ModestDatetimeFormatterPrivate *priv;
205 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (obj);
206 gconf = gconf_client_get_default ();
207 gconf_client_notify_remove (gconf,
208 priv->gconf_handler);
209 priv->gconf_handler = 0;
210 gconf_client_remove_dir (gconf, HILDON2_GCONF_FORMAT_DIR,
213 G_OBJECT_CLASS (parent_class)->finalize (obj);
216 ModestDatetimeFormatter*
217 modest_datetime_formatter_new (void)
219 return g_object_new (MODEST_TYPE_DATETIME_FORMATTER, NULL);
223 modest_datetime_formatter_format_date (ModestDatetimeFormatter *self,
226 #define DATE_BUF_SIZE 64
228 static gchar date_buf[DATE_BUF_SIZE];
229 ModestDatetimeFormatterPrivate *priv;
230 const gchar *format_string = NULL;
232 g_return_val_if_fail (MODEST_IS_DATETIME_FORMATTER (self), NULL);
233 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (self);
235 switch (priv->current_format) {
236 case DATETIME_FORMAT_12H:
237 case DATETIME_FORMAT_24H:
238 format_string = _HL("wdgt_va_date");
240 case DATETIME_FORMAT_LOCALE:
241 format_string = "%x";
244 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, format_string, date);
250 modest_datetime_formatter_format_time (ModestDatetimeFormatter *self,
253 #define DATE_BUF_SIZE 64
255 static gchar date_buf[DATE_BUF_SIZE];
256 ModestDatetimeFormatterPrivate *priv;
257 const gchar *format_string = NULL;
260 g_return_val_if_fail (MODEST_IS_DATETIME_FORMATTER (self), NULL);
261 priv = MODEST_DATETIME_FORMATTER_GET_PRIVATE (self);
263 is_pm = (date / (60 * 60 * 12)) % 2;
265 switch (priv->current_format) {
266 case DATETIME_FORMAT_12H:
267 format_string = is_pm?_HL("wdgt_va_12h_time_pm"):_HL("wdgt_va_12h_time_am");
269 case DATETIME_FORMAT_24H:
270 format_string = _HL("wdgt_va_24h_time");
272 case DATETIME_FORMAT_LOCALE:
273 format_string = "%X";
276 modest_text_utils_strftime (date_buf, DATE_BUF_SIZE, format_string, date);
282 modest_datetime_formatter_display_long_datetime (ModestDatetimeFormatter *self,
286 #define DATE_BUF_DOUBLE_SIZE 128
288 static gchar date_buf[DATE_BUF_DOUBLE_SIZE];
290 snprintf (date_buf, DATE_BUF_DOUBLE_SIZE,
291 "%s %s", modest_datetime_formatter_format_date (self, date),
292 modest_datetime_formatter_format_time (self, date));
298 modest_datetime_formatter_display_datetime (ModestDatetimeFormatter *self,
302 int day = time (NULL) / (24*60*60);
303 int date_day = date / (24*60*60);
306 return modest_datetime_formatter_format_time (self, date);
308 return modest_datetime_formatter_format_date (self, date);