xref: /haiku/src/apps/expander/ExpanderPreferences.cpp (revision 0e50eab75e25d0d82090e22dbff766dfaa6f5e86)
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_ALL,
36 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
37 	box->SetLabel("Expander Preferences");
38 	background->AddChild(box);
39 
40 
41 	BRect frameRect = box->Bounds();
42 	frameRect.OffsetBy(15,23);
43 	frameRect.right = frameRect.left + 200;
44 	frameRect.bottom = frameRect.top + 20;
45 	BRect textRect(frameRect);
46 	textRect.OffsetTo(B_ORIGIN);
47 	textRect.InsetBy(1,1);
48 	BStringView *stringView = new BStringView(frameRect, "expansion", "Expansion:",
49 		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
50 	box->AddChild(stringView);
51 
52 	frameRect.OffsetBy(0, 60);
53 	stringView = new BStringView(frameRect, "destinationFolder", "Destination Folder:",
54 		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
55 	box->AddChild(stringView);
56 
57 	frameRect.OffsetBy(0, 90);
58 	stringView = new BStringView(frameRect, "other", "Other:",
59 		B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
60 	box->AddChild(stringView);
61 
62 	rect = box->Bounds();
63 	rect.OffsetBy(25, 42);
64 	rect.bottom = rect.top + 20;
65 	rect.right = rect.right - 30;
66 	fAutoExpand = new BCheckBox(rect, "autoExpand", "Automatically expand files", NULL);
67 	box->AddChild(fAutoExpand);
68 
69 	rect.OffsetBy(0, 17);
70 	fCloseWindow = new BCheckBox(rect, "closeWindowWhenDone", "Close window when done expanding", NULL);
71 	box->AddChild(fCloseWindow);
72 
73 	rect.OffsetBy(0, 44);
74 	fLeaveDest = new BRadioButton(rect, "leaveDest", "Leave destination folder path empty",
75 		new BMessage(MSG_LEAVEDEST));
76 	box->AddChild(fLeaveDest);
77 
78 	rect.OffsetBy(0, 20);
79 	fSameDest = new BRadioButton(rect, "sameDir", "Same directory as source (archive) file",
80 		new BMessage(MSG_SAMEDIR));
81 	box->AddChild(fSameDest);
82 
83 	rect.OffsetBy(0, 20);
84 	BRect useRect = rect;
85 	useRect.right = useRect.left + 50;
86 	fDestUse = new BRadioButton(useRect, "destUse", "Use:",
87 		new BMessage(MSG_DESTUSE));
88 	box->AddChild(fDestUse);
89 
90 	textRect = rect;
91 	textRect.OffsetBy(fDestUse->StringWidth("Use:") + 20, 0);
92 	textRect.right = textRect.left + 158;
93 	fDestText = new BTextControl(textRect, "destText", "", "", new BMessage(MSG_DESTTEXT));
94 	fDestText->SetDivider(0);
95 	fDestText->TextView()->MakeEditable(false);
96 	box->AddChild(fDestText);
97 	fDestText->SetEnabled(false);
98 
99 	textRect.OffsetBy(168, -4);
100 	textRect.right = textRect.left + 50;
101 	fSelect = new BButton(textRect, "selectButton", "Select", new BMessage(MSG_DESTSELECT));
102 	box->AddChild(fSelect);
103 	fSelect->SetEnabled(false);
104 
105 	rect.OffsetBy(0, 50);
106 	fOpenDest = new BCheckBox(rect, "openDestination", "Open destination folder after extraction", NULL);
107 	box->AddChild(fOpenDest);
108 
109 	rect.OffsetBy(0, 17);
110 	fAutoShow = new BCheckBox(rect, "autoShow", "Automatically show contents listing", NULL);
111 	box->AddChild(fAutoShow);
112 
113 	rect = BRect(Bounds().right-89, Bounds().bottom-40,
114 		Bounds().right-14, Bounds().bottom-16);
115 	BButton *button = new BButton(rect, "OKButton", "OK",
116 		new BMessage(MSG_OK));
117 	background->AddChild(button);
118 	button->MakeDefault(true);
119 
120 	rect.OffsetBy(-89, 0);
121 	button = new BButton(rect, "CancelButton", "Cancel",
122 		new BMessage(MSG_CANCEL));
123 	background->AddChild(button);
124 
125 	BScreen screen(this);
126 	MoveBy((screen.Frame().Width()-Bounds().Width())/2,
127 		(screen.Frame().Height()-Bounds().Height())/2);
128 
129 	bool automatically_expand_files;
130 	bool close_when_done;
131 	int8 destination_folder;
132 	entry_ref ref;
133 	bool open_destination_folder;
134 	bool show_contents_listing;
135 	if ((settings->FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
136 		&& automatically_expand_files)
137 		fAutoExpand->SetValue(B_CONTROL_ON);
138 
139 	if ((settings->FindBool("close_when_done", &close_when_done) == B_OK)
140 		&& close_when_done)
141 		fCloseWindow->SetValue(B_CONTROL_ON);
142 
143 	if (settings->FindInt8("destination_folder", &destination_folder) == B_OK) {
144 		switch (destination_folder) {
145 			case 0x63:
146 				fSameDest->SetValue(B_CONTROL_ON);
147 				break;
148 			case 0x65:
149 				fDestUse->SetValue(B_CONTROL_ON);
150 				fDestText->SetEnabled(true);
151 				fSelect->SetEnabled(true);
152 				break;
153 			case 0x66:
154 				fLeaveDest->SetValue(B_CONTROL_ON);
155 				break;
156 		}
157 	}
158 
159 	if (settings->FindRef("destination_folder_use", &fRef) == B_OK) {
160 		BEntry entry(&fRef);
161 		if (entry.Exists()) {
162 			BPath path(&entry);
163 			fDestText->SetText(path.Path());
164 		}
165 	}
166 
167 	if ((settings->FindBool("open_destination_folder", &open_destination_folder) == B_OK)
168 		&& open_destination_folder)
169 		fOpenDest->SetValue(B_CONTROL_ON);
170 
171 	if ((settings->FindBool("show_contents_listing", &show_contents_listing) == B_OK)
172 		&& show_contents_listing)
173 		fAutoShow->SetValue(B_CONTROL_ON);
174 }
175 
176 
177 ExpanderPreferences::~ExpanderPreferences()
178 {
179 }
180 
181 
182 void
183 ExpanderPreferences::MessageReceived(BMessage *msg)
184 {
185 	switch (msg->what) {
186 		case MSG_DESTSELECT:
187 			if (!fUsePanel)
188 				fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, new BMessenger(this), NULL,
189 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter(), true);
190 			fUsePanel->Show();
191 			break;
192 		case MSG_DIRECTORY:
193 			{
194 				entry_ref ref;
195 				fUsePanel->GetPanelDirectory(&ref);
196 				fRef = ref;
197 				BEntry entry(&ref);
198 				BPath path(&entry);
199 				fDestText->SetText(path.Path());
200 				fUsePanel->Hide();
201 			}
202 			break;
203 		case B_REFS_RECEIVED:
204 			if (msg->FindRef("refs", 0, &fRef) == B_OK) {
205 				BEntry entry(&fRef, true);
206 				BPath path(&entry);
207 				fDestText->SetText(path.Path());
208 			}
209 			break;
210 		case MSG_LEAVEDEST:
211 		case MSG_SAMEDIR:
212 			fDestText->SetEnabled(false);
213 			fSelect->SetEnabled(false);
214 			break;
215 		case MSG_DESTUSE:
216 			fDestText->SetEnabled(true);
217 			fSelect->SetEnabled(true);
218 			fDestText->TextView()->MakeEditable(false);
219 			break;
220 		case MSG_CANCEL:
221 			Hide();
222 			break;
223 		case MSG_OK:
224 			fSettings->ReplaceBool("automatically_expand_files", fAutoExpand->Value() == B_CONTROL_ON);
225 			fSettings->ReplaceBool("close_when_done", fCloseWindow->Value() == B_CONTROL_ON);
226 			fSettings->ReplaceInt8("destination_folder", (fSameDest->Value() == B_CONTROL_ON) ? 0x63
227 				: ( (fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65) );
228 			fSettings->ReplaceRef("destination_folder_use", &fRef);
229 			fSettings->ReplaceBool("open_destination_folder", fOpenDest->Value() == B_CONTROL_ON);
230 			fSettings->ReplaceBool("show_contents_listing", fAutoShow->Value() == B_CONTROL_ON);
231 			Hide();
232 			break;
233 		default:
234 			break;
235 	}
236 }
237