/* * timezone.c -- dtsystemsettings: choosing the time zone (Date & Time) * * License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree. * * Two lists, the regions of the tz database (Europe, America, ...) and the * places in the one chosen, with the zone in use selected; the zones come * from McdeSys (mcde_sys_timezones: zone1970.tab). "Set Time Zone" sets * it through McdeSys -- as root at once; otherwise in a terminal where * su(1) asks for the administrator's password and runs * dtsystemsettings -set-timezone ZONE, as Software Update does. * * C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md). */ #define _POSIX_C_SOURCE 200809L #include #include #include #include #include #include #include #include #include #include #include #include "ui.h" #define MAX_ZONES 700 static char (*zones)[MCDE_SYS_ZONE_LEN]; static int nzones; static char current[MCDE_SYS_ZONE_LEN], region[MCDE_SYS_ZONE_LEN]; static Widget regionList, placeList, setButton, zoneNote; /* REGION of ZONE ("Europe" of "Europe/Lisbon"; "Other" for "UTC") */ static void RegionOf(const char *zone, char *buf, size_t len) { const char *slash = strchr(zone, '/'); if (slash == NULL) snprintf(buf, len, "Other"); else snprintf(buf, len, "%.*s", (int) (slash - zone), zone); } /* the place, for the eye: "America/Argentina/Buenos_Aires" -> * "Argentina / Buenos Aires" */ static void PlaceOf(const char *zone, char *buf, size_t len) { const char *slash = strchr(zone, '/'); size_t i, n = 0; slash = slash != NULL ? slash + 1 : zone; for (i = 0; slash[i] != '\0' && n + 4 < len; i++) { if (slash[i] == '_') buf[n++] = ' '; else if (slash[i] == '/') { buf[n++] = ' '; buf[n++] = '/'; buf[n++] = ' '; } else buf[n++] = slash[i]; } buf[n] = '\0'; } /* the zone chosen in the lists, or NULL */ static const char * Chosen(void) { int *pos = NULL, n = 0, i, k = 0, want; char r[MCDE_SYS_ZONE_LEN]; if (!XmListGetSelectedPos(placeList, &pos, &n) || n < 1) return NULL; want = pos[0]; XtFree((char *) pos); for (i = 0; i < nzones; i++) { RegionOf(zones[i], r, sizeof r); if (strcmp(r, region) == 0 && ++k == want) return zones[i]; } return NULL; } static void UpdateButton(void) { const char *z = Chosen(); XtSetSensitive(setButton, z != NULL && strcmp(z, current) != 0); } static void FillPlaces(const char *select) { char r[MCDE_SYS_ZONE_LEN], p[MCDE_SYS_ZONE_LEN * 2]; int i, k = 0, sel = 0; XmListDeleteAllItems(placeList); for (i = 0; i < nzones; i++) { XmString s; RegionOf(zones[i], r, sizeof r); if (strcmp(r, region) != 0) continue; PlaceOf(zones[i], p, sizeof p); s = XmStringCreateLocalized(p); XmListAddItemUnselected(placeList, s, 0); XmStringFree(s); k++; if (select != NULL && strcmp(zones[i], select) == 0) sel = k; } if (sel > 0) { XmListSelectPos(placeList, sel, False); XmListSetPos(placeList, sel > 3 ? sel - 3 : 1); } UpdateButton(); } static void RegionChosen(Widget w, XtPointer data, XtPointer call) { char *r = XmStringUnparse(((XmListCallbackStruct *) call)->item, NULL, XmCHARSET_TEXT, XmCHARSET_TEXT, NULL, 0, XmOUTPUT_ALL); (void) w; (void) data; if (r == NULL) return; snprintf(region, sizeof region, "%s", r); XtFree(r); FillPlaces(current); } static void PlaceChosen(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; UpdateButton(); } static void SetZone(Widget w, XtPointer data, XtPointer call) { const char *z = Chosen(); char cmd[512], text[256]; (void) w; (void) data; (void) call; if (z == NULL) return; if (geteuid() != 0) { /* the names of zone1970.tab are letters, digits, "/_+-": * nothing the shell of su -c would read otherwise */ snprintf(text, sizeof text, "dtsystemsettings -set-timezone %s", z); AsRoot("Set Time Zone", text, cmd, sizeof cmd); Spawn(cmd); SetText(zoneNote, "The administrator's password is asked for " "in the window that opened."); return; } if (mcde_sys_timezone_set(z) != MCDE_SYS_OK) { Message(w, mcde_sys_error(), 1); return; } snprintf(current, sizeof current, "%s", z); tzset(); snprintf(text, sizeof text, "The time zone is %s.", z); SetText(zoneNote, text); UpdateButton(); } static void ZonesGone(Widget w, XtPointer data, XtPointer call) { (void) w; (void) data; (void) call; free(zones); zones = NULL; nzones = 0; } Widget MakeZoneChooser(Widget parent) { Widget box = XtVaCreateManagedWidget("zone", xmRowColumnWidgetClass, parent, XmNspacing, 6, NULL); Widget form; char r[MCDE_SYS_ZONE_LEN], last[MCDE_SYS_ZONE_LEN] = ""; Arg a[8]; int n, i; zones = calloc(MAX_ZONES, sizeof *zones); nzones = zones != NULL ? mcde_sys_timezones(zones, MAX_ZONES) : 0; if (nzones < 0) nzones = 0; (void) mcde_sys_timezone_get(current, sizeof current); RegionOf(current, region, sizeof region); XtAddCallback(box, XmNdestroyCallback, ZonesGone, NULL); (void) Text(box, "label", "Choose the time zone by the place nearest " "to you:"); form = XtVaCreateManagedWidget("lists", xmFormWidgetClass, box, XmNwidth, 520, XmNheight, 170, NULL); n = 0; XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++; XtSetArg(a[n], XmNtopAttachment, XmATTACH_FORM); n++; XtSetArg(a[n], XmNbottomAttachment, XmATTACH_FORM); n++; XtSetArg(a[n], XmNleftAttachment, XmATTACH_FORM); n++; regionList = XmCreateScrolledList(form, "regions", a, n); XtVaSetValues(XtParent(regionList), XmNwidth, 160, NULL); n = 0; XtSetArg(a[n], XmNselectionPolicy, XmBROWSE_SELECT); n++; XtSetArg(a[n], XmNtopAttachment, XmATTACH_FORM); n++; XtSetArg(a[n], XmNbottomAttachment, XmATTACH_FORM); n++; XtSetArg(a[n], XmNleftAttachment, XmATTACH_WIDGET); n++; XtSetArg(a[n], XmNleftWidget, XtParent(regionList)); n++; XtSetArg(a[n], XmNleftOffset, 8); n++; XtSetArg(a[n], XmNrightAttachment, XmATTACH_FORM); n++; placeList = XmCreateScrolledList(form, "places", a, n); XtManageChild(regionList); XtManageChild(placeList); /* the regions, once each (the zones are sorted, so a region's zones * follow each other) */ for (i = 0; i < nzones; i++) { RegionOf(zones[i], r, sizeof r); if (strcmp(r, last) != 0) { XmString s = XmStringCreateLocalized(r); XmListAddItemUnselected(regionList, s, 0); if (strcmp(r, region) == 0) XmListSelectItem(regionList, s, False); XmStringFree(s); snprintf(last, sizeof last, "%s", r); } } XtAddCallback(regionList, XmNbrowseSelectionCallback, RegionChosen, NULL); XtAddCallback(placeList, XmNbrowseSelectionCallback, PlaceChosen, NULL); setButton = PaneButton(box, "Set Time Zone", SetZone, NULL); zoneNote = Text(box, "note", nzones == 0 ? mcde_sys_error() : ""); FillPlaces(current); return box; } /* dtsystemsettings -set-timezone ZONE, in the terminal of "Set Time Zone" */ int SetTimezoneCommand(const char *zone) { int r = mcde_sys_timezone_set(zone); if (r == MCDE_SYS_OK) printf("The time zone is %s now.\n", zone); else printf("The time zone was not changed: %s\n", mcde_sys_error()); printf("\nPress Return to close this window.\n"); fflush(stdout); (void) getchar(); return r == MCDE_SYS_OK ? 0 : 1; }