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