xref: /haiku/src/apps/webpositive/SettingsWindow.cpp (revision 5ffbe7d778424c9c59f00b37a3baff5c4c648790)
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(fNewTabBehaviorMenu->CreateLabelLayoutItem(), 0, 4)
381 			.Add(fNewTabBehaviorMenu->CreateMenuBarLayoutItem(), 1, 4)
382 
383 			.Add(fDownloadFolderControl->CreateLabelLayoutItem(), 0, 5)
384 			.Add(fDownloadFolderControl->CreateTextViewLayoutItem(), 1, 5)
385 			.Add(fChooseButton, 2, 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 		.AddGroup(B_HORIZONTAL)
397 			.Add(fDaysInHistory)
398 			.AddGlue()
399 			.End()
400 		.AddGlue()
401 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
402 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
403 		.TopView()
404 	;
405 	view->SetName(B_TRANSLATE("General"));
406 	return view;
407 }
408 
409 
410 BView*
411 SettingsWindow::_CreateFontsPage(float spacing)
412 {
413 	fStandardFontView = new FontSelectionView("standard",
414 		B_TRANSLATE("Standard font:"), true, be_plain_font);
415 	BFont defaultSerifFont = _FindDefaultSerifFont();
416 	fSerifFontView = new FontSelectionView("serif",
417 		B_TRANSLATE("Serif font:"), true, &defaultSerifFont);
418 	fSansSerifFontView = new FontSelectionView("sans serif",
419 		B_TRANSLATE("Sans serif font:"), true, be_plain_font);
420 	fFixedFontView = new FontSelectionView("fixed",
421 		B_TRANSLATE("Fixed font:"), true, be_fixed_font);
422 
423 	fStandardSizesSpinner = new BSpinner("standard font size",
424 		B_TRANSLATE("Default standard font size:"),
425 		new BMessage(MSG_STANDARD_FONT_SIZE_SELECTED));
426 	fStandardSizesSpinner->SetAlignment(B_ALIGN_RIGHT);
427 
428 	fFixedSizesSpinner = new BSpinner("fixed font size",
429 		B_TRANSLATE("Default fixed font size:"),
430 		new BMessage(MSG_FIXED_FONT_SIZE_SELECTED));
431 	fFixedSizesSpinner->SetAlignment(B_ALIGN_RIGHT);
432 
433 	BView* view = BGridLayoutBuilder(spacing / 2, spacing / 2)
434 		.Add(fStandardFontView->CreateFontsLabelLayoutItem(), 0, 0)
435 		.Add(fStandardFontView->CreateFontsMenuBarLayoutItem(), 1, 0)
436 		.Add(fStandardSizesSpinner->CreateLabelLayoutItem(), 2, 0)
437 		.Add(fStandardSizesSpinner->CreateTextViewLayoutItem(), 3, 0)
438 		.Add(fStandardFontView->PreviewBox(), 1, 1, 3)
439 		.Add(fSerifFontView->CreateFontsLabelLayoutItem(), 0, 2)
440 		.Add(fSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 2)
441 		.Add(fSerifFontView->PreviewBox(), 1, 3, 3)
442 		.Add(fSansSerifFontView->CreateFontsLabelLayoutItem(), 0, 4)
443 		.Add(fSansSerifFontView->CreateFontsMenuBarLayoutItem(), 1, 4)
444 		.Add(fSansSerifFontView->PreviewBox(), 1, 5, 3)
445 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing / 2), 0, 6, 2)
446 		.Add(fFixedFontView->CreateFontsLabelLayoutItem(), 0, 7)
447 		.Add(fFixedFontView->CreateFontsMenuBarLayoutItem(), 1, 7)
448 		.Add(fFixedSizesSpinner->CreateLabelLayoutItem(), 2, 7)
449 		.Add(fFixedSizesSpinner->CreateTextViewLayoutItem(), 3, 7)
450 		.Add(fFixedFontView->PreviewBox(), 1, 8, 3)
451 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
452 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
453 		.View();
454 
455 	view->SetName(B_TRANSLATE("Fonts"));
456 	return view;
457 }
458 
459 
460 BView*
461 SettingsWindow::_CreateProxyPage(float spacing)
462 {
463 	fUseProxyCheckBox = new BCheckBox("use proxy",
464 		B_TRANSLATE("Use proxy server to connect to the internet"),
465 		new BMessage(MSG_USE_PROXY_CHANGED));
466 	fUseProxyCheckBox->SetValue(B_CONTROL_ON);
467 
468 	fProxyAddressControl = new BTextControl("proxy address",
469 		B_TRANSLATE("Proxy server address:"), "",
470 		new BMessage(MSG_PROXY_ADDRESS_CHANGED));
471 	fProxyAddressControl->SetModificationMessage(
472 		new BMessage(MSG_PROXY_ADDRESS_CHANGED));
473 	fProxyAddressControl->SetText(
474 		fSettings->GetValue(kSettingsKeyProxyAddress, ""));
475 
476 	fProxyPortControl = new BTextControl("proxy port",
477 		B_TRANSLATE("Proxy server port:"), "",
478 		new BMessage(MSG_PROXY_PORT_CHANGED));
479 	fProxyPortControl->SetModificationMessage(
480 		new BMessage(MSG_PROXY_PORT_CHANGED));
481 	fProxyPortControl->SetText(
482 		fSettings->GetValue(kSettingsKeyProxyPort, ""));
483 
484 	fUseProxyAuthCheckBox = new BCheckBox("use authentication",
485 		B_TRANSLATE("Proxy server requires authentication"),
486 		new BMessage(MSG_USE_PROXY_AUTH_CHANGED));
487 	fUseProxyAuthCheckBox->SetValue(B_CONTROL_ON);
488 
489 	fProxyUsernameControl = new BTextControl("proxy username",
490 		B_TRANSLATE("Proxy username:"), "",
491 		new BMessage(MSG_PROXY_USERNAME_CHANGED));
492 	fProxyUsernameControl->SetModificationMessage(
493 		new BMessage(MSG_PROXY_USERNAME_CHANGED));
494 	fProxyUsernameControl->SetText(
495 		fSettings->GetValue(kSettingsKeyProxyUsername, ""));
496 
497 	fProxyPasswordControl = new BTextControl("proxy password",
498 		B_TRANSLATE("Proxy password:"), "",
499 		new BMessage(MSG_PROXY_PASSWORD_CHANGED));
500 	fProxyPasswordControl->SetModificationMessage(
501 		new BMessage(MSG_PROXY_PASSWORD_CHANGED));
502 	fProxyPasswordControl->TextView()->HideTyping(true);
503 	fProxyPasswordControl->SetText(
504 		fSettings->GetValue(kSettingsKeyProxyPassword, ""));
505 
506 	BView* view = BGridLayoutBuilder(spacing / 2, spacing / 2)
507 		.Add(fUseProxyCheckBox, 0, 0, 2)
508 		.Add(fProxyAddressControl->CreateLabelLayoutItem(), 0, 1)
509 		.Add(fProxyAddressControl->CreateTextViewLayoutItem(), 1, 1, 2)
510 		.Add(fProxyPortControl->CreateLabelLayoutItem(), 0, 2)
511 		.Add(fProxyPortControl->CreateTextViewLayoutItem(), 1, 2, 2)
512 		.Add(BSpaceLayoutItem::CreateVerticalStrut(spacing), 0, 3)
513 		.Add(fUseProxyAuthCheckBox, 0, 4, 2)
514 		.Add(fProxyUsernameControl->CreateLabelLayoutItem(), 0, 5)
515 		.Add(fProxyUsernameControl->CreateTextViewLayoutItem(), 1, 5, 2)
516 		.Add(fProxyPasswordControl->CreateLabelLayoutItem(), 0, 6)
517 		.Add(fProxyPasswordControl->CreateTextViewLayoutItem(), 1, 6, 2)
518 		.Add(BSpaceLayoutItem::CreateGlue(), 0, 7)
519 		.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
520 			B_USE_WINDOW_SPACING, B_USE_DEFAULT_SPACING)
521 		.View();
522 
523 	view->SetName(B_TRANSLATE("Proxy server"));
524 	return view;
525 }
526 
527 
528 void
529 SettingsWindow::_SetupFontSelectionView(FontSelectionView* view,
530 	BMessage* message)
531 {
532 	AddHandler(view);
533 	view->AttachedToLooper();
534 	view->SetMessage(message);
535 	view->SetTarget(this);
536 }
537 
538 
539 // #pragma mark -
540 
541 
542 bool
543 SettingsWindow::_CanApplySettings() const
544 {
545 	bool canApply = false;
546 
547 	// General settings
548 	canApply = canApply || (strcmp(fStartPageControl->Text(),
549 		fSettings->GetValue(kSettingsKeyStartPageURL,
550 			kDefaultStartPageURL)) != 0);
551 
552 	canApply = canApply || (strcmp(fSearchPageControl->Text(),
553 		fSettings->GetValue(kSettingsKeySearchPageURL,
554 			kDefaultSearchPageURL)) != 0);
555 
556 	canApply = canApply || (strcmp(fDownloadFolderControl->Text(),
557 		fSettings->GetValue(kSettingsKeyDownloadPath,
558 			kDefaultDownloadPath)) != 0);
559 
560 	canApply = canApply || ((fShowTabsIfOnlyOnePage->Value() == B_CONTROL_ON)
561 		!= fSettings->GetValue(kSettingsKeyShowTabsIfSinglePageOpen, true));
562 
563 	canApply = canApply || (
564 		(fAutoHideInterfaceInFullscreenMode->Value() == B_CONTROL_ON)
565 		!= fSettings->GetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
566 			false));
567 
568 	canApply = canApply || (
569 		(fAutoHidePointer->Value() == B_CONTROL_ON)
570 		!= fSettings->GetValue(kSettingsKeyAutoHidePointer, false));
571 
572 	canApply = canApply || ((fShowHomeButton->Value() == B_CONTROL_ON)
573 		!= fSettings->GetValue(kSettingsKeyShowHomeButton, true));
574 
575 	canApply = canApply || (fDaysInHistory->Value()
576 		!= BrowsingHistory::DefaultInstance()->MaxHistoryItemAge());
577 
578 	// Start up policy
579 	canApply = canApply || (_StartUpPolicy()
580 		!= fSettings->GetValue(kSettingsKeyStartUpPolicy,
581 			(uint32)ResumePriorSession));
582 
583 	// New window policy
584 	canApply = canApply || (_NewWindowPolicy()
585 		!= fSettings->GetValue(kSettingsKeyNewWindowPolicy,
586 			(uint32)OpenStartPage));
587 
588 	// New tab policy
589 	canApply = canApply || (_NewTabPolicy()
590 		!= fSettings->GetValue(kSettingsKeyNewTabPolicy,
591 			(uint32)OpenBlankPage));
592 
593 	// Font settings
594 	canApply = canApply || (fStandardFontView->Font()
595 		!= fSettings->GetValue("standard font", *be_plain_font));
596 
597 	canApply = canApply || (fSerifFontView->Font()
598 		!= fSettings->GetValue("serif font", _FindDefaultSerifFont()));
599 
600 	canApply = canApply || (fSansSerifFontView->Font()
601 		!= fSettings->GetValue("sans serif font", *be_plain_font));
602 
603 	canApply = canApply || (fFixedFontView->Font()
604 		!= fSettings->GetValue("fixed font", *be_fixed_font));
605 
606 	canApply = canApply || (fStandardSizesSpinner->Value()
607 		!= fSettings->GetValue("standard font size", kDefaultFontSize));
608 
609 	canApply = canApply || (fFixedSizesSpinner->Value()
610 		!= fSettings->GetValue("fixed font size", kDefaultFontSize));
611 
612 	// Proxy settings
613 	canApply = canApply || ((fUseProxyCheckBox->Value() == B_CONTROL_ON)
614 		!= fSettings->GetValue(kSettingsKeyUseProxy, false));
615 
616 	canApply = canApply || (strcmp(fProxyAddressControl->Text(),
617 		fSettings->GetValue(kSettingsKeyProxyAddress, "")) != 0);
618 
619 	canApply = canApply || (_ProxyPort()
620 		!= fSettings->GetValue(kSettingsKeyProxyPort, (uint32)0));
621 
622 	canApply = canApply || ((fUseProxyAuthCheckBox->Value() == B_CONTROL_ON)
623 		!= fSettings->GetValue(kSettingsKeyUseProxyAuth, false));
624 
625 	canApply = canApply || (strcmp(fProxyUsernameControl->Text(),
626 		fSettings->GetValue(kSettingsKeyProxyUsername, "")) != 0);
627 
628 	canApply = canApply || (strcmp(fProxyPasswordControl->Text(),
629 		fSettings->GetValue(kSettingsKeyProxyPassword, "")) != 0);
630 
631 	return canApply;
632 }
633 
634 
635 void
636 SettingsWindow::_ApplySettings()
637 {
638 	// Store general settings
639 	BrowsingHistory::DefaultInstance()->SetMaxHistoryItemAge(
640 		(uint32)fDaysInHistory->Value());
641 	fSettings->SetValue(kSettingsKeyStartPageURL, fStartPageControl->Text());
642 	fSettings->SetValue(kSettingsKeySearchPageURL, fSearchPageControl->Text());
643 	fSettings->SetValue(kSettingsKeyDownloadPath, fDownloadFolderControl->Text());
644 	fSettings->SetValue(kSettingsKeyShowTabsIfSinglePageOpen,
645 		fShowTabsIfOnlyOnePage->Value() == B_CONTROL_ON);
646 	fSettings->SetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
647 		fAutoHideInterfaceInFullscreenMode->Value() == B_CONTROL_ON);
648 	fSettings->SetValue(kSettingsKeyAutoHidePointer,
649 		fAutoHidePointer->Value() == B_CONTROL_ON);
650 	fSettings->SetValue(kSettingsKeyShowHomeButton,
651 		fShowHomeButton->Value() == B_CONTROL_ON);
652 
653 	// New page policies
654 	fSettings->SetValue(kSettingsKeyStartUpPolicy, _StartUpPolicy());
655 	fSettings->SetValue(kSettingsKeyNewWindowPolicy, _NewWindowPolicy());
656 	fSettings->SetValue(kSettingsKeyNewTabPolicy, _NewTabPolicy());
657 
658 	// Store fond settings
659 	fSettings->SetValue("standard font", fStandardFontView->Font());
660 	fSettings->SetValue("serif font", fSerifFontView->Font());
661 	fSettings->SetValue("sans serif font", fSansSerifFontView->Font());
662 	fSettings->SetValue("fixed font", fFixedFontView->Font());
663 	int32 standardFontSize = fStandardSizesSpinner->Value();
664 	int32 fixedFontSize = fFixedSizesSpinner->Value();
665 	fSettings->SetValue("standard font size", standardFontSize);
666 	fSettings->SetValue("fixed font size", fixedFontSize);
667 
668 	// Store proxy settings
669 
670 	fSettings->SetValue(kSettingsKeyUseProxy,
671 		fUseProxyCheckBox->Value() == B_CONTROL_ON);
672 	fSettings->SetValue(kSettingsKeyProxyAddress,
673 		fProxyAddressControl->Text());
674 	uint32 proxyPort = _ProxyPort();
675 	fSettings->SetValue(kSettingsKeyProxyPort, proxyPort);
676 	fSettings->SetValue(kSettingsKeyUseProxyAuth,
677 		fUseProxyAuthCheckBox->Value() == B_CONTROL_ON);
678 	fSettings->SetValue(kSettingsKeyProxyUsername,
679 		fProxyUsernameControl->Text());
680 	fSettings->SetValue(kSettingsKeyProxyPassword,
681 		fProxyPasswordControl->Text());
682 
683 	fSettings->Save();
684 
685 	// Apply settings to default web page settings.
686 	BWebSettings::Default()->SetStandardFont(fStandardFontView->Font());
687 	BWebSettings::Default()->SetSerifFont(fSerifFontView->Font());
688 	BWebSettings::Default()->SetSansSerifFont(fSansSerifFontView->Font());
689 	BWebSettings::Default()->SetFixedFont(fFixedFontView->Font());
690 	BWebSettings::Default()->SetDefaultStandardFontSize(standardFontSize);
691 	BWebSettings::Default()->SetDefaultFixedFontSize(fixedFontSize);
692 
693 	if (fUseProxyCheckBox->Value() == B_CONTROL_ON) {
694 		if (fUseProxyAuthCheckBox->Value() == B_CONTROL_ON) {
695 			BWebSettings::Default()->SetProxyInfo(fProxyAddressControl->Text(),
696 				proxyPort, B_PROXY_TYPE_HTTP, fProxyUsernameControl->Text(),
697 				fProxyPasswordControl->Text());
698 		} else {
699 			BWebSettings::Default()->SetProxyInfo(fProxyAddressControl->Text(),
700 				proxyPort, B_PROXY_TYPE_HTTP, "", "");
701 		}
702 	} else
703 		BWebSettings::Default()->SetProxyInfo();
704 
705 	// This will find all currently instantiated page settings and apply
706 	// the default values, unless the page settings have local overrides.
707 	BWebSettings::Default()->Apply();
708 
709 	_ValidateControlsEnabledStatus();
710 }
711 
712 
713 void
714 SettingsWindow::_RevertSettings()
715 {
716 	fStartPageControl->SetText(
717 		fSettings->GetValue(kSettingsKeyStartPageURL, kDefaultStartPageURL));
718 
719 	fSearchPageControl->SetText(
720 		fSettings->GetValue(kSettingsKeySearchPageURL, kDefaultSearchPageURL));
721 
722 	fDownloadFolderControl->SetText(
723 		fSettings->GetValue(kSettingsKeyDownloadPath, kDefaultDownloadPath));
724 	fShowTabsIfOnlyOnePage->SetValue(
725 		fSettings->GetValue(kSettingsKeyShowTabsIfSinglePageOpen, true));
726 	fAutoHideInterfaceInFullscreenMode->SetValue(
727 		fSettings->GetValue(kSettingsKeyAutoHideInterfaceInFullscreenMode,
728 			false));
729 	fAutoHidePointer->SetValue(
730 		fSettings->GetValue(kSettingsKeyAutoHidePointer, false));
731 	fShowHomeButton->SetValue(
732 		fSettings->GetValue(kSettingsKeyShowHomeButton, true));
733 
734 	fDaysInHistory->SetValue(
735 		BrowsingHistory::DefaultInstance()->MaxHistoryItemAge());
736 
737 	// Start Up policy
738 	uint32 startUpPolicy = fSettings->GetValue(kSettingsKeyStartUpPolicy,
739 		(uint32)ResumePriorSession);
740 	switch (startUpPolicy) {
741 		default:
742 		case ResumePriorSession:
743 			fStartUpBehaviorResumePriorSession->SetMarked(true);
744 			break;
745 		case StartNewSession:
746 			fStartUpBehaviorStartNewSession->SetMarked(true);
747 			break;
748 	}
749 
750 	// New window policy
751 	uint32 newWindowPolicy = fSettings->GetValue(kSettingsKeyNewWindowPolicy,
752 		(uint32)OpenStartPage);
753 	switch (newWindowPolicy) {
754 		default:
755 		case OpenStartPage:
756 			fNewWindowBehaviorOpenHomeItem->SetMarked(true);
757 			break;
758 		case OpenSearchPage:
759 			fNewWindowBehaviorOpenSearchItem->SetMarked(true);
760 			break;
761 		case OpenBlankPage:
762 			fNewWindowBehaviorOpenBlankItem->SetMarked(true);
763 			break;
764 	}
765 
766 	// New tab policy
767 	uint32 newTabPolicy = fSettings->GetValue(kSettingsKeyNewTabPolicy,
768 		(uint32)OpenBlankPage);
769 	switch (newTabPolicy) {
770 		default:
771 		case OpenBlankPage:
772 			fNewTabBehaviorOpenBlankItem->SetMarked(true);
773 			break;
774 		case OpenStartPage:
775 			fNewTabBehaviorOpenHomeItem->SetMarked(true);
776 			break;
777 		case OpenSearchPage:
778 			fNewTabBehaviorOpenSearchItem->SetMarked(true);
779 			break;
780 		case CloneCurrentPage:
781 			fNewTabBehaviorCloneCurrentItem->SetMarked(true);
782 			break;
783 	}
784 
785 	// Font settings
786 	int32 defaultFontSize = fSettings->GetValue("standard font size",
787 		kDefaultFontSize);
788 	int32 defaultFixedFontSize = fSettings->GetValue("fixed font size",
789 		kDefaultFontSize);
790 
791 	fStandardSizesSpinner->SetValue(defaultFontSize);
792 	fFixedSizesSpinner->SetValue(defaultFixedFontSize);
793 
794 	fStandardFontView->SetFont(fSettings->GetValue("standard font",
795 		*be_plain_font), defaultFontSize);
796 	fSerifFontView->SetFont(fSettings->GetValue("serif font",
797 		_FindDefaultSerifFont()), defaultFontSize);
798 	fSansSerifFontView->SetFont(fSettings->GetValue("sans serif font",
799 		*be_plain_font), defaultFontSize);
800 	fFixedFontView->SetFont(fSettings->GetValue("fixed font",
801 		*be_fixed_font), defaultFixedFontSize);
802 
803 	// Proxy settings
804 	fUseProxyCheckBox->SetValue(fSettings->GetValue(kSettingsKeyUseProxy,
805 		false));
806 	fProxyAddressControl->SetText(fSettings->GetValue(kSettingsKeyProxyAddress,
807 		""));
808 	BString keyProxyPort;
809 	keyProxyPort << fSettings->GetValue(kSettingsKeyProxyPort, (uint32)0);
810 	fProxyPortControl->SetText(keyProxyPort.String());
811 	fUseProxyAuthCheckBox->SetValue(fSettings->GetValue(kSettingsKeyUseProxyAuth,
812 		false));
813 	fProxyUsernameControl->SetText(fSettings->GetValue(kSettingsKeyProxyUsername,
814 		""));
815 	fProxyPasswordControl->SetText(fSettings->GetValue(kSettingsKeyProxyPassword,
816 		""));
817 
818 	_ValidateControlsEnabledStatus();
819 }
820 
821 
822 void
823 SettingsWindow::_ChooseDownloadFolder(const BMessage* message)
824 {
825 	if (fOpenFilePanel == NULL) {
826 		BMessenger target(this);
827 		fOpenFilePanel = new (std::nothrow) BFilePanel(B_OPEN_PANEL,
828 			&target, NULL, B_DIRECTORY_NODE);
829 	}
830 	BMessage panelMessage(MSG_HANDLE_DOWNLOAD_FOLDER);
831 	fOpenFilePanel->SetMessage(&panelMessage);
832 	fOpenFilePanel->Show();
833 }
834 
835 
836 void
837 SettingsWindow:: _HandleDownloadPanelResult(BFilePanel* panel,
838 	const BMessage* message)
839 {
840 	entry_ref ref;
841 	if (message->FindRef("refs", 0, &ref) == B_OK)
842 	{
843 		BPath path(&ref);
844 		fDownloadFolderControl->SetText(path.Path());
845 	}
846 }
847 
848 
849 void
850 SettingsWindow::_ValidateControlsEnabledStatus()
851 {
852 	bool canApply = _CanApplySettings();
853 	fApplyButton->SetEnabled(canApply);
854 	fRevertButton->SetEnabled(canApply);
855 	// Let the Cancel button be enabled always, as another way to close the
856 	// window...
857 	fCancelButton->SetEnabled(true);
858 
859 	bool useProxy = fUseProxyCheckBox->Value() == B_CONTROL_ON;
860 	fProxyAddressControl->SetEnabled(useProxy);
861 	fProxyPortControl->SetEnabled(useProxy);
862 	fUseProxyAuthCheckBox->SetEnabled(useProxy);
863 	bool useProxyAuth = useProxy && fUseProxyAuthCheckBox->Value() == B_CONTROL_ON;
864 	fProxyUsernameControl->SetEnabled(useProxyAuth);
865 	fProxyPasswordControl->SetEnabled(useProxyAuth);
866 }
867 
868 
869 // #pragma mark -
870 
871 
872 uint32
873 SettingsWindow::_StartUpPolicy() const
874 {
875 	uint32 startUpPolicy = ResumePriorSession;
876 	BMenuItem* markedItem = fStartUpBehaviorMenu->Menu()->FindMarked();
877 	if (markedItem == fStartUpBehaviorStartNewSession)
878 		startUpPolicy = StartNewSession;
879 	return startUpPolicy;
880 }
881 
882 uint32
883 SettingsWindow::_NewWindowPolicy() const
884 {
885 	uint32 newWindowPolicy = OpenStartPage;
886 	BMenuItem* markedItem = fNewWindowBehaviorMenu->Menu()->FindMarked();
887 	if (markedItem == fNewWindowBehaviorOpenSearchItem)
888 		newWindowPolicy = OpenSearchPage;
889 	else if (markedItem == fNewWindowBehaviorOpenBlankItem)
890 		newWindowPolicy = OpenBlankPage;
891 	return newWindowPolicy;
892 }
893 
894 
895 uint32
896 SettingsWindow::_NewTabPolicy() const
897 {
898 	uint32 newTabPolicy = OpenBlankPage;
899 	BMenuItem* markedItem = fNewTabBehaviorMenu->Menu()->FindMarked();
900 	if (markedItem == fNewTabBehaviorCloneCurrentItem)
901 		newTabPolicy = CloneCurrentPage;
902 	else if (markedItem == fNewTabBehaviorOpenHomeItem)
903 		newTabPolicy = OpenStartPage;
904 	else if (markedItem == fNewTabBehaviorOpenSearchItem)
905 		newTabPolicy = OpenSearchPage;
906 	return newTabPolicy;
907 }
908 
909 
910 BFont
911 SettingsWindow::_FindDefaultSerifFont() const
912 {
913 	// Default to the first "serif" font we find.
914 	BFont serifFont(*be_plain_font);
915 	font_family family;
916 	int32 familyCount = count_font_families();
917 	for (int32 i = 0; i < familyCount; i++) {
918 		if (get_font_family(i, &family) == B_OK) {
919 			BString familyString(family);
920 			if (familyString.IFindFirst("sans") >= 0)
921 				continue;
922 			if (familyString.IFindFirst("serif") >= 0) {
923 				serifFont.SetFamilyAndFace(family, B_REGULAR_FACE);
924 				break;
925 			}
926 		}
927 	}
928 	return serifFont;
929 }
930 
931 
932 uint32
933 SettingsWindow::_ProxyPort() const
934 {
935 	return atoul(fProxyPortControl->Text());
936 }
937 
938 
939