[haiku-commits] r40349 - haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn

Author: kallisti5
Date: 2011-02-01 23:34:37 +0100 (Tue, 01 Feb 2011)
New Revision: 40349
Changeset: http://dev.haiku-os.org/changeset/40349

Modified:
   haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesAddOn.cpp
   haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesIcons.rdef
   haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.cpp
   haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.h
Log:
added small function to convert netmask to cidr; added customized overlay HVIF 
status icons to list view items thanks to NetworkStatus; code cleanup and 
reorganization

Modified: 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesAddOn.cpp
===================================================================
--- haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesAddOn.cpp  
2011-02-01 22:01:44 UTC (rev 40348)
+++ haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesAddOn.cpp  
2011-02-01 22:34:37 UTC (rev 40349)
@@ -23,7 +23,6 @@
 #include <Alert.h>
 
 
-
 NetworkSetupAddOn*
 get_nth_addon(image_id image, int index)
 {
@@ -79,7 +78,7 @@
        fListview->SetSelectionMessage(new BMessage(INTERFACE_SELECTED_MSG));
        fListview->SetInvocationMessage(new BMessage(CONFIGURE_INTERFACE_MSG));
        AddChild(new BScrollView(NULL, fListview, B_FOLLOW_ALL_SIDES, 
B_WILL_DRAW
-                       | B_FRAME_EVENTS, false, true));
+               | B_FRAME_EVENTS, false, true));
 
        r.top = r.bottom - 60;
        fConfigure = new BButton(r, "configure", "Configure" B_UTF8_ELLIPSIS,
@@ -93,8 +92,8 @@
        r.left += w + SMALL_MARGIN;
 
        fOnOff = new BButton(r, "onoff", "Disable",
-                               new BMessage(ONOFF_INTERFACE_MSG),
-                               B_FOLLOW_BOTTOM | B_FOLLOW_LEFT);
+               new BMessage(ONOFF_INTERFACE_MSG),
+               B_FOLLOW_BOTTOM | B_FOLLOW_LEFT);
        fOnOff->GetPreferredSize(&w, &h);
        fOnOff->ResizeToPreferred();
        fOnOff->Hide();

Modified: 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesIcons.rdef
===================================================================
--- haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesIcons.rdef 
2011-02-01 22:01:44 UTC (rev 40348)
+++ haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesIcons.rdef 
2011-02-01 22:34:37 UTC (rev 40349)
@@ -56,4 +56,18 @@
        $"22040A000200021001178622040A020201031001178222040A01020002100117"
        $"8222040A0001041001178400040A03010400"
 };
+resource(2, "online") #'VICN' array {
+       $"6E63696603050004016E020006023959B6382F60BA2F603B59B64B80504AE1D3"
+       $"0056FF22FF05D005010204554FC96F4FC6CC4F4F554FC6CC4FC96F555BC6CC5B"
+       $"C96F5B5B555BC96F5BC6CC020A0001001001178800040A02010000"
+};
 
+resource(3, "warning") #'VICN' array {
+       $"6E6369660205000200060238DB5A38CCBABACCBA3ADB5A4B8BE14AD7C900FFF3"
+       $"75FFFFA406010A034F4F555B5B4F020A0001001001178802040A01010000"
+};
+
+resource(4, "offline") #'VICN' array {
+       $"6E6369660205000200060238DB5A38CCBABACCBA3ADB5A4BBBE14B07C900FF88"
+       $"75FFFF0505010A044F4F4F5B5B5B5B4F020A0001001001178802040A01010000"
+};

Modified: 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.cpp
===================================================================
--- 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.cpp   
    2011-02-01 22:01:44 UTC (rev 40348)
+++ 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.cpp   
    2011-02-01 22:34:37 UTC (rev 40349)
@@ -52,6 +52,16 @@
 }
 
 
+/* Takes the provided ip-style netmask and converts
+ * it to an unsigned int, then counts the binary 1's
+ */
+int
+netmaskbitcount(const char* buf)
+{
+       return(__builtin_popcount(inet_addr(buf)));
+}
+
+
 // #pragma mark -
 
 
