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