evolution-3.6.4/widgets/misc/e-interval-chooser.c

No issues found

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