xref: /haiku/src/apps/expander/ExpanderPreferences.cpp (revision 93a78ecaa45114d68952d08c4778f073515102f2)
1 /*
2  * Copyright 2004-2006, Jérôme DUVAL. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "ExpanderPreferences.h"
7 #include <Box.h>
8 #include <Path.h>
9 #include <Screen.h>
10 #include <StringView.h>
11 
12 const uint32 MSG_OK			= 'mgOK';
13 const uint32 MSG_CANCEL		= 'mCan';
14 const uint32 MSG_LEAVEDEST = 'mLed';
15 const uint32 MSG_SAMEDIR = 'mSad';
16 const uint32 MSG_DESTUSE = 'mDeu';
17 const uint32 MSG_DESTTEXT = 'mDet';
18 const uint32 MSG_DESTSELECT = 'mDes';
19 
20 ExpanderPreferences::ExpanderPreferences(BMessage *settings)
21 	: BWindow(BRect(0, 0, 325, 305), "Expand-O-Matic", B_MODAL_WINDOW,
22 		B_NOT_CLOSABLE | B_NOT_RESIZABLE),
23 	fSettings(settings),
24 	fUsePanel(NULL)
25 {
26 
27 	BRect rect = Bounds();
28 	BBox *background = new BBox(rect, "background", B_FOLLOW_ALL,
29 		B_WILL_DRAW | B_FRAME_EVENTS, B_PLAIN_BORDER);
30 	AddChild(background);
31 
32 	rect.OffsetBy(11, 9);
33 	rect.bottom -= 64;
34 	rect.right -= 22;
35 	BBox *box = new BBox(rect, "background", B_FOLLOW_NONE,
36 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
37 	box->SetLabel("Expander Preferences");
38 	background->AddChild(box);
39 
40 	float maxWidth = box->Bounds().right;
41 
42 	BRect frameRect = box->Bounds();
43 	frameRect.OffsetBy(15, 23);
44 	frameRect.right = frameRect.left + 200;
45 	frameRect.bottom = frameRect.top + 20;
46 	BRect textRect(frameRect);
47 	textRect.OffsetTo(B_ORIGIN);
48 	textRect.InsetBy(1, 1);
49 	BStringView *stringView = new BStringView(frameRect, "expansion", "Expansion:",
50 		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
51 	stringView->ResizeToPreferred();
52 	if (stringView->Frame().right > maxWidth)
53 		maxWidth = stringView->Frame().right;
54 	box->AddChild(stringView);
55 
56 	frameRect.top = stringView->Frame().bottom + 5;
57 	frameRect.left += 10;
58 
59 	fAutoExpand = new BCheckBox(frameRect, "autoExpand", "Automatically expand files", NULL);
60 	fAutoExpand->ResizeToPreferred();
61 	if (fAutoExpand->Frame().right > maxWidth)
62 		maxWidth = fAutoExpand->Frame().right;
63 	box->AddChild(fAutoExpand);
64 
65 	frameRect = fAutoExpand->Frame();
66 	frameRect.top = fAutoExpand->Frame().bottom + 1;
67 	fCloseWindow = new BCheckBox(frameRect, "closeWindowWhenDone", "Close window when done expanding", NULL);
68 	fCloseWindow->ResizeToPreferred();
69 	if (fCloseWindow->Frame().right > maxWidth)
70 		maxWidth = fCloseWindow->Frame().right;
71 	box->AddChild(fCloseWindow);
72 
73 	frameRect = stringView->Frame();
74 	frameRect.top = fCloseWindow->Frame().bottom + 10;
75 	stringView = new BStringView(frameRect, "destinationFolder", "Destination Folder:",
76 		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
77 	stringView->ResizeToPreferred();
78 	if (stringView->Frame().right > maxWidth)
79 		maxWidth = stringView->Frame().right;
80 	box->AddChild(stringView);
81 
82 	frameRect.top = stringView->Frame().bottom + 5;
83 	frameRect.left += 10;
84 
85 	fLeaveDest = new BRadioButton(frameRect, "leaveDest", "Leave destination folder path empty",
86 		new BMessage(MSG_LEAVEDEST));
87 	fLeaveDest->ResizeToPreferred();
88 	if (fLeaveDest->Frame().right > maxWidth)
89 		maxWidth = fLeaveDest->Frame().right;
90 	box->AddChild(fLeaveDest);
91 
92 	frameRect = fLeaveDest->Frame();
93 	frameRect.top = fLeaveDest->Frame().bottom + 1;
94 	fSameDest = new BRadioButton(frameRect, "sameDir", "Same directory as source (archive) file",
95 		new BMessage(MSG_SAMEDIR));
96 	fSameDest->ResizeToPreferred();
97 	if (fSameDest->Frame().right > maxWidth)
98 		maxWidth = fSameDest->Frame().right;
99 	box->AddChild(fSameDest);
100 
101 	frameRect = fSameDest->Frame();
102 	frameRect.top = frameRect.bottom + 1;
103 	fDestUse = new BRadioButton(frameRect, "destUse", "Use:", new BMessage(MSG_DESTUSE));
104 	fDestUse->ResizeToPreferred();
105 	if (fDestUse->Frame().right > maxWidth)
106 		maxWidth = fDestUse->Frame().right;
107 	box->AddChild(fDestUse);
108 
109 	frameRect = fDestUse->Frame();
110 	frameRect.left  = fDestUse->Frame().right + 1;
111 	frameRect.right  = frameRect.left + 58;
112 	frameRect.bottom  = frameRect.top + 38;
113 
114 	fDestText = new BTextControl(frameRect, "destText", "", "", new BMessage(MSG_DESTTEXT));
115 	box->AddChild(fDestText);
116 	fDestText->ResizeToPreferred();
117 	fDestText->SetDivider(0);
118 	fDestText->TextView()->MakeEditable(false);
119 	fDestText->ResizeTo(158, fDestText->Frame().Height());
120 
121 	fDestText->SetEnabled(false);
122 
123 	frameRect = fDestText->Frame();
124 	frameRect.left = frameRect.right + 5;
125 	fSelect = new BButton(frameRect, "selectButton", "Select", new BMessage(MSG_DESTSELECT));
126 	fSelect->ResizeToPreferred();
127 	if (fSelect->Frame().right > maxWidth)
128 		maxWidth = fSelect->Frame().right;
129 	box->AddChild(fSelect);
130 	fSelect->SetEnabled(false);
131 
132 	fDestText->MoveBy(0, (fSelect->Frame().Height() - fDestText->Frame().Height()) / 2.0);
133 	fDestText->ResizeTo(158, fDestText->Frame().Height());
134 
135 	frameRect = stringView->Frame();
136 	frameRect.top = fDestUse->Frame().bottom + 10;
137 
138 	stringView = new BStringView(frameRect, "other", "Other:", B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
139 	stringView->ResizeToPreferred();
140 	if (stringView->Frame().right > maxWidth)
141 		maxWidth = stringView->Frame().right;
142 	box->AddChild(stringView);
143 
144 	frameRect.top = stringView->Frame().bottom + 5;
145 	frameRect.left += 10;
146 
147 	fOpenDest = new BCheckBox(frameRect, "openDestination", "Open destination folder after extraction", NULL);
148 	fOpenDest->ResizeToPreferred();
149 	if (fOpenDest->Frame().right > maxWidth)
150 		maxWidth = fOpenDest->Frame().right;
151 	box->AddChild(fOpenDest);
152 
153 	frameRect = fOpenDest->Frame();
154 	frameRect.top = frameRect.bottom + 1;
155 	fAutoShow = new BCheckBox(frameRect, "autoShow", "Automatically show contents listing", NULL);
156 	fAutoShow->ResizeToPreferred();
157 	if (fAutoShow->Frame().right > maxWidth)
158 		maxWidth = fAutoShow->Frame().right;
159 	box->AddChild(fAutoShow);
160 
161 	box->ResizeTo(maxWidth + 15, fAutoShow->Frame().bottom + 10);
162 
163 	rect = BRect(Bounds().right - 89, Bounds().bottom - 40, Bounds().right - 14, Bounds().bottom - 16);
164 
165 	rect = Bounds();
166 	BButton *button = new BButton(rect, "OKButton", "OK", new BMessage(MSG_OK));
167 	button->MakeDefault(true);
168 	button->ResizeToPreferred();
169 	button->MoveTo(box->Frame().right - button->Frame().Width(), box->Frame().bottom + 10);
170 	background->AddChild(button);
171 
172 	rect = button->Frame();
173 	BButton *cancel = new BButton(rect, "CancelButton", "Cancel", new BMessage(MSG_CANCEL));
174 	cancel->ResizeToPreferred();
175 	cancel->MoveBy(-cancel->Frame().Width() - 10, (button->Frame().Height() - cancel->Frame().Height()) / 2.0);
176 	background->AddChild(cancel);
177 
178 	ResizeTo(box->Frame().right + 11 , button->Frame().bottom + 11);
179 
180 	BScreen screen(this);
181 	MoveBy((screen.Frame().Width() - Bounds().Width()) / 2,
182 		(screen.Frame().Height() - Bounds().Height()) / 2);
183 
184 	bool automatically_expand_files;
185 	bool close_when_done;
186 	int8 destination_folder;
187 	entry_ref ref;
188 	bool open_destination_folder;
189 	bool show_contents_listing;
190 	if ((settings->FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
191 		&& automatically_expand_files)
192 		fAutoExpand->SetValue(B_CONTROL_ON);
193 
194 	if ((settings->FindBool("close_when_done", &close_when_done) == B_OK)
195 		&& close_when_done)
196 		fCloseWindow->SetValue(B_CONTROL_ON);
197 
198 	if (settings->FindInt8("destination_folder", &destination_folder) == B_OK) {
199 		switch (destination_folder) {
200 			case 0x63:
201 				fSameDest->SetValue(B_CONTROL_ON);
202 				break;
203 			case 0x65:
204 				fDestUse->SetValue(B_CONTROL_ON);
205 				fDestText->SetEnabled(true);
206 				fSelect->SetEnabled(true);
207 				break;
208 			case 0x66:
209 				fLeaveDest->SetValue(B_CONTROL_ON);
210 				break;
211 		}
212 	}
213 
214 	if (settings->FindRef("destination_folder_use", &fRef) == B_OK) {
215 		BEntry entry(&fRef);
216 		if (entry.Exists()) {
217 			BPath path(&entry);
218 			fDestText->SetText(path.Path());
219 		}
220 	}
221 
222 	if ((settings->FindBool("open_destination_folder", &open_destination_folder) == B_OK)
223 		&& open_destination_folder)
224 		fOpenDest->SetValue(B_CONTROL_ON);
225 
226 	if ((settings->FindBool("show_contents_listing", &show_contents_listing) == B_OK)
227 		&& show_contents_listing)
228 		fAutoShow->SetValue(B_CONTROL_ON);
229 }
230 
231 
232 ExpanderPreferences::~ExpanderPreferences()
233 {
234 }
235 
236 
237 void
238 ExpanderPreferences::MessageReceived(BMessage *msg)
239 {
240 	switch (msg->what) {
241 		case MSG_DESTSELECT:
242 			if (!fUsePanel)
243 				fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, new BMessenger(this), NULL,
244 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter(), true);
245 			fUsePanel->Show();
246 			break;
247 		case MSG_DIRECTORY:
248 		{
249 			entry_ref ref;
250 			fUsePanel->GetPanelDirectory(&ref);
251 			fRef = ref;
252 			BEntry entry(&ref);
253 			BPath path(&entry);
254 			fDestText->SetText(path.Path());
255 			fUsePanel->Hide();
256 		}
257 		break;
258 		case B_REFS_RECEIVED:
259 			if (msg->FindRef("refs", 0, &fRef) == B_OK) {
260 				BEntry entry(&fRef, true);
261 				BPath path(&entry);
262 				fDestText->SetText(path.Path());
263 			}
264 			break;
265 		case MSG_LEAVEDEST:
266 		case MSG_SAMEDIR:
267 			fDestText->SetEnabled(false);
268 			fSelect->SetEnabled(false);
269 			break;
270 		case MSG_DESTUSE:
271 			fDestText->SetEnabled(true);
272 			fSelect->SetEnabled(true);
273 			fDestText->TextView()->MakeEditable(false);
274 			break;
275 		case MSG_CANCEL:
276 			Hide();
277 			break;
278 		case MSG_OK:
279 			fSettings->ReplaceBool("automatically_expand_files", fAutoExpand->Value() == B_CONTROL_ON);
280 			fSettings->ReplaceBool("close_when_done", fCloseWindow->Value() == B_CONTROL_ON);
281 			fSettings->ReplaceInt8("destination_folder", (fSameDest->Value() == B_CONTROL_ON) ? 0x63
282 				: ((fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65));
283 			fSettings->ReplaceRef("destination_folder_use", &fRef);
284 			fSettings->ReplaceBool("open_destination_folder", fOpenDest->Value() == B_CONTROL_ON);
285 			fSettings->ReplaceBool("show_contents_listing", fAutoShow->Value() == B_CONTROL_ON);
286 			Hide();
287 			break;
288 		default:
289 			break;
290 	}
291 }
292