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