2 * Copyright 2004-2015 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 #include <NetworkSettingsAddOn.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()
30 BNetworkSettingsItem::~BNetworkSettingsItem()
36 BNetworkSettingsItem::ProfileChanged(const BNetworkProfile
* newProfile
)
38 fProfile
= newProfile
;
43 const BNetworkProfile
*
44 BNetworkSettingsItem::Profile() const
51 BNetworkSettingsItem::SettingsUpdated(uint32 type
)
57 BNetworkSettingsItem::ConfigurationUpdated(const BMessage
& message
)
63 BNetworkSettingsItem::NotifySettingsUpdated()
65 // TODO: post to network window
66 BMessage
updated(kMsgSettingsItemUpdated
);
67 updated
.AddPointer("item", this);
68 gNetworkWindow
.SendMessage(&updated
);
75 BNetworkSettingsInterfaceItem::BNetworkSettingsInterfaceItem(
76 const char* interface
)
83 BNetworkSettingsInterfaceItem::~BNetworkSettingsInterfaceItem()
89 BNetworkSettingsInterfaceItem::Type() const
91 return B_NETWORK_SETTINGS_TYPE_INTERFACE
;
96 BNetworkSettingsInterfaceItem::Interface() const
105 BNetworkInterfaceListItem::BNetworkInterfaceListItem(int family
,
106 const char* interface
, const char* label
, BNetworkSettings
& settings
)
110 fInterface(interface
),
119 BNetworkInterfaceListItem::~BNetworkInterfaceListItem()
125 BNetworkInterfaceListItem::Label() const
132 BNetworkInterfaceListItem::DrawItem(BView
* owner
, BRect bounds
, bool complete
)
136 if (IsSelected() || complete
) {
138 owner
->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR
));
139 owner
->SetLowColor(owner
->HighColor());
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
);
152 textColor
= ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR
);
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
));
159 owner
->SetHighColor(tint_color(textColor
, B_LIGHTEN_1_TINT
));
162 owner
->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR
));
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
);
182 BNetworkInterfaceListItem::Update(BView
* owner
, const BFont
* font
)
186 fSpacing
= be_control_look
->DefaultLabelSpacing();
188 BListItem::Update(owner
, font
);
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);
203 BNetworkInterfaceListItem::ConfigurationUpdated(const BMessage
& message
)
210 BNetworkInterfaceListItem::_AddressFont()
213 font
.SetFace(B_ITALIC_FACE
);
214 font
.SetSize(font
.Size() * 0.9f
);
220 BNetworkInterfaceListItem::_UpdateState()
222 BNetworkInterfaceAddress address
;
223 BNetworkInterface
interface(fInterface
);
225 bool autoConfigure
= fSettings
.Interface(fInterface
).IsAutoConfigure(
229 fDisabled
= !autoConfigure
;
231 int32 index
= interface
.FindFirstAddress(fFamily
);
235 interface
.GetAddressAt(index
, address
);
237 fDisabled
= address
.Address().IsEmpty() && !autoConfigure
;
238 if (!address
.Address().IsEmpty())
239 fAddress
<< "(" << address
.Address().ToString() << ")";
246 BNetworkSettingsAddOn::BNetworkSettingsAddOn(image_id image
,
247 BNetworkSettings
& settings
)
256 BNetworkSettingsAddOn::~BNetworkSettingsAddOn()
262 BNetworkSettingsInterfaceItem
*
263 BNetworkSettingsAddOn::CreateNextInterfaceItem(uint32
& cookie
,
264 const char* interface
)
270 BNetworkSettingsItem
*
271 BNetworkSettingsAddOn::CreateNextItem(uint32
& cookie
)
278 BNetworkSettingsAddOn::Image()
285 BNetworkSettingsAddOn::Resources()
287 if (fResources
== NULL
) {
289 if (get_image_info(fImage
, &info
) != B_OK
)
292 BResources
* resources
= new BResources();
293 BFile
file(info
.name
, B_READ_ONLY
);
294 if (resources
->SetTo(&file
) == B_OK
)
295 fResources
= resources
;
304 BNetworkSettingsAddOn::Settings()