1 /*
2  * e-interval-chooser.c
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11  * for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #include "evolution-config.h"
19 
20 #include <glib/gi18n-lib.h>
21 
22 #include "e-util-enums.h"
23 #include "e-misc-utils.h"
24 
25 #include "e-interval-chooser.h"
26 
27 #define E_INTERVAL_CHOOSER_GET_PRIVATE(obj) \
28 	(G_TYPE_INSTANCE_GET_PRIVATE \
29 	((obj), E_TYPE_INTERVAL_CHOOSER, EIntervalChooserPrivate))
30 
31 #define MINUTES_PER_HOUR	(60)
32 #define MINUTES_PER_DAY		(MINUTES_PER_HOUR * 24)
33 
34 struct _EIntervalChooserPrivate {
35 	GtkComboBox *combo_box;		/* not referenced */
36 	GtkSpinButton *spin_button;	/* not referenced */
37 };
38 
39 enum {
40 	PROP_0,
41 	PROP_INTERVAL_MINUTES
42 };
43 
G_DEFINE_TYPE(EIntervalChooser,e_interval_chooser,GTK_TYPE_BOX)44 G_DEFINE_TYPE (
45 	EIntervalChooser,
46 	e_interval_chooser,
47 	GTK_TYPE_BOX)
48 
49 static void
50 interval_chooser_notify_interval (GObject *object)
51 {
52 	g_object_notify (object, "interval-minutes");
53 }
54 
55 static void
interval_chooser_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)56 interval_chooser_set_property (GObject *object,
57                                guint property_id,
58                                const GValue *value,
59                                GParamSpec *pspec)
60 {
61 	switch (property_id) {
62 		case PROP_INTERVAL_MINUTES:
63 			e_interval_chooser_set_interval_minutes (
64 				E_INTERVAL_CHOOSER (object),
65 				g_value_get_uint (value));
66 			return;
67 	}
68 
69 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
70 }
71 
72 static void
interval_chooser_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)73 interval_chooser_get_property (GObject *object,
74                                guint property_id,
75                                GValue *value,
76                                GParamSpec *pspec)
77 {
78 	switch (property_id) {
79 		case PROP_INTERVAL_MINUTES:
80 			g_value_set_uint (
81 				value,
82 				e_interval_chooser_get_interval_minutes (
83 				E_INTERVAL_CHOOSER (object)));
84 			return;
85 	}
86 
87 	G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 }
89 
90 static void
e_interval_chooser_class_init(EIntervalChooserClass * class)91 e_interval_chooser_class_init (EIntervalChooserClass *class)
92 {
93 	GObjectClass *object_class;
94 
95 	g_type_class_add_private (class, sizeof (EIntervalChooserPrivate));
96 
97 	object_class = G_OBJECT_CLASS (class);
98 	object_class->set_property = interval_chooser_set_property;
99 	object_class->get_property = interval_chooser_get_property;
100 
101 	g_object_class_install_property (
102 		object_class,
103 		PROP_INTERVAL_MINUTES,
104 		g_param_spec_uint (
105 			"interval-minutes",
106 			"Interval in Minutes",
107 			"Refresh interval in minutes",
108 			0, G_MAXUINT, 60,
109 			G_PARAM_READWRITE |
110 			G_PARAM_CONSTRUCT |
111 			G_PARAM_STATIC_STRINGS));
112 }
113 
114 static void
e_interval_chooser_init(EIntervalChooser * chooser)115 e_interval_chooser_init (EIntervalChooser *chooser)
116 {
117 	GtkWidget *widget;
118 
119 	chooser->priv = E_INTERVAL_CHOOSER_GET_PRIVATE (chooser);
120 
121 	gtk_orientable_set_orientation (
122 		GTK_ORIENTABLE (chooser), GTK_ORIENTATION_HORIZONTAL);
123 
124 	gtk_box_set_spacing (GTK_BOX (chooser), 6);
125 
126 	widget = gtk_spin_button_new_with_range (0, G_MAXUINT, 1);
127 	gtk_spin_button_set_numeric (GTK_SPIN_BUTTON (widget), TRUE);
128 	gtk_spin_button_set_update_policy (
129 		GTK_SPIN_BUTTON (widget), GTK_UPDATE_IF_VALID);
130 	gtk_box_pack_start (GTK_BOX (chooser), widget, TRUE, TRUE, 0);
131 	chooser->priv->spin_button = GTK_SPIN_BUTTON (widget);
132 	gtk_widget_show (widget);
133 
134 	e_signal_connect_notify_swapped (
135 		widget, "notify::value",
136 		G_CALLBACK (interval_chooser_notify_interval), chooser);
137 
138 	widget = gtk_combo_box_text_new ();
139 	gtk_combo_box_text_append_text (
140 		GTK_COMBO_BOX_TEXT (widget), _("minutes"));
141 	gtk_combo_box_text_append_text (
142 		GTK_COMBO_BOX_TEXT (widget), _("hours"));
143 	gtk_combo_box_text_append_text (
144 		GTK_COMBO_BOX_TEXT (widget), _("days"));
145 	gtk_box_pack_start (GTK_BOX (chooser), widget, FALSE, FALSE, 0);
146 	chooser->priv->combo_box = GTK_COMBO_BOX (widget);
147 	gtk_widget_show (widget);
148 
149 	e_signal_connect_notify_swapped (
150 		widget, "notify::active",
151 		G_CALLBACK (interval_chooser_notify_interval), chooser);
152 }
153 
154 GtkWidget *
e_interval_chooser_new(void)155 e_interval_chooser_new (void)
156 {
157 	return g_object_new (E_TYPE_INTERVAL_CHOOSER, NULL);
158 }
159 
160 guint
e_interval_chooser_get_interval_minutes(EIntervalChooser * chooser)161 e_interval_chooser_get_interval_minutes (EIntervalChooser *chooser)
162 {
163 	EDurationType units;
164 	gdouble interval_minutes;
165 
166 	g_return_val_if_fail (E_IS_INTERVAL_CHOOSER (chooser), 0);
167 
168 	units = gtk_combo_box_get_active (chooser->priv->combo_box);
169 
170 	interval_minutes = gtk_spin_button_get_value (
171 		chooser->priv->spin_button);
172 
173 	switch (units) {
174 		case E_DURATION_HOURS:
175 			interval_minutes *= MINUTES_PER_HOUR;
176 			break;
177 		case E_DURATION_DAYS:
178 			interval_minutes *= MINUTES_PER_DAY;
179 			break;
180 		default:
181 			break;
182 	}
183 
184 	return (guint) interval_minutes;
185 }
186 
187 void
e_interval_chooser_set_interval_minutes(EIntervalChooser * chooser,guint interval_minutes)188 e_interval_chooser_set_interval_minutes (EIntervalChooser *chooser,
189                                          guint interval_minutes)
190 {
191 	EDurationType units;
192 
193 	g_return_if_fail (E_IS_INTERVAL_CHOOSER (chooser));
194 
195 	if (interval_minutes == 0) {
196 		units = E_DURATION_MINUTES;
197 	} else if (interval_minutes % MINUTES_PER_DAY == 0) {
198 		interval_minutes /= MINUTES_PER_DAY;
199 		units = E_DURATION_DAYS;
200 	} else if (interval_minutes % MINUTES_PER_HOUR == 0) {
201 		interval_minutes /= MINUTES_PER_HOUR;
202 		units = E_DURATION_HOURS;
203 	} else {
204 		units = E_DURATION_MINUTES;
205 	}
206 
207 	g_object_freeze_notify (G_OBJECT (chooser));
208 
209 	gtk_combo_box_set_active (chooser->priv->combo_box, units);
210 
211 	gtk_spin_button_set_value (
212 		chooser->priv->spin_button, interval_minutes);
213 
214 	g_object_thaw_notify (G_OBJECT (chooser));
215 }
216