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 char path[B_PATH_NAME_LENGTH]; 111 if (find_directory(B_USER_SETTINGS_DIRECTORY, -1, true, path, 112 B_PATH_NAME_LENGTH) != B_OK) { 113 fprintf(stderr, "%s: can't find settings directory\n", argv[0]); 114 exit(1); 115 } 116 117 strcat(path, "/time_dststatus"); 118 bool dst = false; 119 int fd = open(path, O_RDWR | O_EXCL | O_CREAT, S_IRUSR | S_IWUSR); 120 if (fd < 0) { 121 fd = open(path, O_RDWR); 122 if (fd < 0) { 123 perror("couldn't open dst status settings file"); 124 exit(1); 125 } 126 127 char dst_byte; 128 read(fd, &dst_byte, 1); 129 130 dst = dst_byte == '1'; 131 } else { 132 dst = tm.tm_isdst; 133 } 134 135 if (dst != tm.tm_isdst || argc > 1) { 136 BApplication app("application/x-vnd.Haiku-cmd-dstconfig"); 137 138 BString string; 139 TimedAlert::GetLabel(string); 140 141 int32 index = (new TimedAlert("timedAlert", string.String(), 142 B_TRANSLATE("Keep this time"), B_TRANSLATE("Ask me later"), 143 B_TRANSLATE("Manually adjust time" B_UTF8_ELLIPSIS)))->Go(); 144 if (index == 1) 145 exit(0); 146 147 if (index == 2) 148 be_roster->Launch("application/x-vnd.Haiku-Time"); 149 } 150 151 lseek(fd, 0, SEEK_SET); 152 char dst_byte = tm.tm_isdst ? '1' : '0'; 153 write(fd, &dst_byte, 1); 154 close(fd); 155 156 return 0; 157 } 158