xref: /haiku/src/apps/expander/ExpanderPreferences.cpp (revision d374a27286b8a52974a97dba0d5966ea026a665d)
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 <Catalog.h>
9 #include <GroupLayout.h>
10 #include <GroupLayoutBuilder.h>
11 #include <Locale.h>
12 #include <Path.h>
13 #include <Screen.h>
14 #include <StringView.h>
15 
16 const uint32 MSG_OK			= 'mgOK';
17 const uint32 MSG_CANCEL		= 'mCan';
18 const uint32 MSG_LEAVEDEST = 'mLed';
19 const uint32 MSG_SAMEDIR = 'mSad';
20 const uint32 MSG_DESTUSE = 'mDeu';
21 const uint32 MSG_DESTTEXT = 'mDet';
22 const uint32 MSG_DESTSELECT = 'mDes';
23 
24 #undef B_TRANSLATE_CONTEXT
25 #define B_TRANSLATE_CONTEXT "ExpanderPreferences"
26 
27 ExpanderPreferences::ExpanderPreferences(BMessage *settings)
28 	: BWindow(BRect(0, 0, 325, 305), "Expander", B_MODAL_WINDOW,
29 		B_NOT_CLOSABLE | B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
30 	fSettings(settings),
31 	fUsePanel(NULL)
32 {
33 	BBox* box = new BBox("background");
34 	box->SetLabel(B_TRANSLATE("Expander settings"));
35 
36 	fAutoExpand = new BCheckBox("autoExpand",
37 		B_TRANSLATE("Automatically expand files"), NULL);
38 	fCloseWindow = new BCheckBox("closeWindowWhenDone",
39 		B_TRANSLATE("Close window when done expanding"), NULL);
40 
41 	fLeaveDest = new BRadioButton("leaveDest",
42 		B_TRANSLATE("Leave destination folder path empty"),
43 		new BMessage(MSG_LEAVEDEST));
44 	fSameDest = new BRadioButton("sameDir",
45 		B_TRANSLATE("Same directory as source (archive) file"),
46 		new BMessage(MSG_SAMEDIR));
47 	fDestUse = new BRadioButton("destUse",
48 		B_TRANSLATE("Use:"), new BMessage(MSG_DESTUSE));
49 	fDestText = new BTextControl("destText", "", "",
50 		new BMessage(MSG_DESTTEXT));
51 	fDestText->SetDivider(0);
52 	fDestText->TextView()->MakeEditable(false);
53 	fDestText->SetEnabled(false);
54 	fSelect = new BButton("selectButton", B_TRANSLATE("Select"),
55 		new BMessage(MSG_DESTSELECT));
56 	fSelect->SetEnabled(false);
57 
58 	fOpenDest = new BCheckBox("openDestination",
59 		B_TRANSLATE("Open destination folder after extraction"), NULL);
60 	fAutoShow = new BCheckBox("autoShow",
61 		B_TRANSLATE("Automatically show contents listing"), NULL);
62 
63 	BView* view = new BGroupView();
64 	view->SetLayout(new BGroupLayout(B_HORIZONTAL));
65 	view->AddChild(BGroupLayoutBuilder(B_VERTICAL)
66 		.AddGroup(B_HORIZONTAL)
67 			.Add(new BStringView("expansion", B_TRANSLATE("Expansion:")))
68 			.AddGlue()
69 		.End()
70 		.AddGroup(B_VERTICAL, 1)
71 			.Add(fAutoExpand)
72 			.Add(fCloseWindow)
73 			.SetInsets(10, 0, 0, 10)
74 		.End()
75 		.AddGroup(B_HORIZONTAL)
76 			.Add(new BStringView("destinationFolder",
77 				B_TRANSLATE("Destination folder:")))
78 			.AddGlue()
79 		.End()
80 		.AddGroup(B_VERTICAL, 1)
81 			.Add(fLeaveDest)
82 			.Add(fSameDest)
83 			.Add(fDestUse)
84 			.AddGroup(B_HORIZONTAL, 5)
85 				.Add(fDestText, 0.8)
86 				.Add(fSelect, 0.2)
87 				.SetInsets(20, 0, 0, 0)
88 			.End()
89 			.SetInsets(10, 0, 0, 10)
90 		.End()
91 		.AddGroup(B_HORIZONTAL)
92 			.Add(new BStringView("other", B_TRANSLATE("Other:")))
93 			.AddGlue()
94 		.End()
95 		.AddGroup(B_VERTICAL, 1)
96 			.Add(fOpenDest)
97 			.Add(fAutoShow)
98 			.SetInsets(10, 0, 0, 0)
99 		.End()
100 		.SetInsets(10, 10, 10, 10)
101 	);
102 	box->AddChild(view);
103 
104 	BButton* button = new BButton("OKButton", B_TRANSLATE("OK"),
105 		new BMessage(MSG_OK));
106 	button->MakeDefault(true);
107 	BButton* cancel = new BButton("CancelButton", B_TRANSLATE("Cancel"),
108 		new BMessage(MSG_CANCEL));
109 
110 	SetLayout(new BGroupLayout(B_HORIZONTAL));
111 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 11)
112 		.Add(box)
113 		.AddGroup(B_HORIZONTAL, 10)
114 			.AddGlue()
115 			.Add(cancel)
116 			.Add(button)
117 		.End()
118 		.SetInsets(10, 10, 10, 10)
119 	);
120 
121 	CenterOnScreen();
122 
123 	bool automatically_expand_files;
124 	bool close_when_done;
125 	int8 destination_folder;
126 	entry_ref ref;
127 	bool open_destination_folder;
128 	bool show_contents_listing;
129 	if ((settings->FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
130 		&& automatically_expand_files)
131 		fAutoExpand->SetValue(B_CONTROL_ON);
132 
133 	if ((settings->FindBool("close_when_done", &close_when_done) == B_OK)
134 		&& close_when_done)
135 		fCloseWindow->SetValue(B_CONTROL_ON);
136 
137 	if (settings->FindInt8("destination_folder", &destination_folder) == B_OK) {
138 		switch (destination_folder) {
139 			case 0x63:
140 				fSameDest->SetValue(B_CONTROL_ON);
141 				break;
142 			case 0x65:
143 				fDestUse->SetValue(B_CONTROL_ON);
144 				fDestText->SetEnabled(true);
145 				fSelect->SetEnabled(true);
146 				break;
147 			case 0x66:
148 				fLeaveDest->SetValue(B_CONTROL_ON);
149 				break;
150 		}
151 	}
152 
153 	if (settings->FindRef("destination_folder_use", &fRef) == B_OK) {
154 		BEntry entry(&fRef);
155 		if (entry.Exists()) {
156 			BPath path(&entry);
157 			fDestText->SetText(path.Path());
158 		}
159 	}
160 
161 	if ((settings->FindBool("open_destination_folder", &open_destination_folder) == B_OK)
162 		&& open_destination_folder)
163 		fOpenDest->SetValue(B_CONTROL_ON);
164 
165 	if ((settings->FindBool("show_contents_listing", &show_contents_listing) == B_OK)
166 		&& show_contents_listing)
167 		fAutoShow->SetValue(B_CONTROL_ON);
168 }
169 
170 
171 ExpanderPreferences::~ExpanderPreferences()
172 {
173 	if (fUsePanel && fUsePanel->RefFilter())
174 		delete fUsePanel->RefFilter();
175 
176 	delete fUsePanel;
177 }
178 
179 
180 void
181 ExpanderPreferences::MessageReceived(BMessage* msg)
182 {
183 	switch (msg->what) {
184 		case MSG_DESTSELECT:
185 		{
186 			if (!fUsePanel) {
187 				BMessenger messenger(this);
188 				fUsePanel = new DirectoryFilePanel(B_OPEN_PANEL, &messenger, NULL,
189 					B_DIRECTORY_NODE, false, NULL, new DirectoryRefFilter(), true);
190 			}
191 			fUsePanel->Show();
192 			break;
193 		}
194 		case MSG_DIRECTORY:
195 		{
196 			entry_ref ref;
197 			fUsePanel->GetPanelDirectory(&ref);
198 			fRef = ref;
199 			BEntry entry(&ref);
200 			BPath path(&entry);
201 			fDestText->SetText(path.Path());
202 			fUsePanel->Hide();
203 			break;
204 		}
205 		case B_REFS_RECEIVED:
206 			if (msg->FindRef("refs", 0, &fRef) == B_OK) {
207 				BEntry entry(&fRef, true);
208 				BPath path(&entry);
209 				fDestText->SetText(path.Path());
210 			}
211 			break;
212 		case MSG_LEAVEDEST:
213 		case MSG_SAMEDIR:
214 			fDestText->SetEnabled(false);
215 			fSelect->SetEnabled(false);
216 			break;
217 		case MSG_DESTUSE:
218 			fDestText->SetEnabled(true);
219 			fSelect->SetEnabled(true);
220 			fDestText->TextView()->MakeEditable(false);
221 			break;
222 		case MSG_CANCEL:
223 			Hide();
224 			break;
225 		case MSG_OK:
226 			fSettings->ReplaceBool("automatically_expand_files",
227 				fAutoExpand->Value() == B_CONTROL_ON);
228 			fSettings->ReplaceBool("close_when_done",
229 				fCloseWindow->Value() == B_CONTROL_ON);
230 			fSettings->ReplaceInt8("destination_folder",
231 				(fSameDest->Value() == B_CONTROL_ON) ? 0x63
232 				: ((fLeaveDest->Value() == B_CONTROL_ON) ? 0x66 : 0x65));
233 			fSettings->ReplaceRef("destination_folder_use", &fRef);
234 			fSettings->ReplaceBool("open_destination_folder",
235 				fOpenDest->Value() == B_CONTROL_ON);
236 			fSettings->ReplaceBool("show_contents_listing",
237 				fAutoShow->Value() == B_CONTROL_ON);
238 			Hide();
239 			break;
240 		default:
241 			break;
242 	}
243 }
244