1 /* 2 * Copyright 2004-2015 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include <NetworkSettingsAddOn.h> 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 #include <ControlLook.h> 13 #include <NetworkAddress.h> 14 #include <NetworkInterface.h> 15 #include <NetworkSettings.h> 16 17 #include "NetworkWindow.h" 18 19 20 using namespace BNetworkKit; 21 22 23 BNetworkSettingsItem::BNetworkSettingsItem() 24 : 25 fProfile(NULL) 26 { 27 } 28 29 30 BNetworkSettingsItem::~BNetworkSettingsItem() 31 { 32 } 33 34 35 status_t 36 BNetworkSettingsItem::ProfileChanged(const BNetworkProfile* newProfile) 37 { 38 fProfile = newProfile; 39 return B_OK; 40 } 41 42 43 const BNetworkProfile* 44 BNetworkSettingsItem::Profile() const 45 { 46 return fProfile; 47 } 48 49 50 void 51 BNetworkSettingsItem::SettingsUpdated(uint32 type) 52 { 53 } 54 55 56 void 57 BNetworkSettingsItem::ConfigurationUpdated(const BMessage& message) 58 { 59 } 60 61 62 void 63 BNetworkSettingsItem::NotifySettingsUpdated() 64 { 65 // TODO: post to network window 66 BMessage updated(kMsgSettingsItemUpdated); 67 updated.AddPointer("item", this); 68 gNetworkWindow.SendMessage(&updated); 69 } 70 71 72 // #pragma mark - 73 74 75 BNetworkSettingsInterfaceItem::BNetworkSettingsInterfaceItem( 76 const char* interface) 77 : 78 fInterface(interface) 79 { 80 } 81 82 83 BNetworkSettingsInterfaceItem::~BNetworkSettingsInterfaceItem() 84 { 85 } 86 87 88 BNetworkSettingsType 89 BNetworkSettingsInterfaceItem::Type() const 90 { 91 return B_NETWORK_SETTINGS_TYPE_INTERFACE; 92 } 93 94 95 const char* 96 BNetworkSettingsInterfaceItem::Interface() const 97 { 98 return fInterface; 99 } 100 101 102 // #pragma mark - 103 104 105 BNetworkInterfaceListItem::BNetworkInterfaceListItem(int family, 106 const char* interface, const char* label, BNetworkSettings& settings) 107 : 108 fSettings(settings), 109 fFamily(family), 110 fInterface(interface), 111 fLabel(label), 112 fDisabled(false), 113 fLineOffset(0), 114 fSpacing(0) 115 { 116 } 117 118 119 BNetworkInterfaceListItem::~BNetworkInterfaceListItem() 120 { 121 } 122 123 124 const char* 125 BNetworkInterfaceListItem::Label() const 126 { 127 return fLabel; 128 } 129 130 131 void 132 BNetworkInterfaceListItem::DrawItem(BView* owner, BRect bounds, bool complete) 133 { 134 owner->PushState(); 135 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()); 142 143 owner->FillRect(bounds); 144 } 145 146 // Set the initial bounds of item contents 147 BPoint labelLocation = bounds.LeftTop() + BPoint(fSpacing, fLineOffset); 148 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); 155 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)); 165 } 166 167 owner->SetFont(be_plain_font); 168 owner->DrawString(fLabel, labelLocation); 169 170 if (!fAddress.IsEmpty()) { 171 BFont font = _AddressFont(); 172 owner->MovePenBy(fSpacing, 0); 173 owner->SetFont(&font); 174 owner->DrawString(fAddress); 175 } 176 177 owner->PopState(); 178 } 179 180 181 void 182 BNetworkInterfaceListItem::Update(BView* owner, const BFont* font) 183 { 184 _UpdateState(); 185 186 fSpacing = be_control_look->DefaultLabelSpacing(); 187 188 BListItem::Update(owner, font); 189 font_height height; 190 font->GetHeight(&height); 191 192 float lineHeight = ceilf(height.ascent) + ceilf(height.descent) 193 + ceilf(height.leading); 194 fLineOffset = 2 + ceilf(height.ascent + height.leading / 2); 195 196 SetWidth(owner->StringWidth(fLabel) + 2 * fSpacing 197 + _AddressFont().StringWidth(fAddress.String())); 198 SetHeight(lineHeight + 4); 199 } 200 201 202 void 203 BNetworkInterfaceListItem::ConfigurationUpdated(const BMessage& message) 204 { 205 _UpdateState(); 206 } 207 208 209 BFont 210 BNetworkInterfaceListItem::_AddressFont() 211 { 212 BFont font; 213 font.SetFace(B_ITALIC_FACE); 214 font.SetSize(font.Size() * 0.9f); 215 return font; 216 } 217 218 219 void 220 BNetworkInterfaceListItem::_UpdateState() 221 { 222 BNetworkInterfaceAddress address; 223 BNetworkInterface interface(fInterface); 224 225 bool autoConfigure = fSettings.Interface(fInterface).IsAutoConfigure( 226 fFamily); 227 228 fAddress = ""; 229 fDisabled = !autoConfigure; 230 231 int32 index = interface.FindFirstAddress(fFamily); 232 if (index < 0) 233 return; 234 235 interface.GetAddressAt(index, address); 236 237 fDisabled = address.Address().IsEmpty() && !autoConfigure; 238 if (!address.Address().IsEmpty()) 239 fAddress << "(" << address.Address().ToString() << ")"; 240 } 241 242 243 // #pragma mark - 244 245 246 BNetworkSettingsAddOn::BNetworkSettingsAddOn(image_id image, 247 BNetworkSettings& settings) 248 : 249 fImage(image), 250 fResources(NULL), 251 fSettings(settings) 252 { 253 } 254 255 256 BNetworkSettingsAddOn::~BNetworkSettingsAddOn() 257 { 258 delete fResources; 259 } 260 261 262 BNetworkSettingsInterfaceItem* 263 BNetworkSettingsAddOn::CreateNextInterfaceItem(uint32& cookie, 264 const char* interface) 265 { 266 return NULL; 267 } 268 269 270 BNetworkSettingsItem* 271 BNetworkSettingsAddOn::CreateNextItem(uint32& cookie) 272 { 273 return NULL; 274 } 275 276 277 image_id 278 BNetworkSettingsAddOn::Image() 279 { 280 return fImage; 281 } 282 283 284 BResources* 285 BNetworkSettingsAddOn::Resources() 286 { 287 if (fResources == NULL) { 288 image_info info; 289 if (get_image_info(fImage, &info) != B_OK) 290 return NULL; 291 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; 298 } 299 return fResources; 300 } 301 302 303 BNetworkSettings& 304 BNetworkSettingsAddOn::Settings() 305 { 306 return fSettings; 307 } 308