xref: /haiku/src/apps/expander/ExpanderPreferences.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 /*****************************************************************************/
2 // Expander
3 // Written by Jérôme Duval
4 //
5 // ExpanderPreferences.cpp
6 //
7 //
8 // Copyright (c) 2004 OpenBeOS Project
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 // DEALINGS IN THE SOFTWARE.
27 /*****************************************************************************/
28 
29 #include "ExpanderPreferences.h"
30 #include <Box.h>
31 #include <Path.h>
32 #include <Screen.h>
33 #include <TextView.h>
34 
35 const uint32 MSG_OK			= 'mgOK';
36 const uint32 MSG_CANCEL		= 'mCan';
37 const uint32 MSG_LEAVEDEST = 'mLed';
38 const uint32 MSG_SAMEDIR = 'mSad';
39 const uint32 MSG_DESTUSE = 'mDeu';
40 const uint32 MSG_DESTTEXT = 'mDet';
41 const uint32 MSG_DESTSELECT = 'mDes';
42 
43 ExpanderPreferences::ExpanderPreferences(BMessage *settings)
44 	: BWindow(BRect(0, 0, 325, 305), "Expand-O-Matic", B_MODAL_WINDOW,
45 		B_NOT_CLOSABLE | B_NOT_RESIZABLE),
46 	fSettings(settings),
47 	fUsePanel(NULL)
48 {
49 
50 	BRect rect = Bounds();
51 	BBox *background = new BBox(rect, "background", B_FOLLOW_ALL,
52 		B_WILL_DRAW | B_FRAME_EVENTS, B_PLAIN_BORDER);
53 	AddChild(background);
54 
55 	rect.OffsetBy(11,9);
56 	rect.bottom -= 64;
57 	rect.right -= 22;
58 	BBox *box = new BBox(rect, "background", B_FOLLOW_ALL,
59 		B_WILL_DRAW | B_FRAME_EVENTS, B_FANCY_BORDER);
60 	box->SetLabel("Expander Preferences");
61 	background->AddChild(box);
62 
63 
64 	BRect frameRect = box->Bounds();
65 	frameRect.OffsetBy(15,23);
66 	frameRect.right = frameRect.left + 100;
67 	frameRect.bottom = frameRect.top + 20;
68 	BRect textRect(frameRect);
69 	textRect.OffsetTo(B_ORIGIN);
70 	textRect.InsetBy(1,1);
71 	BTextView *textView = new BTextView(frameRect, "expansion", textRect,
72 		be_plain_font, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
73 	textView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
74 	box->AddChild(textView);
75 	textView->SetText("Expansion:");
76 
77 	frameRect.OffsetBy(0, 58);
78 	textRect.OffsetBy(0, 58);
79 	textView = new BTextView(frameRect, "destinationFolder", textRect,
80 		be_plain_font, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
81 	textView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
82 	box->AddChild(textView);
83 	textView->SetText("Destination Folder:");
84 
85 	frameRect.OffsetBy(0, 84);
86 	textRect.OffsetBy(0, 84);
87 	textView = new BTextView(frameRect, "other", textRect,
88 		be_plain_font, NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP, B_WILL_DRAW);
89 	textView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
90 	box->AddChild(textView);
91 	textView->SetText("Other:");
92 
93 	rect = box->Bounds();
94 	rect.OffsetBy(25, 42);
95 	rect.bottom = rect.top + 20;
96 	rect.right = rect.left + 220;
97 	fAutoExpand = new BCheckBox(rect, "autoExpand", "Automatically expand files", NULL);
98 	box->AddChild(fAutoExpand);
99 
100 	rect.OffsetBy(0, 17);
101 	fCloseWindow = new BCheckBox(rect, "closeWindowWhenDone", "Close window when done expanding", NULL);
102 	box->AddChild(fCloseWindow);
103 
104 	rect.OffsetBy(0, 50);
105 	fLeaveDest = new BRadioButton(rect, "leaveDest", "Leave destination folder path empty",
106 		new BMessage(MSG_LEAVEDEST));
107 	box->AddChild(fLeaveDest);
108 
109 	rect.OffsetBy(0, 17);
110 	fSameDest = new BRadioButton(rect, "sameDir", "Same directory as source (archive) file",
111 		new BMessage(MSG_SAMEDIR));
112 	box->AddChild(fSameDest);
113 
114 	rect.OffsetBy(0, 17);
115 	BRect useRect = rect;
116 	useRect.right = useRect.left + 50;
117 	fDestUse = new BRadioButton(useRect, "destUse", "Use:",
118 		new BMessage(MSG_DESTUSE));
119 	box->AddChild(fDestUse);
120 
121 	textRect = rect;
122 	textRect.OffsetBy(45, 0);
123 	textRect.right = textRect.left + 158;
124 	fDestText = new BTextControl(textRect, "destText", "", "", new BMessage(MSG_DESTTEXT));
125 	fDestText->SetDivider(0);
126 	fDestText->TextView()->MakeEditable(false);
127 	box->AddChild(fDestText);
128 	fDestText->SetEnabled(false);
129 
130 	textRect.OffsetBy(168, -4);
131 	textRect.right = textRect.left + 50;
132 	fSelect = new BButton(textRect, "selectButton", "Select", new BMessage(MSG_DESTSELECT));
133 	box->AddChild(fSelect);
134 	fSelect->SetEnabled(false);
135 
136 	rect.OffsetBy(0, 50);
137 	fOpenDest = new BCheckBox(rect, "openDestination", "Open destination folder after extraction", NULL);
138 	box->AddChild(fOpenDest);
139 
140 	rect.OffsetBy(0, 17);
141 	fAutoShow = new BCheckBox(rect, "autoShow", "Automatically show contents listing", NULL);
142 	box->AddChild(fAutoShow);
143 
144 	rect = BRect(Bounds().right-89, Bounds().bottom-40,
145 		Bounds().right-14, Bounds().bottom-16);
146 	BButton *button = new BButton(rect, "OKButton", "OK",
147 		new BMessage(MSG_OK));
148 	background->AddChild(button);
149 	button->MakeDefault(true);
150 
151 	rect.OffsetBy(-89, 0);
152 	button = new BButton(rect, "CancelButton", "Cancel",
153 		new BMessage(MSG_CANCEL));
154 	background->AddChild(button);
155 
156 	BScreen screen;
157 	MoveBy((screen.Frame().Width()-Bounds().Width())/2,
158 		(screen.Frame().Height()-Bounds().Height())/2);
159 
160 	bool automatically_expand_files;
161 	bool close_when_done;
162 	int8 destination_folder;
163 	entry_ref ref;
164 	bool open_destination_folder;
165 	bool show_contents_listing;
166 	if ((settings->FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
167 		&& automatically_expand_files)
168 		fAutoExpand->SetValue(B_CONTROL_ON);
169 
170 	if ((settings->FindBool("close_when_done", &close_when_done) == B_OK)
171 		&& close_when_done)
172 		fCloseWindow->SetValue(B_CONTROL_ON);
173 
174 	if (settings->FindInt8("destination_folder", &destination_folder) == B_OK) {
175 		switch (destination_folder) {
176 			case 0x63:
177 				fSameDest->SetValue(B_CONTROL_ON);
178 				break;
179 			case 0x65:
180 				fDestUse->SetValue(B_CONTROL_ON);
181 				fDestText->SetEnabled(true);
182 				fSelect->SetEnabled(true);
183 				break;
184 			case 0x66:
185 				fLeaveDest->SetValue(B_CONTROL_ON);
186 				break;
187 		}
188 	}
189 
190 	if (settings->FindRef("destination_folder_use", &fRef) == B_OK) {
191 		BEntry entry(&fRef);
192 		if (entry.Exists()) {
193 			BPath path(&entry);
194 			fDestText->SetText(path.Path());
195 		}
196 	}
197 
198 	if ((settings->FindBool("open_destination_folder", &open_destination_folder) == B_OK)
199 		&& open_destination_folder)
200 		fOpenDest->SetValue(B_CONTROL_ON);
201 
202 	if ((settings->FindBool("show_contents_listing", &show_contents_listing) == B_OK)
203 		&& show_contents_listing)
204 		fAutoShow->SetValue(B_CONTROL_ON);
205 }
206 
207 
208 ExpanderPreferences::~ExpanderPreferences()
209 {
210 }
211 
212 
213 void
214 ExpanderPreferences::MessageReceived(BMessage *msg)
215 {
216 	switch (msg->what) {
217 		case MSG_DESTSELECT:
218 			if (!fUsePanel)
219 				fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, new BMessenger(this), NULL,
220 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter(), true);
221 			fUsePanel->Show();
222 			break;
223 		case MSG_DIRECTORY:
224 			{
225 				entry_ref ref;
226 				fUsePanel->GetPanelDirectory(&ref);
227 				fRef = ref;
228 				BEntry entry(&ref);
229 				BPath path(&entry);
230 				fDestText->SetText(path.Path());
231 				fUsePanel->Hide();
232 			}
233 			break;
234 		case B_REFS_RECEIVED:
235 			if (msg->FindRef("refs", 0, &fRef) == B_OK) {
236 				BEntry entry(&fRef, true);
237 				BPath path(&entry);
238 				fDestText->SetText(path.Path());
239 			}
240 			break;
241 		case MSG_LEAVEDEST:
242 		case MSG_SAMEDIR:
243 			fDestText->SetEnabled(false);
244 			fSelect->SetEnabled(false);
245 			break;
246 		case MSG_DESTUSE:
247 			fDestText->SetEnabled(true);
248 			fSelect->SetEnabled(true);
249 			fDestText->TextView()->MakeEditable(false);
250 			break;
251 		case MSG_CANCEL:
252 			Hide();
253 			break;
254 		case MSG_OK:
255 			fSettings->ReplaceBool("automatically_expand_files", fAutoExpand->Value() == B_CONTROL_ON);
256 			fSettings->ReplaceBool("close_when_done", fCloseWindow->Value() == B_CONTROL_ON);
257 			fSettings->ReplaceInt8("destination_folder", (fSameDest->Value() == B_CONTROL_ON) ? 0x63
258 				: ( (fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65) );
259 			fSettings->ReplaceRef("destination_folder_use", &fRef);
260 			fSettings->ReplaceBool("open_destination_folder", fOpenDest->Value() == B_CONTROL_ON);
261 			fSettings->ReplaceBool("show_contents_listing", fAutoShow->Value() == B_CONTROL_ON);
262 			Hide();
263 			break;
264 		default:
265 			break;
266 	}
267 }
268