1 /* 2 * Copyright 2010, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "WirelessNetworkMenuItem.h" 8 9 #include <string.h> 10 11 #include <Catalog.h> 12 #include <String.h> 13 14 #include "RadioView.h" 15 16 17 #undef B_TRANSLATION_CONTEXT 18 #define B_TRANSLATION_CONTEXT "WirelessNetworkMenuItem" 19 20 21 WirelessNetworkMenuItem::WirelessNetworkMenuItem(wireless_network network, 22 BMessage* message) 23 : 24 BMenuItem(network.name, message), 25 fNetwork(network) 26 { 27 // Append authentication mode to label 28 BString label = B_TRANSLATE("%name% (%authenticationMode%)"); 29 label.Replace("%name%", network.name, 1); 30 label.Replace("%authenticationMode%", 31 AuthenticationName(network.authentication_mode), 1); 32 33 SetLabel(label.String()); 34 } 35 36 37 WirelessNetworkMenuItem::~WirelessNetworkMenuItem() 38 { 39 } 40 41 42 BString 43 WirelessNetworkMenuItem::AuthenticationName(int32 mode) 44 { 45 switch (mode) { 46 default: 47 case B_NETWORK_AUTHENTICATION_NONE: 48 return B_TRANSLATE_CONTEXT("open", "Open network"); 49 break; 50 case B_NETWORK_AUTHENTICATION_WEP: 51 return B_TRANSLATE_CONTEXT("WEP", "WEP protected network"); 52 break; 53 case B_NETWORK_AUTHENTICATION_WPA: 54 return B_TRANSLATE_CONTEXT("WPA", "WPA protected network"); 55 break; 56 case B_NETWORK_AUTHENTICATION_WPA2: 57 return B_TRANSLATE_CONTEXT("WPA2", "WPA2 protected network"); 58 break; 59 case B_NETWORK_AUTHENTICATION_EAP: 60 return B_TRANSLATE_CONTEXT("EAP", "EAP protected network"); 61 break; 62 } 63 } 64 65 66 void 67 WirelessNetworkMenuItem::DrawContent() 68 { 69 DrawRadioIcon(); 70 BMenuItem::DrawContent(); 71 } 72 73 74 void 75 WirelessNetworkMenuItem::Highlight(bool isHighlighted) 76 { 77 BMenuItem::Highlight(isHighlighted); 78 } 79 80 81 void 82 WirelessNetworkMenuItem::GetContentSize(float* width, float* height) 83 { 84 BMenuItem::GetContentSize(width, height); 85 *width += *height + 4; 86 } 87 88 89 void 90 WirelessNetworkMenuItem::DrawRadioIcon() 91 { 92 BRect bounds = Frame(); 93 bounds.left = bounds.right - 4 - bounds.Height(); 94 bounds.right -= 4; 95 bounds.bottom -= 2; 96 97 RadioView::Draw(Menu(), bounds, fNetwork.signal_strength, 98 RadioView::DefaultMax()); 99 } 100 101 102 /*static*/ int 103 WirelessNetworkMenuItem::CompareSignalStrength(const BMenuItem* a, 104 const BMenuItem* b) 105 { 106 WirelessNetworkMenuItem* aItem = *(WirelessNetworkMenuItem**)a; 107 WirelessNetworkMenuItem* bItem = *(WirelessNetworkMenuItem**)b; 108 109 wireless_network aNetwork = aItem->Network(); 110 wireless_network bNetwork = bItem->Network(); 111 112 if (aNetwork.signal_strength == bNetwork.signal_strength) 113 return strncasecmp(aNetwork.name, bNetwork.name, 32); 114 115 return bNetwork.signal_strength - aNetwork.signal_strength; 116 } 117