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