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