1/*
2 * Copyright 2013, Axel D��rfler, axeld@pinc-software.de.
3 * Copyright 2009-2010, Stephan A��mus <superstippi@gmx.de>
4 * Copyright 2009, Bryce Groff, brycegroff@gmail.com.
5 * Distributed under the terms of the MIT License.
6 */
7
8
9#include <stdio.h>
10
11#include "InitializeParameterEditor.h"
12
13#include <Button.h>
14#include <Catalog.h>
15#include <CheckBox.h>
16#include <ControlLook.h>
17#include <GridLayoutBuilder.h>
18#include <MenuField.h>
19#include <MenuItem.h>
20#include <Partition.h>
21#include <PopUpMenu.h>
22#include <SpaceLayoutItem.h>
23#include <TextControl.h>
24#include <Variant.h>
25#include <View.h>
26#include <Window.h>
27
28
29#undef B_TRANSLATION_CONTEXT
30#define B_TRANSLATION_CONTEXT "BFS_Initialize_Parameter"
31
32
33static uint32 MSG_BLOCK_SIZE = 'blsz';
34static uint32 MSG_NAME_CHANGED = 'nmch';
35
36
37InitializeBFSEditor::InitializeBFSEditor()
38	:
39	BPartitionParameterEditor(),
40	fView(NULL),
41	fNameControl(NULL),
42	fBlockSizeMenuField(NULL),
43	fUseIndicesCheckBox(NULL)
44{
45	_CreateViewControls();
46}
47
48
49InitializeBFSEditor::~InitializeBFSEditor()
50{
51}
52
53
54void
55InitializeBFSEditor::SetTo(BPartition* partition)
56{
57	BString name = partition->Name();
58	if (name.IsEmpty())
59		name = partition->RawContentName();
60	if (!name.IsEmpty())
61		fNameControl->SetText(name.String());
62	off_t size = partition->Size();
63	if (size < 2 * 1024 * 1024) {
64		f1KBlockMenuItem->SetMarked(true);
65		f2KBlockMenuItem->SetEnabled(false);
66	} else {
67		f2KBlockMenuItem->SetEnabled(true);
68		f2KBlockMenuItem->SetMarked(true);
69	}
70	f4KBlockMenuItem->SetEnabled(size >= 4 * 1024 * 1024);
71	f8KBlockMenuItem->SetEnabled(size >= 8 * 1024 * 1024);
72}
73
74
75BView*
76InitializeBFSEditor::View()
77{
78	return fView;
79}
80
81
82bool
83InitializeBFSEditor::ValidateParameters() const
84{
85	// The name must be set
86	return fNameControl->TextView()->TextLength() > 0;
87}
88
89
90status_t
91InitializeBFSEditor::ParameterChanged(const char* name, const BVariant& variant)
92{
93	if (!strcmp(name, "name"))
94		fNameControl->SetText(variant.ToString());
95	return B_OK;
96}
97
98
99status_t
100InitializeBFSEditor::GetParameters(BString& parameters)
101{
102	parameters = "";
103
104	if (BMenuItem* item = fBlockSizeMenuField->Menu()->FindMarked()) {
105		const char* size;
106		BMessage* message = item->Message();
107		if (!message || message->FindString("size", &size) < B_OK)
108			size = "2048";
109		// TODO: use libroot driver settings API
110		parameters << "block_size " << size << ";\n";
111	}
112	if (fUseIndicesCheckBox->Value() == B_CONTROL_OFF)
113		parameters << "noindex;\n";
114
115	parameters << "name \"" << fNameControl->Text() << "\";\n";
116	return B_OK;
117}
118
119
120void
121InitializeBFSEditor::_CreateViewControls()
122{
123	fNameControl = new BTextControl(B_TRANSLATE("Name:"), "Haiku", NULL);
124	fNameControl->SetModificationMessage(new BMessage(MSG_NAME_CHANGED));
125	fNameControl->TextView()->SetMaxBytes(31);
126
127	BPopUpMenu* blocksizeMenu = new BPopUpMenu("blocksize");
128	BMessage* message = new BMessage(MSG_BLOCK_SIZE);
129	message->AddString("size", "1024");
130	f1KBlockMenuItem = new BMenuItem(B_TRANSLATE("1024 (Mostly small files)"), message);
131	blocksizeMenu->AddItem(f1KBlockMenuItem);
132
133	message = new BMessage(MSG_BLOCK_SIZE);
134	message->AddString("size", "2048");
135	f2KBlockMenuItem = new BMenuItem(B_TRANSLATE("2048 (Recommended)"),
136		message);
137	blocksizeMenu->AddItem(f2KBlockMenuItem);
138	message = new BMessage(MSG_BLOCK_SIZE);
139	message->AddString("size", "4096");
140	f4KBlockMenuItem = new BMenuItem("4096", message);
141	blocksizeMenu->AddItem(f4KBlockMenuItem);
142	message = new BMessage(MSG_BLOCK_SIZE);
143	message->AddString("size", "8192");
144	f8KBlockMenuItem = new BMenuItem(
145		B_TRANSLATE("8192 (Mostly large files)"), message);
146	blocksizeMenu->AddItem(f8KBlockMenuItem);
147
148	fBlockSizeMenuField = new BMenuField(B_TRANSLATE("Blocksize:"),
149		blocksizeMenu);
150
151	fUseIndicesCheckBox = new BCheckBox(B_TRANSLATE("Enable query support"),
152		NULL);
153	fUseIndicesCheckBox->SetValue(true);
154	fUseIndicesCheckBox->SetToolTip(B_TRANSLATE("Disabling query support "
155		"may speed up certain file system operations, but should\nonly be "
156		"used if one is absolutely certain that one will not need queries."
157		"\nAny volume that is intended for booting Haiku must have query "
158		"support enabled."));
159
160	float spacing = be_control_look->DefaultItemSpacing();
161
162	fView = BGridLayoutBuilder(spacing, spacing)
163		// row 1
164		.Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
165		.Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
166
167		// row 2
168		.Add(fBlockSizeMenuField->CreateLabelLayoutItem(), 0, 1)
169		.Add(fBlockSizeMenuField->CreateMenuBarLayoutItem(), 1, 1)
170
171		// row 3
172		.Add(fUseIndicesCheckBox, 0, 2, 2).View()
173	;
174}
175