xref: /haiku/src/apps/webpositive/SettingsWindow.cpp (revision 41ddfe2a44cfd077483c9bf8fa80912431f772a3)
1 /*
2  * Copyright 2010 Stephan Aßmus <superstippi@gmx.de>
3  * Copyright 2019, Haiku, Inc.
4  * All rights reserved. Distributed under the terms of the MIT License.
5  */
6 #include "SettingsWindow.h"
7 
8 #include <Button.h>
9 #include <CheckBox.h>
10 #include <ControlLook.h>
11 #include <FilePanel.h>
12 #include <GridLayoutBuilder.h>
13 #include <GroupLayout.h>
14 #include <GroupLayoutBuilder.h>
15 #include <LayoutBuilder.h>
16 #include <Locale.h>
17 #include <MenuItem.h>
18 #include <MenuField.h>
19 #include <Message.h>
20 #include <PopUpMenu.h>
21 #include <ScrollView.h>
22 #include <SeparatorView.h>
23 #include <SpaceLayoutItem.h>
24 #include <Spinner.h>
25 #include <TabView.h>
26 #include <TextControl.h>
27 #include <debugger.h>
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 
32 #include "BrowserApp.h"
33 #include "BrowsingHistory.h"
34 #include "BrowserWindow.h"
35 #include "FontSelectionView.h"
36 #include "SettingsKeys.h"
37 #include "SettingsMessage.h"
38 #include "WebSettings.h"
39 
40 
41 #undef B_TRANSLATION_CONTEXT
42 #define B_TRANSLATION_CONTEXT "Settings Window"
43 
44 enum {
45 	MSG_APPLY									= 'aply',
46 	MSG_CANCEL									= 'cncl',
47 	MSG_REVERT									= 'rvrt',
48 
49 	MSG_START_PAGE_CHANGED						= 'hpch',
50 	MSG_SEARCH_PAGE_CHANGED						= 'spch',
51 	MSG_DOWNLOAD_FOLDER_CHANGED					= 'dnfc',
52 	MSG_NEW_WINDOWS_BEHAVIOR_CHANGED			= 'nwbc',
53 	MSG_NEW_TABS_BEHAVIOR_CHANGED				= 'ntbc',
54 	MSG_START_UP_BEHAVIOR_CHANGED				= 'subc',
55 	MSG_HISTORY_MENU_DAYS_CHANGED				= 'digm',
56 	MSG_TAB_DISPLAY_BEHAVIOR_CHANGED			= 'tdbc',
57 	MSG_AUTO_HIDE_INTERFACE_BEHAVIOR_CHANGED	= 'ahic',
58 	MSG_AUTO_HIDE_POINTER_BEHAVIOR_CHANGED		= 'ahpc',
59 	MSG_SHOW_HOME_BUTTON_CHANGED				= 'shbc',
60 
61 	MSG_STANDARD_FONT_CHANGED					= 'stfc',
62 	MSG_SERIF_FONT_CHANGED						= 'sefc',
63 	MSG_SANS_SERIF_FONT_CHANGED					= 'ssfc',
64 	MSG_FIXED_FONT_CHANGED						= 'ffch',
65 
66 	MSG_STANDARD_FONT_SIZE_SELECTED				= 'sfss',
67 	MSG_FIXED_FONT_SIZE_SELECTED				= 'ffss',
68 
69 	MSG_USE_PROXY_CHANGED						= 'upsc',
70 	MSG_PROXY_ADDRESS_CHANGED					= 'psac',
71 	MSG_PROXY_PORT_CHANGED						= 'pspc',
72 	MSG_USE_PROXY_AUTH_CHANGED					= 'upsa',
73 	MSG_PROXY_USERNAME_CHANGED					= 'psuc',
74 	MSG_PROXY_PASSWORD_CHANGED					= 'pswc',
75 
76 	MSG_CHOOSE_DOWNLOAD_FOLDER					= 'swop',
77 	MSG_HANDLE_DOWNLOAD_FOLDER					= 'oprs',
78 };
79 
80 static const int32 kDefaultFontSize = 14;
81 
82 
83 SettingsWindow::SettingsWindow(BRect frame, SettingsMessage* settings)
84 	:
85 	BWindow(frame, B_TRANSLATE("Settings"), B_TITLED_WINDOW_LOOK,
86 		B_NORMAL_WINDOW_FEEL, B_AUTO_UPDATE_SIZE_LIMITS
87 			| B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE),
88 	fSettings(settings)
89 {
90 	fApplyButton = new BButton(B_TRANSLATE("Apply"), new BMessage(MSG_APPLY));
91 	fCancelButton = new BButton(B_TRANSLATE("Cancel"),
92 		new BMessage(MSG_CANCEL));
93 	fRevertButton = new BButton(B_TRANSLATE("Revert"),
94 		new BMessage(MSG_REVERT));
95 
96 	fOpenFilePanel = NULL;
97 
98 	float spacing = be_control_look->DefaultItemSpacing();
99 
100 	BTabView* tabView = new BTabView("settings pages", B_WIDTH_FROM_LABEL);
101 	tabView->SetBorder(B_NO_BORDER);
102 
103 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
104 		.SetInsets(0, B_USE_DEFAULT_SPACING, 0, B_USE_WINDOW_SPACING)
105 		.Add(tabView)
106 		.Add(new BSeparatorView(B_HORIZONTAL))
107 		.AddGroup(B_HORIZONTAL)
108 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING,
109 				B_USE_WINDOW_SPACING, 0)
110 			.Add(fRevertButton)
111 			.AddGlue()
112 			.Add(fCancelButton)
113 			.Add(fApplyButton);
114 
115 	tabView->AddTab(_CreateGeneralPage(spacing));
116 	tabView->AddTab(_CreateFontsPage(spacing));
117 	tabView->AddTab(_CreateProxyPage(spacing));
118 
119 	_SetupFontSelectionView(fStandardFontView,
120 		new BMessage(MSG_STANDARD_FONT_CHANGED));
121 	_SetupFontSelectionView(fSerifFontView,
122 		new BMessage(MSG_SERIF_FONT_CHANGED));
123 	_SetupFontSelectionView(fSansSerifFontView,
124 		new BMessage(MSG_SANS_SERIF_FONT_CHANGED));
125 	_SetupFontSelectionView(fFixedFontView,
126 		new BMessage(MSG_FIXED_FONT_CHANGED));
127 
128 	fApplyButton->MakeDefault(true);
129 
130 	if (!frame.IsValid())
131 		CenterOnScreen();
132 
133 	// load settings from disk
134 	_RevertSettings();
135 	// apply to WebKit
136 	_ApplySettings();
137 
138 	// Start hidden
139 	Hide();
140 	Show();
141 }
142 
143 
144 SettingsWindow::~SettingsWindow()
145 {
146 	RemoveHandler(fStandardFontView);
147 	delete fStandardFontView;
148 	RemoveHandler(fSerifFontView);
149 	delete fSerifFontView;
150 	RemoveHandler(fSansSerifFontView);
151 	delete fSansSerifFontView;
152 	RemoveHandler(fFixedFontView);
153 	delete fFixedFontView;
154 	delete fOpenFilePanel;
155 }
156 
157 
158 void
159 SettingsWindow::MessageReceived(BMessage* message)
160 {
161 	switch (message->what) {
162 		case MSG_APPLY:
163 			_ApplySettings();
164 			break;
165 		case MSG_CANCEL:
166 			_RevertSettings();
167 			PostMessage(B_QUIT_REQUESTED);
168 			break;
169 		case MSG_REVERT:
170 			_RevertSettings();
171 			break;
172 		case MSG_CHOOSE_DOWNLOAD_FOLDER:
173 			_ChooseDownloadFolder(message);
174 			break;
175 		case MSG_HANDLE_DOWNLOAD_FOLDER:
176 			_HandleDownloadPanelResult(fOpenFilePanel, message);
177 			break;
178 		case MSG_STANDARD_FONT_SIZE_SELECTED:
179 		{
180 			int32 size = fStandardSizesSpinner->Value();
181 			fStandardFontView->SetSize(size);
182 			fSerifFontView->SetSize(size);
183 			fSansSerifFontView->SetSize(size);
184 			_ValidateControlsEnabledStatus();
185 			break;
186 		}
187 		case MSG_FIXED_FONT_SIZE_SELECTED:
188 		{
189 			int32 size = fFixedSizesSpinner->Value();
190 			fFixedFontView->SetSize(size);
191 			_ValidateControlsEnabledStatus();
192 			break;
193 		}
194 
195 		case MSG_START_PAGE_CHANGED:
196 		case MSG_SEARCH_PAGE_CHANGED:
197 		case MSG_DOWNLOAD_FOLDER_CHANGED:
198 		case MSG_START_UP_BEHAVIOR_CHANGED:
199 		case MSG_NEW_WINDOWS_BEHAVIOR_CHANGED:
200 		case MSG_NEW_TABS_BEHAVIOR_CHANGED:
201 		case MSG_HISTORY_MENU_DAYS_CHANGED:
202 		case MSG_TAB_DISPLAY_BEHAVIOR_CHANGED:
203 		case MSG_AUTO_HIDE_INTERFACE_BEHAVIOR_CHANGED:
204 		case MSG_AUTO_HIDE_POINTER_BEHAVIOR_CHANGED:
205 		case MSG_SHOW_HOME_BUTTON_CHANGED:
206 		case MSG_STANDARD_FONT_CHANGED:
207 		case MSG_SERIF_FONT_CHANGED:
208 		case MSG_SANS_SERIF_FONT_CHANGED:
209 		case MSG_FIXED_FONT_CHANGED:
210 		case MSG_USE_PROXY_CHANGED:
211 		case MSG_PROXY_ADDRESS_CHANGED:
212 		case MSG_PROXY_PORT_CHANGED:
213 		case MSG_USE_PROXY_AUTH_CHANGED:
214 		case MSG_PROXY_USERNAME_CHANGED:
215 		case MSG_PROXY_PASSWORD_CHANGED:
216 			// TODO: Some settings could change live, some others not?
217 			_ValidateControlsEnabledStatus();
218 			break;
219 
220 		default:
221 			BWindow::MessageReceived(message);
222 			break;
223 	}
224 }
225 
226 
227 bool
228 SettingsWindow::QuitRequested()
229 {
230 	if (!IsHidden())
231 		Hide();
232 	return false;
233 }
234 
235 
236 void
237 SettingsWindow::Show()
238 {
239 	// When showing the window, the this is always the
240 	// point to which we can revert the settings.
241 	_RevertSettings();
242 	BWindow::Show();
243 }
244 
245 
246 // #pragma mark - private
247 
248 
249 BView*
250 SettingsWindow::_CreateGeneralPage(float spacing)
251 {
252 	fStartPageControl = new BTextControl("start page",
253 		B_TRANSLATE("Start page:"), "", new BMessage(MSG_START_PAGE_CHANGED));
254 	fStartPageControl->SetModificationMessage(
255 		new BMessage(MSG_START_PAGE_CHANGED));
256 	fStartPageControl->SetText(
257 		fSettings->GetValue(kSettingsKeyStartPageURL, kDefaultStartPageURL));
258 
259 	fSearchPageControl = new BTextControl("search page",
260 		B_TRANSLATE("Search page:"), "",
261 		new BMessage(MSG_SEARCH_PAGE_CHANGED));
262 	fSearchPageControl->SetModificationMessage(
263 		new BMessage(MSG_SEARCH_PAGE_CHANGED));
264 	BString searchURL = fSettings->GetValue(kSettingsKeySearchPageURL,
265 		kDefaultSearchPageURL);
266 	if (searchURL == "http://www.google.com") {
267 		// Migrate old settings files.
268 		searchURL = kDefaultSearchPageURL;
269 		fSettings->SetValue(kSettingsKeySearchPageURL, kDefaultSearchPageURL);
270 	}
271 	fSearchPageControl->SetText(searchURL);
272 
273 	fDownloadFolderControl = new BTextControl("download folder",
274 		B_TRANSLATE("Download folder:"), "",
275 		new BMessage(MSG_DOWNLOAD_FOLDER_CHANGED));
276 	fDownloadFolderControl->SetModificationMessage(
277 		new BMessage(MSG_DOWNLOAD_FOLDER_CHANGED));
278 	fDownloadFolderControl->SetText(
279 		fSettings->GetValue(kSettingsKeyDownloadPath, kDefaultDownloadPath));
280 
281 	fStartUpBehaviorResumePriorSession = new BMenuItem(
282 		B_TRANSLATE("Resume prior session"),
283 		new BMessage(MSG_START_UP_BEHAVIOR_CHANGED));
284 	fStartUpBehaviorStartNewSession = new BMenuItem(
285 		B_TRANSLATE("Start new session"),
286 		new BMessage(MSG_START_UP_BEHAVIOR_CHANGED));
287 
288 	fNewWindowBehaviorOpenHomeItem = new BMenuItem(
289 		B_TRANSLATE("Open start page"),
290 		new BMessage(MSG_NEW_WINDOWS_BEHAVIOR_CHANGED));
291 	fNewWindowBehaviorOpenSearchItem = new BMenuItem(
292 		B_TRANSLATE("Open search page"),
293 		new BMessage(MSG_NEW_WINDOWS_BEHAVIOR_CHANGED));
294 	fNewWindowBehaviorOpenBlankItem = new BMenuItem(
295 		B_TRANSLATE("Open blank page"),
296 		new BMessage(MSG_NEW_WINDOWS_BEHAVIOR_CHANGED));
297 
298 	fNewTabBehaviorCloneCurrentItem = new BMenuItem(
299 		B_TRANSLATE("Clone current page"),
300 		new BMessage(MSG_NEW_TABS_BEHAVIOR_CHANGED));
301 	fNewTabBehaviorOpenHomeItem = new BMenuItem(
302 		B_TRANSLATE("Open start page"),
303 		new BMessage(MSG_NEW_TABS_BEHAVIOR_CHANGED));
304 	fNewTabBehaviorOpenSearchItem = new BMenuItem(
305 		B_TRANSLATE("Open search page"),
306 		new BMessage(MSG_NEW_TABS_BEHAVIOR_CHANGED));
307 	fNewTabBehaviorOpenBlankItem = new BMenuItem(
308 		B_TRANSLATE("Open blank page"),
309 		new BMessage(MSG_NEW_TABS_BEHAVIOR_CHANGED));
310 	fChooseButton = new BButton(B_TRANSLATE("Browse" B_UTF8_ELLIPSIS),
311 		new BMessage(MSG_CHOOSE_DOWNLOAD_FOLDER));
312 
313 	fNewWindowBehaviorOpenHomeItem->SetMarked(true);
314 	fNewTabBehaviorOpenBlankItem->SetMarked(true);
315 	fStartUpBehaviorResumePriorSession->SetMarked(true);
316 
317 	BPopUpMenu* startUpBehaviorMenu = new BPopUpMenu("Start up");
318 	startUpBehaviorMenu->AddItem(fStartUpBehaviorResumePriorSession);
319 	startUpBehaviorMenu->AddItem(fStartUpBehaviorStartNewSession);
320 	fStartUpBehaviorMenu = new BMenuField("start up behavior",
321 		B_TRANSLATE("Start up:"), startUpBehaviorMenu);
322 
323 
324 	BPopUpMenu* newWindowBehaviorMenu = new BPopUpMenu("New windows");
325 	newWindowBehaviorMenu->AddItem(fNewWindowBehaviorOpenHomeItem);
326 	newWindowBehaviorMenu->AddItem(fNewWindowBehaviorOpenSearchItem);
327 	newWindowBehaviorMenu->AddItem(fNewWindowBehaviorOpenBlankItem);
328 	fNewWindowBehaviorMenu = new BMenuField("new window behavior",
329 		B_TRANSLATE("New windows:"), newWindowBehaviorMenu);
330 
331 	BPopUpMenu* newTabBehaviorMenu = new BPopUpMenu("New tabs");
332 	newTabBehaviorMenu->AddItem(fNewTabBehaviorOpenBlankItem);
333 	newTabBehaviorMenu->AddItem(fNewTabBehaviorOpenHomeItem);
334 	newTabBehaviorMenu->AddItem(fNewTabBehaviorOpenSearchItem);
335 	newTabBehaviorMenu->AddItem(fNewTabBehaviorCloneCurrentItem);
336 	fNewTabBehaviorMenu = new BMenuField("new tab behavior",
337 		B_TRANSLATE("New tabs:"), newTabBehaviorMenu);
338 
339 	fDaysInHistory = new BSpinner("days in history",
340 		B_TRANSLATE("Number of days to keep links in History menu:"),
341 		new BMessage(MSG_HISTORY_MENU_DAYS_CHANGED));
342 	fDaysInHistory->SetRange(1, 35);
343 	fDaysInHistory->SetValue(
344 		BrowsingHistory::DefaultInstance()->MaxHistoryItemAge());
345 
346 	fShowTabsIfOnlyOnePage = new BCheckBox("show tabs if only one page",
347 		B_TRANSLATE("Show tabs if only one page is open"),
348 		new BMessage(MSG_TAB_DISPLAY_BEHAVIOR_CHANGED));
349 	fShowTabsIfOnlyOnePage->SetValue(B_CONTROL_ON);
350 
351 	fAutoHideInterfaceInFullscreenMode = new BCheckBox("auto-hide interface",
352 		B_TRANSLATE("Auto-hide interface in full screen mode"),
353 		new BMessage(MSG_AUTO_HIDE_INTERFACE_BEHAVIOR_CHANGED));
354 	fAutoHideInterfaceInFullscreenMode->SetValue(B_CONTROL_OFF);
355 
356 	fAutoHidePointer = new BCheckBox("auto-hide pointer",
357 		B_TRANSLATE("Auto-hide mouse pointer"),
358 		new BMessage(MSG_AUTO_HIDE_POINTER_BEHAVIOR_CHANGED));
359 	fAutoHidePointer->SetValue(B_CONTROL_OFF);
360 
361 	fShowHomeButton = new BCheckBox("show home button",
362 		B_TRANSLATE("Show home button"),
363 		new BMessage(MSG_SHOW_HOME_BUTTON_CHANGED));
364 	fShowHomeButton->SetValue(B_CONTROL_ON);
365 
366 	BView* view = BGroupLayoutBuilder(B_VERTICAL, 0)
367 		.Add(BGridLayoutBuilder(spacing / 2, spacing / 2)
368 			.Add(fStartPageControl->CreateLabelLayoutItem(), 0, 0)
369 			.Add(fStartPageControl->CreateTextViewLayoutItem(), 1, 0)
370 
371 			.Add(fSearchPageControl->CreateLabelLayoutItem(), 0, 1)
372 			.Add(fSearchPageControl->CreateTextViewLayoutItem(), 1, 1)
373 
374 			.Add(fStartUpBehaviorMenu->CreateLabelLayoutItem(), 0, 2)
375 			.Add(fStartUpBehaviorMenu->CreateMenuBarLayoutItem(), 1, 2)
376 
377 			.Add(fNewWindowBehaviorMenu->CreateLabelLayoutItem(), 0, 3)
378 			.Add(fNewWindowBehaviorMenu->CreateMenuBarLayoutItem(), 1, 3)
379 
380 			.Add(fDownloadFolderControl->CreateLabelLayoutItem(), 0, 4)
381 			.Add(fDownloadFolderControl->CreateTextViewLayoutItem(), 1, 4)
382 			.Add(fChooseButton, 2, 3)
383 
384 			.Add(fNewTabBehaviorMenu->CreateLabelLayoutItem(), 0, 5)
385 			.Add(fNewTabBehaviorMenu->CreateMenuBarLayoutItem(), 1, 5)
386 		)
387 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing))
388 		.Add(new BSeparatorView(B_HORIZONTAL, B_PLAIN_BORDER))
389 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing))
390 		.Add(fShowTabsIfOnlyOnePage)
391 		.Add(fAutoHideInterfaceInFullscreenMode)
392 		.Add(fAutoHidePointer)
393 		.Add(fShowHomeButton)
394 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing))
395 
396 		.Add(fDaysInHistory)
397 		.AddGlue()
398 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
399 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
400 		.TopView()
401 	;
402 	view->SetName(B_TRANSLATE("General"));
403 	return view;
404 }
405 
406 
407 BView*
408 SettingsWindow::_CreateFontsPage(float spacing)
409 {
410 	fStandardFontView = new FontSelectionView("standard",
411 		B_TRANSLATE("Standard font:"), true, be_plain_font);
412 	BFont defaultSerifFont = _FindDefaultSerifFont();
413 	fSerifFontView = new FontSelectionView("serif",
414 		B_TRANSLATE("Serif font:"), true, &defaultSerifFont);
415 	fSansSerifFontView = new FontSelectionView("sans serif",
416 		B_TRANSLATE("Sans serif font:"), true, be_plain_font);
417 	fFixedFontView = new FontSelectionView("fixed",
418 		B_TRANSLATE("Fixed font:"), true, be_fixed_font);
419 
420 	fStandardSizesSpinner = new BSpinner("standard font size",
421 		B_TRANSLATE("Default standard font size:"),
422 		new BMessage(MSG_STANDARD_FONT_SIZE_SELECTED));
423 	fStandardSizesSpinner->SetAlignment(B_ALIGN_RIGHT);
424 
425 	fFixedSizesSpinner = new BSpinner("fixed font size",
426 		B_TRANSLATE("Default fixed font size:"),
427 		new BMessage(MSG_FIXED_FONT_SIZE_SELECTED));
428 	fFixedSizesSpinner->SetAlignment(B_ALIGN_RIGHT);
429 
430 	BView* view = BGridLayoutBuilder(spacing / 2, spacing / 2)
431 		.Add(fStandardFontView->CreateFontsLabelLayoutItem(), 0, 0)
432 		.Add(fStandardFontView->CreateFontsMenuBarLayoutItem(), 1, 0)
433 		.Add(fStandardSizesSpinner->CreateLabelLayoutItem(), 2, 0)
434 		.Add(fStandardSizesSpinner->CreateTextViewLayoutItem(), 3, 0)
435 		.Add(fStandardFontView->PreviewBox(), 1, 1, 3)
436 		.Add(fSerifFontView->CreateFontsLabelLayoutItem(), 0, 2)
437 		.Add(fSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 2)
438 		.Add(fSerifFontView->PreviewBox(), 1, 3, 3)
439 		.Add(fSansSerifFontView->CreateFontsLabelLayoutItem(), 0, 4)
440 		.Add(fSansSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 4)
441 		.Add(fSansSerifFontView->PreviewBox(), 1, 5, 3)
442 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing / 2), 0, 6, 2)
443 		.Add(fFixedFontView->CreateFontsLabelLayoutItem(), 0, 7)
444 		.Add(fFixedFontView->CreateFontsMenuBarLayoutItem(), 1, 7)
445 		.Add(fFixedSizesSpinner->CreateLabelLayoutItem(), 2, 7)
446 		.Add(fFixedSizesSpinner->CreateTextViewLayoutItem(), 3, 7)
447 		.Add(fFixedFontView->PreviewBox(), 1, 8, 3)
448 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
449 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
450 		.View();
451 
452 	view->SetName(B_TRANSLATE("Fonts"));
453 	return view;
454 }
455 
456 
457 BView*
458 SettingsWindow::_CreateProxyPage(float spacing)
459 {
460 	fUseProxyCheckBox = new BCheckBox("use proxy",
461 		B_TRANSLATE("Use proxy server to connect to the internet"),
462 		new BMessage(MSG_USE_PROXY_CHANGED));
463 	fUseProxyCheckBox->SetValue(B_CONTROL_ON);
464 
465 	fProxyAddressControl = new BTextControl("proxy address",
466 		B_TRANSLATE("Proxy server address:"), "",
467 		new BMessage(MSG_PROXY_ADDRESS_CHANGED));
468 	fProxyAddressControl->SetModificationMessage(
469 		new BMessage(MSG_PROXY_ADDRESS_CHANGED));
470 	fProxyAddressControl->SetText(
471 		fSettings->GetValue(kSettingsKeyProxyAddress, ""));
472 
473 	fProxyPortControl = new BTextControl("proxy port",
474 		B_TRANSLATE("Proxy server port:"), "",
475 		new BMessage(MSG_PROXY_PORT_CHANGED));
476 	fProxyPortControl->SetModificationMessage(
477 		new BMessage(MSG_PROXY_PORT_CHANGED));
478 	fProxyPortControl->SetText(
479 		fSettings->GetValue(kSettingsKeyProxyPort, ""));
480 
481 	fUseProxyAuthCheckBox = new BCheckBox("use authentication",
482 		B_TRANSLATE("Proxy server requires authentication"),
483 		new BMessage(MSG_USE_PROXY_AUTH_CHANGED));
484 	fUseProxyAuthCheckBox->SetValue(B_CONTROL_ON);
485 
486 	fProxyUsernameControl = new BTextControl("proxy username",
487 		B_TRANSLATE("Proxy username:"), "",
488 		new BMessage(MSG_PROXY_USERNAME_CHANGED));
489 	fProxyUsernameControl->SetModificationMessage(
490 		new BMessage(MSG_PROXY_USERNAME_CHANGED));
491 	fProxyUsernameControl->SetText(
492 		fSettings->GetValue(kSettingsKeyProxyUsername, ""));
493 
494 	fProxyPasswordControl = new BTextControl("proxy password",
495 		B_TRANSLATE("Proxy password:"), "",
496 		new BMessage(MSG_PROXY_PASSWORD_CHANGED));
497 	fProxyPasswordControl->SetModificationMessage(
498 		new BMessage(MSG_PROXY_PASSWORD_CHANGED));
499 	fProxyPasswordControl->TextView()->HideTyping(true);
500 	fProxyPasswordControl->SetText(
501 		fSettings->GetValue(kSettingsKeyProxyPassword, ""));
502 
503 	BView* view = BGridLayoutBuilder(spacing / 2, spacing / 2)
504 		.Add(fUseProxyCheckBox, 0, 0, 2)
505 		.Add(fProxyAddressControl->CreateLabelLayoutItem(), 0, 1)
506 		.Add(fProxyAddressControl->CreateTextViewLayoutItem(), 1, 1, 2)
507 		.Add(fProxyPortControl->CreateLabelLayoutItem(), 0, 2)
508 		.Add(fProxyPortControl->CreateTextViewLayoutItem(), 1, 2, 2)
509 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing), 0, 3)
510 		.Add(fUseProxyAuthCheckBox, 0, 4, 2)
511 		.Add(fProxyUsernameControl->CreateLabelLayoutItem(), 0, 5)
512 		.Add(fProxyUsernameControl->CreateTextViewLayoutItem(), 1, 5, 2)
513 		.Add(fProxyPasswordControl->CreateLabelLayoutItem(), 0, 6)
514 		.Add(fProxyPasswordControl->CreateTextViewLayoutItem(), 1, 6, 2)
515 		.Add(BSpaceLayoutItem::CreateGlue(), 0, 7)
516 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
517 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
518 		.View();
519 
520 	view->SetName(B_TRANSLATE("Proxy server"));
521 	return view;
522 }
523 
524 
525 void
526 SettingsWindow::_SetupFontSelectionView(FontSelectionView* view,
527 	BMessage* message)
528 {
529 	AddHandler(view);
530 	view->AttachedToLooper();
531 	view->SetMessage(message);
532 	view->SetTarget(this);
533 }
534 
535 
536 // #pragma mark -
537 
538 
539 bool
540 SettingsWindow::_CanApplySettings() const
541 {
542 	bool canApply = false;
543 
544 	// General settings
545 	canApply = canApply || (strcmp(fStartPageControl->Text(),
546 		fSettings->GetValue(kSettingsKeyStartPageURL,
547 			kDefaultStartPageURL)) != 0);
548 
549 	canApply = canApply || (strcmp(fSearchPageControl->Text(),
550 		fSettings->GetValue(kSettingsKeySearchPageURL,
551 			kDefaultSearchPageURL)) != 0);
552 
553 	canApply = canApply || (strcmp(fDownloadFolderControl->Text(),
554 		fSettings->GetValue(kSettingsKeyDownloadPath,
555 			kDefaultDownloadPath)) != 0);
556 
557 	canApply = canApply || ((fShowTabsIfOnlyOnePage->Value() == B_CONTROL_ON)
558 		!= fSettings->GetValue(kSettingsKeyShowTabsIfSinglePageOpen, true));
559 
560 	canApply = canApply || (
561 		(fAutoHideInterfaceInFullscreenMode->Value() == B_CONTROL_ON)
562 		!= fSettings->GetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
563 			false));
564 
565 	canApply = canApply || (
566 		(fAutoHidePointer->Value() == B_CONTROL_ON)
567 		!= fSettings->GetValue(kSettingsKeyAutoHidePointer, false));
568 
569 	canApply = canApply || ((fShowHomeButton->Value() == B_CONTROL_ON)
570 		!= fSettings->GetValue(kSettingsKeyShowHomeButton, true));
571 
572 	canApply = canApply || (fDaysInHistory->Value()
573 		!= BrowsingHistory::DefaultInstance()->MaxHistoryItemAge());
574 
575 	// Start up policy
576 	canApply = canApply || (_StartUpPolicy()
577 		!= fSettings->GetValue(kSettingsKeyStartUpPolicy,
578 			(uint32)ResumePriorSession));
579 
580 	// New window policy
581 	canApply = canApply || (_NewWindowPolicy()
582 		!= fSettings->GetValue(kSettingsKeyNewWindowPolicy,
583 			(uint32)OpenStartPage));
584 
585 	// New tab policy
586 	canApply = canApply || (_NewTabPolicy()
587 		!= fSettings->GetValue(kSettingsKeyNewTabPolicy,
588 			(uint32)OpenBlankPage));
589 
590 	// Font settings
591 	canApply = canApply || (fStandardFontView->Font()
592 		!= fSettings->GetValue("standard font", *be_plain_font));
593 
594 	canApply = canApply || (fSerifFontView->Font()
595 		!= fSettings->GetValue("serif font", _FindDefaultSerifFont()));
596 
597 	canApply = canApply || (fSansSerifFontView->Font()
598 		!= fSettings->GetValue("sans serif font", *be_plain_font));
599 
600 	canApply = canApply || (fFixedFontView->Font()
601 		!= fSettings->GetValue("fixed font", *be_fixed_font));
602 
603 	canApply = canApply || (fStandardSizesSpinner->Value()
604 		!= fSettings->GetValue("standard font size", kDefaultFontSize));
605 
606 	canApply = canApply || (fFixedSizesSpinner->Value()
607 		!= fSettings->GetValue("fixed font size", kDefaultFontSize));
608 
609 	// Proxy settings
610 	canApply = canApply || ((fUseProxyCheckBox->Value() == B_CONTROL_ON)
611 		!= fSettings->GetValue(kSettingsKeyUseProxy, false));
612 
613 	canApply = canApply || (strcmp(fProxyAddressControl->Text(),
614 		fSettings->GetValue(kSettingsKeyProxyAddress, "")) != 0);
615 
616 	canApply = canApply || (_ProxyPort()
617 		!= fSettings->GetValue(kSettingsKeyProxyPort, (uint32)0));
618 
619 	canApply = canApply || ((fUseProxyAuthCheckBox->Value() == B_CONTROL_ON)
620 		!= fSettings->GetValue(kSettingsKeyUseProxyAuth, false));
621 
622 	canApply = canApply || (strcmp(fProxyUsernameControl->Text(),
623 		fSettings->GetValue(kSettingsKeyProxyUsername, "")) != 0);
624 
625 	canApply = canApply || (strcmp(fProxyPasswordControl->Text(),
626 		fSettings->GetValue(kSettingsKeyProxyPassword, "")) != 0);
627 
628 	return canApply;
629 }
630 
631 
632 void
633 SettingsWindow::_ApplySettings()
634 {
635 	// Store general settings
636 	BrowsingHistory::DefaultInstance()->SetMaxHistoryItemAge(
637 		(uint32)fDaysInHistory->Value());
638 	fSettings->SetValue(kSettingsKeyStartPageURL, fStartPageControl->Text());
639 	fSettings->SetValue(kSettingsKeySearchPageURL, fSearchPageControl->Text());
640 	fSettings->SetValue(kSettingsKeyDownloadPath, fDownloadFolderControl->Text());
641 	fSettings->SetValue(kSettingsKeyShowTabsIfSinglePageOpen,
642 		fShowTabsIfOnlyOnePage->Value() == B_CONTROL_ON);
643 	fSettings->SetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
644 		fAutoHideInterfaceInFullscreenMode->Value() == B_CONTROL_ON);
645 	fSettings->SetValue(kSettingsKeyAutoHidePointer,
646 		fAutoHidePointer->Value() == B_CONTROL_ON);
647 	fSettings->SetValue(kSettingsKeyShowHomeButton,
648 		fShowHomeButton->Value() == B_CONTROL_ON);
649 
650 	// New page policies
651 	fSettings->SetValue(kSettingsKeyStartUpPolicy, _StartUpPolicy());
652 	fSettings->SetValue(kSettingsKeyNewWindowPolicy, _NewWindowPolicy());
653 	fSettings->SetValue(kSettingsKeyNewTabPolicy, _NewTabPolicy());
654 
655 	// Store fond settings
656 	fSettings->SetValue("standard font", fStandardFontView->Font());
657 	fSettings->SetValue("serif font", fSerifFontView->Font());
658 	fSettings->SetValue("sans serif font", fSansSerifFontView->Font());
659 	fSettings->SetValue("fixed font", fFixedFontView->Font());
660 	int32 standardFontSize = fStandardSizesSpinner->Value();
661 	int32 fixedFontSize = fFixedSizesSpinner->Value();
662 	fSettings->SetValue("standard font size", standardFontSize);
663 	fSettings->SetValue("fixed font size", fixedFontSize);
664 
665 	// Store proxy settings
666 
667 	fSettings->SetValue(kSettingsKeyUseProxy,
668 		fUseProxyCheckBox->Value() == B_CONTROL_ON);
669 	fSettings->SetValue(kSettingsKeyProxyAddress,
670 		fProxyAddressControl->Text());
671 	uint32 proxyPort = _ProxyPort();
672 	fSettings->SetValue(kSettingsKeyProxyPort, proxyPort);
673 	fSettings->SetValue(kSettingsKeyUseProxyAuth,
674 		fUseProxyAuthCheckBox->Value() == B_CONTROL_ON);
675 	fSettings->SetValue(kSettingsKeyProxyUsername,
676 		fProxyUsernameControl->Text());
677 	fSettings->SetValue(kSettingsKeyProxyPassword,
678 		fProxyPasswordControl->Text());
679 
680 	fSettings->Save();
681 
682 	// Apply settings to default web page settings.
683 	BWebSettings::Default()->SetStandardFont(fStandardFontView->Font());
684 	BWebSettings::Default()->SetSerifFont(fSerifFontView->Font());
685 	BWebSettings::Default()->SetSansSerifFont(fSansSerifFontView->Font());
686 	BWebSettings::Default()->SetFixedFont(fFixedFontView->Font());
687 	BWebSettings::Default()->SetDefaultStandardFontSize(standardFontSize);
688 	BWebSettings::Default()->SetDefaultFixedFontSize(fixedFontSize);
689 
690 	if (fUseProxyCheckBox->Value() == B_CONTROL_ON) {
691 		if (fUseProxyAuthCheckBox->Value() == B_CONTROL_ON) {
692 			BWebSettings::Default()->SetProxyInfo(fProxyAddressControl->Text(),
693 				proxyPort, B_PROXY_TYPE_HTTP, fProxyUsernameControl->Text(),
694 				fProxyPasswordControl->Text());
695 		} else {
696 			BWebSettings::Default()->SetProxyInfo(fProxyAddressControl->Text(),
697 				proxyPort, B_PROXY_TYPE_HTTP, "", "");
698 		}
699 	} else
700 		BWebSettings::Default()->SetProxyInfo();
701 
702 	// This will find all currently instantiated page settings and apply
703 	// the default values, unless the page settings have local overrides.
704 	BWebSettings::Default()->Apply();
705 
706 	_ValidateControlsEnabledStatus();
707 }
708 
709 
710 void
711 SettingsWindow::_RevertSettings()
712 {
713 	fStartPageControl->SetText(
714 		fSettings->GetValue(kSettingsKeyStartPageURL, kDefaultStartPageURL));
715 
716 	fSearchPageControl->SetText(
717 		fSettings->GetValue(kSettingsKeySearchPageURL, kDefaultSearchPageURL));
718 
719 	fDownloadFolderControl->SetText(
720 		fSettings->GetValue(kSettingsKeyDownloadPath, kDefaultDownloadPath));
721 	fShowTabsIfOnlyOnePage->SetValue(
722 		fSettings->GetValue(kSettingsKeyShowTabsIfSinglePageOpen, true));
723 	fAutoHideInterfaceInFullscreenMode->SetValue(
724 		fSettings->GetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
725 			false));
726 	fAutoHidePointer->SetValue(
727 		fSettings->GetValue(kSettingsKeyAutoHidePointer, false));
728 	fShowHomeButton->SetValue(
729 		fSettings->GetValue(kSettingsKeyShowHomeButton, true));
730 
731 	fDaysInHistory->SetValue(
732 		BrowsingHistory::DefaultInstance()->MaxHistoryItemAge());
733 
734 	// Start Up policy
735 	uint32 startUpPolicy = fSettings->GetValue(kSettingsKeyStartUpPolicy,
736 		(uint32)ResumePriorSession);
737 	switch (startUpPolicy) {
738 		default:
739 		case ResumePriorSession:
740 			fStartUpBehaviorResumePriorSession->SetMarked(true);
741 			break;
742 		case StartNewSession:
743 			fStartUpBehaviorStartNewSession->SetMarked(true);
744 			break;
745 	}
746 
747 	// New window policy
748 	uint32 newWindowPolicy = fSettings->GetValue(kSettingsKeyNewWindowPolicy,
749 		(uint32)OpenStartPage);
750 	switch (newWindowPolicy) {
751 		default:
752 		case OpenStartPage:
753 			fNewWindowBehaviorOpenHomeItem->SetMarked(true);
754 			break;
755 		case OpenSearchPage:
756 			fNewWindowBehaviorOpenSearchItem->SetMarked(true);
757 			break;
758 		case OpenBlankPage:
759 			fNewWindowBehaviorOpenBlankItem->SetMarked(true);
760 			break;
761 	}
762 
763 	// New tab policy
764 	uint32 newTabPolicy = fSettings->GetValue(kSettingsKeyNewTabPolicy,
765 		(uint32)OpenBlankPage);
766 	switch (newTabPolicy) {
767 		default:
768 		case OpenBlankPage:
769 			fNewTabBehaviorOpenBlankItem->SetMarked(true);
770 			break;
771 		case OpenStartPage:
772 			fNewTabBehaviorOpenHomeItem->SetMarked(true);
773 			break;
774 		case OpenSearchPage:
775 			fNewTabBehaviorOpenSearchItem->SetMarked(true);
776 			break;
777 		case CloneCurrentPage:
778 			fNewTabBehaviorCloneCurrentItem->SetMarked(true);
779 			break;
780 	}
781 
782 	// Font settings
783 	int32 defaultFontSize = fSettings->GetValue("standard font size",
784 		kDefaultFontSize);
785 	int32 defaultFixedFontSize = fSettings->GetValue("fixed font size",
786 		kDefaultFontSize);
787 
788 	fStandardSizesSpinner->SetValue(defaultFontSize);
789 	fFixedSizesSpinner->SetValue(defaultFixedFontSize);
790 
791 	fStandardFontView->SetFont(fSettings->GetValue("standard font",
792 		*be_plain_font), defaultFontSize);
793 	fSerifFontView->SetFont(fSettings->GetValue("serif font",
794 		_FindDefaultSerifFont()), defaultFontSize);
795 	fSansSerifFontView->SetFont(fSettings->GetValue("sans serif font",
796 		*be_plain_font), defaultFontSize);
797 	fFixedFontView->SetFont(fSettings->GetValue("fixed font",
798 		*be_fixed_font), defaultFixedFontSize);
799 
800 	// Proxy settings
801 	fUseProxyCheckBox->SetValue(fSettings->GetValue(kSettingsKeyUseProxy,
802 		false));
803 	fProxyAddressControl->SetText(fSettings->GetValue(kSettingsKeyProxyAddress,
804 		""));
805 	BString keyProxyPort;
806 	keyProxyPort << fSettings->GetValue(kSettingsKeyProxyPort, (uint32)0);
807 	fProxyPortControl->SetText(keyProxyPort.String());
808 	fUseProxyAuthCheckBox->SetValue(fSettings->GetValue(kSettingsKeyUseProxyAuth,
809 		false));
810 	fProxyUsernameControl->SetText(fSettings->GetValue(kSettingsKeyProxyUsername,
811 		""));
812 	fProxyPasswordControl->SetText(fSettings->GetValue(kSettingsKeyProxyPassword,
813 		""));
814 
815 	_ValidateControlsEnabledStatus();
816 }
817 
818 
819 void
820 SettingsWindow::_ChooseDownloadFolder(const BMessage* message)
821 {
822 	if (fOpenFilePanel == NULL) {
823 		BMessenger target(this);
824 		fOpenFilePanel = new (std::nothrow) BFilePanel(B_OPEN_PANEL,
825 			&target, NULL, B_DIRECTORY_NODE);
826 	}
827 	BMessage panelMessage(MSG_HANDLE_DOWNLOAD_FOLDER);
828 	fOpenFilePanel->SetMessage(&panelMessage);
829 	fOpenFilePanel->Show();
830 }
831 
832 
833 void
834 SettingsWindow:: _HandleDownloadPanelResult(BFilePanel* panel,
835 	const BMessage* message)
836 {
837 	entry_ref ref;
838 	if (message->FindRef("refs", 0, &ref) == B_OK)
839 	{
840 		BPath path(&ref);
841 		fDownloadFolderControl->SetText(path.Path());
842 	}
843 }
844 
845 
846 void
847 SettingsWindow::_ValidateControlsEnabledStatus()
848 {
849 	bool canApply = _CanApplySettings();
850 	fApplyButton->SetEnabled(canApply);
851 	fRevertButton->SetEnabled(canApply);
852 	// Let the Cancel button be enabled always, as another way to close the
853 	// window...
854 	fCancelButton->SetEnabled(true);
855 
856 	bool useProxy = fUseProxyCheckBox->Value() == B_CONTROL_ON;
857 	fProxyAddressControl->SetEnabled(useProxy);
858 	fProxyPortControl->SetEnabled(useProxy);
859 	fUseProxyAuthCheckBox->SetEnabled(useProxy);
860 	bool useProxyAuth = useProxy && fUseProxyAuthCheckBox->Value() == B_CONTROL_ON;
861 	fProxyUsernameControl->SetEnabled(useProxyAuth);
862 	fProxyPasswordControl->SetEnabled(useProxyAuth);
863 }
864 
865 
866 // #pragma mark -
867 
868 
869 uint32
870 SettingsWindow::_StartUpPolicy() const
871 {
872 	uint32 startUpPolicy = ResumePriorSession;
873 	BMenuItem* markedItem = fStartUpBehaviorMenu->Menu()->FindMarked();
874 	if (markedItem == fStartUpBehaviorStartNewSession)
875 		startUpPolicy = StartNewSession;
876 	return startUpPolicy;
877 }
878 
879 uint32
880 SettingsWindow::_NewWindowPolicy() const
881 {
882 	uint32 newWindowPolicy = OpenStartPage;
883 	BMenuItem* markedItem = fNewWindowBehaviorMenu->Menu()->FindMarked();
884 	if (markedItem == fNewWindowBehaviorOpenSearchItem)
885 		newWindowPolicy = OpenSearchPage;
886 	else if (markedItem == fNewWindowBehaviorOpenBlankItem)
887 		newWindowPolicy = OpenBlankPage;
888 	return newWindowPolicy;
889 }
890 
891 
892 uint32
893 SettingsWindow::_NewTabPolicy() const
894 {
895 	uint32 newTabPolicy = OpenBlankPage;
896 	BMenuItem* markedItem = fNewTabBehaviorMenu->Menu()->FindMarked();
897 	if (markedItem == fNewTabBehaviorCloneCurrentItem)
898 		newTabPolicy = CloneCurrentPage;
899 	else if (markedItem == fNewTabBehaviorOpenHomeItem)
900 		newTabPolicy = OpenStartPage;
901 	else if (markedItem == fNewTabBehaviorOpenSearchItem)
902 		newTabPolicy = OpenSearchPage;
903 	return newTabPolicy;
904 }
905 
906 
907 BFont
908 SettingsWindow::_FindDefaultSerifFont() const
909 {
910 	// Default to the first "serif" font we find.
911 	BFont serifFont(*be_plain_font);
912 	font_family family;
913 	int32 familyCount = count_font_families();
914 	for (int32 i = 0; i < familyCount; i++) {
915 		if (get_font_family(i, &family) == B_OK) {
916 			BString familyString(family);
917 			if (familyString.IFindFirst("sans") >= 0)
918 				continue;
919 			if (familyString.IFindFirst("serif") >= 0) {
920 				serifFont.SetFamilyAndFace(family, B_REGULAR_FACE);
921 				break;
922 			}
923 		}
924 	}
925 	return serifFont;
926 }
927 
928 
929 uint32
930 SettingsWindow::_ProxyPort() const
931 {
932 	return atoul(fProxyPortControl->Text());
933 }
934 
935 
936