vfs: check userland buffers before reading them.
[haiku.git] / src / apps / terminal / PrefWindow.cpp
blob73935ade2bd0e216f87f1e78e793c302841d7c7c
1 /*
2 * Copyright 2007-2009, Haiku, Inc. All rights reserved.
3 * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net
4 * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai.
5 * All rights reserved. Distributed under the terms of the MIT license.
6 */
9 #include "AppearPrefView.h"
10 #include "PrefHandler.h"
11 #include "PrefWindow.h"
12 #include "TermConst.h"
14 #include <Alert.h>
15 #include <Box.h>
16 #include <Button.h>
17 #include <Catalog.h>
18 #include <FilePanel.h>
19 #include <GroupLayoutBuilder.h>
20 #include <LayoutBuilder.h>
21 #include <Locale.h>
22 #include <Path.h>
24 #include <stdio.h>
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "Terminal PrefWindow"
29 PrefWindow::PrefWindow(const BMessenger& messenger)
31 BWindow(BRect(0, 0, 375, 185), "Terminal settings",
32 B_TITLED_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
33 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
34 fPreviousPref(new PrefHandler(PrefHandler::Default())),
35 fSavePanel(NULL),
36 fDirty(false),
37 fTerminalMessenger(messenger)
39 BString app = B_TRANSLATE_SYSTEM_NAME("Terminal");
40 BString title = B_TRANSLATE_COMMENT("%app% settings", "window title");
41 title.ReplaceFirst("%app%", app);
42 SetTitle(title);
44 BLayoutBuilder::Group<>(this, B_VERTICAL)
45 .AddGroup(B_VERTICAL)
46 .SetInsets(B_USE_WINDOW_SPACING)
47 .Add(fAppearanceView = new AppearancePrefView(
48 B_TRANSLATE("Appearance"), fTerminalMessenger))
49 .AddGroup(B_HORIZONTAL)
50 .Add(fDefaultsButton = new BButton("defaultsbutton",
51 B_TRANSLATE("Defaults"), new BMessage(MSG_DEFAULTS_PRESSED),
52 B_WILL_DRAW))
53 .Add(fRevertButton = new BButton("revertbutton",
54 B_TRANSLATE("Revert"), new BMessage(MSG_REVERT_PRESSED),
55 B_WILL_DRAW))
56 .AddGlue()
57 .Add(fSaveAsFileButton = new BButton("savebutton",
58 B_TRANSLATE("Save to file" B_UTF8_ELLIPSIS),
59 new BMessage(MSG_SAVEAS_PRESSED), B_WILL_DRAW))
60 .End()
61 .End();
63 fRevertButton->SetEnabled(fDirty);
65 AddShortcut('Q', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
66 AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
68 CenterOnScreen();
69 Show();
73 PrefWindow::~PrefWindow()
78 void
79 PrefWindow::Quit()
81 fTerminalMessenger.SendMessage(MSG_PREF_CLOSED);
82 delete fPreviousPref;
83 delete fSavePanel;
84 BWindow::Quit();
88 bool
89 PrefWindow::QuitRequested()
91 if (fDirty)
92 _Save();
94 return true;
98 void
99 PrefWindow::_SaveAs()
101 if (!fSavePanel) {
102 BMessenger messenger(this);
103 fSavePanel = new BFilePanel(B_SAVE_PANEL, &messenger);
106 fSavePanel->Show();
110 void
111 PrefWindow::_SaveRequested(BMessage *msg)
113 entry_ref dirref;
114 const char* filename;
116 msg->FindRef("directory", &dirref);
117 msg->FindString("name", &filename);
119 BDirectory dir(&dirref);
120 BPath path(&dir, filename);
122 PrefHandler::Default()->SaveAsText(path.Path(), PREFFILE_MIMETYPE,
123 TERM_SIGNATURE);
127 void
128 PrefWindow::_Save()
130 delete fPreviousPref;
131 fPreviousPref = new PrefHandler(PrefHandler::Default());
133 PrefHandler::Default()->SaveDefaultAsText();
134 fDirty = false;
138 void
139 PrefWindow::_Revert()
141 if (fDirty) {
142 PrefHandler::SetDefault(new PrefHandler(fPreviousPref));
144 fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
145 fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED);
146 fTerminalMessenger.SendMessage(MSG_TAB_TITLE_SETTING_CHANGED);
147 fTerminalMessenger.SendMessage(MSG_WINDOW_TITLE_SETTING_CHANGED);
149 fAppearanceView->Revert();
151 fDirty = false;
152 fRevertButton->SetEnabled(fDirty);
157 void
158 PrefWindow::MessageReceived(BMessage* msg)
160 switch (msg->what) {
161 case MSG_SAVEAS_PRESSED:
162 _SaveAs();
163 break;
165 case MSG_REVERT_PRESSED:
166 _Revert();
167 break;
169 case MSG_DEFAULTS_PRESSED:
170 PrefHandler::SetDefault(new PrefHandler(false));
171 fTerminalMessenger.SendMessage(MSG_HALF_FONT_CHANGED);
172 fTerminalMessenger.SendMessage(MSG_COLOR_CHANGED);
173 fTerminalMessenger.SendMessage(MSG_TAB_TITLE_SETTING_CHANGED);
174 fTerminalMessenger.SendMessage(MSG_WINDOW_TITLE_SETTING_CHANGED);
175 fAppearanceView->Revert();
176 // fallthrough
178 case MSG_PREF_MODIFIED:
179 fDirty = true;
180 fRevertButton->SetEnabled(fDirty);
181 break;
183 case B_SAVE_REQUESTED:
184 _SaveRequested(msg);
185 break;
187 default:
188 BWindow::MessageReceived(msg);
189 break;