xref: /haiku/src/apps/expander/ExpanderPreferences.cpp (revision 23d878482ed22e55dad6d1fca1df7bea42eb157c)
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 	CenterOnScreen();
144 
145 	_ReadSettings();
146 }
147 
148 
149 ExpanderPreferences::~ExpanderPreferences()
150 {
151 	if (fUsePanel && fUsePanel->RefFilter())
152 		delete fUsePanel->RefFilter();
153 
154 	delete fUsePanel;
155 }
156 
157 
158 void
159 ExpanderPreferences::_ReadSettings()
160 {
161 	bool automatically_expand_files;
162 	bool close_when_done;
163 	int8 destination_folder;
164 	entry_ref ref;
165 	bool open_destination_folder;
166 	bool show_contents_listing;
167 	if ((fSettings->FindBool("automatically_expand_files",
168 			&automatically_expand_files) == B_OK)
169 				&& automatically_expand_files)
170 		fAutoExpand->SetValue(B_CONTROL_ON);
171 
172 	if ((fSettings->FindBool("close_when_done", &close_when_done) == B_OK)
173 			&& close_when_done)
174 		fCloseWindow->SetValue(B_CONTROL_ON);
175 
176 	if (fSettings->FindInt8("destination_folder", &destination_folder) == B_OK) {
177 		switch (destination_folder) {
178 			case 0x63:
179 				fSameDest->SetValue(B_CONTROL_ON);
180 				break;
181 			case 0x65:
182 				fDestUse->SetValue(B_CONTROL_ON);
183 				fDestText->SetEnabled(true);
184 				fSelect->SetEnabled(true);
185 				break;
186 			case 0x66:
187 				fLeaveDest->SetValue(B_CONTROL_ON);
188 				break;
189 		}
190 	}
191 
192 	if (fSettings->FindRef("destination_folder_use", &fRef) == B_OK) {
193 		BEntry entry(&fRef);
194 		if (entry.Exists()) {
195 			BPath path(&entry);
196 			fDestText->SetText(path.Path());
197 		}
198 	}
199 
200 	if ((fSettings->FindBool("open_destination_folder",
201 			&open_destination_folder) == B_OK)
202 				&& open_destination_folder)
203 		fOpenDest->SetValue(B_CONTROL_ON);
204 
205 	if ((fSettings->FindBool("show_contents_listing",
206 			&show_contents_listing) == B_OK)
207 				&& show_contents_listing)
208 		fAutoShow->SetValue(B_CONTROL_ON);
209 }
210 
211 
212 void
213 ExpanderPreferences::_WriteSettings()
214 {
215 	fSettings->ReplaceBool("automatically_expand_files",
216 		fAutoExpand->Value() == B_CONTROL_ON);
217 	fSettings->ReplaceBool("close_when_done",
218 		fCloseWindow->Value() == B_CONTROL_ON);
219 	fSettings->ReplaceInt8("destination_folder",
220 		(fSameDest->Value() == B_CONTROL_ON) ? 0x63
221 		: ((fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65));
222 	fSettings->ReplaceRef("destination_folder_use", &fRef);
223 	fSettings->ReplaceBool("open_destination_folder",
224 		fOpenDest->Value() == B_CONTROL_ON);
225 	fSettings->ReplaceBool("show_contents_listing",
226 		fAutoShow->Value() == B_CONTROL_ON);
227 }
228 
229 
230 void
231 ExpanderPreferences::MessageReceived(BMessage* msg)
232 {
233 	switch (msg->what) {
234 		case MSG_DESTSELECT:
235 		{
236 			if (!fUsePanel) {
237 				BMessenger messenger(this);
238 				fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, &messenger,
239 					NULL, B_DIRECTORY_NODE, false, NULL,
240 					new DirectoryRefFilter(), true);
241 			}
242 			fUsePanel->Show();
243 			break;
244 		}
245 		case MSG_DIRECTORY:
246 		{
247 			entry_ref ref;
248 			fUsePanel->GetPanelDirectory(&ref);
249 			fRef = ref;
250 			BEntry entry(&ref);
251 			BPath path(&entry);
252 			fDestText->SetText(path.Path());
253 			fUsePanel->Hide();
254 			break;
255 		}
256 		case B_REFS_RECEIVED:
257 		{
258 			if (msg->FindRef("refs", 0, &fRef) == B_OK) {
259 				BEntry entry(&fRef, true);
260 				BPath path(&entry);
261 				fDestText->SetText(path.Path());
262 			}
263 			break;
264 		}
265 		case MSG_LEAVEDEST:
266 		case MSG_SAMEDIR:
267 		{
268 			fDestText->SetEnabled(false);
269 			fSelect->SetEnabled(false);
270 			break;
271 		}
272 		case MSG_DESTUSE:
273 		{
274 			fDestText->SetEnabled(true);
275 			fSelect->SetEnabled(true);
276 			fDestText->TextView()->MakeEditable(false);
277 			break;
278 		}
279 		case B_KEY_DOWN:
280 		{
281 			int32 index;
282 			if (msg->FindInt32("key", &index) == B_OK && index != 1)
283 				break;
284 			// Falling through on ESC
285 		}
286 		case MSG_CANCEL:
287 		{
288 			_ReadSettings();
289 			Hide();
290 			break;
291 		}
292 		case MSG_OK:
293 		{
294 			_WriteSettings();
295 			Hide();
296 			break;
297 		}
298 		default:
299 			break;
300 	}
301 }
302