xref: /haiku/src/bin/dstcheck.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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. Currently, "
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 	BCountry* here;
88 	be_locale_roster->GetDefaultCountry(&here);
89 
90 	here->FormatTime(timestring, 15, t, false);
91 
92 	string += timestring;
93 
94 	string += B_TRANSLATE(".\n\nIs this the correct time?");
95 }
96 
97 
98 //	#pragma mark -
99 
100 
101 int
102 main(int argc, char **argv)
103 {
104 	time_t t;
105 	struct tm tm;
106 	tzset();
107 	time(&t);
108 	localtime_r(&t, &tm);
109 
110 	char path[B_PATH_NAME_LENGTH];
111 	if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path, B_PATH_NAME_LENGTH) != B_OK) {
112 		fprintf(stderr, "%s: can't find settings directory\n", argv[0]);
113 		exit(1);
114 	}
115 
116 	strcat(path, "/time_dststatus");
117 	bool newFile = false;
118 	bool dst = false;
119 	int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR);
120 	if (fd < 0) {
121 		newFile = false;
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("Yes"),
144 			B_TRANSLATE("No")))->Go();
145 		if (index == 0)
146 			exit(0);
147 
148 		if (index == 2) {
149 			index = (new BAlert("dstcheck",
150 				B_TRANSLATE("Would you like to set the clock using the Time and"
151 				"\nDate preference utility?"),
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