1 /* 2 * Copyright 2010-2014, Haiku, Inc. All Rights Reserved. 3 * Copyright 2009, Pier Luigi Fiorini. 4 * Distributed under the terms of the MIT License. 5 * 6 * Authors: 7 * Pier Luigi Fiorini, pierluigi.fiorini@gmail.com 8 */ 9 10 #include "PrefletWin.h" 11 12 #include <Alert.h> 13 #include <Application.h> 14 #include <Button.h> 15 #include <Catalog.h> 16 #include <FindDirectory.h> 17 #include <LayoutBuilder.h> 18 #include <Path.h> 19 #include <SeparatorView.h> 20 21 #include <notification/Notifications.h> 22 23 #include "PrefletView.h" 24 25 26 #undef B_TRANSLATION_CONTEXT 27 #define B_TRANSLATION_CONTEXT "PrefletWin" 28 29 30 const int32 kRevert = '_RVT'; 31 const int32 kApply = '_APY'; 32 33 34 PrefletWin::PrefletWin() 35 : 36 BWindow(BRect(0, 0, 500, 400), B_TRANSLATE_SYSTEM_NAME("Notifications"), 37 B_TITLED_WINDOW, B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS 38 | B_AUTO_UPDATE_SIZE_LIMITS) 39 { 40 // Preflet container view 41 fMainView = new PrefletView(this); 42 fMainView->SetBorder(B_NO_BORDER); 43 fMainView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 44 45 // Apply and revert buttons 46 fRevert = new BButton("revert", B_TRANSLATE("Revert"), 47 new BMessage(kRevert)); 48 fRevert->SetEnabled(false); 49 fApply = new BButton("apply", B_TRANSLATE("Apply"), new BMessage(kApply)); 50 fApply->SetEnabled(false); 51 52 // Build the layout 53 BLayoutBuilder::Group<>(this, B_VERTICAL, 0) 54 .SetInsets(0, B_USE_DEFAULT_SPACING, 0, 0) 55 .Add(fMainView) 56 .Add(new BSeparatorView(B_HORIZONTAL)) 57 .AddGroup(B_HORIZONTAL) 58 .Add(fRevert) 59 .AddGlue() 60 .Add(fApply) 61 .SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING, 62 B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING); 63 64 ReloadSettings(); 65 66 // Center this window on screen and show it 67 CenterOnScreen(); 68 Show(); 69 } 70 71 72 void 73 PrefletWin::MessageReceived(BMessage* msg) 74 { 75 switch (msg->what) { 76 case kApply: 77 { 78 BPath path; 79 80 status_t ret = B_OK; 81 ret = find_directory(B_USER_SETTINGS_DIRECTORY, &path); 82 if (ret != B_OK) 83 return; 84 85 path.Append(kSettingsFile); 86 87 BMessage settingsStore; 88 for (int32 i = 0; i < fMainView->CountPages(); i++) { 89 SettingsPane* pane = 90 dynamic_cast<SettingsPane*>(fMainView->PageAt(i)); 91 if (pane) { 92 if (pane->Save(settingsStore) == B_OK) { 93 fApply->SetEnabled(false); 94 fRevert->SetEnabled(true); 95 } else 96 break; 97 } 98 } 99 100 // Save settings file 101 BFile file(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); 102 ret = settingsStore.Flatten(&file); 103 if (ret != B_OK) { 104 BAlert* alert = new BAlert("", 105 B_TRANSLATE("An error occurred saving the preferences.\n" 106 "It's possible you are running out of disk space."), 107 B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, 108 B_STOP_ALERT); 109 alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 110 (void)alert->Go(); 111 } 112 113 break; 114 } 115 case kRevert: 116 for (int32 i = 0; i < fMainView->CountPages(); i++) { 117 SettingsPane* pane = 118 dynamic_cast<SettingsPane*>(fMainView->PageAt(i)); 119 if (pane) { 120 if (pane->Revert() == B_OK) 121 fRevert->SetEnabled(false); 122 } 123 } 124 break; 125 default: 126 BWindow::MessageReceived(msg); 127 } 128 } 129 130 131 bool 132 PrefletWin::QuitRequested() 133 { 134 be_app_messenger.SendMessage(B_QUIT_REQUESTED); 135 return true; 136 } 137 138 139 void 140 PrefletWin::SettingChanged() 141 { 142 fApply->SetEnabled(true); 143 } 144 145 146 void 147 PrefletWin::ReloadSettings() 148 { 149 BPath path; 150 151 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK) 152 return; 153 154 // FIXME don't load this again here, share with other tabs! 155 path.Append(kSettingsFile); 156 157 BMessage settings; 158 BFile file(path.Path(), B_READ_ONLY); 159 settings.Unflatten(&file); 160 161 for (int32 i = 0; i < fMainView->CountPages(); i++) { 162 SettingsPane* pane = 163 dynamic_cast<SettingsPane*>(fMainView->PageAt(i)); 164 if (pane) 165 pane->Load(settings); 166 } 167 } 168