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