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