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 <Catalog.h> 10 #include <FindDirectory.h> 11 #include <Locale.h> 12 #include <MessageRunner.h> 13 #include <Roster.h> 14 #include <String.h> 15 #include <TextView.h> 16 #include <TimeFormat.h> 17 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 22 23 #undef B_TRANSLATION_CONTEXT 24 #define B_TRANSLATION_CONTEXT "dstcheck" 25 26 27 const uint32 TIMEDALERT_UPDATE = 'taup'; 28 29 class TimedAlert : public BAlert { 30 public: 31 TimedAlert(const char *title, const char *text, const char *button1, 32 const char *button2 = NULL, const char *button3 = NULL, 33 button_width width = B_WIDTH_AS_USUAL, 34 alert_type type = B_INFO_ALERT); 35 void MessageReceived(BMessage *); 36 void Show(); 37 38 static void GetLabel(BString &string); 39 40 private: 41 BMessageRunner *fRunner; 42 }; 43 44 45 TimedAlert::TimedAlert(const char *title, const char *text, const char *button1, 46 const char *button2, const char *button3, 47 button_width width, alert_type type) 48 : BAlert(title, text, button1, button2, button3, width, type), 49 fRunner(NULL) 50 { 51 SetShortcut(0, B_ESCAPE); 52 } 53 54 55 void 56 TimedAlert::Show() 57 { 58 fRunner 59 = new BMessageRunner(this, new BMessage(TIMEDALERT_UPDATE), 60000000); 60 SetFeel(B_FLOATING_ALL_WINDOW_FEEL); 61 BAlert::Show(); 62 } 63 64 65 void 66 TimedAlert::MessageReceived(BMessage *msg) 67 { 68 if (msg->what == TIMEDALERT_UPDATE) { 69 BString string; 70 GetLabel(string); 71 TextView()->SetText(string.String()); 72 } else { 73 BAlert::MessageReceived(msg); 74 } 75 } 76 77 78 void 79 TimedAlert::GetLabel(BString &string) 80 { 81 string = B_TRANSLATE("Attention!\n\nBecause of the switch from daylight " 82 "saving time, your computer's clock may be an hour off.\n" 83 "Your computer thinks it is %current time%.\n\nIs this the correct " 84 "time?"); 85 86 time_t t; 87 struct tm tm; 88 char timestring[15]; 89 time(&t); 90 localtime_r(&t, &tm); 91 92 BTimeFormat().Format(timestring, 15, t, B_SHORT_TIME_FORMAT); 93 94 string.ReplaceFirst("%current time%", timestring); 95 } 96 97 98 // #pragma mark - 99 100 101 int 102 main(int argc, char **argv) 103 { 104 time_t t; 105 struct tm tm; 106 tzset(); 107 time(&t); 108 localtime_r(&t, &tm); 109 110 if (tm.tm_year < (2020 - 1900)) { 111 fprintf(stderr, "%s: not checking because clock is not set.\n", 112 argv[0]); 113 exit(1); 114 } 115 116 char path[B_PATH_NAME_LENGTH]; 117 if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path, 118 B_PATH_NAME_LENGTH) != B_OK) { 119 fprintf(stderr, "%s: can't find settings directory\n", argv[0]); 120 exit(1); 121 } 122 123 strcat(path, "/time_dststatus"); 124 bool dst = false; 125 int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR); 126 if (fd < 0) { 127 fd = open(path, O_RDWR); 128 if (fd < 0) { 129 perror("couldn't open dst status settings file"); 130 exit(1); 131 } 132 133 char dst_byte; 134 read(fd, &dst_byte, 1); 135 136 dst = dst_byte == '1'; 137 } else { 138 dst = tm.tm_isdst; 139 } 140 141 if (dst != tm.tm_isdst || argc > 1) { 142 BApplication app("application/x-vnd.Haiku-cmd-dstconfig"); 143 144 BString string; 145 TimedAlert::GetLabel(string); 146 147 int32 index = (new TimedAlert("timedAlert", string.String(), 148 B_TRANSLATE("Keep this time"), B_TRANSLATE("Ask me later"), 149 B_TRANSLATE("Manually adjust time" B_UTF8_ELLIPSIS)))->Go(); 150 if (index == 1) 151 exit(0); 152 153 if (index == 2) 154 be_roster->Launch("application/x-vnd.Haiku-Time"); 155 } 156 157 lseek(fd, 0, SEEK_SET); 158 char dst_byte = tm.tm_isdst ? '1' : '0'; 159 write(fd, &dst_byte, 1); 160 close(fd); 161 162 return 0; 163 } 164