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