1 /* Time tab related code */
2 #include "disk.h"
3
4 /* 'Date and Time' Widgets */
5 GtkWidget *gyear;
6 GtkWidget *gmonth;
7 GtkWidget *gday;
8 GtkWidget *ghour;
9 GtkWidget *gminute;
10 GtkWidget *gsecond;
11
12 GtkWidget *newyear;
13 GtkWidget *newmonth;
14 GtkWidget *newday;
15 GtkWidget *newhour;
16 GtkWidget *newminute;
17 GtkWidget *newsecond;
18
19 /* Timezone widgets */
20 GtkWidget *gregion;
21 GtkWidget *gcountry;
22
23 gint timeup;
24
write_time_to_widgets()25 void write_time_to_widgets() {
26
27 char *thetime;
28 char *sep = ":";
29 char *brk;
30 char *snippet;
31
32 thetime = command("date +%Y:%m:%d:%H:%M:%S");
33 if(thetime == NULL)
34 return;
35
36 /* Year */
37 snippet = strtok_r(thetime, sep, &brk);
38 gtk_entry_set_text(GTK_ENTRY(gyear), snippet);
39
40 /* Month */
41 snippet = strtok_r(NULL, sep, &brk);
42 gtk_entry_set_text(GTK_ENTRY(gmonth), snippet);
43
44 /* Day */
45 snippet = strtok_r(NULL, sep, &brk);
46 gtk_entry_set_text(GTK_ENTRY(gday), snippet);
47
48 /* Hour */
49 snippet = strtok_r(NULL, sep, &brk);
50 gtk_entry_set_text(GTK_ENTRY(ghour), snippet);
51
52 /* Minute */
53 snippet = strtok_r(NULL, sep, &brk);
54 gtk_entry_set_text(GTK_ENTRY(gminute), snippet);
55
56 /* Second */
57 snippet = strtok_r(NULL, sep, &brk);
58 gtk_entry_set_text(GTK_ENTRY(gsecond), snippet);
59
60 }
61
sync_time(GtkMenuItem * item,gpointer userdata)62 void sync_time(GtkMenuItem *item, gpointer userdata) {
63 char *cmd = malloc(20);
64 sprintf(cmd, "ntpd -gq");
65 execute_me(cmd, MOUNT);
66 }
67
timeup_cb(gpointer data)68 gint timeup_cb(gpointer data) {
69
70 /* Is our tab still open? */
71 gint n = gtk_notebook_get_current_page(GTK_NOTEBOOK (tabs));
72 const gchar *tab = gtk_notebook_get_tab_label_text(GTK_NOTEBOOK (tabs), gtk_notebook_get_nth_page(GTK_NOTEBOOK (tabs), n));
73
74 if(strcmp(tab, l.tabtime) != 0) {
75 timeup = 0;
76 return FALSE;
77 }
78
79 write_time_to_widgets();
80 return TRUE;
81 }
82
get_timezone()83 char *get_timezone() {
84
85 time_t currtime = time(NULL);
86 struct tm *t = localtime(&currtime);
87
88 if(t != NULL)
89 return t->tm_zone;
90 else
91 return NULL;
92 }
93
set_new_time(GtkMenuItem * item,gpointer userdata)94 void set_new_time(GtkMenuItem *item, gpointer userdata) {
95
96 /* Be safe. */
97 g_source_remove(timeup);
98
99 const gchar *syear;
100 const gchar *smonth;
101 const gchar *sday;
102 const gchar *shour;
103 const gchar *sminute;
104 const gchar *ssecond;
105
106 /*
107 * Read the new values.
108 * If empty, keep the current time insofar.
109 */
110
111 syear = gtk_entry_get_text(GTK_ENTRY (newyear));
112 if(strlen(syear) == 0)
113 syear = gtk_entry_get_text(GTK_ENTRY (gyear));
114
115 smonth = gtk_entry_get_text(GTK_ENTRY (newmonth));
116 if(strlen(smonth) == 0)
117 smonth = gtk_entry_get_text(GTK_ENTRY (gmonth));
118
119 sday = gtk_entry_get_text(GTK_ENTRY (newday));
120 if(strlen(sday) == 0)
121 sday = gtk_entry_get_text(GTK_ENTRY (gday));
122
123 shour = gtk_entry_get_text(GTK_ENTRY (newhour));
124 if(strlen(shour) == 0)
125 shour = gtk_entry_get_text(GTK_ENTRY (ghour));
126
127 sminute = gtk_entry_get_text(GTK_ENTRY (newminute));
128 if(strlen(sminute) == 0)
129 sminute = gtk_entry_get_text(GTK_ENTRY (gminute));
130
131
132 char *cmd = malloc(CMDSIZE);
133 sprintf(cmd, "date %s%s%s%s%s", syear, smonth, sday, shour, sminute);
134 execute_me(cmd, GPART);
135
136 /* Restart the timeout. */
137 timeup = g_timeout_add(6000, timeup_cb, NULL);
138
139 }
140
set_timezone(GtkMenuItem * item,gpointer userdata)141 void set_timezone(GtkMenuItem *item, gpointer userdata) {
142
143 char *string;
144 string = gtk_combo_box_text_get_active_text( GTK_COMBO_BOX_TEXT (gregion));
145 if(string == NULL)
146 return;
147
148 char *string1;
149 string1 = gtk_combo_box_text_get_active_text( GTK_COMBO_BOX_TEXT (gcountry));
150
151 char *cmd;
152 if(string1 == NULL) {
153 cmd = malloc(strlen(string) + 50);
154 sprintf(cmd, "cp /usr/share/zoneinfo/%s /etc/localtime", string);
155 }
156 else {
157 cmd = malloc(strlen(string) + strlen(string1) + 50);
158 sprintf(cmd, "cp /usr/share/zoneinfo/%s/%s /etc/localtime", string, string1);
159 }
160
161 execute_me(cmd, MOUNT);
162 }
163
region_changed()164 void region_changed() {
165
166 /* Cleanup */
167 gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT (gcountry));
168
169 char *string;
170 string = gtk_combo_box_text_get_active_text( GTK_COMBO_BOX_TEXT (gregion));
171 if(string == NULL)
172 return;
173
174 char *cmd = malloc(strlen(string) + 40);
175 if(cmd == NULL)
176 return;
177 sprintf(cmd, "ls /usr/share/zoneinfo/%s", string);
178
179 FILE *fp = popen(cmd, "r");
180 if (fp == NULL) {
181 msg("couldnt popen");
182 return;
183 }
184 char info[200];
185 memset(info, 0, 200);
186
187 while( fgets(info, sizeof info, fp)) {
188 int len = strlen(info);
189 if((len > 0) && (info[0] != '/')) {
190 info[len-1]='\0';
191 gtk_combo_box_text_append( GTK_COMBO_BOX_TEXT (gcountry), NULL, info);
192 }
193 }
194
195 pclose(fp);
196 free(cmd);
197 gtk_widget_show(gcountry);
198 }
199
fill_region()200 void fill_region() {
201
202 FILE *fp = popen("ls /usr/share/zoneinfo", "r");
203 if (fp == NULL) {
204 msg("couldnt popen");
205 return;
206 }
207 char info[200];
208 memset(info, 0, 200);
209
210 while( fgets(info, sizeof info, fp)) {
211 int len = strlen(info);
212 if(len > 0) {
213 info[len-1]='\0';
214 gtk_combo_box_text_append( GTK_COMBO_BOX_TEXT (gregion), NULL, info);
215 }
216 }
217 pclose(fp);
218 }
219
add_time()220 void add_time() {
221
222 gtk_container_add(GTK_CONTAINER (timebox), gtk_label_new(l.ctime3));
223 gtk_container_add(GTK_CONTAINER (timebox), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL));
224
225 /* Grid */
226 GtkWidget *gtime = gtk_grid_new();
227 gtk_grid_set_column_homogeneous(GTK_GRID(gtime), FALSE);
228 gtk_grid_insert_column(GTK_GRID(gtime), 7);
229 gtk_grid_set_row_spacing(GTK_GRID(gtime), 5);
230 gtk_grid_set_column_spacing(GTK_GRID(gtime), 5);
231 gtk_box_pack_start(GTK_BOX(timebox), gtime, FALSE, FALSE, 0);
232
233 /* Grid entries */
234
235 /* First row */
236 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tyear), 0, 0, 1, 1);
237 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tmonth), 1, 0, 1, 1);
238 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tday), 2, 0, 1, 1);
239 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new("--"), 3, 0, 1, 1);
240 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.thour), 4, 0, 1, 1);
241 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tminute), 5, 0, 1, 1);
242 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tsecond), 6, 0, 1, 1);
243
244 /* Second row */
245 gyear = gtk_entry_new();
246 gmonth = gtk_entry_new();
247 gday = gtk_entry_new();
248 ghour = gtk_entry_new();
249 gminute = gtk_entry_new();
250 gsecond = gtk_entry_new();
251
252 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (gyear)), 4);
253 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (gmonth)), 2);
254 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (gday)), 2);
255 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (ghour)), 2);
256 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (gminute)), 2);
257 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (gsecond)), 2);
258
259 gtk_entry_set_width_chars(GTK_ENTRY(gyear), 4);
260 gtk_entry_set_width_chars(GTK_ENTRY(gmonth), 2);
261 gtk_entry_set_width_chars(GTK_ENTRY(gday), 2);
262 gtk_entry_set_width_chars(GTK_ENTRY(ghour), 2);
263 gtk_entry_set_width_chars(GTK_ENTRY(gminute), 2);
264 gtk_entry_set_width_chars(GTK_ENTRY(gsecond), 2);
265
266 g_object_set(gyear, "editable", FALSE, "can_focus", FALSE, NULL);
267 g_object_set(gmonth, "editable", FALSE, "can_focus", FALSE, NULL);
268 g_object_set(gday, "editable", FALSE, "can_focus", FALSE, NULL);
269 g_object_set(ghour, "editable", FALSE, "can_focus", FALSE, NULL);
270 g_object_set(gminute, "editable", FALSE, "can_focus", FALSE, NULL);
271 g_object_set(gsecond, "editable", FALSE, "can_focus", FALSE, NULL);
272
273 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (gyear), 0, 1, 1, 1);
274 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (gmonth), 1, 1, 1, 1);
275 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (gday), 2, 1, 1, 1);
276 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (ghour), 4, 1, 1, 1);
277 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (gminute), 5, 1, 1, 1);
278 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (gsecond), 6, 1, 1, 1);
279
280 /* Third row */
281 newyear = gtk_entry_new();
282 newmonth = gtk_entry_new();
283 newday = gtk_entry_new();
284 newhour = gtk_entry_new();
285 newminute = gtk_entry_new();
286 newsecond = gtk_entry_new();
287
288 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newyear)), 4);
289 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newmonth)), 2);
290 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newday)), 2);
291 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newhour)), 2);
292 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newminute)), 2);
293 gtk_entry_buffer_set_max_length (gtk_entry_get_buffer(GTK_ENTRY (newsecond)), 2);
294
295 gtk_entry_set_width_chars(GTK_ENTRY(newyear), 4);
296 gtk_entry_set_width_chars(GTK_ENTRY(newmonth), 2);
297 gtk_entry_set_width_chars(GTK_ENTRY(newday), 2);
298 gtk_entry_set_width_chars(GTK_ENTRY(newhour), 2);
299 gtk_entry_set_width_chars(GTK_ENTRY(newminute), 2);
300 gtk_entry_set_width_chars(GTK_ENTRY(newsecond), 2);
301
302 g_object_set(newsecond, "editable", FALSE, "can_focus", FALSE, NULL);
303
304 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newyear), 0, 2, 1, 1);
305 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newmonth), 1, 2, 1, 1);
306 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newday), 2, 2, 1, 1);
307 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newhour), 4, 2, 1, 1);
308 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newminute), 5, 2, 1, 1);
309 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (newsecond), 6, 2, 1, 1);
310
311 GtkWidget *b = gtk_button_new_with_label(l.tsettime);
312 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (b), 7, 2, 1, 1);
313 g_signal_connect(b, "clicked", G_CALLBACK(set_new_time), NULL);
314
315
316 /* Synchronize with network time GUI */
317 gtk_container_add(GTK_CONTAINER (timebox), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL));
318
319 gtime = gtk_grid_new();
320 gtk_grid_set_column_homogeneous(GTK_GRID(gtime), FALSE);
321 gtk_grid_set_row_spacing(GTK_GRID(gtime), 5);
322 gtk_grid_set_column_spacing(GTK_GRID(gtime), 20);
323 gtk_box_pack_start(GTK_BOX(timebox), gtime, FALSE, FALSE, 0);
324
325 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(l.tsync), 0, 0, 1, 1);
326
327 b = gtk_button_new_with_label("Ok");
328 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (b), 1, 0, 1, 1);
329 g_signal_connect(b, "clicked", G_CALLBACK(sync_time), NULL);
330
331 }
332
add_timezone()333 void add_timezone() {
334
335 gtk_container_add(GTK_CONTAINER (timebox), gtk_separator_new(GTK_ORIENTATION_HORIZONTAL));
336 gtk_container_add(GTK_CONTAINER (timebox), gtk_label_new(l.ttimezone));
337
338 /* A grid. */
339 GtkWidget *gtime = gtk_grid_new();
340 gtk_grid_set_column_homogeneous(GTK_GRID(gtime), FALSE);
341 gtk_grid_set_row_spacing(GTK_GRID(gtime), 5);
342 gtk_grid_set_column_spacing(GTK_GRID(gtime), 5);
343 gtk_box_pack_start(GTK_BOX(timebox), gtime, TRUE, FALSE, 0);
344
345 /* Grid elements */
346
347 /*Current timezone */
348 char *mytimezone = get_timezone();
349 if(mytimezone != NULL)
350 gtk_grid_attach(GTK_GRID (gtime), gtk_label_new(mytimezone), 0, 0, 1, 1);
351
352 /* Region combo box */
353 gregion = gtk_combo_box_text_new();
354 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(gregion), 3);
355 fill_region();
356 g_signal_connect(gregion, "changed", G_CALLBACK (region_changed), NULL);
357 gtk_grid_attach(GTK_GRID (gtime), gregion, 1, 0, 1, 1);
358
359 /* Country combo box */
360 gcountry = gtk_combo_box_text_new();
361 gtk_combo_box_set_wrap_width(GTK_COMBO_BOX(gcountry), 3);
362 gtk_grid_attach(GTK_GRID (gtime), gcountry, 2, 0, 1, 1);
363
364 /* Button */
365 GtkWidget *b = gtk_button_new_with_label(l.tsetzone);
366 g_signal_connect(b, "clicked", G_CALLBACK(set_timezone), NULL);
367 gtk_widget_set_tooltip_text(b, l.ttooltip);
368 gtk_grid_attach(GTK_GRID (gtime), GTK_WIDGET (b), 3, 0, 1, 1);
369 }
370
timetab()371 void timetab() {
372
373 if(GTK_IS_WIDGET(timebox))
374 gtk_widget_destroy(timebox);
375
376 /* A top level container. */
377 timebox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 15);
378 gtk_container_add (GTK_CONTAINER (tab4), timebox);
379
380 /* Raw GUI elements */
381 add_time();
382 add_timezone();
383
384 /* Write time once, then update regularly. */
385 write_time_to_widgets();
386 if(timeup)
387 g_source_remove(timeup);
388 timeup = g_timeout_add(4000, timeup_cb, NULL);
389
390 gtk_widget_show_all(timebox);
391 }
392