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