00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00031 #include <time.h>
00032 #include <unistd.h>
00033 #include "iLiadDateTimeScreen.h"
00034 #include <libergtk/ergtk.h>
00035 #include "gtkInfoItem.h"
00036 #include "setupLog.h"
00037 #include "gtkSettingItem.h"
00038 #include "background.h"
00039 #include "languages.h"
00040
00041
00042 static gtk_datetime_screen* g_datetime_screen = NULL;
00043 static const tz_info timezones[TOTAL_TIMEZONES] =
00044 {
00045 {-39600, "GMT+11:00"},
00046 {-36000, "GMT+10:00"},
00047 {-32400, "GMT+09:00"},
00048 {-28800, "GMT+08:00"},
00049 {-25200, "GMT+07:00"},
00050 {-21600, "GMT+06:00"},
00051 {-18000, "GMT+05:00"},
00052 {-14400, "GMT+04:00"},
00053 {-12600, "GMT+03:30"},
00054 {-10800, "GMT+03:00"},
00055 { -9000, "GMT+02:30"},
00056 { -7200, "GMT+02:00"},
00057 { -3600, "GMT+01:00"},
00058 { 0, "GMT+00:00"},
00059 { 3600, "GMT-01:00"},
00060 { 7200, "GMT-02:00"},
00061 { 10800, "GMT-03:00"},
00062 { 12600, "GMT-03:30"},
00063 { 14400, "GMT-04:00"},
00064 { 16200, "GMT-04:30"},
00065 { 18000, "GMT-05:00"},
00066 { 19800, "GMT-05:30"},
00067 { 20700, "GMT-05:45"},
00068 { 21600, "GMT-06:00"},
00069 { 23400, "GMT-06:30"},
00070 { 25200, "GMT-07:00"},
00071 { 28800, "GMT-08:00"},
00072 { 32400, "GMT-09:00"},
00073 { 34200, "GMT-09:30"},
00074 { 36000, "GMT-10:00"},
00075 { 37800, "GMT-10:30"},
00076 { 39600, "GMT-11:00"},
00077 { 43200, "GMT-12:00"},
00078 { 46800, "GMT-13:00"}
00079 };
00080
00081 static time_t g_utc_time = 0;
00082 static int g_date_boundary = 0;
00083 static int g_current_tz_offset = -1;
00084
00085
00086 static GtkWidget *create_current_time_display(void);
00087 static GtkWidget *create_timezone_selection(void);
00088 static void iLiad_update_datetime(int tzoffset);
00089 static int get_current_tz_offset();
00090 static gboolean set_tz_offset(const char* tzfile);
00091 static void on_selection_update(erGtkSelectionGroup * selection, gpointer button, gpointer data);
00092
00093
00094
00095
00096
00097 GtkWidget *iLiad_date_time_create()
00098 {
00099 GtkWidget* vbox;
00100 GtkWidget* settingitem;
00101 GtkWidget* background;
00102 GtkWidget* info;
00103 GtkWidget* selection;
00104
00105 ST_LOGPRINTF("entry");
00106 g_datetime_screen = g_new0(gtk_datetime_screen, 1);
00107
00108 vbox = gtk_vbox_new(FALSE, DATETIME_SCREEN_ITEM_SPACING);
00109 g_datetime_screen->top_vbox = vbox;
00110
00111 settingitem = create_current_time_display();
00112 gtk_box_pack_start(GTK_BOX(vbox), settingitem, FALSE, FALSE, 0);
00113 g_datetime_screen->settingitem = settingitem;
00114
00115 background = create_timezone_selection();
00116 gtk_box_pack_start(GTK_BOX(vbox), background, FALSE, FALSE, 0);
00117 g_datetime_screen->timezones = background;
00118
00119
00120 selection = ergtk_selection_group_new(g_datetime_screen->button_timezones);
00121 ergtk_selection_group_set_details(ERGTK_SELECTION_GROUP(selection), 1, 1);
00122 g_signal_connect(G_OBJECT(selection), "selection-update", G_CALLBACK(on_selection_update), NULL);
00123 g_datetime_screen->selection = selection;
00124
00125 info = gtk_infoitem_new(FALSE);
00126 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
00127 g_datetime_screen->infoitem = info;
00128
00129
00130 gtk_widget_show(vbox);
00131 return vbox;
00132 }
00133
00134
00135
00136
00137
00138
00139
00140
00141 GtkWidget *create_current_time_display(void)
00142 {
00143 GtkWidget* settingitem;
00144 GtkWidget* background;
00145 GtkWidget* alignment;
00146 GtkWidget* vbox;
00147 GtkWidget* label;
00148
00149
00150 settingitem = gtk_settingitem_new("");
00151 gtk_widget_set_size_request(settingitem, -1, -1);
00152
00153
00154 background = gtk_event_box_new();
00155 gtk_widget_set_name(background, "datetime_display_background");
00156 gtk_widget_set_size_request(background, TITLE_MIN_WIDTH, DATETIME_DISPLAY_HEIGHT);
00157 gtk_settingitem_add_details(GTK_SETTINGITEM(settingitem), background);
00158
00159
00160 alignment = gtk_alignment_new(0, 0, 0, 0);
00161 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
00162 DATETIME_SCREEN_ITEM_PADDING_TOP,
00163 DATETIME_SCREEN_ITEM_PADDING_BOTTOM,
00164 DATETIME_SCREEN_ITEM_PADDING_LEFT,
00165 DATETIME_SCREEN_ITEM_PADDING_RIGHT);
00166 gtk_container_add(GTK_CONTAINER(background), alignment);
00167
00168
00169 vbox = gtk_vbox_new(FALSE, DATETIME_DISPLAY_SPACING);
00170 gtk_container_add(GTK_CONTAINER(alignment), vbox);
00171
00172 label = gtk_label_new("");
00173 gtk_widget_set_name(label, "datetime_screen_info_text");
00174 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00175 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
00176 g_datetime_screen->label_show_time_prompt = label;
00177
00178 label = gtk_label_new("");
00179 gtk_widget_set_name(label, "datetime_screen_date_text");
00180 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00181 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
00182 g_datetime_screen->label_current_time = label;
00183
00184 gtk_widget_show_all(background);
00185 gtk_widget_show(settingitem);
00186 return settingitem;
00187 }
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 GtkWidget *create_timezone_selection(void)
00211 {
00212 GtkWidget* background;
00213 GtkWidget* alignment;
00214 GtkWidget* vbox;
00215 GtkWidget* label;
00216 GtkWidget* button;
00217
00218
00219 background = gtk_event_box_new();
00220 gtk_widget_set_name(background, "timezone_selection_background");
00221 gtk_widget_set_size_request(background, TITLE_MIN_WIDTH, TIMEZONE_SELECTION_HEIGHT);
00222
00223
00224 alignment = gtk_alignment_new(0, 0, 0, 0);
00225 gtk_alignment_set_padding(GTK_ALIGNMENT(alignment),
00226 DATETIME_SCREEN_ITEM_PADDING_TOP,
00227 DATETIME_SCREEN_ITEM_PADDING_BOTTOM,
00228 DATETIME_SCREEN_ITEM_PADDING_LEFT,
00229 DATETIME_SCREEN_ITEM_PADDING_RIGHT);
00230 gtk_container_add(GTK_CONTAINER(background), alignment);
00231
00232
00233 vbox = gtk_vbox_new(FALSE, DATETIME_SCREEN_ITEM_SPACING);
00234 gtk_container_add(GTK_CONTAINER(alignment), vbox);
00235
00236 label = gtk_label_new("");
00237 gtk_widget_set_name(label, "datetime_screen_info_text");
00238 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00239 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
00240 g_datetime_screen->label_timezone_prompt = label;
00241
00242 g_utc_time = time(NULL);
00243
00244
00245 int i;
00246 for (i=0; i<TOTAL_TIMEZONES; i++)
00247 {
00248 if ((g_utc_time+timezones[i].tzoffset) / 86400 > (g_utc_time+timezones[0].tzoffset) / 86400)
00249 {
00250 g_date_boundary = i;
00251 break;
00252 }
00253 }
00254
00255
00256 label = gtk_label_new("");
00257 gtk_widget_set_name(label, "datetime_date_heading_text");
00258 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00259 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
00260 g_datetime_screen->label_possible_date[0] = label;
00261
00262
00263 int minutes_in_tz;
00264 char button_text[8];
00265 GtkWidget* hbox = NULL;
00266 for (i=0; i<TOTAL_TIMEZONES; i++)
00267 {
00268 if (i == g_date_boundary)
00269 {
00270
00271 label = gtk_label_new("");
00272 gtk_widget_set_name(label, "datetime_date_heading_text");
00273 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
00274 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
00275 g_datetime_screen->label_possible_date[1] = label;
00276 }
00277
00278 if ((i<g_date_boundary && i%5 == 0) ||
00279 (i>=g_date_boundary && (i-g_date_boundary)%5 == 0))
00280 {
00281
00282 hbox = gtk_hbox_new(FALSE, TIMEZONE_BUTTON_ITEM_SPACING);
00283 gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(hbox), FALSE, FALSE, 0);
00284 }
00285
00286 minutes_in_tz = ((g_utc_time + timezones[i].tzoffset) % 86400) / 60;
00287 sprintf(button_text, "%02d:%02d", minutes_in_tz / 60, minutes_in_tz % 60);
00288
00289 button = ergtk_toggle_button_new_with_label(button_text);
00290 gtk_widget_set_size_request(GTK_WIDGET(button), TZ_BUTTON_WIDTH, TZ_BUTTON_HEIGHT);
00291 gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(button), FALSE, FALSE, 0);
00292 g_datetime_screen->button_timezones[i] = GTK_TOGGLE_BUTTON(button);
00293 }
00294
00295 g_datetime_screen->button_timezones[TOTAL_TIMEZONES] = NULL;
00296
00297 gtk_widget_show_all(background);
00298 return background;
00299 }
00300
00301 void iLiad_date_time_set_text(void)
00302 {
00303 ST_LOGPRINTF("entry");
00304 if (g_datetime_screen->settingitem)
00305 {
00306 gtk_settingitem_set_header_text(GTK_SETTINGITEM(g_datetime_screen->settingitem),
00307 _("Time Selection"));
00308
00309 gtk_label_set_text(GTK_LABEL(g_datetime_screen->label_show_time_prompt),
00310 _("The current time and date on your iLiad is:"));
00311
00312 if (g_current_tz_offset != -1)
00313 {
00314 iLiad_update_datetime(g_current_tz_offset);
00315 }
00316 }
00317
00318 if (g_datetime_screen->timezones)
00319 {
00320 gtk_label_set_text(GTK_LABEL(g_datetime_screen->label_timezone_prompt),
00321 _("To change the clock, select your current date and time from the list below."));
00322
00323
00324 time_t tm = g_utc_time + timezones[0].tzoffset;
00325 char buf[DATE_STRING_LENGTH];
00326
00327
00328
00329 strftime(buf, DATE_STRING_LENGTH, _("%A, %x"), gmtime(&tm));
00330 gtk_label_set_text(GTK_LABEL(g_datetime_screen->label_possible_date[0]), buf);
00331
00332
00333 tm = g_utc_time + timezones[g_date_boundary].tzoffset;
00334
00335 strftime(buf, DATE_STRING_LENGTH, _("%A, %x"), gmtime(&tm));
00336 gtk_label_set_text(GTK_LABEL(g_datetime_screen->label_possible_date[1]), buf);
00337 }
00338
00339 if (g_datetime_screen->infoitem)
00340 {
00341
00342 gtk_infoitem_set_text(GTK_INFOITEM(g_datetime_screen->infoitem),
00343 _("You can set your date and time by connecting to iDS. Afterwards you can use this page to update the time for your current timezone."));
00344 }
00345 }
00346
00347 void iLiad_date_time_display_data(void)
00348 {
00349 int i = 0;
00350
00351 ST_LOGPRINTF("entry");
00352
00353 g_current_tz_offset = get_current_tz_offset();
00354
00355 for (i=0; i<TOTAL_TIMEZONES; i++)
00356 {
00357 if (timezones[i].tzoffset == g_current_tz_offset)
00358 {
00359 break;
00360 }
00361 }
00362
00363 GtkToggleButton* button_to_be_active = g_datetime_screen->button_timezones[i];
00364 gtk_toggle_button_set_active(button_to_be_active, TRUE);
00365 }
00366
00367 void iLiad_date_time_handle_refresh(void)
00368 {
00369 GtkWidget* background;
00370 GtkWidget* info;
00371 GtkWidget* selection;
00372 GtkWidget* vbox = g_datetime_screen->top_vbox;
00373
00374 ST_LOGPRINTF("entry");
00375
00376 if (g_datetime_screen->timezones)
00377 {
00378 gtk_widget_destroy(g_datetime_screen->timezones);
00379 g_datetime_screen->timezones = NULL;
00380 }
00381
00382 if (g_datetime_screen->selection)
00383 {
00384 gtk_object_destroy(GTK_OBJECT(g_datetime_screen->selection));
00385 g_datetime_screen->selection = NULL;
00386 }
00387
00388 if (g_datetime_screen->infoitem)
00389 {
00390 gtk_widget_destroy(g_datetime_screen->infoitem);
00391 g_datetime_screen->infoitem = NULL;
00392 }
00393
00394 background = create_timezone_selection();
00395 gtk_box_pack_start(GTK_BOX(vbox), background, FALSE, FALSE, 0);
00396 g_datetime_screen->timezones = background;
00397
00398
00399 selection = ergtk_selection_group_new(g_datetime_screen->button_timezones);
00400 ergtk_selection_group_set_details(ERGTK_SELECTION_GROUP(selection), 1, 1);
00401 g_signal_connect(G_OBJECT(selection), "selection-update", G_CALLBACK(on_selection_update), NULL);
00402 g_datetime_screen->selection = selection;
00403
00404 info = gtk_infoitem_new(FALSE);
00405 gtk_box_pack_start(GTK_BOX(vbox), info, FALSE, FALSE, 0);
00406 g_datetime_screen->infoitem = info;
00407
00408
00409 g_current_tz_offset = -1;
00410 iLiad_date_time_set_text();
00411
00412
00413 iLiad_date_time_display_data();
00414 }
00415
00416 void on_selection_update(erGtkSelectionGroup * selection, gpointer button, gpointer data)
00417 {
00418 int index = 0;
00419
00420 ST_LOGPRINTF("entry");
00421 ergtk_selection_group_get_selected_buttons(ERGTK_SELECTION_GROUP(selection), &index, 1);
00422
00423 int tzoffset = timezones[index].tzoffset;
00424
00425
00426 if (g_current_tz_offset != tzoffset)
00427 {
00428 g_current_tz_offset = tzoffset;
00429
00430
00431
00432 set_tz_offset(timezones[index].tzname);
00433 }
00434
00435
00436 iLiad_update_datetime(g_current_tz_offset);
00437 }
00438
00439 gboolean set_tz_offset(const char* tzfile)
00440 {
00441 char actual_tzfile[256];
00442
00443 snprintf(actual_tzfile, 256, "%s%s", TZ_FILES_DIR, tzfile);
00444 if (access(TZ_SETTING_FILE, F_OK) == 0)
00445 {
00446 if (unlink(TZ_SETTING_FILE) != 0)
00447 {
00448 ST_ERRORPRINTF("Can't remove %s", TZ_SETTING_FILE);
00449 return FALSE;
00450 }
00451 }
00452
00453 if (symlink(actual_tzfile, TZ_SETTING_FILE) != 0)
00454 {
00455 ST_ERRORPRINTF("Can't create symbol link from %s to %s", actual_tzfile, TZ_SETTING_FILE);
00456 return FALSE;
00457 }
00458
00459 return TRUE;
00460 }
00461
00462 void iLiad_update_datetime(int tzoffset)
00463 {
00464 if (g_datetime_screen)
00465 {
00466 char buf[DATE_STRING_LENGTH];
00467 time_t tm_local = g_utc_time + tzoffset;
00468
00469
00470
00471
00472 strftime(buf, DATE_STRING_LENGTH, _("%R %A, %x"), gmtime(&tm_local));
00473 gtk_label_set_text(GTK_LABEL(g_datetime_screen->label_current_time), buf);
00474 }
00475 }
00476
00477 static int get_current_tz_offset()
00478 {
00479 FILE* fp = fopen(TZ_FILE, "rb");
00480
00481 if (NULL == fp)
00482 {
00483 ST_ERRORPRINTF("Can't open %s, use default timezone settings.", TZ_FILE);
00484 goto error;
00485 }
00486
00487 fseek(fp, sizeof(struct tzhead), SEEK_SET);
00488
00489 int offset;
00490 if (fread(&offset, sizeof(int), 1, fp) != 1)
00491 {
00492 ST_ERRORPRINTF("Error reading %s, use default timezone settings.", TZ_FILE);
00493 fclose(fp);
00494 goto error;
00495 }
00496
00497 fclose(fp);
00498
00499 return GINT32_FROM_BE(offset);
00500
00501 error:
00502 set_tz_offset(DEFAULT_TIMEZONE_NAME);
00503 return DEFAULT_TIMEZONE_OFFSET;
00504 }
00505