vfs: check userland buffers before reading them.
[haiku.git] / src / preferences / network / NetworkSettingsAddOn.cpp
blobc7a23a4e41f14981437db9ad3508aa5dc11c378a
1 /*
2 * Copyright 2004-2015 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <NetworkSettingsAddOn.h>
9 #include <stdio.h>
10 #include <stdlib.h>
12 #include <ControlLook.h>
13 #include <NetworkAddress.h>
14 #include <NetworkInterface.h>
15 #include <NetworkSettings.h>
17 #include "NetworkWindow.h"
20 using namespace BNetworkKit;
23 BNetworkSettingsItem::BNetworkSettingsItem()
25 fProfile(NULL)
30 BNetworkSettingsItem::~BNetworkSettingsItem()
35 status_t
36 BNetworkSettingsItem::ProfileChanged(const BNetworkProfile* newProfile)
38 fProfile = newProfile;
39 return B_OK;
43 const BNetworkProfile*
44 BNetworkSettingsItem::Profile() const
46 return fProfile;
50 void
51 BNetworkSettingsItem::SettingsUpdated(uint32 type)
56 void
57 BNetworkSettingsItem::ConfigurationUpdated(const BMessage& message)
62 void
63 BNetworkSettingsItem::NotifySettingsUpdated()
65 // TODO: post to network window
66 BMessage updated(kMsgSettingsItemUpdated);
67 updated.AddPointer("item", this);
68 gNetworkWindow.SendMessage(&updated);
72 // #pragma mark -
75 BNetworkSettingsInterfaceItem::BNetworkSettingsInterfaceItem(
76 const char* interface)
78 fInterface(interface)
83 BNetworkSettingsInterfaceItem::~BNetworkSettingsInterfaceItem()
88 BNetworkSettingsType
89 BNetworkSettingsInterfaceItem::Type() const
91 return B_NETWORK_SETTINGS_TYPE_INTERFACE;
95 const char*
96 BNetworkSettingsInterfaceItem::Interface() const
98 return fInterface;
102 // #pragma mark -
105 BNetworkInterfaceListItem::BNetworkInterfaceListItem(int family,
106 const char* interface, const char* label, BNetworkSettings& settings)
108 fSettings(settings),
109 fFamily(family),
110 fInterface(interface),
111 fLabel(label),
112 fDisabled(false),
113 fLineOffset(0),
114 fSpacing(0)
119 BNetworkInterfaceListItem::~BNetworkInterfaceListItem()
124 const char*
125 BNetworkInterfaceListItem::Label() const
127 return fLabel;
131 void
132 BNetworkInterfaceListItem::DrawItem(BView* owner, BRect bounds, bool complete)
134 owner->PushState();
136 if (IsSelected() || complete) {
137 if (IsSelected()) {
138 owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
139 owner->SetLowColor(owner->HighColor());
140 } else
141 owner->SetHighColor(owner->LowColor());
143 owner->FillRect(bounds);
146 // Set the initial bounds of item contents
147 BPoint labelLocation = bounds.LeftTop() + BPoint(fSpacing, fLineOffset);
149 if (fDisabled) {
150 rgb_color textColor;
151 if (IsSelected())
152 textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
153 else
154 textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
156 if (textColor.red + textColor.green + textColor.blue > 128 * 3)
157 owner->SetHighColor(tint_color(textColor, B_DARKEN_1_TINT));
158 else
159 owner->SetHighColor(tint_color(textColor, B_LIGHTEN_1_TINT));
160 } else {
161 if (IsSelected())
162 owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
163 else
164 owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
167 owner->SetFont(be_plain_font);
168 owner->DrawString(fLabel, labelLocation);
170 if (!fAddress.IsEmpty()) {
171 BFont font = _AddressFont();
172 owner->MovePenBy(fSpacing, 0);
173 owner->SetFont(&font);
174 owner->DrawString(fAddress);
177 owner->PopState();
181 void
182 BNetworkInterfaceListItem::Update(BView* owner, const BFont* font)
184 _UpdateState();
186 fSpacing = be_control_look->DefaultLabelSpacing();
188 BListItem::Update(owner, font);
189 font_height height;
190 font->GetHeight(&height);
192 float lineHeight = ceilf(height.ascent) + ceilf(height.descent)
193 + ceilf(height.leading);
194 fLineOffset = 2 + ceilf(height.ascent + height.leading / 2);
196 SetWidth(owner->StringWidth(fLabel) + 2 * fSpacing
197 + _AddressFont().StringWidth(fAddress.String()));
198 SetHeight(lineHeight + 4);
202 void
203 BNetworkInterfaceListItem::ConfigurationUpdated(const BMessage& message)
205 _UpdateState();
209 BFont
210 BNetworkInterfaceListItem::_AddressFont()
212 BFont font;
213 font.SetFace(B_ITALIC_FACE);
214 font.SetSize(font.Size() * 0.9f);
215 return font;
219 void
220 BNetworkInterfaceListItem::_UpdateState()
222 BNetworkInterfaceAddress address;
223 BNetworkInterface interface(fInterface);
225 bool autoConfigure = fSettings.Interface(fInterface).IsAutoConfigure(
226 fFamily);
228 fAddress = "";
229 fDisabled = !autoConfigure;
231 int32 index = interface.FindFirstAddress(fFamily);
232 if (index < 0)
233 return;
235 interface.GetAddressAt(index, address);
237 fDisabled = address.Address().IsEmpty() && !autoConfigure;
238 if (!address.Address().IsEmpty())
239 fAddress << "(" << address.Address().ToString() << ")";
243 // #pragma mark -
246 BNetworkSettingsAddOn::BNetworkSettingsAddOn(image_id image,
247 BNetworkSettings& settings)
249 fImage(image),
250 fResources(NULL),
251 fSettings(settings)
256 BNetworkSettingsAddOn::~BNetworkSettingsAddOn()
258 delete fResources;
262 BNetworkSettingsInterfaceItem*
263 BNetworkSettingsAddOn::CreateNextInterfaceItem(uint32& cookie,
264 const char* interface)
266 return NULL;
270 BNetworkSettingsItem*
271 BNetworkSettingsAddOn::CreateNextItem(uint32& cookie)
273 return NULL;
277 image_id
278 BNetworkSettingsAddOn::Image()
280 return fImage;
284 BResources*
285 BNetworkSettingsAddOn::Resources()
287 if (fResources == NULL) {
288 image_info info;
289 if (get_image_info(fImage, &info) != B_OK)
290 return NULL;
292 BResources* resources = new BResources();
293 BFile file(info.name, B_READ_ONLY);
294 if (resources->SetTo(&file) == B_OK)
295 fResources = resources;
296 else
297 delete resources;
299 return fResources;
303 BNetworkSettings&
304 BNetworkSettingsAddOn::Settings()
306 return fSettings;