vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / mail / ConfigViews.cpp
blob7695af938ebcbab1c20e1af34c16074d26d4dc91
1 /*
2 * Copyright 2007-2012, Haiku, Inc. All rights reserved.
3 * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
5 * Distributed under the terms of the MIT License.
6 */
9 //! Config views for the account, protocols, and filters.
12 #include "ConfigViews.h"
14 #include <Alert.h>
15 #include <Button.h>
16 #include <Catalog.h>
17 #include <Directory.h>
18 #include <Entry.h>
19 #include <FindDirectory.h>
20 #include <ListView.h>
21 #include <LayoutBuilder.h>
22 #include <Locale.h>
23 #include <Looper.h>
24 #include <MenuField.h>
25 #include <MenuItem.h>
26 #include <Path.h>
27 #include <PopUpMenu.h>
28 #include <TextControl.h>
30 #include <string.h>
32 #include <MailSettings.h>
34 #include "FilterConfigView.h"
37 #undef B_TRANSLATION_CONTEXT
38 #define B_TRANSLATION_CONTEXT "Config Views"
41 // AccountConfigView
42 const uint32 kMsgAccountNameChanged = 'anmc';
44 // ProtocolsConfigView
45 const uint32 kMsgProtocolChanged = 'prch';
48 // #pragma mark -
51 AccountConfigView::AccountConfigView(BMailAccountSettings* account)
53 BBox("account"),
54 fAccount(account)
56 SetLabel(B_TRANSLATE("Account settings"));
58 fNameControl = new BTextControl(NULL, B_TRANSLATE("Account name:"), NULL,
59 new BMessage(kMsgAccountNameChanged));
60 fRealNameControl = new BTextControl(NULL, B_TRANSLATE("Real name:"), NULL,
61 NULL);
62 fReturnAddressControl = new BTextControl(NULL,
63 B_TRANSLATE("Return address:"), NULL, NULL);
65 BView* contents = new BView(NULL, 0);
66 AddChild(contents);
68 BLayoutBuilder::Grid<>(contents, 0.f)
69 .SetInsets(B_USE_DEFAULT_SPACING)
70 .Add(fNameControl->CreateLabelLayoutItem(), 0, 0)
71 .Add(fNameControl->CreateTextViewLayoutItem(), 1, 0)
72 .Add(fRealNameControl->CreateLabelLayoutItem(), 0, 1)
73 .Add(fRealNameControl->CreateTextViewLayoutItem(), 1, 1)
74 .Add(fReturnAddressControl->CreateLabelLayoutItem(), 0, 2)
75 .Add(fReturnAddressControl->CreateTextViewLayoutItem(), 1, 2)
76 .AddGlue(0, 3);
80 void
81 AccountConfigView::DetachedFromWindow()
83 fAccount->SetName(fNameControl->Text());
84 fAccount->SetRealName(fRealNameControl->Text());
85 fAccount->SetReturnAddress(fReturnAddressControl->Text());
89 void
90 AccountConfigView::AttachedToWindow()
92 UpdateViews();
93 fNameControl->SetTarget(this);
97 void
98 AccountConfigView::MessageReceived(BMessage *msg)
100 switch (msg->what) {
101 case kMsgAccountNameChanged:
102 fAccount->SetName(fNameControl->Text());
103 break;
105 default:
106 BView::MessageReceived(msg);
111 void
112 AccountConfigView::UpdateViews()
114 fNameControl->SetText(fAccount->Name());
115 fRealNameControl->SetText(fAccount->RealName());
116 fReturnAddressControl->SetText(fAccount->ReturnAddress());
120 // #pragma mark -
123 ProtocolSettingsView::ProtocolSettingsView(const entry_ref& ref,
124 const BMailAccountSettings& accountSettings,
125 BMailProtocolSettings& settings)
127 BBox("protocol"),
128 fSettings(settings),
129 fSettingsView(NULL)
131 status_t status = _CreateSettingsView(ref, accountSettings, settings);
132 BView* view = fSettingsView;
134 if (status == B_OK) {
135 SetLabel(ref.name);
136 } else {
137 BString text(B_TRANSLATE("An error occurred while creating the "
138 "config view: %error."));
139 text.ReplaceAll("%error", strerror(status));
140 view = new BStringView("error", text.String());
142 SetLabel(B_TRANSLATE("Error!"));
145 BView* contents = new BView(NULL, 0);
146 AddChild(contents);
148 BLayoutBuilder::Group<>(contents, B_VERTICAL)
149 .SetInsets(B_USE_DEFAULT_SPACING)
150 .Add(view)
151 .AddGlue();
155 void
156 ProtocolSettingsView::DetachedFromWindow()
158 if (fSettingsView == NULL)
159 return;
161 if (fSettingsView->SaveInto(fSettings) != B_OK)
162 return;
164 // We need to remove the settings view before unloading its add-on
165 fSettingsView->RemoveSelf();
166 delete fSettingsView;
167 fSettingsView = NULL;
168 unload_add_on(fImage);
172 status_t
173 ProtocolSettingsView::_CreateSettingsView(const entry_ref& ref,
174 const BMailAccountSettings& accountSettings,
175 BMailProtocolSettings& settings)
177 BMailSettingsView* (*instantiateConfig)(
178 const BMailAccountSettings& accountSettings,
179 BMailProtocolSettings& settings);
180 BPath path(&ref);
181 image_id image = load_add_on(path.Path());
182 if (image < 0)
183 return image;
185 if (get_image_symbol(image, "instantiate_protocol_settings_view",
186 B_SYMBOL_TYPE_TEXT, (void**)&instantiateConfig) != B_OK) {
187 unload_add_on(image);
188 return B_MISSING_SYMBOL;
191 fImage = image;
192 fSettingsView = instantiateConfig(accountSettings, settings);
193 return B_OK;