* Added WirelessNetworkMenuItem class that uses the RadioView static Draw()

method to indicate the signal quality.
* If a wireless driver is found, we now use the new BNetworkDevice class to
  retrieve a list of known networks, and show them in the popup menu. You can't
  do anything with this list yet, though (ie. you cannot connect this way yet).
* Use the new BNetworkRoster/BNetworkInterface class to retrieve the list of
  interfaces, and the flags for an interface instead of using ioctls.
* Removed the about menu item for now.
* Minor coding style cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39395 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2010-11-10 22:21:39 +00:00
parent 223cae9fe6
commit 5fe97f21b6
2 changed files with 137 additions and 60 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ Application NetworkStatus :
NetworkStatus.cpp NetworkStatus.cpp
RadioView.cpp RadioView.cpp
: be $(HAIKU_LOCALE_LIBS) $(icon_libs) $(TARGET_NETWORK_LIBS) : be $(HAIKU_LOCALE_LIBS) $(icon_libs) $(TARGET_NETWORK_LIBS)
$(TARGET_LIBSUPC++) libbnetapi.so $(TARGET_LIBSUPC++)
: NetworkStatus.rdef NetworkStatusIcons.rdef : NetworkStatus.rdef NetworkStatusIcons.rdef
; ;
+136 -59
View File
@@ -31,16 +31,18 @@
#include <Locale.h> #include <Locale.h>
#include <MenuItem.h> #include <MenuItem.h>
#include <MessageRunner.h> #include <MessageRunner.h>
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <PopUpMenu.h> #include <PopUpMenu.h>
#include <Resources.h> #include <Resources.h>
#include <Roster.h> #include <Roster.h>
#include <String.h> #include <String.h>
#include <TextView.h> #include <TextView.h>
#include <net_notifications.h>
#include "NetworkStatus.h" #include "NetworkStatus.h"
#include "NetworkStatusIcons.h" #include "NetworkStatusIcons.h"
#include "RadioView.h"
#undef B_TRANSLATE_CONTEXT #undef B_TRANSLATE_CONTEXT
@@ -65,6 +67,31 @@ const uint32 kMinIconWidth = 16;
const uint32 kMinIconHeight = 16; const uint32 kMinIconHeight = 16;
class WirelessNetworkMenuItem : public BMenuItem {
public:
WirelessNetworkMenuItem(const char* name,
int32 signalQuality, bool encrypted,
BMessage* message);
virtual ~WirelessNetworkMenuItem();
void SetSignalQuality(int32 quality);
int32 SignalQuality() const
{ return fQuality; }
bool IsEncrypted() const
{ return fIsEncrypted; }
protected:
virtual void DrawContent();
virtual void Highlight(bool isHighlighted);
virtual void GetContentSize(float* width, float* height);
void DrawRadioIcon();
private:
int32 fQuality;
bool fIsEncrypted;
};
class SocketOpener { class SocketOpener {
public: public:
SocketOpener() SocketOpener()
@@ -92,6 +119,66 @@ private:
}; };
// #pragma mark - WirelessNetworkMenuItem
WirelessNetworkMenuItem::WirelessNetworkMenuItem(const char* name,
int32 signalQuality, bool encrypted, BMessage* message)
:
BMenuItem(name, message),
fQuality(signalQuality),
fIsEncrypted(encrypted)
{
}
WirelessNetworkMenuItem::~WirelessNetworkMenuItem()
{
}
void
WirelessNetworkMenuItem::SetSignalQuality(int32 quality)
{
fQuality = quality;
}
void
WirelessNetworkMenuItem::DrawContent()
{
DrawRadioIcon();
BMenuItem::DrawContent();
}
void
WirelessNetworkMenuItem::Highlight(bool isHighlighted)
{
BMenuItem::Highlight(isHighlighted);
}
void
WirelessNetworkMenuItem::GetContentSize(float* width, float* height)
{
BMenuItem::GetContentSize(width, height);
*width += *height + 4;
}
void
WirelessNetworkMenuItem::DrawRadioIcon()
{
BRect bounds = Frame();
bounds.left = bounds.right - 4 - bounds.Height();
bounds.right -= 4;
bounds.bottom -= 2;
RadioView::Draw(Menu(), bounds, fQuality, RadioView::DefaultMax());
}
// #pragma mark - // #pragma mark -
@@ -392,6 +479,8 @@ NetworkStatusView::MouseDown(BPoint point)
menu->SetAsyncAutoDestruct(true); menu->SetAsyncAutoDestruct(true);
menu->SetFont(be_plain_font); menu->SetFont(be_plain_font);
// Add interfaces
for (int32 i = 0; i < fInterfaces.CountItems(); i++) { for (int32 i = 0; i < fInterfaces.CountItems(); i++) {
BString& name = *fInterfaces.ItemAt(i); BString& name = *fInterfaces.ItemAt(i);
@@ -406,16 +495,43 @@ NetworkStatusView::MouseDown(BPoint point)
} }
menu->AddSeparatorItem(); menu->AddSeparatorItem();
//menu->AddItem(new BMenuItem(B_TRANSLATE(
// "About NetworkStatus" B_UTF8_ELLIPSIS), // Add wireless networks, if any
// new BMessage(B_ABOUT_REQUESTED)));
for (int32 i = 0; i < fInterfaces.CountItems(); i++) {
BNetworkDevice device(fInterfaces.ItemAt(i)->String());
if (!device.IsWireless())
continue;
wireless_network network;
int32 count = 0;
for (int32 i = 0; device.GetScanResultAt(i, network) == B_OK; i++) {
printf("%s: noise %u : rssi %u\n", network.name,
network.noise_level, network.signal_strength);
menu->AddItem(new WirelessNetworkMenuItem(network.name,
network.signal_strength, false, NULL));
count++;
}
if (count == 0) {
BMenuItem* item = new BMenuItem(
B_TRANSLATE("<no wireless networks found>"), NULL);
item->SetEnabled(false);
menu->AddItem(item);
}
menu->AddSeparatorItem();
// We only show the networks of the first wireless device we find.
break;
}
menu->AddItem(new BMenuItem(B_TRANSLATE( menu->AddItem(new BMenuItem(B_TRANSLATE(
"Open network preferences" B_UTF8_ELLIPSIS), "Open network preferences" B_UTF8_ELLIPSIS),
new BMessage(kMsgOpenNetworkPreferences))); new BMessage(kMsgOpenNetworkPreferences)));
if (fInDeskbar) if (fInDeskbar) {
menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"), menu->AddItem(new BMenuItem(B_TRANSLATE("Quit"),
new BMessage(B_QUIT_REQUESTED))); new BMessage(B_QUIT_REQUESTED)));
}
menu->SetTargetForItems(this); menu->SetTargetForItems(this);
ConvertToScreen(&point); ConvertToScreen(&point);
@@ -426,14 +542,14 @@ NetworkStatusView::MouseDown(BPoint point)
void void
NetworkStatusView::_AboutRequested() NetworkStatusView::_AboutRequested()
{ {
BString _about = B_TRANSLATE( BString about = B_TRANSLATE(
"NetworkStatus\n\twritten by %1 and Hugo Santos\n\t%2, Haiku, Inc.\n" "NetworkStatus\n\twritten by %1 and Hugo Santos\n\t%2, Haiku, Inc.\n"
); );
_about.ReplaceFirst("%1", "Axel Dörfler"); about.ReplaceFirst("%1", "Axel Dörfler");
// Append a new developer here // Append a new developer here
_about.ReplaceFirst("%2", "Copyright 2007-2010"); about.ReplaceFirst("%2", "Copyright 2007-2010");
// Append a new year here // Append a new year here
BAlert* alert = new BAlert("about", _about, B_TRANSLATE("OK")); BAlert* alert = new BAlert("about", about, B_TRANSLATE("OK"));
BTextView *view = alert->TextView(); BTextView *view = alert->TextView();
BFont font; BFont font;
@@ -462,18 +578,8 @@ NetworkStatusView::_PrepareRequest(struct ifreq& request, const char* name)
int32 int32
NetworkStatusView::_DetermineInterfaceStatus(const char* name) NetworkStatusView::_DetermineInterfaceStatus(const char* name)
{ {
SocketOpener socket; BNetworkInterface interface(name);
if (socket.InitCheck() != B_OK) uint32 flags = interface.Flags();
return kStatusUnknown;
ifreq request;
if (!_PrepareRequest(request, name))
return kStatusUnknown;
uint32 flags = 0;
if (ioctl(socket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
flags = request.ifr_flags;
int32 status = kStatusNoLink; int32 status = kStatusNoLink;
// TODO: no kStatusLinkNoConfig yet // TODO: no kStatusLinkNoConfig yet
@@ -490,52 +596,23 @@ NetworkStatusView::_DetermineInterfaceStatus(const char* name)
void void
NetworkStatusView::_Update(bool force) NetworkStatusView::_Update(bool force)
{ {
SocketOpener socket;
if (socket.InitCheck() != B_OK)
return;
// iterate over all interfaces and retrieve minimal status
ifconf config;
config.ifc_len = sizeof(config.ifc_value);
if (ioctl(socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
return;
uint32 count = (uint32)config.ifc_value;
if (count == 0)
return;
void* buffer = malloc(count * sizeof(struct ifreq));
if (buffer == NULL)
return;
config.ifc_len = count * sizeof(struct ifreq);
config.ifc_buf = buffer;
if (ioctl(socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0) {
free(buffer);
return;
}
ifreq* interface = (ifreq*)buffer;
int32 oldStatus = fStatus; int32 oldStatus = fStatus;
fStatus = kStatusUnknown; fStatus = kStatusUnknown;
fInterfaces.MakeEmpty(); fInterfaces.MakeEmpty();
for (uint32 i = 0; i < count; i++) { BNetworkRoster& roster = BNetworkRoster::Default();
if (strncmp(interface->ifr_name, "loop", 4) && interface->ifr_name[0]) { BNetworkInterface interface;
fInterfaces.AddItem(new BString(interface->ifr_name)); uint32 cookie = 0;
int32 status = _DetermineInterfaceStatus(interface->ifr_name);
while (roster.GetNextInterface(&cookie, interface) == B_OK) {
if ((interface.Flags() & IFF_LOOPBACK) == 0) {
fInterfaces.AddItem(new BString(interface.Name()));
int32 status = _DetermineInterfaceStatus(interface.Name());
if (status > fStatus) if (status > fStatus)
fStatus = status; fStatus = status;
} }
interface = (ifreq *)((addr_t)interface
+ _SIZEOF_ADDR_IFREQ(interface[0]));
} }
free(buffer);
if (fStatus != oldStatus) if (fStatus != oldStatus)
Invalidate(); Invalidate();
} }