1 /* 2 * Copyright 2004-2012, Haiku. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Jérôme Duval 7 * Humdinger <humdingerb@gmail.com> 8 */ 9 10 #include <Box.h> 11 #include <Catalog.h> 12 #include <ControlLook.h> 13 #include <LayoutBuilder.h> 14 #include <Locale.h> 15 #include <Path.h> 16 #include <Screen.h> 17 #include <StringView.h> 18 19 #include "ExpanderPreferences.h" 20 21 const uint32 MSG_OK = 'mgOK'; 22 const uint32 MSG_CANCEL = 'mCan'; 23 const uint32 MSG_LEAVEDEST = 'mLed'; 24 const uint32 MSG_SAMEDIR = 'mSad'; 25 const uint32 MSG_DESTUSE = 'mDeu'; 26 const uint32 MSG_DESTTEXT = 'mDet'; 27 const uint32 MSG_DESTSELECT = 'mDes'; 28 29 #undef B_TRANSLATION_CONTEXT 30 #define B_TRANSLATION_CONTEXT "ExpanderPreferences" 31 32 33 ExpanderPreferences::ExpanderPreferences(BMessage* settings) 34 : 35 BWindow(BRect(0, 0, 325, 305), B_TRANSLATE("Expander settings"), 36 B_FLOATING_WINDOW_LOOK, B_FLOATING_APP_WINDOW_FEEL, B_NOT_RESIZABLE 37 | B_NOT_CLOSABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), 38 fSettings(settings), 39 fUsePanel(NULL) 40 { 41 const float kSpacing = be_control_look->DefaultItemSpacing(); 42 43 BBox* settingsBox = new BBox(B_PLAIN_BORDER, NULL); 44 BGroupLayout* settingsLayout = new BGroupLayout(B_VERTICAL, kSpacing / 2); 45 settingsBox->SetLayout(settingsLayout); 46 BBox* buttonBox = new BBox(B_PLAIN_BORDER, NULL); 47 BGroupLayout* buttonLayout = new BGroupLayout(B_HORIZONTAL, kSpacing / 2); 48 buttonBox->SetLayout(buttonLayout); 49 50 BStringView* expansionLabel = new BStringView("stringViewExpansion", 51 B_TRANSLATE("Expansion")); 52 expansionLabel->SetFont(be_bold_font); 53 BStringView* destinationLabel = new BStringView("stringViewDestination", 54 B_TRANSLATE("Destination folder")); 55 destinationLabel->SetFont(be_bold_font); 56 BStringView* otherLabel = new BStringView("stringViewOther", 57 B_TRANSLATE("Other")); 58 otherLabel->SetFont(be_bold_font); 59 60 fAutoExpand = new BCheckBox("autoExpand", 61 B_TRANSLATE("Automatically expand files"), NULL); 62 fCloseWindow = new BCheckBox("closeWindowWhenDone", 63 B_TRANSLATE("Close window when done expanding"), NULL); 64 65 fLeaveDest = new BRadioButton("leaveDest", 66 B_TRANSLATE("Leave destination folder path empty"), 67 new BMessage(MSG_LEAVEDEST)); 68 fSameDest = new BRadioButton("sameDir", 69 B_TRANSLATE("Same directory as source (archive) file"), 70 new BMessage(MSG_SAMEDIR)); 71 fDestUse = new BRadioButton("destUse", 72 B_TRANSLATE("Use:"), new BMessage(MSG_DESTUSE)); 73 fDestText = new BTextControl("destText", "", "", 74 new BMessage(MSG_DESTTEXT)); 75 fDestText->SetDivider(0); 76 fDestText->TextView()->MakeEditable(false); 77 fDestText->SetEnabled(false); 78 fSelect = new BButton("selectButton", B_TRANSLATE("Select"), 79 new BMessage(MSG_DESTSELECT)); 80 fSelect->SetEnabled(false); 81 82 fOpenDest = new BCheckBox("openDestination", 83 B_TRANSLATE("Open destination folder after extraction"), NULL); 84 fAutoShow = new BCheckBox("autoShow", 85 B_TRANSLATE("Automatically show contents listing"), NULL); 86 87 BButton* okbutton = new BButton("OKButton", B_TRANSLATE("OK"), 88 new BMessage(MSG_OK)); 89 okbutton->MakeDefault(true); 90 BButton* cancel = new BButton("CancelButton", B_TRANSLATE("Cancel"), 91 new BMessage(MSG_CANCEL)); 92 93 // Build the layout 94 BLayoutBuilder::Group<>(this, B_VERTICAL, 0) 95 .AddGroup(settingsLayout) 96 .AddGroup(B_HORIZONTAL) 97 .Add(expansionLabel) 98 .AddGlue() 99 .End() 100 .AddGroup(B_VERTICAL, 0) 101 .Add(fAutoExpand) 102 .Add(fCloseWindow) 103 .SetInsets(kSpacing, 0, 0, 0) 104 .End() 105 .AddGroup(B_HORIZONTAL, 0) 106 .Add(destinationLabel) 107 .AddGlue() 108 .SetInsets(0, kSpacing, 0, 0) 109 .End() 110 .AddGroup(B_VERTICAL, 0) 111 .Add(fLeaveDest) 112 .Add(fSameDest) 113 .Add(fDestUse) 114 .AddGroup(B_HORIZONTAL, 0) 115 .Add(fDestText, 0.8) 116 .AddStrut(be_control_look->DefaultLabelSpacing()) 117 .Add(fSelect, 0.2) 118 .SetInsets(kSpacing * 2, 0, kSpacing / 2, 0) 119 .End() 120 .SetInsets(kSpacing, 0, 0, 0) 121 .End() 122 .AddGroup(B_HORIZONTAL, 0) 123 .Add(otherLabel) 124 .AddGlue() 125 .SetInsets(0, kSpacing / 2, 0, 0) 126 .End() 127 .AddGroup(B_VERTICAL, 0) 128 .Add(fOpenDest) 129 .Add(fAutoShow) 130 .SetInsets(kSpacing, 0, 0, 0) 131 .End() 132 .SetInsets(kSpacing, kSpacing, kSpacing, kSpacing) 133 .End() 134 .AddGroup(buttonLayout) 135 .AddGroup(B_HORIZONTAL, kSpacing) 136 .AddGlue() 137 .Add(cancel) 138 .Add(okbutton) 139 .End() 140 .SetInsets(kSpacing, kSpacing, kSpacing, kSpacing) 141 .End(); 142 143 fDestText->SetExplicitAlignment( 144 BAlignment(B_ALIGN_HORIZONTAL_UNSET, B_ALIGN_VERTICAL_CENTER)); 145 146 CenterOnScreen(); 147 148 _ReadSettings(); 149 } 150 151 152 ExpanderPreferences::~ExpanderPreferences() 153 { 154 if (fUsePanel && fUsePanel->RefFilter()) 155 delete fUsePanel->RefFilter(); 156 157 delete fUsePanel; 158 } 159 160 161 void 162 ExpanderPreferences::_ReadSettings() 163 { 164 bool automatically_expand_files; 165 bool close_when_done; 166 int8 destination_folder; 167 entry_ref ref; 168 bool open_destination_folder; 169 bool show_contents_listing; 170 if ((fSettings->FindBool("automatically_expand_files", 171 &automatically_expand_files) == B_OK) 172 && automatically_expand_files) 173 fAutoExpand->SetValue(B_CONTROL_ON); 174 175 if ((fSettings->FindBool("close_when_done", &close_when_done) == B_OK) 176 && close_when_done) 177 fCloseWindow->SetValue(B_CONTROL_ON); 178 179 if (fSettings->FindInt8("destination_folder", &destination_folder) == B_OK) { 180 switch (destination_folder) { 181 case 0x63: 182 fSameDest->SetValue(B_CONTROL_ON); 183 break; 184 case 0x65: 185 fDestUse->SetValue(B_CONTROL_ON); 186 fDestText->SetEnabled(true); 187 fSelect->SetEnabled(true); 188 break; 189 case 0x66: 190 fLeaveDest->SetValue(B_CONTROL_ON); 191 break; 192 } 193 } 194 195 if (fSettings->FindRef("destination_folder_use", &fRef) == B_OK) { 196 BEntry entry(&fRef); 197 if (entry.Exists()) { 198 BPath path(&entry); 199 fDestText->SetText(path.Path()); 200 } 201 } 202 203 if ((fSettings->FindBool("open_destination_folder", 204 &open_destination_folder) == B_OK) 205 && open_destination_folder) 206 fOpenDest->SetValue(B_CONTROL_ON); 207 208 if ((fSettings->FindBool("show_contents_listing", 209 &show_contents_listing) == B_OK) 210 && show_contents_listing) 211 fAutoShow->SetValue(B_CONTROL_ON); 212 } 213 214 215 void 216 ExpanderPreferences::_WriteSettings() 217 { 218 fSettings->ReplaceBool("automatically_expand_files", 219 fAutoExpand->Value() == B_CONTROL_ON); 220 fSettings->ReplaceBool("close_when_done", 221 fCloseWindow->Value() == B_CONTROL_ON); 222 fSettings->ReplaceInt8("destination_folder", 223 (fSameDest->Value() == B_CONTROL_ON) ? 0x63 224 : ((fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65)); 225 fSettings->ReplaceRef("destination_folder_use", &fRef); 226 fSettings->ReplaceBool("open_destination_folder", 227 fOpenDest->Value() == B_CONTROL_ON); 228 fSettings->ReplaceBool("show_contents_listing", 229 fAutoShow->Value() == B_CONTROL_ON); 230 } 231 232 233 void 234 ExpanderPreferences::MessageReceived(BMessage* msg) 235 { 236 switch (msg->what) { 237 case MSG_DESTSELECT: 238 { 239 if (!fUsePanel) { 240 BMessenger messenger(this); 241 fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, &messenger, 242 NULL, B_DIRECTORY_NODE, false, NULL, 243 new DirectoryRefFilter(), true); 244 } 245 fUsePanel->Show(); 246 break; 247 } 248 case MSG_DIRECTORY: 249 { 250 entry_ref ref; 251 fUsePanel->GetPanelDirectory(&ref); 252 fRef = ref; 253 BEntry entry(&ref); 254 BPath path(&entry); 255 fDestText->SetText(path.Path()); 256 fUsePanel->Hide(); 257 break; 258 } 259 case B_REFS_RECEIVED: 260 { 261 if (msg->FindRef("refs", 0, &fRef) == B_OK) { 262 BEntry entry(&fRef, true); 263 BPath path(&entry); 264 fDestText->SetText(path.Path()); 265 } 266 break; 267 } 268 case MSG_LEAVEDEST: 269 case MSG_SAMEDIR: 270 { 271 fDestText->SetEnabled(false); 272 fSelect->SetEnabled(false); 273 break; 274 } 275 case MSG_DESTUSE: 276 { 277 fDestText->SetEnabled(true); 278 fSelect->SetEnabled(true); 279 fDestText->TextView()->MakeEditable(false); 280 break; 281 } 282 case B_KEY_DOWN: 283 { 284 int32 index; 285 if (msg->FindInt32("key", &index) == B_OK && index != 1) 286 break; 287 // Falling through on ESC 288 } 289 case MSG_CANCEL: 290 { 291 _ReadSettings(); 292 Hide(); 293 break; 294 } 295 case MSG_OK: 296 { 297 _WriteSettings(); 298 Hide(); 299 break; 300 } 301 default: 302 break; 303 } 304 } 305