1f01106c3SAxel Dörfler /* 26c7caf44SAxel Dörfler * Copyright 2006-2013, Haiku, Inc. All rights reserved. 3f01106c3SAxel Dörfler * Distributed under the terms of the MIT License. 4f01106c3SAxel Dörfler * 5f01106c3SAxel Dörfler * Authors: 63b41ad86SStephan Aßmus * Dario Casalinuovo 7937ca113SRene Gollent * Axel Dörfler, axeld@pinc-software.de 85c46b171SRene Gollent * Rene Gollent, rene@gollent.com 9937ca113SRene Gollent * Hugo Santos, hugosantos@gmail.com 10f01106c3SAxel Dörfler */ 11f01106c3SAxel Dörfler 12f01106c3SAxel Dörfler 13f01106c3SAxel Dörfler #include "NetworkStatusView.h" 14f01106c3SAxel Dörfler 15aba4cb20SMichael Lotz #include <algorithm> 16440d0e61SAxel Dörfler #include <set> 17aba4cb20SMichael Lotz #include <vector> 18440d0e61SAxel Dörfler 19195981bbSAxel Dörfler #include <arpa/inet.h> 20195981bbSAxel Dörfler #include <net/if.h> 21195981bbSAxel Dörfler #include <stdio.h> 22195981bbSAxel Dörfler #include <stdlib.h> 233aeed660SJérôme Duval #include <strings.h> 24195981bbSAxel Dörfler #include <sys/socket.h> 25195981bbSAxel Dörfler #include <sys/sockio.h> 26195981bbSAxel Dörfler #include <unistd.h> 27f01106c3SAxel Dörfler 28730a45eeSJohn Scipione #include <AboutWindow.h> 29f01106c3SAxel Dörfler #include <Alert.h> 30f01106c3SAxel Dörfler #include <Application.h> 31757e7059SAdrien Destugues #include <Catalog.h> 32f01106c3SAxel Dörfler #include <Bitmap.h> 33f01106c3SAxel Dörfler #include <Deskbar.h> 34f01106c3SAxel Dörfler #include <Dragger.h> 35f01106c3SAxel Dörfler #include <Drivers.h> 36f01106c3SAxel Dörfler #include <IconUtils.h> 37757e7059SAdrien Destugues #include <Locale.h> 38f01106c3SAxel Dörfler #include <MenuItem.h> 39f01106c3SAxel Dörfler #include <MessageRunner.h> 405fe97f21SAxel Dörfler #include <NetworkDevice.h> 415fe97f21SAxel Dörfler #include <NetworkInterface.h> 425fe97f21SAxel Dörfler #include <NetworkRoster.h> 43f01106c3SAxel Dörfler #include <PopUpMenu.h> 44f01106c3SAxel Dörfler #include <Resources.h> 45d5ba07a3SAxel Dörfler #include <Roster.h> 46f01106c3SAxel Dörfler #include <String.h> 47f01106c3SAxel Dörfler #include <TextView.h> 48f01106c3SAxel Dörfler 49195981bbSAxel Dörfler #include "NetworkStatus.h" 50195981bbSAxel Dörfler #include "NetworkStatusIcons.h" 515fe97f21SAxel Dörfler #include "RadioView.h" 523ca00ffdSAxel Dörfler #include "WirelessNetworkMenuItem.h" 53195981bbSAxel Dörfler 54f01106c3SAxel Dörfler 55546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT 56546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "NetworkStatusView" 57757e7059SAdrien Destugues 58757e7059SAdrien Destugues 59f01106c3SAxel Dörfler static const char *kStatusDescriptions[] = { 602ee8f3f6SSiarzhuk Zharski B_TRANSLATE("Unknown"), 612ee8f3f6SSiarzhuk Zharski B_TRANSLATE("No link"), 622ee8f3f6SSiarzhuk Zharski B_TRANSLATE("No stateful configuration"), 632ee8f3f6SSiarzhuk Zharski B_TRANSLATE("Configuring"), 642ee8f3f6SSiarzhuk Zharski B_TRANSLATE("Ready") 65f01106c3SAxel Dörfler }; 66f01106c3SAxel Dörfler 67*efafab64SAxel Dörfler extern "C" _EXPORT BView *instantiate_deskbar_item(float maxWidth, float maxHeight); 68f01106c3SAxel Dörfler 69f01106c3SAxel Dörfler 70f01106c3SAxel Dörfler const uint32 kMsgShowConfiguration = 'shcf'; 71c1502c9aSStephan Aßmus const uint32 kMsgOpenNetworkPreferences = 'onwp'; 722e1a6286SAxel Dörfler const uint32 kMsgJoinNetwork = 'join'; 73f01106c3SAxel Dörfler 74f01106c3SAxel Dörfler const uint32 kMinIconWidth = 16; 75f01106c3SAxel Dörfler const uint32 kMinIconHeight = 16; 76f01106c3SAxel Dörfler 77195981bbSAxel Dörfler 78195981bbSAxel Dörfler // #pragma mark - 79f01106c3SAxel Dörfler 80f01106c3SAxel Dörfler 81aba4cb20SMichael Lotz static bool 82aba4cb20SMichael Lotz signal_strength_compare(const wireless_network &a, 83aba4cb20SMichael Lotz const wireless_network &b) 84aba4cb20SMichael Lotz { 85aba4cb20SMichael Lotz if (a.signal_strength == b.signal_strength) 86aba4cb20SMichael Lotz return strcmp(a.name, b.name) > 0; 87aba4cb20SMichael Lotz return a.signal_strength > b.signal_strength; 88aba4cb20SMichael Lotz } 89aba4cb20SMichael Lotz 90aba4cb20SMichael Lotz 91aba4cb20SMichael Lotz // #pragma mark - 92aba4cb20SMichael Lotz 93aba4cb20SMichael Lotz 94f01106c3SAxel Dörfler NetworkStatusView::NetworkStatusView(BRect frame, int32 resizingMode, 95f01106c3SAxel Dörfler bool inDeskbar) 96f01106c3SAxel Dörfler : BView(frame, kDeskbarItemName, resizingMode, 97d5ba07a3SAxel Dörfler B_WILL_DRAW | B_FRAME_EVENTS), 985c46b171SRene Gollent fInDeskbar(inDeskbar) 99f01106c3SAxel Dörfler { 100f01106c3SAxel Dörfler _Init(); 101f01106c3SAxel Dörfler 102f01106c3SAxel Dörfler if (!inDeskbar) { 103f01106c3SAxel Dörfler // we were obviously added to a standard window - let's add a dragger 104f01106c3SAxel Dörfler frame.OffsetTo(B_ORIGIN); 105f01106c3SAxel Dörfler frame.top = frame.bottom - 7; 106f01106c3SAxel Dörfler frame.left = frame.right - 7; 107f01106c3SAxel Dörfler BDragger* dragger = new BDragger(frame, this, 108f01106c3SAxel Dörfler B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM); 109f01106c3SAxel Dörfler AddChild(dragger); 110f01106c3SAxel Dörfler } else 111f01106c3SAxel Dörfler _Update(); 112f01106c3SAxel Dörfler } 113f01106c3SAxel Dörfler 114f01106c3SAxel Dörfler 115f01106c3SAxel Dörfler NetworkStatusView::NetworkStatusView(BMessage* archive) 116d5ba07a3SAxel Dörfler : BView(archive), 117d5ba07a3SAxel Dörfler fInDeskbar(false) 118f01106c3SAxel Dörfler { 119d5ba07a3SAxel Dörfler app_info info; 120d5ba07a3SAxel Dörfler if (be_app->GetAppInfo(&info) == B_OK 121d5ba07a3SAxel Dörfler && !strcasecmp(info.signature, "application/x-vnd.Be-TSKB")) 122d5ba07a3SAxel Dörfler fInDeskbar = true; 123d5ba07a3SAxel Dörfler 124f01106c3SAxel Dörfler _Init(); 125f01106c3SAxel Dörfler } 126f01106c3SAxel Dörfler 127f01106c3SAxel Dörfler 128f01106c3SAxel Dörfler NetworkStatusView::~NetworkStatusView() 129f01106c3SAxel Dörfler { 130f01106c3SAxel Dörfler } 131f01106c3SAxel Dörfler 132f01106c3SAxel Dörfler 133f01106c3SAxel Dörfler void 134f01106c3SAxel Dörfler NetworkStatusView::_Init() 135f01106c3SAxel Dörfler { 136f01106c3SAxel Dörfler for (int i = 0; i < kStatusCount; i++) { 137e58807edSAlexander von Gluck IV fTrayIcons[i] = NULL; 138e58807edSAlexander von Gluck IV fNotifyIcons[i] = NULL; 139f01106c3SAxel Dörfler } 140f01106c3SAxel Dörfler 141f01106c3SAxel Dörfler _UpdateBitmaps(); 142f01106c3SAxel Dörfler } 143f01106c3SAxel Dörfler 144f01106c3SAxel Dörfler 145f01106c3SAxel Dörfler void 146f01106c3SAxel Dörfler NetworkStatusView::_UpdateBitmaps() 147f01106c3SAxel Dörfler { 148f01106c3SAxel Dörfler for (int i = 0; i < kStatusCount; i++) { 149e58807edSAlexander von Gluck IV delete fTrayIcons[i]; 150e58807edSAlexander von Gluck IV delete fNotifyIcons[i]; 151e58807edSAlexander von Gluck IV fTrayIcons[i] = NULL; 152e58807edSAlexander von Gluck IV fNotifyIcons[i] = NULL; 153f01106c3SAxel Dörfler } 154f01106c3SAxel Dörfler 155f01106c3SAxel Dörfler image_info info; 156f01106c3SAxel Dörfler if (our_image(info) != B_OK) 157f01106c3SAxel Dörfler return; 158f01106c3SAxel Dörfler 159f01106c3SAxel Dörfler BFile file(info.name, B_READ_ONLY); 160f01106c3SAxel Dörfler if (file.InitCheck() < B_OK) 161f01106c3SAxel Dörfler return; 162f01106c3SAxel Dörfler 163f01106c3SAxel Dörfler BResources resources(&file); 164f01106c3SAxel Dörfler #ifdef HAIKU_TARGET_PLATFORM_HAIKU 165f01106c3SAxel Dörfler if (resources.InitCheck() < B_OK) 166f01106c3SAxel Dörfler return; 167f01106c3SAxel Dörfler #endif 168f01106c3SAxel Dörfler 169f01106c3SAxel Dörfler for (int i = 0; i < kStatusCount; i++) { 170f01106c3SAxel Dörfler const void* data = NULL; 171f01106c3SAxel Dörfler size_t size; 172f01106c3SAxel Dörfler data = resources.LoadResource(B_VECTOR_ICON_TYPE, 173f01106c3SAxel Dörfler kNetworkStatusNoDevice + i, &size); 174f01106c3SAxel Dörfler if (data != NULL) { 175e58807edSAlexander von Gluck IV // Scale main tray icon 176e58807edSAlexander von Gluck IV BBitmap* trayIcon = new BBitmap(Bounds(), B_RGBA32); 177e58807edSAlexander von Gluck IV if (trayIcon->InitCheck() == B_OK 178f01106c3SAxel Dörfler && BIconUtils::GetVectorIcon((const uint8 *)data, 179e58807edSAlexander von Gluck IV size, trayIcon) == B_OK) { 180e58807edSAlexander von Gluck IV fTrayIcons[i] = trayIcon; 181f01106c3SAxel Dörfler } else 182e58807edSAlexander von Gluck IV delete trayIcon; 183e58807edSAlexander von Gluck IV 184e58807edSAlexander von Gluck IV // Scale notification icon 185e58807edSAlexander von Gluck IV BBitmap* notifyIcon = new BBitmap(BRect(0, 0, 31, 31), B_RGBA32); 186e58807edSAlexander von Gluck IV if (notifyIcon->InitCheck() == B_OK 187e58807edSAlexander von Gluck IV && BIconUtils::GetVectorIcon((const uint8 *)data, 188e58807edSAlexander von Gluck IV size, notifyIcon) == B_OK) { 189e58807edSAlexander von Gluck IV fNotifyIcons[i] = notifyIcon; 190e58807edSAlexander von Gluck IV } else 191e58807edSAlexander von Gluck IV delete notifyIcon; 192f01106c3SAxel Dörfler } 193f01106c3SAxel Dörfler } 194f01106c3SAxel Dörfler } 195f01106c3SAxel Dörfler 196f01106c3SAxel Dörfler 197f01106c3SAxel Dörfler void 198f01106c3SAxel Dörfler NetworkStatusView::_Quit() 199f01106c3SAxel Dörfler { 200f01106c3SAxel Dörfler if (fInDeskbar) { 201f01106c3SAxel Dörfler BDeskbar deskbar; 202f01106c3SAxel Dörfler deskbar.RemoveItem(kDeskbarItemName); 203f01106c3SAxel Dörfler } else 204f01106c3SAxel Dörfler be_app->PostMessage(B_QUIT_REQUESTED); 205f01106c3SAxel Dörfler } 206f01106c3SAxel Dörfler 207f01106c3SAxel Dörfler 208f01106c3SAxel Dörfler NetworkStatusView* 209f01106c3SAxel Dörfler NetworkStatusView::Instantiate(BMessage* archive) 210f01106c3SAxel Dörfler { 211f01106c3SAxel Dörfler if (!validate_instantiation(archive, "NetworkStatusView")) 212f01106c3SAxel Dörfler return NULL; 213f01106c3SAxel Dörfler 214f01106c3SAxel Dörfler return new NetworkStatusView(archive); 215f01106c3SAxel Dörfler } 216f01106c3SAxel Dörfler 217f01106c3SAxel Dörfler 218f01106c3SAxel Dörfler status_t 219f01106c3SAxel Dörfler NetworkStatusView::Archive(BMessage* archive, bool deep) const 220f01106c3SAxel Dörfler { 221f01106c3SAxel Dörfler status_t status = BView::Archive(archive, deep); 222f01106c3SAxel Dörfler if (status == B_OK) 223f01106c3SAxel Dörfler status = archive->AddString("add_on", kSignature); 224f01106c3SAxel Dörfler if (status == B_OK) 225f01106c3SAxel Dörfler status = archive->AddString("class", "NetworkStatusView"); 226f01106c3SAxel Dörfler 227f01106c3SAxel Dörfler return status; 228f01106c3SAxel Dörfler } 229f01106c3SAxel Dörfler 230f01106c3SAxel Dörfler 231f01106c3SAxel Dörfler void 232f01106c3SAxel Dörfler NetworkStatusView::AttachedToWindow() 233f01106c3SAxel Dörfler { 234f01106c3SAxel Dörfler BView::AttachedToWindow(); 235ff09c7bfSAxel Dörfler if (Parent() != NULL) { 236ff09c7bfSAxel Dörfler if ((Parent()->Flags() & B_DRAW_ON_CHILDREN) != 0) 237ff09c7bfSAxel Dörfler SetViewColor(B_TRANSPARENT_COLOR); 238f01106c3SAxel Dörfler else 239fa19dd44Slooncraz AdoptParentColors(); 240ff09c7bfSAxel Dörfler } else 241fa19dd44Slooncraz SetViewUIColor(B_PANEL_BACKGROUND_COLOR); 242f01106c3SAxel Dörfler 243fa19dd44Slooncraz if (ViewUIColor() != B_NO_COLOR) 244fa19dd44Slooncraz SetLowUIColor(ViewUIColor()); 245fa19dd44Slooncraz else 246f01106c3SAxel Dörfler SetLowColor(ViewColor()); 247f01106c3SAxel Dörfler 248195981bbSAxel Dörfler start_watching_network( 249195981bbSAxel Dörfler B_WATCH_NETWORK_INTERFACE_CHANGES | B_WATCH_NETWORK_LINK_CHANGES, this); 250f01106c3SAxel Dörfler 251f01106c3SAxel Dörfler _Update(); 252f01106c3SAxel Dörfler } 253f01106c3SAxel Dörfler 254f01106c3SAxel Dörfler 255f01106c3SAxel Dörfler void 256f01106c3SAxel Dörfler NetworkStatusView::DetachedFromWindow() 257f01106c3SAxel Dörfler { 258195981bbSAxel Dörfler stop_watching_network(this); 259f01106c3SAxel Dörfler } 260f01106c3SAxel Dörfler 261f01106c3SAxel Dörfler 262f01106c3SAxel Dörfler void 263f01106c3SAxel Dörfler NetworkStatusView::MessageReceived(BMessage* message) 264f01106c3SAxel Dörfler { 265f01106c3SAxel Dörfler switch (message->what) { 266195981bbSAxel Dörfler case B_NETWORK_MONITOR: 267f01106c3SAxel Dörfler _Update(); 268f01106c3SAxel Dörfler break; 269f01106c3SAxel Dörfler 270f01106c3SAxel Dörfler case kMsgShowConfiguration: 271f01106c3SAxel Dörfler _ShowConfiguration(message); 272f01106c3SAxel Dörfler break; 273f01106c3SAxel Dörfler 274c1502c9aSStephan Aßmus case kMsgOpenNetworkPreferences: 2753b41ad86SStephan Aßmus _OpenNetworksPreferences(); 2763b41ad86SStephan Aßmus break; 2773b41ad86SStephan Aßmus 2782e1a6286SAxel Dörfler case kMsgJoinNetwork: 2792e1a6286SAxel Dörfler { 2802e1a6286SAxel Dörfler const char* deviceName; 2812e1a6286SAxel Dörfler const char* name; 2826c7caf44SAxel Dörfler BNetworkAddress address; 2832e1a6286SAxel Dörfler if (message->FindString("device", &deviceName) == B_OK 2846c7caf44SAxel Dörfler && message->FindString("name", &name) == B_OK 2856c7caf44SAxel Dörfler && message->FindFlat("address", &address) == B_OK) { 2862e1a6286SAxel Dörfler BNetworkDevice device(deviceName); 2876c7caf44SAxel Dörfler status_t status = device.JoinNetwork(address); 2882e1a6286SAxel Dörfler if (status != B_OK) { 2892e1a6286SAxel Dörfler BString text 2902e1a6286SAxel Dörfler = B_TRANSLATE("Could not join wireless network:\n"); 2912e1a6286SAxel Dörfler text << strerror(status); 2922e1a6286SAxel Dörfler BAlert* alert = new BAlert(name, text.String(), 2932e1a6286SAxel Dörfler B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, 2942e1a6286SAxel Dörfler B_STOP_ALERT); 295aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 2962e1a6286SAxel Dörfler alert->Go(NULL); 2972e1a6286SAxel Dörfler } 2982e1a6286SAxel Dörfler } 2992e1a6286SAxel Dörfler break; 3002e1a6286SAxel Dörfler } 3012e1a6286SAxel Dörfler 302f01106c3SAxel Dörfler case B_ABOUT_REQUESTED: 303f01106c3SAxel Dörfler _AboutRequested(); 304f01106c3SAxel Dörfler break; 305f01106c3SAxel Dörfler 306f01106c3SAxel Dörfler case B_QUIT_REQUESTED: 307f01106c3SAxel Dörfler _Quit(); 308f01106c3SAxel Dörfler break; 309f01106c3SAxel Dörfler 310f01106c3SAxel Dörfler default: 311f01106c3SAxel Dörfler BView::MessageReceived(message); 312f01106c3SAxel Dörfler } 313f01106c3SAxel Dörfler } 314f01106c3SAxel Dörfler 315f01106c3SAxel Dörfler 316f01106c3SAxel Dörfler void 317f01106c3SAxel Dörfler NetworkStatusView::FrameResized(float width, float height) 318f01106c3SAxel Dörfler { 319f01106c3SAxel Dörfler _UpdateBitmaps(); 320d5ba07a3SAxel Dörfler Invalidate(); 321f01106c3SAxel Dörfler } 322f01106c3SAxel Dörfler 323f01106c3SAxel Dörfler 324f01106c3SAxel Dörfler void 325f01106c3SAxel Dörfler NetworkStatusView::Draw(BRect updateRect) 326f01106c3SAxel Dörfler { 3275c46b171SRene Gollent int32 status = kStatusUnknown; 3285c46b171SRene Gollent for (std::map<BString, int32>::const_iterator it 3295c46b171SRene Gollent = fInterfaceStatuses.begin(); it != fInterfaceStatuses.end(); ++it) { 3305c46b171SRene Gollent if (it->second > status) 3315c46b171SRene Gollent status = it->second; 3325c46b171SRene Gollent } 3335c46b171SRene Gollent 3345c46b171SRene Gollent if (fTrayIcons[status] == NULL) 335f01106c3SAxel Dörfler return; 336f01106c3SAxel Dörfler 337f01106c3SAxel Dörfler SetDrawingMode(B_OP_ALPHA); 3385c46b171SRene Gollent DrawBitmap(fTrayIcons[status]); 339f01106c3SAxel Dörfler SetDrawingMode(B_OP_COPY); 340f01106c3SAxel Dörfler } 341f01106c3SAxel Dörfler 342f01106c3SAxel Dörfler 343f01106c3SAxel Dörfler void 344f01106c3SAxel Dörfler NetworkStatusView::_ShowConfiguration(BMessage* message) 345f01106c3SAxel Dörfler { 346f01106c3SAxel Dörfler const char* name; 347f01106c3SAxel Dörfler if (message->FindString("interface", &name) != B_OK) 348f01106c3SAxel Dörfler return; 349f01106c3SAxel Dörfler 3508897f278SStefano Ceccherini BNetworkInterface networkInterface(name); 3518897f278SStefano Ceccherini if (!networkInterface.Exists()) 352f01106c3SAxel Dörfler return; 353f01106c3SAxel Dörfler 3548897f278SStefano Ceccherini BNetworkInterfaceAddress address; 3558897f278SStefano Ceccherini networkInterface.GetAddressAt(0, address); 3568897f278SStefano Ceccherini // TODO: We should get all addresses, 3578897f278SStefano Ceccherini // not just the first one. 3582ee8f3f6SSiarzhuk Zharski BString text(B_TRANSLATE("%ifaceName information:\n")); 3592ee8f3f6SSiarzhuk Zharski text.ReplaceFirst("%ifaceName", name); 360757e7059SAdrien Destugues 361f01106c3SAxel Dörfler size_t boldLength = text.Length(); 362f01106c3SAxel Dörfler 36377206143SAxel Dörfler text << "\n" << B_TRANSLATE("Address") << ": " << address.Address().ToString(); 36477206143SAxel Dörfler text << "\n" << B_TRANSLATE("Broadcast") << ": " << address.Broadcast().ToString(); 36577206143SAxel Dörfler text << "\n" << B_TRANSLATE("Netmask") << ": " << address.Mask().ToString(); 366f01106c3SAxel Dörfler 367757e7059SAdrien Destugues BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK")); 368aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 369f01106c3SAxel Dörfler BTextView* view = alert->TextView(); 370f01106c3SAxel Dörfler BFont font; 371f01106c3SAxel Dörfler 372f01106c3SAxel Dörfler view->SetStylable(true); 373f01106c3SAxel Dörfler view->GetFont(&font); 374f01106c3SAxel Dörfler font.SetFace(B_BOLD_FACE); 375f01106c3SAxel Dörfler view->SetFontAndColor(0, boldLength, &font); 376f01106c3SAxel Dörfler 377f01106c3SAxel Dörfler alert->Go(NULL); 378f01106c3SAxel Dörfler } 379f01106c3SAxel Dörfler 380f01106c3SAxel Dörfler 381f01106c3SAxel Dörfler void 382f01106c3SAxel Dörfler NetworkStatusView::MouseDown(BPoint point) 383f01106c3SAxel Dörfler { 384f01106c3SAxel Dörfler BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false); 385a31688d2SAxel Dörfler menu->SetAsyncAutoDestruct(true); 386f01106c3SAxel Dörfler menu->SetFont(be_plain_font); 3875c46b171SRene Gollent BString wifiInterface; 3885c46b171SRene Gollent BNetworkDevice wifiDevice; 389f01106c3SAxel Dörfler 3905fe97f21SAxel Dörfler // Add interfaces 3915fe97f21SAxel Dörfler 3925c46b171SRene Gollent for (std::map<BString, int32>::const_iterator it 3935c46b171SRene Gollent = fInterfaceStatuses.begin(); it != fInterfaceStatuses.end(); ++it) { 3945c46b171SRene Gollent const BString& name = it->first; 395f01106c3SAxel Dörfler 396f01106c3SAxel Dörfler BString label = name; 397f01106c3SAxel Dörfler label += ": "; 398f01106c3SAxel Dörfler label += kStatusDescriptions[ 399f01106c3SAxel Dörfler _DetermineInterfaceStatus(name.String())]; 400f01106c3SAxel Dörfler 401f01106c3SAxel Dörfler BMessage* info = new BMessage(kMsgShowConfiguration); 402f01106c3SAxel Dörfler info->AddString("interface", name.String()); 403f01106c3SAxel Dörfler menu->AddItem(new BMenuItem(label.String(), info)); 4045c46b171SRene Gollent 4055c46b171SRene Gollent // We only show the networks of the first wireless device we find. 4065c46b171SRene Gollent if (wifiInterface.IsEmpty()) { 4075c46b171SRene Gollent wifiDevice.SetTo(name); 4085c46b171SRene Gollent if (wifiDevice.IsWireless()) 4095c46b171SRene Gollent wifiInterface = name; 4105c46b171SRene Gollent } 411f01106c3SAxel Dörfler } 412f01106c3SAxel Dörfler 4135c46b171SRene Gollent if (!fInterfaceStatuses.empty()) 414f01106c3SAxel Dörfler menu->AddSeparatorItem(); 4155fe97f21SAxel Dörfler 4165fe97f21SAxel Dörfler // Add wireless networks, if any 4175fe97f21SAxel Dörfler 4185c46b171SRene Gollent if (!wifiInterface.IsEmpty()) { 419440d0e61SAxel Dörfler std::set<BNetworkAddress> associated; 420440d0e61SAxel Dörfler BNetworkAddress address; 421440d0e61SAxel Dörfler uint32 cookie = 0; 4225c46b171SRene Gollent while (wifiDevice.GetNextAssociatedNetwork(cookie, address) == B_OK) 423440d0e61SAxel Dörfler associated.insert(address); 424440d0e61SAxel Dörfler 425440d0e61SAxel Dörfler cookie = 0; 426aba4cb20SMichael Lotz wireless_network network; 427aba4cb20SMichael Lotz typedef std::vector<wireless_network> WirelessNetworkVector; 428aba4cb20SMichael Lotz WirelessNetworkVector wirelessNetworks; 429aba4cb20SMichael Lotz while (wifiDevice.GetNextNetwork(cookie, network) == B_OK) 430aba4cb20SMichael Lotz wirelessNetworks.push_back(network); 431aba4cb20SMichael Lotz 432aba4cb20SMichael Lotz std::sort(wirelessNetworks.begin(), wirelessNetworks.end(), 433aba4cb20SMichael Lotz signal_strength_compare); 434aba4cb20SMichael Lotz 435aba4cb20SMichael Lotz int32 count = 0; 436aba4cb20SMichael Lotz for (WirelessNetworkVector::iterator it = wirelessNetworks.begin(); 437aba4cb20SMichael Lotz it != wirelessNetworks.end(); it++) { 438aba4cb20SMichael Lotz wireless_network &network = *it; 439aba4cb20SMichael Lotz 4402e1a6286SAxel Dörfler BMessage* message = new BMessage(kMsgJoinNetwork); 4415c46b171SRene Gollent message->AddString("device", wifiInterface); 4422e1a6286SAxel Dörfler message->AddString("name", network.name); 4436c7caf44SAxel Dörfler message->AddFlat("address", &network.address); 4442e1a6286SAxel Dörfler 445440d0e61SAxel Dörfler BMenuItem* item = new WirelessNetworkMenuItem(network.name, 4465bb799c2SPrzemysław Buczkowski network.signal_strength, network.authentication_mode, message); 447440d0e61SAxel Dörfler menu->AddItem(item); 448440d0e61SAxel Dörfler if (associated.find(network.address) != associated.end()) 449440d0e61SAxel Dörfler item->SetMarked(true); 450440d0e61SAxel Dörfler 4515fe97f21SAxel Dörfler count++; 4525fe97f21SAxel Dörfler } 4535fe97f21SAxel Dörfler if (count == 0) { 4545fe97f21SAxel Dörfler BMenuItem* item = new BMenuItem( 4555fe97f21SAxel Dörfler B_TRANSLATE("<no wireless networks found>"), NULL); 4565fe97f21SAxel Dörfler item->SetEnabled(false); 4575fe97f21SAxel Dörfler menu->AddItem(item); 4585fe97f21SAxel Dörfler } 4595fe97f21SAxel Dörfler menu->AddSeparatorItem(); 4605fe97f21SAxel Dörfler } 4615fe97f21SAxel Dörfler 462757e7059SAdrien Destugues menu->AddItem(new BMenuItem(B_TRANSLATE( 463757e7059SAdrien Destugues "Open network preferences" B_UTF8_ELLIPSIS), 464c1502c9aSStephan Aßmus new BMessage(kMsgOpenNetworkPreferences))); 4653b41ad86SStephan Aßmus 4665fe97f21SAxel Dörfler if (fInDeskbar) { 467757e7059SAdrien Destugues menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"), 468757e7059SAdrien Destugues new BMessage(B_QUIT_REQUESTED))); 4695fe97f21SAxel Dörfler } 470f01106c3SAxel Dörfler menu->SetTargetForItems(this); 471f01106c3SAxel Dörfler 472f01106c3SAxel Dörfler ConvertToScreen(&point); 473a31688d2SAxel Dörfler menu->Go(point, true, true, true); 474f01106c3SAxel Dörfler } 475f01106c3SAxel Dörfler 476f01106c3SAxel Dörfler 477f01106c3SAxel Dörfler void 478f01106c3SAxel Dörfler NetworkStatusView::_AboutRequested() 479f01106c3SAxel Dörfler { 48031535ac6SAdrien Destugues BAboutWindow* window = new BAboutWindow( 48131535ac6SAdrien Destugues B_TRANSLATE_SYSTEM_NAME("NetworkStatus"), kSignature); 482fd19c736SAdrien Destugues 483730a45eeSJohn Scipione const char* authors[] = { 484730a45eeSJohn Scipione "Axel Dörfler", 485730a45eeSJohn Scipione "Hugo Santos", 486730a45eeSJohn Scipione NULL 487730a45eeSJohn Scipione }; 488f01106c3SAxel Dörfler 489fd19c736SAdrien Destugues window->AddCopyright(2007, "Haiku, Inc."); 490fd19c736SAdrien Destugues window->AddAuthors(authors); 491fd19c736SAdrien Destugues 492fd19c736SAdrien Destugues window->Show(); 493f01106c3SAxel Dörfler } 494f01106c3SAxel Dörfler 495f01106c3SAxel Dörfler 496f01106c3SAxel Dörfler int32 4975c46b171SRene Gollent NetworkStatusView::_DetermineInterfaceStatus( 4985c46b171SRene Gollent const BNetworkInterface& interface) 499f01106c3SAxel Dörfler { 5005fe97f21SAxel Dörfler uint32 flags = interface.Flags(); 501f01106c3SAxel Dörfler 502cb7833c7SPhilippe Houdoin if ((flags & IFF_LINK) == 0) 503cb7833c7SPhilippe Houdoin return kStatusNoLink; 504cb7833c7SPhilippe Houdoin if ((flags & (IFF_UP | IFF_LINK | IFF_CONFIGURING)) == IFF_LINK) 505cb7833c7SPhilippe Houdoin return kStatusLinkNoConfig; 5064a531db1SPhilippe Houdoin if ((flags & IFF_CONFIGURING) == IFF_CONFIGURING) 507cb7833c7SPhilippe Houdoin return kStatusConnecting; 508cb7833c7SPhilippe Houdoin if ((flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK)) 509cb7833c7SPhilippe Houdoin return kStatusReady; 510f01106c3SAxel Dörfler 511cb7833c7SPhilippe Houdoin return kStatusUnknown; 512f01106c3SAxel Dörfler } 513f01106c3SAxel Dörfler 514f01106c3SAxel Dörfler 515f01106c3SAxel Dörfler void 516f01106c3SAxel Dörfler NetworkStatusView::_Update(bool force) 517f01106c3SAxel Dörfler { 5185fe97f21SAxel Dörfler BNetworkRoster& roster = BNetworkRoster::Default(); 5195fe97f21SAxel Dörfler BNetworkInterface interface; 5205fe97f21SAxel Dörfler uint32 cookie = 0; 5215fe97f21SAxel Dörfler 5225fe97f21SAxel Dörfler while (roster.GetNextInterface(&cookie, interface) == B_OK) { 5235fe97f21SAxel Dörfler if ((interface.Flags() & IFF_LOOPBACK) == 0) { 5245c46b171SRene Gollent int32 oldStatus = kStatusUnknown; 5255c46b171SRene Gollent if (fInterfaceStatuses.find(interface.Name()) 5265c46b171SRene Gollent != fInterfaceStatuses.end()) { 5275c46b171SRene Gollent oldStatus = fInterfaceStatuses[interface.Name()]; 528f01106c3SAxel Dörfler } 5295c46b171SRene Gollent int32 status = _DetermineInterfaceStatus(interface); 5305c46b171SRene Gollent if (oldStatus != status) { 531b44d24c0SAlexander von Gluck IV BNotification notification(B_INFORMATION_NOTIFICATION); 532b44d24c0SAlexander von Gluck IV notification.SetGroup(B_TRANSLATE("Network Status")); 533b44d24c0SAlexander von Gluck IV notification.SetTitle(interface.Name()); 534b44d24c0SAlexander von Gluck IV notification.SetMessageID(interface.Name()); 5355c46b171SRene Gollent notification.SetIcon(fNotifyIcons[status]); 5365c46b171SRene Gollent if (status == kStatusConnecting 5375c46b171SRene Gollent || (status == kStatusReady 5385c46b171SRene Gollent && oldStatus == kStatusConnecting) 5395c46b171SRene Gollent || (status == kStatusNoLink 5405c46b171SRene Gollent && oldStatus == kStatusReady) 5415c46b171SRene Gollent || (status == kStatusNoLink 5425c46b171SRene Gollent && oldStatus == kStatusConnecting)) { 54343c68287SAlexander von Gluck IV // A significant state change, raise notification. 5445c46b171SRene Gollent notification.SetContent(kStatusDescriptions[status]); 545b44d24c0SAlexander von Gluck IV notification.Send(); 546b44d24c0SAlexander von Gluck IV } 547f01106c3SAxel Dörfler Invalidate(); 548f01106c3SAxel Dörfler } 5495c46b171SRene Gollent fInterfaceStatuses[interface.Name()] = status; 5505c46b171SRene Gollent } 5515c46b171SRene Gollent } 552b44d24c0SAlexander von Gluck IV } 553f01106c3SAxel Dörfler 554f01106c3SAxel Dörfler 5553b41ad86SStephan Aßmus void 5563b41ad86SStephan Aßmus NetworkStatusView::_OpenNetworksPreferences() 5573b41ad86SStephan Aßmus { 558195981bbSAxel Dörfler status_t status = be_roster->Launch("application/x-vnd.Haiku-Network"); 55946cac7f7SAlexandre Deckner if (status != B_OK && status != B_ALREADY_RUNNING) { 560757e7059SAdrien Destugues BString errorMessage(B_TRANSLATE("Launching the network preflet " 561757e7059SAdrien Destugues "failed.\n\nError: ")); 562195981bbSAxel Dörfler errorMessage << strerror(status); 5633b41ad86SStephan Aßmus BAlert* alert = new BAlert("launch error", errorMessage.String(), 564757e7059SAdrien Destugues B_TRANSLATE("OK")); 565aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 566195981bbSAxel Dörfler 567195981bbSAxel Dörfler // asynchronous alert in order to not block replicant host application 5683b41ad86SStephan Aßmus alert->Go(NULL); 5693b41ad86SStephan Aßmus } 5703b41ad86SStephan Aßmus } 5713b41ad86SStephan Aßmus 5723b41ad86SStephan Aßmus 573f01106c3SAxel Dörfler // #pragma mark - 574f01106c3SAxel Dörfler 575f01106c3SAxel Dörfler 576f01106c3SAxel Dörfler extern "C" _EXPORT BView * 577*efafab64SAxel Dörfler instantiate_deskbar_item(float maxWidth, float maxHeight) 578f01106c3SAxel Dörfler { 579*efafab64SAxel Dörfler return new NetworkStatusView(BRect(0, 0, maxHeight - 1, maxHeight - 1), 58066eba86fSAxel Dörfler B_FOLLOW_LEFT | B_FOLLOW_TOP, true); 581f01106c3SAxel Dörfler } 582f01106c3SAxel Dörfler 583