xref: /haiku/src/bin/dstcheck.cpp (revision e0ef64750f3169cd634bb2f7a001e22488b05231)
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 <LocaleRoster.h>
13 #include <MessageRunner.h>
14 #include <Roster.h>
15 #include <String.h>
16 #include <TextView.h>
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21 
22 
23 #undef B_TRANSLATE_CONTEXT
24 #define B_TRANSLATE_CONTEXT "dstcheck"
25 
26 
27 const uint32 TIMEDALERT_UPDATE = 'taup';
28 
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, 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 }
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 here;
88 	be_locale_roster->GetDefaultLocale(&here);
89 
90 	here.FormatTime(timestring, 15, t, false);
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, B_PATH_NAME_LENGTH) != B_OK) {
113 		fprintf(stderr, "%s: can't find settings directory\n", argv[0]);
114 		exit(1);
115 	}
116 
117 	strcat(path, "/time_dststatus");
118 	bool newFile = false;
119 	bool dst = false;
120 	int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
121 	if (fd < 0) {
122 		newFile = false;
123 		fd = open(path, O_RDWR);
124 		if (fd < 0) {
125 			perror("couldn't open dst status settings file");
126 			exit(1);
127 		}
128 
129 		char dst_byte;
130 		read(fd, &dst_byte, 1);
131 
132 		dst = dst_byte == '1';
133 	} else {
134 		dst = tm.tm_isdst;
135 	}
136 
137 	if (dst != tm.tm_isdst || argc > 1) {
138 		BApplication app("application/x-vnd.Haiku-cmd-dstconfig");
139 
140 		BString string;
141 		TimedAlert::GetLabel(string);
142 
143 		int32 index = (new TimedAlert("timedAlert", string.String(),
144 			B_TRANSLATE("Ask me later"), B_TRANSLATE("Yes"),
145 			B_TRANSLATE("No")))->Go();
146 		if (index == 0)
147 			exit(0);
148 
149 		if (index == 2) {
150 			index = (new BAlert("dstcheck",
151 				B_TRANSLATE("Would you like to set the clock?"),
152 				B_TRANSLATE("No"), B_TRANSLATE("Yes")))->Go();
153 
154 			if (index == 1)
155 				be_roster->Launch("application/x-vnd.Haiku-Time");
156 		}
157 	}
158 
159 	lseek(fd, 0, SEEK_SET);
160 	char dst_byte = tm.tm_isdst ? '1' : '0';
161 	write(fd, &dst_byte, 1);
162 	close(fd);
163 
164 	return 0;
165 }
166 
167