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