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 67f01106c3SAxel Dörfler extern "C" _EXPORT BView *instantiate_deskbar_item(void); 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 239ff09c7bfSAxel Dörfler SetViewColor(Parent()->ViewColor()); 240ff09c7bfSAxel Dörfler } else 241f01106c3SAxel Dörfler SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 242f01106c3SAxel Dörfler 243f01106c3SAxel Dörfler SetLowColor(ViewColor()); 244f01106c3SAxel Dörfler 245195981bbSAxel Dörfler start_watching_network( 246195981bbSAxel Dörfler B_WATCH_NETWORK_INTERFACE_CHANGES | B_WATCH_NETWORK_LINK_CHANGES, this); 247f01106c3SAxel Dörfler 248f01106c3SAxel Dörfler _Update(); 249f01106c3SAxel Dörfler } 250f01106c3SAxel Dörfler 251f01106c3SAxel Dörfler 252f01106c3SAxel Dörfler void 253f01106c3SAxel Dörfler NetworkStatusView::DetachedFromWindow() 254f01106c3SAxel Dörfler { 255195981bbSAxel Dörfler stop_watching_network(this); 256f01106c3SAxel Dörfler } 257f01106c3SAxel Dörfler 258f01106c3SAxel Dörfler 259f01106c3SAxel Dörfler void 260f01106c3SAxel Dörfler NetworkStatusView::MessageReceived(BMessage* message) 261f01106c3SAxel Dörfler { 262f01106c3SAxel Dörfler switch (message->what) { 263195981bbSAxel Dörfler case B_NETWORK_MONITOR: 264f01106c3SAxel Dörfler _Update(); 265f01106c3SAxel Dörfler break; 266f01106c3SAxel Dörfler 267f01106c3SAxel Dörfler case kMsgShowConfiguration: 268f01106c3SAxel Dörfler _ShowConfiguration(message); 269f01106c3SAxel Dörfler break; 270f01106c3SAxel Dörfler 271c1502c9aSStephan Aßmus case kMsgOpenNetworkPreferences: 2723b41ad86SStephan Aßmus _OpenNetworksPreferences(); 2733b41ad86SStephan Aßmus break; 2743b41ad86SStephan Aßmus 2752e1a6286SAxel Dörfler case kMsgJoinNetwork: 2762e1a6286SAxel Dörfler { 2772e1a6286SAxel Dörfler const char* deviceName; 2782e1a6286SAxel Dörfler const char* name; 2796c7caf44SAxel Dörfler BNetworkAddress address; 2802e1a6286SAxel Dörfler if (message->FindString("device", &deviceName) == B_OK 2816c7caf44SAxel Dörfler && message->FindString("name", &name) == B_OK 2826c7caf44SAxel Dörfler && message->FindFlat("address", &address) == B_OK) { 2832e1a6286SAxel Dörfler BNetworkDevice device(deviceName); 2846c7caf44SAxel Dörfler status_t status = device.JoinNetwork(address); 2852e1a6286SAxel Dörfler if (status != B_OK) { 2862e1a6286SAxel Dörfler BString text 2872e1a6286SAxel Dörfler = B_TRANSLATE("Could not join wireless network:\n"); 2882e1a6286SAxel Dörfler text << strerror(status); 2892e1a6286SAxel Dörfler BAlert* alert = new BAlert(name, text.String(), 2902e1a6286SAxel Dörfler B_TRANSLATE("OK"), NULL, NULL, B_WIDTH_AS_USUAL, 2912e1a6286SAxel Dörfler B_STOP_ALERT); 292aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 2932e1a6286SAxel Dörfler alert->Go(NULL); 2942e1a6286SAxel Dörfler } 2952e1a6286SAxel Dörfler } 2962e1a6286SAxel Dörfler break; 2972e1a6286SAxel Dörfler } 2982e1a6286SAxel Dörfler 299f01106c3SAxel Dörfler case B_ABOUT_REQUESTED: 300f01106c3SAxel Dörfler _AboutRequested(); 301f01106c3SAxel Dörfler break; 302f01106c3SAxel Dörfler 303f01106c3SAxel Dörfler case B_QUIT_REQUESTED: 304f01106c3SAxel Dörfler _Quit(); 305f01106c3SAxel Dörfler break; 306f01106c3SAxel Dörfler 307f01106c3SAxel Dörfler default: 308f01106c3SAxel Dörfler BView::MessageReceived(message); 309f01106c3SAxel Dörfler } 310f01106c3SAxel Dörfler } 311f01106c3SAxel Dörfler 312f01106c3SAxel Dörfler 313f01106c3SAxel Dörfler void 314f01106c3SAxel Dörfler NetworkStatusView::FrameResized(float width, float height) 315f01106c3SAxel Dörfler { 316f01106c3SAxel Dörfler _UpdateBitmaps(); 317d5ba07a3SAxel Dörfler Invalidate(); 318f01106c3SAxel Dörfler } 319f01106c3SAxel Dörfler 320f01106c3SAxel Dörfler 321f01106c3SAxel Dörfler void 322f01106c3SAxel Dörfler NetworkStatusView::Draw(BRect updateRect) 323f01106c3SAxel Dörfler { 3245c46b171SRene Gollent int32 status = kStatusUnknown; 3255c46b171SRene Gollent for (std::map<BString, int32>::const_iterator it 3265c46b171SRene Gollent = fInterfaceStatuses.begin(); it != fInterfaceStatuses.end(); ++it) { 3275c46b171SRene Gollent if (it->second > status) 3285c46b171SRene Gollent status = it->second; 3295c46b171SRene Gollent } 3305c46b171SRene Gollent 3315c46b171SRene Gollent if (fTrayIcons[status] == NULL) 332f01106c3SAxel Dörfler return; 333f01106c3SAxel Dörfler 334f01106c3SAxel Dörfler SetDrawingMode(B_OP_ALPHA); 3355c46b171SRene Gollent DrawBitmap(fTrayIcons[status]); 336f01106c3SAxel Dörfler SetDrawingMode(B_OP_COPY); 337f01106c3SAxel Dörfler } 338f01106c3SAxel Dörfler 339f01106c3SAxel Dörfler 340f01106c3SAxel Dörfler void 341f01106c3SAxel Dörfler NetworkStatusView::_ShowConfiguration(BMessage* message) 342f01106c3SAxel Dörfler { 343f01106c3SAxel Dörfler const char* name; 344f01106c3SAxel Dörfler if (message->FindString("interface", &name) != B_OK) 345f01106c3SAxel Dörfler return; 346f01106c3SAxel Dörfler 3478897f278SStefano Ceccherini BNetworkInterface networkInterface(name); 3488897f278SStefano Ceccherini if (!networkInterface.Exists()) 349f01106c3SAxel Dörfler return; 350f01106c3SAxel Dörfler 3518897f278SStefano Ceccherini BNetworkInterfaceAddress address; 3528897f278SStefano Ceccherini networkInterface.GetAddressAt(0, address); 3538897f278SStefano Ceccherini // TODO: We should get all addresses, 3548897f278SStefano Ceccherini // not just the first one. 3552ee8f3f6SSiarzhuk Zharski BString text(B_TRANSLATE("%ifaceName information:\n")); 3562ee8f3f6SSiarzhuk Zharski text.ReplaceFirst("%ifaceName", name); 357757e7059SAdrien Destugues 358f01106c3SAxel Dörfler size_t boldLength = text.Length(); 359f01106c3SAxel Dörfler 360*77206143SAxel Dörfler text << "\n" << B_TRANSLATE("Address") << ": " << address.Address().ToString(); 361*77206143SAxel Dörfler text << "\n" << B_TRANSLATE("Broadcast") << ": " << address.Broadcast().ToString(); 362*77206143SAxel Dörfler text << "\n" << B_TRANSLATE("Netmask") << ": " << address.Mask().ToString(); 363f01106c3SAxel Dörfler 364757e7059SAdrien Destugues BAlert* alert = new BAlert(name, text.String(), B_TRANSLATE("OK")); 365aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 366f01106c3SAxel Dörfler BTextView* view = alert->TextView(); 367f01106c3SAxel Dörfler BFont font; 368f01106c3SAxel Dörfler 369f01106c3SAxel Dörfler view->SetStylable(true); 370f01106c3SAxel Dörfler view->GetFont(&font); 371f01106c3SAxel Dörfler font.SetFace(B_BOLD_FACE); 372f01106c3SAxel Dörfler view->SetFontAndColor(0, boldLength, &font); 373f01106c3SAxel Dörfler 374f01106c3SAxel Dörfler alert->Go(NULL); 375f01106c3SAxel Dörfler } 376f01106c3SAxel Dörfler 377f01106c3SAxel Dörfler 378f01106c3SAxel Dörfler void 379f01106c3SAxel Dörfler NetworkStatusView::MouseDown(BPoint point) 380f01106c3SAxel Dörfler { 381f01106c3SAxel Dörfler BPopUpMenu* menu = new BPopUpMenu(B_EMPTY_STRING, false, false); 382a31688d2SAxel Dörfler menu->SetAsyncAutoDestruct(true); 383f01106c3SAxel Dörfler menu->SetFont(be_plain_font); 3845c46b171SRene Gollent BString wifiInterface; 3855c46b171SRene Gollent BNetworkDevice wifiDevice; 386f01106c3SAxel Dörfler 3875fe97f21SAxel Dörfler // Add interfaces 3885fe97f21SAxel Dörfler 3895c46b171SRene Gollent for (std::map<BString, int32>::const_iterator it 3905c46b171SRene Gollent = fInterfaceStatuses.begin(); it != fInterfaceStatuses.end(); ++it) { 3915c46b171SRene Gollent const BString& name = it->first; 392f01106c3SAxel Dörfler 393f01106c3SAxel Dörfler BString label = name; 394f01106c3SAxel Dörfler label += ": "; 395f01106c3SAxel Dörfler label += kStatusDescriptions[ 396f01106c3SAxel Dörfler _DetermineInterfaceStatus(name.String())]; 397f01106c3SAxel Dörfler 398f01106c3SAxel Dörfler BMessage* info = new BMessage(kMsgShowConfiguration); 399f01106c3SAxel Dörfler info->AddString("interface", name.String()); 400f01106c3SAxel Dörfler menu->AddItem(new BMenuItem(label.String(), info)); 4015c46b171SRene Gollent 4025c46b171SRene Gollent // We only show the networks of the first wireless device we find. 4035c46b171SRene Gollent if (wifiInterface.IsEmpty()) { 4045c46b171SRene Gollent wifiDevice.SetTo(name); 4055c46b171SRene Gollent if (wifiDevice.IsWireless()) 4065c46b171SRene Gollent wifiInterface = name; 4075c46b171SRene Gollent } 408f01106c3SAxel Dörfler } 409f01106c3SAxel Dörfler 4105c46b171SRene Gollent if (!fInterfaceStatuses.empty()) 411f01106c3SAxel Dörfler menu->AddSeparatorItem(); 4125fe97f21SAxel Dörfler 4135fe97f21SAxel Dörfler // Add wireless networks, if any 4145fe97f21SAxel Dörfler 4155c46b171SRene Gollent if (!wifiInterface.IsEmpty()) { 416440d0e61SAxel Dörfler std::set<BNetworkAddress> associated; 417440d0e61SAxel Dörfler BNetworkAddress address; 418440d0e61SAxel Dörfler uint32 cookie = 0; 4195c46b171SRene Gollent while (wifiDevice.GetNextAssociatedNetwork(cookie, address) == B_OK) 420440d0e61SAxel Dörfler associated.insert(address); 421440d0e61SAxel Dörfler 422440d0e61SAxel Dörfler cookie = 0; 423aba4cb20SMichael Lotz wireless_network network; 424aba4cb20SMichael Lotz typedef std::vector<wireless_network> WirelessNetworkVector; 425aba4cb20SMichael Lotz WirelessNetworkVector wirelessNetworks; 426aba4cb20SMichael Lotz while (wifiDevice.GetNextNetwork(cookie, network) == B_OK) 427aba4cb20SMichael Lotz wirelessNetworks.push_back(network); 428aba4cb20SMichael Lotz 429aba4cb20SMichael Lotz std::sort(wirelessNetworks.begin(), wirelessNetworks.end(), 430aba4cb20SMichael Lotz signal_strength_compare); 431aba4cb20SMichael Lotz 432aba4cb20SMichael Lotz int32 count = 0; 433aba4cb20SMichael Lotz for (WirelessNetworkVector::iterator it = wirelessNetworks.begin(); 434aba4cb20SMichael Lotz it != wirelessNetworks.end(); it++) { 435aba4cb20SMichael Lotz wireless_network &network = *it; 436aba4cb20SMichael Lotz 4372e1a6286SAxel Dörfler BMessage* message = new BMessage(kMsgJoinNetwork); 4385c46b171SRene Gollent message->AddString("device", wifiInterface); 4392e1a6286SAxel Dörfler message->AddString("name", network.name); 4406c7caf44SAxel Dörfler message->AddFlat("address", &network.address); 4412e1a6286SAxel Dörfler 442440d0e61SAxel Dörfler BMenuItem* item = new WirelessNetworkMenuItem(network.name, 4435bb799c2SPrzemysław Buczkowski network.signal_strength, network.authentication_mode, message); 444440d0e61SAxel Dörfler menu->AddItem(item); 445440d0e61SAxel Dörfler if (associated.find(network.address) != associated.end()) 446440d0e61SAxel Dörfler item->SetMarked(true); 447440d0e61SAxel Dörfler 4485fe97f21SAxel Dörfler count++; 4495fe97f21SAxel Dörfler } 4505fe97f21SAxel Dörfler if (count == 0) { 4515fe97f21SAxel Dörfler BMenuItem* item = new BMenuItem( 4525fe97f21SAxel Dörfler B_TRANSLATE("<no wireless networks found>"), NULL); 4535fe97f21SAxel Dörfler item->SetEnabled(false); 4545fe97f21SAxel Dörfler menu->AddItem(item); 4555fe97f21SAxel Dörfler } 4565fe97f21SAxel Dörfler menu->AddSeparatorItem(); 4575fe97f21SAxel Dörfler } 4585fe97f21SAxel Dörfler 459757e7059SAdrien Destugues menu->AddItem(new BMenuItem(B_TRANSLATE( 460757e7059SAdrien Destugues "Open network preferences" B_UTF8_ELLIPSIS), 461c1502c9aSStephan Aßmus new BMessage(kMsgOpenNetworkPreferences))); 4623b41ad86SStephan Aßmus 4635fe97f21SAxel Dörfler if (fInDeskbar) { 464757e7059SAdrien Destugues menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"), 465757e7059SAdrien Destugues new BMessage(B_QUIT_REQUESTED))); 4665fe97f21SAxel Dörfler } 467f01106c3SAxel Dörfler menu->SetTargetForItems(this); 468f01106c3SAxel Dörfler 469f01106c3SAxel Dörfler ConvertToScreen(&point); 470a31688d2SAxel Dörfler menu->Go(point, true, true, true); 471f01106c3SAxel Dörfler } 472f01106c3SAxel Dörfler 473f01106c3SAxel Dörfler 474f01106c3SAxel Dörfler void 475f01106c3SAxel Dörfler NetworkStatusView::_AboutRequested() 476f01106c3SAxel Dörfler { 47731535ac6SAdrien Destugues BAboutWindow* window = new BAboutWindow( 47831535ac6SAdrien Destugues B_TRANSLATE_SYSTEM_NAME("NetworkStatus"), kSignature); 479fd19c736SAdrien Destugues 480730a45eeSJohn Scipione const char* authors[] = { 481730a45eeSJohn Scipione "Axel Dörfler", 482730a45eeSJohn Scipione "Hugo Santos", 483730a45eeSJohn Scipione NULL 484730a45eeSJohn Scipione }; 485f01106c3SAxel Dörfler 486fd19c736SAdrien Destugues window->AddCopyright(2007, "Haiku, Inc."); 487fd19c736SAdrien Destugues window->AddAuthors(authors); 488fd19c736SAdrien Destugues 489fd19c736SAdrien Destugues window->Show(); 490f01106c3SAxel Dörfler } 491f01106c3SAxel Dörfler 492f01106c3SAxel Dörfler 493f01106c3SAxel Dörfler int32 4945c46b171SRene Gollent NetworkStatusView::_DetermineInterfaceStatus( 4955c46b171SRene Gollent const BNetworkInterface& interface) 496f01106c3SAxel Dörfler { 4975fe97f21SAxel Dörfler uint32 flags = interface.Flags(); 498f01106c3SAxel Dörfler int32 status = kStatusNoLink; 499f01106c3SAxel Dörfler 500f01106c3SAxel Dörfler // TODO: no kStatusLinkNoConfig yet 501f01106c3SAxel Dörfler 502f01106c3SAxel Dörfler if (flags & IFF_CONFIGURING) 503f01106c3SAxel Dörfler status = kStatusConnecting; 5042a17be44SAxel Dörfler else if ((flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK)) 505f01106c3SAxel Dörfler status = kStatusReady; 506f01106c3SAxel Dörfler 507f01106c3SAxel Dörfler return status; 508f01106c3SAxel Dörfler } 509f01106c3SAxel Dörfler 510f01106c3SAxel Dörfler 511f01106c3SAxel Dörfler void 512f01106c3SAxel Dörfler NetworkStatusView::_Update(bool force) 513f01106c3SAxel Dörfler { 5145fe97f21SAxel Dörfler BNetworkRoster& roster = BNetworkRoster::Default(); 5155fe97f21SAxel Dörfler BNetworkInterface interface; 5165fe97f21SAxel Dörfler uint32 cookie = 0; 5175fe97f21SAxel Dörfler 5185fe97f21SAxel Dörfler while (roster.GetNextInterface(&cookie, interface) == B_OK) { 5195fe97f21SAxel Dörfler if ((interface.Flags() & IFF_LOOPBACK) == 0) { 5205c46b171SRene Gollent int32 oldStatus = kStatusUnknown; 5215c46b171SRene Gollent if (fInterfaceStatuses.find(interface.Name()) 5225c46b171SRene Gollent != fInterfaceStatuses.end()) { 5235c46b171SRene Gollent oldStatus = fInterfaceStatuses[interface.Name()]; 524f01106c3SAxel Dörfler } 5255c46b171SRene Gollent int32 status = _DetermineInterfaceStatus(interface); 5265c46b171SRene Gollent if (oldStatus != status) { 527b44d24c0SAlexander von Gluck IV BNotification notification(B_INFORMATION_NOTIFICATION); 528b44d24c0SAlexander von Gluck IV notification.SetGroup(B_TRANSLATE("Network Status")); 529b44d24c0SAlexander von Gluck IV notification.SetTitle(interface.Name()); 530b44d24c0SAlexander von Gluck IV notification.SetMessageID(interface.Name()); 5315c46b171SRene Gollent notification.SetIcon(fNotifyIcons[status]); 5325c46b171SRene Gollent if (status == kStatusConnecting 5335c46b171SRene Gollent || (status == kStatusReady 5345c46b171SRene Gollent && oldStatus == kStatusConnecting) 5355c46b171SRene Gollent || (status == kStatusNoLink 5365c46b171SRene Gollent && oldStatus == kStatusReady) 5375c46b171SRene Gollent || (status == kStatusNoLink 5385c46b171SRene Gollent && oldStatus == kStatusConnecting)) { 53943c68287SAlexander von Gluck IV // A significant state change, raise notification. 5405c46b171SRene Gollent notification.SetContent(kStatusDescriptions[status]); 541b44d24c0SAlexander von Gluck IV notification.Send(); 542b44d24c0SAlexander von Gluck IV } 543f01106c3SAxel Dörfler Invalidate(); 544f01106c3SAxel Dörfler } 5455c46b171SRene Gollent fInterfaceStatuses[interface.Name()] = status; 5465c46b171SRene Gollent } 5475c46b171SRene Gollent } 548b44d24c0SAlexander von Gluck IV } 549f01106c3SAxel Dörfler 550f01106c3SAxel Dörfler 5513b41ad86SStephan Aßmus void 5523b41ad86SStephan Aßmus NetworkStatusView::_OpenNetworksPreferences() 5533b41ad86SStephan Aßmus { 554195981bbSAxel Dörfler status_t status = be_roster->Launch("application/x-vnd.Haiku-Network"); 55546cac7f7SAlexandre Deckner if (status != B_OK && status != B_ALREADY_RUNNING) { 556757e7059SAdrien Destugues BString errorMessage(B_TRANSLATE("Launching the network preflet " 557757e7059SAdrien Destugues "failed.\n\nError: ")); 558195981bbSAxel Dörfler errorMessage << strerror(status); 5593b41ad86SStephan Aßmus BAlert* alert = new BAlert("launch error", errorMessage.String(), 560757e7059SAdrien Destugues B_TRANSLATE("OK")); 561aed35104SHumdinger alert->SetFlags(alert->Flags() | B_CLOSE_ON_ESCAPE); 562195981bbSAxel Dörfler 563195981bbSAxel Dörfler // asynchronous alert in order to not block replicant host application 5643b41ad86SStephan Aßmus alert->Go(NULL); 5653b41ad86SStephan Aßmus } 5663b41ad86SStephan Aßmus } 5673b41ad86SStephan Aßmus 5683b41ad86SStephan Aßmus 569f01106c3SAxel Dörfler // #pragma mark - 570f01106c3SAxel Dörfler 571f01106c3SAxel Dörfler 572f01106c3SAxel Dörfler extern "C" _EXPORT BView * 573f01106c3SAxel Dörfler instantiate_deskbar_item(void) 574f01106c3SAxel Dörfler { 57566eba86fSAxel Dörfler return new NetworkStatusView(BRect(0, 0, 15, 15), 57666eba86fSAxel Dörfler B_FOLLOW_LEFT | B_FOLLOW_TOP, true); 577f01106c3SAxel Dörfler } 578f01106c3SAxel Dörfler 579