summaryrefslogtreecommitdiff
path: root/src/modules/elementary/prefs/elm_datetime.c
blob: 929cd9004b94f827a5a22cee95d6b26e37232751 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "private.h"

static Elm_Prefs_Item_Type supported_types[] =
{
   ELM_PREFS_TYPE_DATE,
   ELM_PREFS_TYPE_UNKNOWN
};

static void
_item_changed_cb(void *data, const Eo_Event *event)
{
   Elm_Prefs_Item_Changed_Cb prefs_it_changed_cb = data;

   prefs_it_changed_cb(event->object);
}

static Evas_Object *
elm_prefs_datetime_add(const Elm_Prefs_Item_Iface *iface EINA_UNUSED,
                       Evas_Object *prefs,
                       const Elm_Prefs_Item_Type type EINA_UNUSED,
                       const Elm_Prefs_Item_Spec spec,
                       Elm_Prefs_Item_Changed_Cb cb)
{
   Evas_Object *obj = elm_datetime_add(prefs);
   struct tm t;

   memset(&t, 0, sizeof t);

   elm_datetime_field_visible_set(obj, ELM_DATETIME_HOUR, EINA_FALSE);
   elm_datetime_field_visible_set(obj, ELM_DATETIME_MINUTE, EINA_FALSE);
   elm_datetime_field_visible_set(obj, ELM_DATETIME_AMPM, EINA_FALSE);

   efl_event_callback_add
     (obj, ELM_DATETIME_EVENT_CHANGED, _item_changed_cb, cb);

   t.tm_year = spec.d.min.y - 1900;
   t.tm_mon = spec.d.min.m - 1;
   t.tm_mday = spec.d.min.d;

   elm_datetime_value_min_set(obj, &t);

   t.tm_year = spec.d.max.y - 1900;
   t.tm_mon = spec.d.max.m - 1;
   t.tm_mday = spec.d.max.d;

   elm_datetime_value_max_set(obj, &t);

   return obj;
}

static Eina_Bool
elm_prefs_datetime_value_set(Evas_Object *obj,
                             Eina_Value *value)
{
   struct timeval val;
   struct tm *t;
   time_t gmt;

   if (eina_value_type_get(value) != EINA_VALUE_TYPE_TIMEVAL)
     return EINA_FALSE;

   eina_value_get(value, &val);
   gmt = val.tv_sec;
   t = gmtime(&gmt);

   if (elm_datetime_value_set(obj, t)) return EINA_TRUE;

   return EINA_FALSE;
}

static Eina_Bool
elm_prefs_datetime_value_get(Evas_Object *obj,
                             Eina_Value *value)
{
   struct timeval val;
   struct tm t;

   memset(&val, 0, sizeof val);

   if (!elm_datetime_value_get(obj, &t)) return EINA_FALSE;

   val.tv_sec = mktime(&t);

   if (!eina_value_setup(value, EINA_VALUE_TYPE_TIMEVAL)) return EINA_FALSE;
   if (!eina_value_set(value, val)) return EINA_FALSE;

   return EINA_TRUE;
}

PREFS_ITEM_WIDGET_ADD(datetime,
                      supported_types,
                      elm_prefs_datetime_value_set,
                      elm_prefs_datetime_value_get,
                      NULL,
                      NULL,
                      NULL,
                      NULL,
                      NULL,
                      NULL);