libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / bin / dstcheck.cpp
blob30281e33c424ae619e02de4cf06d481fb5c9b104
1 /*
2 * Copyright 2004, Jérôme Duval, jerome.duval@free.fr.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <Alert.h>
8 #include <Application.h>
9 #include <Catalog.h>
10 #include <FindDirectory.h>
11 #include <Locale.h>
12 #include <MessageRunner.h>
13 #include <Roster.h>
14 #include <String.h>
15 #include <TextView.h>
16 #include <TimeFormat.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
23 #undef B_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_CONTEXT "dstcheck"
27 const uint32 TIMEDALERT_UPDATE = 'taup';
29 class TimedAlert : public BAlert {
30 public:
31 TimedAlert(const char *title, const char *text, const char *button1,
32 const char *button2 = NULL, const char *button3 = NULL,
33 button_width width = B_WIDTH_AS_USUAL,
34 alert_type type = B_INFO_ALERT);
35 void MessageReceived(BMessage *);
36 void Show();
38 static void GetLabel(BString &string);
40 private:
41 BMessageRunner *fRunner;
45 TimedAlert::TimedAlert(const char *title, const char *text, const char *button1,
46 const char *button2, const char *button3,
47 button_width width, alert_type type)
48 : BAlert(title, text, button1, button2, button3, width, type),
49 fRunner(NULL)
51 SetShortcut(0, B_ESCAPE);
55 void
56 TimedAlert::Show()
58 fRunner
59 = new BMessageRunner(this, new BMessage(TIMEDALERT_UPDATE), 60000000);
60 SetFeel(B_FLOATING_ALL_WINDOW_FEEL);
61 BAlert::Show();
65 void
66 TimedAlert::MessageReceived(BMessage *msg)
68 if (msg->what == TIMEDALERT_UPDATE) {
69 BString string;
70 GetLabel(string);
71 TextView()->SetText(string.String());
72 } else {
73 BAlert::MessageReceived(msg);
78 void
79 TimedAlert::GetLabel(BString &string)
81 string = B_TRANSLATE("Attention!\n\nBecause of the switch from daylight "
82 "saving time, your computer's clock may be an hour off.\n"
83 "Your computer thinks it is %current time%.\n\nIs this the correct "
84 "time?");
86 time_t t;
87 struct tm tm;
88 char timestring[15];
89 time(&t);
90 localtime_r(&t, &tm);
92 BTimeFormat().Format(timestring, 15, t, B_SHORT_TIME_FORMAT);
94 string.ReplaceFirst("%current time%", timestring);
98 // #pragma mark -
102 main(int argc, char **argv)
104 time_t t;
105 struct tm tm;
106 tzset();
107 time(&t);
108 localtime_r(&t, &tm);
110 char path[B_PATH_NAME_LENGTH];
111 if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path,
112 B_PATH_NAME_LENGTH) != B_OK) {
113 fprintf(stderr, "%s: can't find settings directory\n", argv[0]);
114 exit(1);
117 strcat(path, "/time_dststatus");
118 bool dst = false;
119 int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
120 if (fd < 0) {
121 fd = open(path, O_RDWR);
122 if (fd < 0) {
123 perror("couldn't open dst status settings file");
124 exit(1);
127 char dst_byte;
128 read(fd, &dst_byte, 1);
130 dst = dst_byte == '1';
131 } else {
132 dst = tm.tm_isdst;
135 if (dst != tm.tm_isdst || argc > 1) {
136 BApplication app("application/x-vnd.Haiku-cmd-dstconfig");
138 BString string;
139 TimedAlert::GetLabel(string);
141 int32 index = (new TimedAlert("timedAlert", string.String(),
142 B_TRANSLATE("Keep this time"), B_TRANSLATE("Ask me later"),
143 B_TRANSLATE("Manually adjust time" B_UTF8_ELLIPSIS)))->Go();
144 if (index == 1)
145 exit(0);
147 if (index == 2)
148 be_roster->Launch("application/x-vnd.Haiku-Time");
151 lseek(fd, 0, SEEK_SET);
152 char dst_byte = tm.tm_isdst ? '1' : '0';
153 write(fd, &dst_byte, 1);
154 close(fd);
156 return 0;