@@ -93,8 +103,9 @@
 void
 InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
 {
-       BListView* list = dynamic_cast<BListView*>(owner);
-       BString interfaceState;
+       BListView*      list = dynamic_cast<BListView*>(owner);
+       BBitmap*        stateIcon(NULL);
+       BString         interfaceState;
 
        if (!list)
                return;
@@ -118,10 +129,16 @@
 
        if (fSettings->IsDisabled()) {
                interfaceState << "disabled";
-       } else if ( !fSettings->IP() && fSettings->AutoConfigure()) {
+               stateIcon = fIconOffline;
+       } else if (!fInterface.HasLink()) {
+               interfaceState << "no link";
+               stateIcon = fIconOffline;
+       } else if (!fSettings->IP() && fSettings->AutoConfigure()) {
                interfaceState << "connecting" B_UTF8_ELLIPSIS;
-       } else { /*if (fSettings->IsConnected()) {*/
+               stateIcon = fIconPending;
+       } else {
                interfaceState << "connected";
+               stateIcon = fIconOnline;
        }
 
        // Set the initial bounds of item contents
@@ -148,6 +165,7 @@
                list->SetDrawingMode(B_OP_OVER);
 
        list->DrawBitmapAsync(fIcon, iconPt);
+       list->DrawBitmapAsync(stateIcon, iconPt);
 
        if (fSettings->IsDisabled())
                list->SetHighColor(tint_color(black, B_LIGHTEN_1_TINT));
@@ -163,6 +181,8 @@
        if (!fSettings->IsDisabled()) {
                BString v4str("IPv4: ");
                v4str << fSettings->IP();
+               v4str << " /";
+               v4str << netmaskbitcount(fSettings->Netmask());
                v4str << " (";
                if (fSettings->AutoConfigure())
                        v4str << "DHCP";
@@ -184,11 +204,8 @@
 {
        fSettings = new Settings(Name());
 
-       // Init icon
-       BBitmap* icon = NULL;
+       const char*     mediaTypeName = NULL;
 
-       const char* mediaTypeName = NULL;
-
        int media = fInterface.Media();
        printf("%s media = 0x%x\n", Name(), media);
        switch (IFM_TYPE(media)) {
@@ -208,40 +225,86 @@
                }
        }
 
+       _PopulateBitmaps(mediaTypeName);
+               // Load the interface icons
+}
+
+
+void
+InterfaceListItem::_PopulateBitmaps(const char* mediaType) {
+
+       const uint8*    HVIFInterfaceIcon;
+       const uint8*    HVIFOffline;
+       const uint8*    HVIFPending;
+       const uint8*    HVIFOnline;
+
+       BBitmap*                interfaceBitmap = NULL;
+
+       /* Load interface icons */
        image_info info;
        if (our_image(info) != B_OK)
                return;
 
-       BFile file(info.name, B_READ_ONLY);
-       if (file.InitCheck() < B_OK)
+       BFile resourcesFile(info.name, B_READ_ONLY);
+       if (resourcesFile.InitCheck() < B_OK)
                return;
 
-       BResources resources(&file);
-       if (resources.InitCheck() < B_OK)
+       BResources addonResources(&resourcesFile);
+
+       if (addonResources.InitCheck() < B_OK)
                return;
 
-       size_t size;
+       size_t iconSize;
+
        // Try specific interface icon?
-       const uint8* rawIcon = (const uint8*)resources.LoadResource(
-               B_VECTOR_ICON_TYPE, Name(), &size);
+       HVIFInterfaceIcon = (const uint8*)addonResources.LoadResource(
+               B_VECTOR_ICON_TYPE, Name(), &iconSize);
 
-       if (rawIcon == NULL && mediaTypeName != NULL)
+       if (HVIFInterfaceIcon == NULL && mediaType != NULL)
                // Not found, try interface media type?
-               rawIcon = (const uint8*)resources.LoadResource(
-                       B_VECTOR_ICON_TYPE, mediaTypeName, &size);
-       if (rawIcon == NULL)
+               HVIFInterfaceIcon = (const uint8*)addonResources.LoadResource(
+                       B_VECTOR_ICON_TYPE, mediaType, &iconSize);
+       if (HVIFInterfaceIcon == NULL)
                // Not found, try default interface icon?
-               rawIcon = (const uint8*)resources.LoadResource(
-                       B_VECTOR_ICON_TYPE, "ether", &size);
+               HVIFInterfaceIcon = (const uint8*)addonResources.LoadResource(
+                       B_VECTOR_ICON_TYPE, "ether", &iconSize);
 
-       if (rawIcon) {
+       if (HVIFInterfaceIcon) {
                // Now build the bitmap
-               icon = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
-               if (BIconUtils::GetVectorIcon(rawIcon, size, icon) == B_OK)
-                       fIcon = icon;
+               interfaceBitmap = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
+               if (BIconUtils::GetVectorIcon(HVIFInterfaceIcon,
+                       iconSize, interfaceBitmap) == B_OK)
+                       fIcon = interfaceBitmap;
                else
-                       delete icon;
+                       delete interfaceBitmap;
        }
+
+       // Load possible state icons
+       HVIFOffline = (const uint8*)addonResources.LoadResource(
+               B_VECTOR_ICON_TYPE, "offline", &iconSize);
+
+       if (HVIFOffline) {
+               fIconOffline = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
+               BIconUtils::GetVectorIcon(HVIFOffline, iconSize, fIconOffline);
+       }
+
+       HVIFPending = (const uint8*)addonResources.LoadResource(
+               B_VECTOR_ICON_TYPE, "pending", &iconSize);
+
+       if (HVIFPending) {
+               fIconOffline = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
+               BIconUtils::GetVectorIcon(HVIFPending, iconSize, fIconPending);
+       }
+
+       HVIFOnline = (const uint8*)addonResources.LoadResource(
+               B_VECTOR_ICON_TYPE, "online", &iconSize);
+
+       if (HVIFOnline) {
+               fIconOnline = new BBitmap(BRect(0, 0, 31, 31), 0, B_RGBA32);
+               BIconUtils::GetVectorIcon(HVIFOnline, iconSize, fIconOnline);
+       }
+
+
 }
 
 

Modified: 
haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.h
===================================================================
--- haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.h 
2011-02-01 22:01:44 UTC (rev 40348)
+++ haiku/trunk/src/tests/kits/net/preflet/InterfacesAddOn/InterfacesListView.h 
2011-02-01 22:34:37 UTC (rev 40349)
@@ -1,10 +1,11 @@
 /*
- * Copyright 2004-2009 Haiku Inc. All rights reserved.
+ * Copyright 2004-2011 Haiku Inc. All rights reserved.
  * Distributed under the terms of the MIT License.
  *
  * Authors:
  *             Philippe Houdoin
- *             Fredrik Modéen
+ *             Fredrik Modéen
+ *             Alexander von Gluck, kallisti5@xxxxxxxxxxx
  */
 
 
@@ -25,50 +26,64 @@
 
 #include "Settings.h"
 
+
+int netmaskbitcount(const char* buf);
+
+
 class InterfaceListItem : public BListItem {
 public:
-       InterfaceListItem(const char* name);
-       ~InterfaceListItem();
+                                                               
InterfaceListItem(const char* name);
+                                                               
~InterfaceListItem();
 
-       void DrawItem(BView* owner, BRect bounds, bool complete);
-       void Update(BView* owner, const BFont* font);
+                       void                            DrawItem(BView* owner,
+                                                                       BRect 
bounds, bool complete);
+                       void                            Update(BView* owner, 
const BFont* font);
 
-       inline const char*              Name()                  { return 
fInterface.Name(); }
-       inline bool                             IsDisabled()    { return 
fSettings->IsDisabled(); }
-       inline void                             SetDisabled(bool disable){ 
fSettings->SetDisabled(disable); }
-       inline Settings*                GetSettings()   { return fSettings; }
+       inline  const char*                     Name() {return 
fInterface.Name();}
+       inline  bool                            IsDisabled() {
+                                                                               
return fSettings->IsDisabled();}
+       inline  void                            SetDisabled(bool disable) {
+                                                                               
fSettings->SetDisabled(disable);}
+       inline  Settings*                       GetSettings() {return 
fSettings;}
 
 private:
-       void                                    _Init();
+                       void                            _Init();
+                       void                            _PopulateBitmaps(const 
char* mediaType);
 
-       BBitmap*                                fIcon;
-       BNetworkInterface               fInterface;
-       Settings*                               fSettings;
-       float                                   fFirstlineOffset;
-       float                                   fSecondlineOffset;
-       float                                   fThirdlineOffset;
-       float                                   fStateWidth;
+                       BBitmap*                        fIcon;
+                       BBitmap*                        fIconOffline;
+                       BBitmap*                        fIconPending;
+                       BBitmap*                        fIconOnline;
+
+                       BNetworkInterface       fInterface;
+                       Settings*                       fSettings;
+                       float                           fFirstlineOffset;
+                       float                           fSecondlineOffset;
+                       float                           fThirdlineOffset;
+                       float                           fStateWidth;
 };
 
 
 class InterfacesListView : public BListView {
 public:
-       InterfacesListView(BRect rect, const char* name,
-               uint32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP);
-       virtual ~InterfacesListView();
+                                                               
InterfacesListView(BRect rect, const char* name,
+                                                                       uint32 
resizingMode
+                                                                       = 
B_FOLLOW_LEFT | B_FOLLOW_TOP);
 
-       InterfaceListItem* FindItem(const char* name);
+       virtual                                         ~InterfacesListView();
 
+                                                               
InterfaceListItem* FindItem(const char* name);
+
 protected:
-       virtual void AttachedToWindow();
-       virtual void DetachedFromWindow();
+       virtual void                            AttachedToWindow();
+       virtual void                            DetachedFromWindow();
 
-       virtual void MessageReceived(BMessage* message);
+       virtual void                            MessageReceived(BMessage* 
message);
 
 private:
-       status_t        _InitList();
-       status_t        _UpdateList();
-       void            _HandleNetworkMessage(BMessage* message);
+                       status_t                        _InitList();
+                       status_t                        _UpdateList();
+                       void                            
_HandleNetworkMessage(BMessage* message);
 };
 
 #endif /*INTERFACES_LIST_VIEW_H*/


Other related posts: