xref: /haiku/src/bin/dstcheck.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 /*
2  * Copyright 2004, Jérôme Duval, jerome.duval@free.fr.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
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 
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 
21 
22 #undef B_TRANSLATION_CONTEXT
23 #define B_TRANSLATION_CONTEXT "dstcheck"
24 
25 
26 const uint32 TIMEDALERT_UPDATE = 'taup';
27 
28 class TimedAlert : public BAlert {
29 public:
30 	TimedAlert(const char *title, const char *text, const char *button1,
31 		const char *button2 = NULL, const char *button3 = NULL,
32 		button_width width = B_WIDTH_AS_USUAL,
33 		alert_type type = B_INFO_ALERT);
34 	void MessageReceived(BMessage *);
35 	void Show();
36 
37 	static void GetLabel(BString &string);
38 
39 private:
40 	BMessageRunner *fRunner;
41 };
42 
43 
44 TimedAlert::TimedAlert(const char *title, const char *text, const char *button1,
45 		const char *button2, const char *button3,
46 		button_width width, alert_type type)
47 	: BAlert(title, text, button1, button2, button3, width, type),
48 	fRunner(NULL)
49 {
50 	SetShortcut(0, B_ESCAPE);
51 }
52 
53 
54 void
55 TimedAlert::Show()
56 {
57 	fRunner
58 		= new BMessageRunner(this, new BMessage(TIMEDALERT_UPDATE), 60000000);
59 	SetFeel(B_FLOATING_ALL_WINDOW_FEEL);
60 	BAlert::Show();
61 }
62 
63 
64 void
65 TimedAlert::MessageReceived(BMessage *msg)
66 {
67 	if (msg->what == TIMEDALERT_UPDATE) {
68 		BString string;
69 		GetLabel(string);
70 		TextView()->SetText(string.String());
71 	} else {
72 		BAlert::MessageReceived(msg);
73 	}
74 }
75 
76 
77 void
78 TimedAlert::GetLabel(BString &string)
79 {
80 	string = B_TRANSLATE("Attention!\n\nBecause of the switch from daylight "
81 		"saving time, your computer's clock may be an hour off.\n"
82 		"Your computer thinks it is");
83 
84 	time_t t;
85 	struct tm tm;
86 	char timestring[15];
87 	time(&t);
88 	localtime_r(&t, &tm);
89 
90 	BLocale::Default()->FormatTime(timestring, 15, t, B_SHORT_TIME_FORMAT);
91 
92 	string += " ";
93 	string += timestring;
94 
95 	string += B_TRANSLATE(".\n\nIs this the correct time?");
96 }
97 
98 
99 //	#pragma mark -
100 
101 
102 int
103 main(int argc, char **argv)
104 {
105 	time_t t;
106 	struct tm tm;
107 	tzset();
108 	time(&t);
109 	localtime_r(&t, &tm);
110 
111 	char path[B_PATH_NAME_LENGTH];
112 	if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path,
113 			B_PATH_NAME_LENGTH) != B_OK) {
114 		fprintf(stderr, "%s: can't find settings directory\n", argv[0]);
115 		exit(1);
116 	}
117 
118 	strcat(path, "/time_dststatus");
119 	bool dst = false;
120 	int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
121 	if (fd < 0) {
122 		fd = open(path, O_RDWR);
123 		if (fd < 0) {
124 			perror("couldn't open dst status settings file");
125 			exit(1);
126 		}
127 
128 		char dst_byte;
129 		read(fd, &dst_byte, 1);
130 
131 		dst = dst_byte == '1';
132 	} else {
133 		dst = tm.tm_isdst;
134 	}
135 
136 	if (dst != tm.tm_isdst || argc > 1) {
137 		BApplication app("application/x-vnd.Haiku-cmd-dstconfig");
138 
139 		BString string;
140 		TimedAlert::GetLabel(string);
141 
142 		int32 index = (new TimedAlert("timedAlert", string.String(),
143 			B_TRANSLATE("Ask me later"), B_TRANSLATE("Use this time"),
144 			B_TRANSLATE("Manually adjust time" B_UTF8_ELLIPSIS)))->Go();
145 		if (index == 0)
146 			exit(0);
147 
148 		if (index == 2)
149 			be_roster->Launch("application/x-vnd.Haiku-Time");
150 	}
151 
152 	lseek(fd, 0, SEEK_SET);
153 	char dst_byte = tm.tm_isdst ? '1' : '0';
154 	write(fd, &dst_byte, 1);
155 	close(fd);
156 
157 	return 0;
158 }
159