Ticket #820: preflet.patch

File preflet.patch, 95.8 KB (added by Barrett, 15 years ago)

Patch for my work on the networks preferences

  • LocationSettings.h

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#ifndef NETWORKS_SETTINGS_H
     10#define NETWORKS_SETTINGS_H
     11
     12#include <ObjectList.h>
     13#include <String.h>
     14
     15#include "Settings.h"
     16
     17class BPath;
     18
     19class LocationSettings : public Settings {
     20public:
     21                LocationSettings(BPath settingsPath);
     22    virtual     ~LocationSettings();
     23
     24    status_t    LoadSettings();
     25    status_t    SaveSettings();
     26
     27    status_t    DeleteLocationDirectory(BPath dirPath);
     28   
     29    status_t    MoveNetworkFile(BPath currentPath, BPath destinationPath);
     30   
     31    status_t    AddNetwork(const char* name);
     32
     33    void        _FindEthernetDevices(BObjectList<BString> *devicesList);
     34    status_t    AutoConfigureAllSettings();
     35    bool        IfConfigured();
     36
     37private:
     38    BPath       fSettingsPath;
     39    bool        fConfigured;
     40
     41    BObjectList<BString>    fInterfaces;
     42};
     43#endif /* NETWORKS_SETTINGS_H */
  • TextEntryWindow.cpp

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo, Barrett666@gmail.com
     7 */
     8
     9#include <Application.h>
     10#include <Button.h>
     11#include <GridView.h>
     12#include <GroupLayout.h>
     13#include <GroupView.h>
     14#include <LayoutItem.h>
     15#include <Message.h>
     16#include <String.h>
     17#include <TextControl.h>
     18#include <View.h>
     19
     20#include <stdio.h>
     21
     22#include "TextEntryWindow.h"
     23
     24const uint32 kMsgSetNetworkName = 'nnam';
     25static const int kSemTimeOut = 50000;
     26
     27TextEntryWindow::TextEntryWindow()
     28    : BWindow(BRect(50, 50, 400, 300), "Warning", B_TITLED_WINDOW,
     29        B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS |
     30            B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
     31{
     32    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     33    SetLayout(rootLayout);
     34    BGroupView* firstGroup = new BGroupView(B_VERTICAL);
     35
     36    firstGroup->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     37   
     38    fNetworkNameControl = new BTextControl(
     39        "Select the name of the new network:", "", NULL);
     40
     41    fConfirmButton = new BButton("OK!", new BMessage(kMsgSetNetworkName));
     42
     43    fConfirmButton->SetTarget(this);
     44
     45    firstGroup->GroupLayout()->AddView(fNetworkNameControl);
     46
     47    firstGroup->GroupLayout()->AddView(fConfirmButton);
     48
     49    rootLayout->AddView(firstGroup);
     50}
     51
     52TextEntryWindow::~TextEntryWindow()
     53{
     54}
     55
     56
     57void
     58TextEntryWindow::Quit()
     59{
     60    BWindow::Quit();
     61}
     62
     63const char*
     64TextEntryWindow::Go() // created heavily-looking to BAlert::Go
     65{
     66    fTextSem = create_sem(0, "TextEntrySem");
     67    if (fTextSem < B_OK) {
     68        Quit();
     69        return NULL;
     70    }
     71   
     72    BWindow* window =
     73        dynamic_cast<BWindow*>(BLooper::LooperForThread(find_thread(NULL)));
     74   
     75    Show();
     76
     77    if (window) {
     78        status_t err;
     79        for (;;) {
     80            do {
     81                err = acquire_sem_etc(fTextSem, 1, B_RELATIVE_TIMEOUT,
     82                                      kSemTimeOut);
     83            } while (err == B_INTERRUPTED);
     84
     85            if (err == B_BAD_SEM_ID) {
     86                break;
     87            }
     88            window->UpdateIfNeeded();
     89        }
     90    } else {
     91        while (acquire_sem(fTextSem) == B_INTERRUPTED) {
     92        }
     93    }
     94
     95    BString string = fString;
     96   
     97    if (Lock())
     98        Quit();
     99
     100    return string.String();
     101}
     102
     103
     104bool
     105TextEntryWindow::QuitRequested()
     106{
     107    be_app->PostMessage(B_QUIT_REQUESTED);
     108    return true;
     109}
     110
     111
     112void
     113TextEntryWindow::MessageReceived(BMessage* message)
     114{
     115    switch (message->what) {
     116
     117        case kMsgSetNetworkName:
     118        {
     119            if (fTextSem < B_OK) {
     120                PostMessage(B_QUIT_REQUESTED);
     121            } else {
     122                fString = BString(fNetworkNameControl->Text());
     123                delete_sem(fTextSem);   
     124            }
     125        }
     126        break;
     127
     128    default:
     129        BWindow::MessageReceived(message);
     130    }
     131
     132}
  • EthernetSettingsView.h

     
    66 *      Andre Alves Garzia, andre@andregarzia.com
    77 *      Axel Dörfler
    88 *      Hugo Santos
     9 *      Dario Casalinuovo, barrett666@gmail.com
    910 */
    1011#ifndef ETHERNET_SETTINGS_VIEW_H
    1112#define ETHERNET_SETTINGS_VIEW_H
    1213
    1314
    14 #include "settings.h"
     15#include "EthernetSettings.h"
     16#include "NetworkTypeView.h"
    1517
    1618#include <ObjectList.h>
    1719#include <View.h>
    1820
    19 #include <posix/regex.h>
    20 
    2121class BButton;
    2222class BMenuField;
    2323class BPath;
    2424class BTextControl;
    25 class BStringView;
    2625
    2726
    28 class EthernetSettingsView : public BView {
    29 public:
    30                                 EthernetSettingsView();
    31         virtual                 ~EthernetSettingsView();
     27class EthernetSettingsView : public NetworkTypeView {
     28  public:
     29                        EthernetSettingsView(BPath path, const char* adapterName);
     30        virtual         ~EthernetSettingsView();
    3231       
    33         virtual void            MessageReceived(BMessage* message);
    34         virtual void            AttachedToWindow();
    35         virtual void            DetachedFromWindow();
     32        virtual void    MessageReceived(BMessage* message);
     33        virtual void    AttachedToWindow();
     34        virtual void    DetachedFromWindow();
    3635       
    37                 void            SaveProfile(BString profileName);
    38                 void            LoadProfile(BString profileName);
     36        void            SaveProfile(BString profileName);
     37        void            LoadProfile(BString profileName);
     38        void            DeleteNetworkAndSettings();
    3939
    4040private:
    41                 void            _GatherInterfaces();
    42                 bool            _PrepareRequest(struct ifreq& request,
    43                                     const char* name);
    44                 void            _ShowConfiguration(Settings* settings);
    45                 void            _EnableTextControls(bool enable);
    46                 void            _SaveConfiguration();
    47                 void            _SaveDNSConfiguration();
    48                 void            _SaveAdaptersConfiguration();
    49                 void            _ApplyControlsToConfiguration();
    50                 status_t        _GetPath(const char* name, BPath& path);
    51                 status_t        _TriggerAutoConfig(const char* device);
    52 
    53                 bool            _ValidateControl(BTextControl* control);
    54 private:
     41        void            _ShowConfiguration();
     42        void            _EnableTextControls(bool enable);
     43        void            _SaveConfiguration();
     44        void            _ApplyControlsToConfiguration();
     45        status_t        _TriggerAutoConfig(const char* device);
    5546       
    56                 BButton*        fApplyButton;
    57                 BButton*        fRevertButton;
     47        BButton*        fApplyButton;
     48        BButton*        fRevertButton;
    5849                    // TODO: buttons should be moved to window instead
    5950
    60                 BMenuField*     fDeviceMenuField;
    61                 BMenuField*     fTypeMenuField;
    62                 BTextControl*   fIPTextControl;
    63                 BTextControl*   fNetMaskTextControl;
    64                 BTextControl*   fGatewayTextControl;
     51        BMenuField*     fTypeMenuField;
     52        BTextControl*   fIPTextControl;
     53        BTextControl*   fNetMaskTextControl;
     54        BTextControl*   fGatewayTextControl;
    6555
    66                 BTextControl*   fPrimaryDNSTextControl;
    67                 BTextControl*   fSecondaryDNSTextControl;
     56        BTextControl*   fPrimaryDNSTextControl;
     57        BTextControl*   fSecondaryDNSTextControl;
    6858
    69                 BStringView*    fErrorMessage;
     59        EthernetSettings*       fCurrentSettings;
    7060
    71                     // TODO: DNS settings do not belong here, do they?
    72                 BObjectList<BString> fInterfaces;
    73                     // TODO: the view should not know about the interfaces,
    74                     // it should only display the given interface, move
    75                     // one level up.
    76                 BObjectList<Settings> fSettings;
    77                     // TODO: the view should not know about a list
    78                     // of settings, instead it should be configured
    79                     // to a specific setting from the code one level up
    80                 Settings*       fCurrentSettings;
    81 
    82                 int32           fStatus;
    83                 int             fSocket;
     61        int32           fStatus;
     62        int             fSocket;
    8463};
    8564
    8665#endif /* ETHERNET_SETTINGS_VIEW_H */
  • settings.cpp

     
    1 /*
    2  * Copyright 2004-2007 Haiku Inc. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  *
    5  * Author:
    6  *      Andre Alves Garzia, andre@andregarzia.com
    7  */
    8  
    9 #include "settings.h"
    10 
    11 #include <String.h>
    12 #include <File.h>
    13 #include <Path.h>
    14  
    15 #include <SupportDefs.h>
    16 #include <AutoDeleter.h>
    17 
    18 #include <arpa/inet.h>
    19 #include <net/if.h>
    20 #include <net/if_dl.h>
    21 #include <net/if_media.h>
    22 #include <net/if_types.h>
    23 #include <netinet/in.h>
    24 #include <sys/socket.h>
    25 #include <sys/sockio.h>
    26 
    27 #include <errno.h>
    28 #include <stdio.h>
    29 #include <stdlib.h>
    30 #include <string.h>
    31 #include <unistd.h>
    32 
    33 Settings::Settings(const char *name)
    34     :
    35     fAuto(true)
    36 {
    37     fSocket = socket(AF_INET, SOCK_DGRAM, 0);
    38     fName = name;
    39     ReadConfiguration();   
    40 }
    41 
    42 Settings::~Settings()
    43 {
    44     close(fSocket);
    45 }
    46 
    47 bool
    48 Settings::_PrepareRequest(struct ifreq& request)
    49 {
    50     //This function is used for talking direct to the stack.
    51     //It´s used by _ShowConfiguration.
    52 
    53     const char* name = fName.String();
    54 
    55     if (strlen(name) > IF_NAMESIZE)
    56         return false;
    57 
    58     strcpy(request.ifr_name, name);
    59     return true;
    60 }
    61 
    62 
    63 void
    64 Settings::ReadConfiguration()
    65 {
    66     ifreq request;
    67     if (!_PrepareRequest(request))
    68         return;
    69 
    70     BString text = "dummy";
    71     char address[32];
    72     sockaddr_in* inetAddress = NULL;
    73 
    74     // Obtain IP.   
    75     if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
    76         return;
    77 
    78     inetAddress = (sockaddr_in*)&request.ifr_addr;
    79     if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
    80             sizeof(address)) == NULL) {
    81         return;
    82     }
    83    
    84     fIP = address;
    85 
    86     // Obtain netmask.
    87     if (ioctl(fSocket, SIOCGIFNETMASK, &request,
    88             sizeof(request)) < 0) {
    89         return;
    90     }
    91 
    92     inetAddress = (sockaddr_in*)&request.ifr_mask;
    93     if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
    94             sizeof(address)) == NULL) {
    95         return;
    96     }
    97 
    98     fNetmask = address;
    99 
    100     // Obtain gateway
    101     ifconf config;
    102     config.ifc_len = sizeof(config.ifc_value);
    103     if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
    104         return;
    105 
    106     uint32 size = (uint32)config.ifc_value;
    107     if (size == 0)
    108         return;
    109 
    110     void *buffer = malloc(size);
    111     if (buffer == NULL)
    112         return;
    113 
    114     MemoryDeleter bufferDeleter(buffer);
    115     config.ifc_len = size;
    116     config.ifc_buf = buffer;
    117 
    118     if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
    119         return;
    120 
    121     ifreq *interface = (ifreq *)buffer;
    122     ifreq *end = (ifreq *)((uint8 *)buffer + size);
    123 
    124 
    125     while (interface < end) {
    126         route_entry& route = interface->ifr_route;
    127 
    128         if (route.flags & RTF_GATEWAY) {
    129             inetAddress = (sockaddr_in*)route.gateway;
    130             fGateway = inet_ntoa(inetAddress->sin_addr);
    131         }
    132 
    133         int32 addressSize = 0;
    134         if (route.destination != NULL)
    135             addressSize += route.destination->sa_len;
    136         if (route.mask != NULL)
    137             addressSize += route.mask->sa_len;
    138         if (route.gateway != NULL)
    139             addressSize += route.gateway->sa_len;
    140 
    141         interface = (ifreq *)((addr_t)interface +
    142             IF_NAMESIZE + sizeof(route_entry) + addressSize);
    143     }
    144 
    145     uint32 flags = 0;
    146     if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
    147         flags = request.ifr_flags;
    148 
    149     fAuto = flags & IFF_AUTO_CONFIGURED;
    150 
    151     // read resolv.conf for the dns.
    152     fNameservers.MakeEmpty();
    153 
    154 #define MATCH(line, name) \
    155     (!strncmp(line, name, sizeof(name) - 1) && \
    156     (line[sizeof(name) - 1] == ' ' || \
    157      line[sizeof(name) - 1] == '\t'))
    158 
    159 
    160     register FILE *fp = fopen("/etc/resolv.conf", "r");
    161     if (fp == NULL) {
    162         fprintf(stderr, "failed to open '/etc/resolv.conf' to "
    163             "read nameservers: %s\n", strerror(errno));
    164         return;
    165     }
    166 
    167     int nserv = 0;
    168     char buf[1024];
    169     register char *cp; //, **pp;
    170 //  register int n;
    171     int MAXNS = 2;
    172 
    173     // read the config file
    174     while (fgets(buf, sizeof(buf), fp) != NULL) {
    175         // skip comments
    176         if (*buf == ';' || *buf == '#')
    177             continue;
    178 
    179         // read nameservers to query
    180         if (MATCH(buf, "nameserver") && nserv < MAXNS) {
    181 //          char sbuf[2];
    182             cp = buf + sizeof("nameserver") - 1;
    183             while (*cp == ' ' || *cp == '\t')
    184                 cp++;
    185             cp[strcspn(cp, ";# \t\n")] = '\0';
    186             if ((*cp != '\0') && (*cp != '\n')) {
    187                 fNameservers.AddItem(new BString(cp));
    188                 nserv++;
    189             }
    190         }
    191         continue;
    192     }
    193 
    194     fclose(fp);
    195 }
  • LocationNameWindow.cpp

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo, Barrett666@gmail.com
     7 */
     8
     9#include <Application.h>
     10#include <Button.h>
     11#include <GridView.h>
     12#include <GroupLayout.h>
     13#include <GroupView.h>
     14#include <LayoutItem.h>
     15#include <Message.h>
     16#include <TextControl.h>
     17#include <View.h>
     18
     19#include <stdio.h>
     20
     21#include "LocationNameWindow.h"
     22#include "NetworkView.h"
     23
     24const uint32 kMsgSetLocationName = 'stln';
     25
     26LocationNameWindow::LocationNameWindow(NetworkView* network)
     27    : BWindow(BRect(50, 50, 400, 300), "Location Name", B_TITLED_WINDOW,
     28        B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
     29        | B_AUTO_UPDATE_SIZE_LIMITS)
     30{
     31    fNetworkView = network;
     32
     33    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     34    SetLayout(rootLayout);
     35   
     36    BGroupView* firstGroup = new BGroupView(B_VERTICAL);
     37   
     38    firstGroup->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     39   
     40    fLocationNameControl = new BTextControl(
     41        "Select the name of the location:", "", NULL);
     42   
     43    fConfirmButton = new BButton("OK!", new BMessage(kMsgSetLocationName));
     44    fConfirmButton->SetTarget(this);
     45   
     46    firstGroup->GroupLayout()->AddView(fLocationNameControl);
     47    firstGroup->GroupLayout()->AddView(fConfirmButton);
     48
     49    rootLayout->AddView(firstGroup);
     50}
     51
     52
     53LocationNameWindow::~LocationNameWindow()
     54{
     55}
     56
     57
     58void
     59LocationNameWindow::MessageReceived(BMessage* message)
     60{
     61    switch (message->what) {
     62       
     63        case kMsgSetLocationName:
     64        {
     65            printf("selected\n");
     66            const char* locationName = fLocationNameControl->Text();
     67
     68            BMessage* message = new BMessage(kMsgSetLocationName);
     69            message->AddString("location_name", locationName);
     70
     71            status_t ret;
     72            BMessenger messenger = BMessenger(fNetworkView,
     73                fNetworkView->Window(), &ret);
     74
     75            //if(ret < B_OK)
     76            //  printf("error: %s",strerror(ret));
     77
     78            messenger.SendMessage(message, message, B_INFINITE_TIMEOUT,
     79                B_INFINITE_TIMEOUT);
     80       
     81            Quit();
     82        }
     83        break;
     84
     85    default:
     86        BWindow::MessageReceived(message);
     87    }
     88
     89}
  • EthernetSettings.h

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#ifndef ETHERNET_SETTINGS_H
     10#define ETHERNET_SETTINGS_H
     11
     12#include "Settings.h"
     13
     14#include <ObjectList.h>
     15#include <String.h>
     16
     17class EthernetSettings : public Settings {
     18public:
     19                    EthernetSettings(BPath settingsPath, const char* name);
     20        virtual     ~EthernetSettings();
     21
     22        status_t    LoadSettings();
     23        status_t    ApplySettings();
     24        status_t    SaveSettings();
     25
     26        void        SetName(BString name);
     27        void        SetIP(const char* ip) {fIP.SetTo(ip); }
     28        void        SetNetmask(const char* ip) {fSubnetMask.SetTo(ip); }
     29        void        SetGateway(const char* ip) {fGateway.SetTo(ip); }
     30
     31        void        SetAutoConfigure(bool t) {fAuto = t; }
     32
     33        bool        GetAutoConfigure() { return fAutoIPv4; }
     34
     35        status_t    AutoConfigureAllSettings();
     36        bool        IfConfigured();
     37
     38        const char* GetName()  {return fName.String(); }
     39        const char* GetIP()  {return fIP.String(); }       
     40        const char* GetNetmask()  {return fSubnetMask.String(); }
     41        const char* GetGateway()  {return fGateway.String(); }
     42       
     43        void        ReadConfiguration();
     44       
     45        BObjectList<BString> fNameservers;
     46
     47private:
     48        bool        _PrepareRequest(struct ifreq& request);
     49        void        _SaveAdapterConfiguration();
     50        void        _SaveDNSConfiguration();
     51        status_t    _GetPath(const char* name, BPath& path);
     52       
     53        int32       fAutoIPv4;
     54
     55        int         fSocket;
     56
     57        bool        fConfigured;
     58        bool        fAuto;
     59
     60        BString     fIP;
     61        BString     fSubnetMask;
     62        BString     fGateway;
     63
     64        BString     fName;
     65
     66        BString     fIPv6;
     67        BString     fPrefix;
     68};
     69#endif /* ETHERNET_SETTINGS_H */
  • Settings.h

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8#ifndef PREFLET_SETTINGS_H
     9#define PREFLET_SETTINGS_H
     10
     11#include <File.h>
     12#include <Message.h>
     13#include <Path.h>
     14#include <String.h>
     15
     16class Settings : public BMessage {
     17    public:
     18                        Settings(BPath path);
     19        virtual         ~Settings();
     20
     21        status_t        OpenSettings();
     22
     23        status_t        ReadSetting(const char* name, BString* string);
     24        status_t        ReadSetting(const char* name, int32* setting);
     25        status_t        ReadSetting(const char* name, bool* setting);
     26
     27        status_t        WriteSetting(const char* name, const char* string);
     28        status_t        WriteSetting(const char* name, int32 setting);
     29        status_t        WriteSetting(const char* name, bool setting);
     30       
     31        status_t        ReplaceSetting(const char* name, const char* string);
     32        status_t        ReplaceSetting(const char* name, int32 setting);
     33        status_t        ReplaceSetting(const char* name, bool setting);
     34
     35        status_t        RemoveSetting(const char* name);
     36
     37        status_t        FlattenSettings();
     38       
     39        status_t        DeleteSettings();
     40    protected:
     41        BFile*          fSettingsFile;
     42        BPath           fSettingsPath;
     43}; /* SETTINGS_H */
     44
     45#endif
  • LocationView.h

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *     
     8 *     
     9 */
     10#ifndef LOCATION_VIEW_H
     11#define LOCATION_VIEW_H
     12
     13class BButton;
     14class BMenuField;
     15class BPath;
     16class BPopUpMenu;
     17class BString;
     18class BView;
     19
     20class LocationSettings;
     21class NetworkTypeView;
     22class NetworksMenuView;
     23
     24class LocationView : public BView {
     25public:
     26                            LocationView(const char* locationName,
     27                                BObjectList<LocationView> &list);
     28
     29                            LocationView(); // just for the global location
     30        virtual             ~LocationView();
     31
     32        void                MessageReceived(BMessage* message);
     33
     34        status_t            MoveNetworkHere(NetworkTypeView* network);
     35
     36        void                SetName(const char* name);
     37        const char*         GetName();
     38       
     39        const char*         GetPath();
     40
     41        void                SetGlobal(bool ifGlobal);
     42        bool                IfGlobal();
     43
     44        status_t            DeleteLocationAndNetworks();
     45        void                LocationRemoved(const char* locationName);
     46        void                LocationAdded(const char* locationName,
     47                            LocationView* newLocation);
     48
     49        virtual void        AttachedToWindow();
     50        virtual void        DetachedFromWindow();
     51
     52 private:
     53        void                _FindAndAddEthernetInterfaces();
     54        void                _PopulateMoveToMenu(BObjectList<LocationView> &list);
     55
     56        BString             fName;
     57        bool                fIfGlobal;
     58
     59        BMenuField*         fPreferredISP;
     60        BMenu*              fIspMenu;
     61
     62        BPopUpMenu*         fNetworkMenu;
     63        BPopUpMenu*         fMoveToMenu;
     64
     65        BButton*            fRemoveButton;
     66        BPath               fLocationPath;
     67        BPath               fSettingsPath;
     68
     69        NetworksMenuView*   fNetworksMenuView;
     70
     71        BMenuField*         fNetworkMenuField;
     72        BMenuField*         fMoveToMenuField;
     73
     74        LocationSettings*   fLocationSettings;
     75};
     76
     77#endif
  • PrefletDefs.h

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *     
     8 *     
     9 */
     10
     11#define LOCATIONS_BASE_PATH "/boot/common/settings/network/preflet/"
     12#define PREFLET_CONF_DIR "preflet"
     13#define NETWORK_BASE_PATH "/boot/common/settings/network/"
  • PPPoESettings.h

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#ifndef PPPoE_SETTINGS_H
     10#define PPPoE_SETTINGS_H
     11
     12#include "Settings.h"
     13
     14#include <ObjectList.h>
     15#include <String.h>
     16
     17class PPPoESettings : public Settings {
     18public:
     19                    PPPoESettings(BPath settingsPath);
     20        virtual     ~PPPoESettings();
     21
     22        status_t    LoadSettings();
     23        status_t    ApplySettings();
     24        status_t    SaveSettings();
     25       
     26        void        ChangeSettingsFilePath(BPath path);
     27
     28private:
     29
     30};
     31#endif /* PPPoE_SETTINGS_H */
  • NetworkSettings.cpp

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8#include "NetworkSettings.h"
     9
     10#include <AutoDeleter.h>
     11#include <Directory.h>
     12
     13#include <stdio.h>
     14
     15#include "PrefletDefs.h"
     16
     17#define PFLT_CONFIGURED "configured"
     18#define PFLT_COMPUTER_NAME "computer_name"
     19
     20NetworkSettings::NetworkSettings(BPath settingsPath)
     21    : Settings(settingsPath)
     22{
     23
     24}
     25
     26
     27NetworkSettings :: ~NetworkSettings()
     28{
     29
     30}
     31
     32
     33status_t
     34NetworkSettings::LoadSettings()
     35{
     36    printf("NetworkSettings LoadSettings\n");
     37    status_t ret;
     38/*
     39    ret = ReadSetting(PFLT_COMPUTER_NAME, &fComputerName);
     40    if(ret < B_OK)
     41        return ret;
     42*/
     43    fComputerName = BString(_ReadComputerName());
     44
     45    ret = ReadSetting(PFLT_CONFIGURED, &fConfigured);
     46    if(ret < B_OK)
     47        return ret;
     48   
     49    return B_OK;
     50}
     51
     52
     53const char*
     54NetworkSettings::_ReadComputerName()
     55{
     56    BFile *hostname;
     57    BString name = BString();
     58    char buf[1024];
     59    ssize_t ret;
     60
     61    hostname = new BFile("/boot/common/settings/network/hostname",
     62        B_CREATE_FILE | B_READ_ONLY);
     63   
     64    while(true) {
     65        ret = hostname->Read((void*) buf, 1024);
     66        printf("%d\n", ret);
     67        if(ret < 0) {
     68            printf("Error reading hostname\n");
     69            return NULL;
     70        } else if (ret == 0) {
     71            break;
     72        } else {
     73        buf[ret] = '\0';
     74        printf("%s\n",buf);
     75        name.Append(buf);
     76        }
     77    }
     78    printf("%s\n",name.String());
     79    return name.String();
     80}
     81
     82
     83void
     84NetworkSettings::_SaveComputerName()
     85{
     86    BFile file("/boot/common/settings/network/hostname",
     87        B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
     88   
     89    file.Write(fComputerName.String(), fComputerName.Length());
     90}
     91
     92
     93status_t
     94NetworkSettings::SaveSettings()
     95{
     96    printf("NetworkSettings: savesettings\n");
     97    status_t ret;
     98
     99    if(fConfigured == true) {
     100/*
     101        ret = ReplaceSetting(PFLT_COMPUTER_NAME, fComputerName.String());
     102        if(ret < B_OK)
     103            return ret;
     104        printf("Name: %s\n", fComputerName.String());
     105*/
     106        _SaveComputerName();
     107
     108        ret = ReplaceSetting(PFLT_CONFIGURED, true);
     109        if(ret < B_OK)
     110            return ret;
     111    } else {
     112/*
     113        ret = WriteSetting(PFLT_COMPUTER_NAME, fComputerName.String());
     114        if(ret < B_OK)
     115            return ret;
     116*/
     117        _SaveComputerName();
     118
     119        ret = WriteSetting(PFLT_CONFIGURED, true);
     120        if(ret < B_OK)
     121            return ret;
     122    }
     123    return FlattenSettings();
     124}
     125
     126status_t
     127NetworkSettings::MakeBaseDir()
     128{
     129    BDirectory directory = BDirectory(NETWORK_BASE_PATH);
     130    BDirectory parentDirectory = BDirectory();
     131
     132    status_t ret = directory.CreateDirectory(PREFLET_CONF_DIR, &parentDirectory);
     133    if(ret < B_OK)
     134        return ret;
     135
     136    return B_OK;
     137}
     138
     139status_t
     140NetworkSettings::AutoConfigureAllSettings()
     141{
     142    SetComputerName("HaikuBox");
     143
     144    return B_OK;
     145}
     146
     147
     148bool
     149NetworkSettings::IfConfigured()
     150{
     151    if(ReadSetting(PFLT_CONFIGURED, &fConfigured) < B_OK)
     152        return false;
     153
     154    if(fConfigured == true) {
     155        return true;
     156    } else if(fConfigured == false) {
     157        return false;
     158    }
     159    return false;
     160}
  • NetworkWindow.cpp

     
    44 *
    55 * Author:
    66 *      Andre Alves Garzia, andre@andregarzia.com
     7 *      Dario "Barrett" Casalinuovo, Barrett666@gmail.com
    78 */
    89
    9 #include "NetworkWindow.h"
    10 
    1110#include <Application.h>
     11#include <File.h>
    1212#include <GroupLayout.h>
    1313
    14 #include "EthernetSettingsView.h"
     14#include <stdio.h>
    1515
     16#include "NetworkWindow.h"
     17#include "NetworkView.h"
    1618
    1719NetworkWindow::NetworkWindow()
    18     : BWindow(BRect(50, 50, 269, 302), "Network", B_TITLED_WINDOW,
    19         B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
     20    : BWindow(BRect(50, 50, 400, 500), "Network", B_TITLED_WINDOW,
     21        B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
    2022        | B_AUTO_UPDATE_SIZE_LIMITS)
    2123{
    2224    SetLayout(new BGroupLayout(B_HORIZONTAL));
    23     fEthernetView = new EthernetSettingsView();
    24     GetLayout()->AddView(fEthernetView);
     25    fNetworkView = new NetworkView();
     26    GetLayout()->AddView(fNetworkView);
    2527}
    2628
    2729
  • PPPoEView.h

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *     
     8 *     
     9 */
     10
     11#ifndef PPPOE_VIEW_H
     12#define PPPOE_VIEW_H
     13
     14
     15#include "NetworkTypeView.h"
     16#include "PPPoESettings.h"
     17
     18class BPath;
     19class BStringView;
     20
     21class PPPoEView : public NetworkTypeView {
     22public:
     23                            PPPoEView(BPath path, BString networkName);
     24        virtual             ~PPPoEView();
     25
     26        virtual void        MessageReceived(BMessage* message);
     27        virtual void        AttachedToWindow();
     28        virtual void        DetachedFromWindow();
     29
     30        void                DeleteNetworkAndSettings();
     31        void                ChangeSettingsFilePath(BPath path);
     32
     33private:
     34        PPPoESettings*      fSettings;
     35        BString             fNetworkName;
     36        BPath               fSettingsPath;
     37        BStringView*        fStringView;
     38
     39};
     40
     41#endif
  • NetworkView.cpp

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com     
     7 *     
     8 */
     9#include <Alert.h>
     10#include <Box.h>
     11#include <Button.h>
     12#include <CheckBox.h>
     13#include <Deskbar.h>
     14#include <Directory.h>
     15#include <Entry.h>
     16#include <File.h>
     17#include <GridView.h>
     18#include <GroupView.h>
     19#include <LayoutItem.h>
     20#include <Menu.h>
     21#include <MenuField.h>
     22#include <MenuItem.h>
     23#include <ObjectList.h>
     24#include <Path.h>
     25#include <PopUpMenu.h>
     26#include <SpaceLayoutItem.h>
     27#include <SupportDefs.h>
     28#include <Roster.h>
     29#include <String.h>
     30#include <TextControl.h>
     31#include <View.h>
     32
     33#include <stdio.h>
     34
     35#include "LocationView.h"
     36#include "LocationNameWindow.h"
     37#include "NetworkView.h"
     38#include "NetworkSettings.h"
     39#include "PrefletDefs.h"
     40
     41const uint32 kMsgSelectedLocation = 'sllc';
     42const uint32 kMsgAddNewLocation = 'anlc';
     43const uint32 kMsgRemoveLocation = 'rmlc';
     44
     45const uint32 kMsgInstallInDeskbar = 'iidb';
     46const uint32 kMsgRevertAll = 'rvrt';
     47const uint32 kMsgSetName = 'stnm';
     48
     49const uint32 kMsgSetLocationName = 'stln';
     50
     51NetworkView::NetworkView()
     52    : BView("NetworkView", 0, NULL)
     53{
     54    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     55
     56    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     57
     58    SetLayout(rootLayout);
     59
     60    BGridView* informationsGroup = new BGridView();
     61
     62    BGridLayout* layout = informationsGroup->GridLayout();
     63
     64    // insets   
     65
     66    float inset = ceilf(be_plain_font->Size() * 0.7);
     67    rootLayout->SetInsets(inset, inset, inset, inset);
     68    rootLayout->SetSpacing(inset);
     69    layout->SetSpacing(inset, inset);
     70
     71    /* making the gui...*/
     72
     73    fLocationsGroup = new BGroupView(B_VERTICAL);
     74   
     75    fNetworkSettings->MakeBaseDir();
     76
     77    _BuildLocationsMenu();
     78
     79    fLocationsMenuField = new BMenuField("Location :", fLocationsMenu);
     80
     81    layout->AddItem(fLocationsMenuField->CreateLabelLayoutItem(), 0, 0);
     82    layout->AddItem(fLocationsMenuField->CreateMenuBarLayoutItem(), 1, 0);
     83
     84    fComputerNameTextControl = new BTextControl("Computer Name :", "",
     85        new BMessage(kMsgSetName));
     86
     87    layout->AddItem(fComputerNameTextControl->CreateLabelLayoutItem(), 0, 1.5);
     88    layout->AddItem(fComputerNameTextControl->CreateTextViewLayoutItem(), 1, 1.5); 
     89
     90    // ---
     91    BGroupView* endofguiGroup = new BGroupView(B_VERTICAL);
     92
     93    fShowNetworkInDeskbar = new BCheckBox("Show Networks icon in Deskbar.",
     94        new BMessage(kMsgInstallInDeskbar));
     95
     96    fShowNetworkInDeskbar->SetValue(_IfInstalled());
     97
     98    endofguiGroup->GroupLayout()->AddView(fShowNetworkInDeskbar);
     99
     100    fRevertButton = new BButton("Revert All", new BMessage(kMsgRevertAll));   
     101
     102    endofguiGroup->GroupLayout()->AddView(fRevertButton);
     103
     104    // attaching all to the window
     105
     106    rootLayout->AddView(informationsGroup);
     107    rootLayout->AddView(fLocationsGroup);
     108    rootLayout->AddView(endofguiGroup);
     109
     110    status_t ret = _LoadConfiguration();
     111
     112    if(ret < B_OK)
     113        printf("LoadConfiguration Error : %s", strerror(ret));
     114}
     115
     116
     117NetworkView::~NetworkView()
     118{
     119    if(_SaveConfiguration() < B_OK)
     120        printf("_SaveConfiguration() failed");
     121}
     122
     123
     124void
     125NetworkView::AttachedToWindow()
     126{
     127    fLocationsMenuField->Menu()->SetTargetForItems(this);
     128    fShowNetworkInDeskbar->SetTarget(this);
     129    fRevertButton->SetTarget(this);
     130    fComputerNameTextControl->SetTarget(this);
     131}
     132
     133
     134void
     135NetworkView::DetachedFromWindow()
     136{
     137
     138}
     139
     140
     141void
     142NetworkView::MessageReceived(BMessage* message)
     143{
     144    switch(message->what) {
     145   
     146        case kMsgInstallInDeskbar:
     147        {   
     148            if(fShowNetworkInDeskbar->Value() == B_CONTROL_ON){
     149                _InstallInDeskbar();
     150            } else if(fShowNetworkInDeskbar->Value() == B_CONTROL_OFF) {
     151                _RemoveFromDeskbar();   
     152            }
     153        }   
     154        break;
     155
     156        case kMsgSetName:
     157        {
     158            fNetworkSettings->SetComputerName(fComputerNameTextControl->Text());
     159        }
     160        break;
     161       
     162        case kMsgSelectedLocation:
     163        {   
     164            fCurrentLocationMenu = fLocationsMenu->FindMarked();
     165
     166            _HideAllLocations();
     167
     168            status_t ret = message->FindPointer("location_pointer",
     169                (void**)&fCurrentLocation);
     170
     171            if(ret < B_OK)
     172                printf("Error : %s", strerror(ret));
     173
     174            fLocationsGroup->AddChild(fCurrentLocation);
     175        }
     176        break;
     177       
     178        case kMsgAddNewLocation:
     179        {
     180            LocationNameWindow* getName = new LocationNameWindow(this);
     181            getName->Show();
     182        }
     183        break;
     184
     185        case kMsgSetLocationName:
     186        {
     187            const char* locationName;
     188            message->FindString("location_name", &locationName);
     189           
     190            status_t ret = _AddNewLocation(locationName);
     191           
     192            if(ret < B_OK)
     193                printf("Error : %s", strerror(ret));
     194        }
     195        break;
     196
     197        case kMsgRemoveLocation:
     198        {
     199            status_t ret = _RemoveLocation();
     200            if(ret < B_OK)
     201                printf("Error : %s", strerror(ret));
     202        }
     203        break;
     204       
     205        default:
     206        BView::MessageReceived(message);
     207        }
     208}
     209
     210
     211void
     212NetworkView::_HideAllLocations()
     213{
     214    int32 childrens = fLocationsGroup->CountChildren();
     215    int i;
     216
     217    for(i = 0; i < childrens; i++) {   
     218        BView* currView = fLocationsGroup->ChildAt(i);
     219
     220        if(currView != NULL) {
     221            fLocationsGroup->RemoveChild(currView);
     222        } else {
     223            break;
     224        }
     225    }
     226}
     227
     228
     229
     230status_t
     231NetworkView::_SaveConfiguration()
     232{
     233    fNetworkSettings->SetComputerName(fComputerNameTextControl->Text());
     234    status_t ret = fNetworkSettings->SaveSettings();
     235    if(ret < B_OK)
     236        printf("NetworkView save error %s\n",strerror(ret));
     237
     238    return B_OK;
     239}
     240
     241
     242status_t
     243NetworkView::_LoadConfiguration()
     244{
     245    status_t ret;
     246    BPath path = BPath(LOCATIONS_BASE_PATH);
     247    path.Append("settings");
     248    fNetworkSettings = new NetworkSettings(path);
     249
     250    ret = fNetworkSettings->OpenSettings();
     251    if(ret < B_OK)
     252        return ret;
     253
     254    if(fNetworkSettings->IfConfigured() == false) {
     255        printf("NetworkView: IfConfigured == false\n");
     256        fNetworkSettings->AutoConfigureAllSettings();
     257    } else {
     258        ret = fNetworkSettings->LoadSettings();
     259        if(ret < B_OK) {
     260            printf("LoadSettings() Error : %s\n",strerror(ret));
     261            return ret;
     262        }
     263    }
     264
     265    fComputerNameTextControl->SetText(fNetworkSettings->GetComputerName());
     266
     267    ret = _FindAndAddLocations();
     268    if(ret < B_OK) {
     269        printf("%s", strerror(ret));
     270        return ret;
     271    }
     272
     273    return B_OK;
     274}
     275
     276
     277void
     278NetworkView::_BuildLocationsMenu()
     279{
     280    fLocationsMenu = new BPopUpMenu("Locations menu");
     281
     282    _BuildGlobalLocation(); //build the menu and the view
     283
     284    fLocationsMenu->AddItem(new BSeparatorItem);
     285
     286    BMenuItem* item = new BMenuItem("Add New Location",
     287        new BMessage(kMsgAddNewLocation));
     288
     289    fLocationsMenu->AddItem(item);
     290
     291    item = new BMenuItem("Remove Current Location",
     292        new BMessage(kMsgRemoveLocation));
     293
     294    fLocationsMenu->AddItem(item);
     295
     296    fLocationsMenu->AddItem(new BSeparatorItem);
     297}
     298
     299
     300void
     301NetworkView::_BuildGlobalLocation()
     302{
     303    BPath path(LOCATIONS_BASE_PATH);
     304
     305    BDirectory directory = BDirectory(path.Path());
     306    BDirectory lcDirectory = BDirectory();
     307
     308    directory.CreateDirectory("global", &lcDirectory);
     309    // the following constructor is used only for the global location
     310    fGlobalLocationView = new LocationView();
     311   
     312    fGlobalLocationView->SetName("Global");
     313   
     314    fLocationsList.AddItem(fGlobalLocationView);
     315
     316    BMessage* message = new BMessage(kMsgSelectedLocation);
     317
     318    message->AddPointer("location_pointer",
     319        (const void*) fGlobalLocationView);
     320
     321    fGlobalMenuItem = new BMenuItem("Global", message);
     322
     323    fGlobalMenuItem->SetMarked(true);
     324
     325    fLocationsMenu->AddItem(fGlobalMenuItem);
     326
     327    fLocationsGroup->AddChild(fGlobalLocationView);
     328}
     329
     330       
     331status_t
     332NetworkView::_AddNewLocation(const char* locationName)
     333{
     334    BMessage* message = new BMessage(kMsgSelectedLocation);
     335   
     336    BString locationPath(LOCATIONS_BASE_PATH);
     337   
     338    BDirectory directory = BDirectory(locationPath.String());
     339    BDirectory parentDirectory = BDirectory();
     340   
     341    status_t ret = directory.CreateDirectory(locationName, &parentDirectory);
     342
     343    if(ret < B_OK) {
     344        BString errorMessage("An error was occurred creating the directory\n\n"
     345            "Error: ");
     346        errorMessage << strerror(ret);
     347        BAlert* alert = new BAlert("launch error", errorMessage.String(),
     348            "Ok");
     349        alert->Go();
     350
     351        _SelectGlobalLocation();
     352    return ret;
     353    }
     354
     355    locationPath.Append(locationName);
     356   
     357    printf("NetworkView:: The name of the location settings file is %s\n",
     358        locationPath.String());
     359   
     360    fCurrentLocation = new LocationView(
     361        locationName, fLocationsList);
     362
     363    fCurrentLocation->SetName(locationName);
     364   
     365    _NotifyLocationAdded(locationName, fCurrentLocation);
     366
     367    fLocationsList.AddItem(fCurrentLocation);
     368   
     369    message->AddPointer("location_pointer",
     370        (const void*) fCurrentLocation);
     371   
     372    fCurrentLocationMenu = new BMenuItem(locationName, message);
     373   
     374    fCurrentLocationMenu->SetMarked(true);
     375   
     376    fLocationsMenu->AddItem(fCurrentLocationMenu);
     377   
     378    fLocationsMenu->SetTargetForItems(this);
     379
     380    _HideAllLocations();
     381   
     382    fLocationsGroup->AddChild(fCurrentLocation);
     383    fCurrentLocation->Show();
     384
     385    return B_OK;
     386}
     387
     388
     389status_t
     390NetworkView::_RestoreLocation(const char* locationName)
     391{
     392    BMessage* message = new BMessage(kMsgSelectedLocation);
     393
     394//  BPath locationPath(LOCATIONS_BASE_PATH);
     395
     396//  locationPath.Append(locationName);
     397    printf("NetworkView::_RestoreLocation() locatioPath %s\n", locationName);
     398    fCurrentLocation = new LocationView(
     399        locationName, fLocationsList);
     400
     401    BString name = BString(locationName);
     402    fCurrentLocation->SetName(name.String());
     403
     404    _NotifyLocationAdded(name.String(), fCurrentLocation);
     405
     406    fLocationsList.AddItem(fCurrentLocation);
     407
     408    message->AddPointer("location_pointer", (const void*) fCurrentLocation);
     409
     410    fCurrentLocationMenu = new BMenuItem(locationName, message);
     411
     412    fCurrentLocationMenu->SetMarked(true);
     413
     414    fLocationsMenu->AddItem(fCurrentLocationMenu);
     415   
     416    fLocationsMenu->SetTargetForItems(this);
     417
     418    _HideAllLocations();
     419
     420    fLocationsGroup->AddChild(fCurrentLocation);
     421    fCurrentLocation->Show();
     422
     423    return B_OK;
     424}
     425
     426
     427status_t
     428NetworkView::_RemoveLocation()
     429{
     430    if(fCurrentLocation->IfGlobal()) {
     431        BAlert* alert = new BAlert("Warning!",
     432            "The location selected is Global and cannot be deleted!\n",
     433            "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
     434        alert->Go();
     435        _SelectGlobalLocation();
     436        return B_OK;
     437    }
     438   
     439    BAlert* alert = new BAlert("Confirm",
     440        "Are you sure? The location will be deleted!\n",
     441        "No","Yes","Cancel", B_WIDTH_AS_USUAL, B_WARNING_ALERT);
     442   
     443    int32 ret = alert->Go();
     444    if(ret == 0 || ret == 2) {
     445        fCurrentLocationMenu->SetMarked(true);
     446        return B_OK;   
     447    }
     448
     449    _NotifyLocationRemoved(fCurrentLocationMenu->Label());
     450
     451    fCurrentLocation->DeleteLocationAndNetworks();
     452    fLocationsMenu->RemoveItem(fCurrentLocationMenu);
     453    fLocationsGroup->RemoveChild(fCurrentLocation);
     454    fLocationsList.RemoveItem(fCurrentLocation);
     455
     456    delete fCurrentLocation;
     457    delete fCurrentLocationMenu;
     458
     459    _SelectGlobalLocation();
     460
     461    return B_OK;       
     462}
     463
     464
     465status_t
     466NetworkView::_FindAndAddLocations()
     467{   
     468    BDirectory* locationsDirectory = new BDirectory(LOCATIONS_BASE_PATH);
     469    int32 entries = locationsDirectory->CountEntries();
     470    int32 i;
     471    status_t ret = B_OK;
     472
     473    for(i = 0; i < entries; i++) {
     474        BEntry* entry = new BEntry();
     475
     476        ret = locationsDirectory->GetNextEntry(entry, false);
     477        if(ret < B_OK || ret == B_ENTRY_NOT_FOUND)
     478            continue;
     479
     480        if(entry->IsDirectory() == true) {
     481            char name[B_FILE_NAME_LENGTH];
     482
     483            entry->GetName(name);
     484
     485            int str = strcmp((const char*) name, "global");
     486            if(name == NULL || str == 0)
     487                continue;
     488
     489            BString st = BString();
     490            st.SetTo(name);
     491            ret = _RestoreLocation(st.String());
     492            printf("NetworkView::_FindAndAddLocations() location added %s\n", st.String());
     493        }
     494    }
     495return ret;
     496}
     497
     498
     499void
     500NetworkView::_NotifyLocationRemoved(const char* locationName)
     501{
     502    int32 i;
     503    int32 loc = fLocationsList.CountItems();
     504    for(i = 0; i < loc; i++) {
     505        LocationView* location = fLocationsList.ItemAt(i);
     506        location->LocationRemoved(locationName);
     507    }
     508}
     509
     510
     511void
     512NetworkView::_NotifyLocationAdded(const char* locationName, LocationView* newLocation)
     513{
     514    int i;
     515    int loc = fLocationsList.CountItems();
     516    for(i = 0; i < loc; i++) {
     517        LocationView* location = fLocationsList.ItemAt(i);
     518        location->LocationAdded(locationName, newLocation);
     519    }
     520}
     521
     522
     523void
     524NetworkView::_SelectGlobalLocation()
     525{
     526    _HideAllLocations();
     527    fGlobalMenuItem->SetMarked(true);
     528    fLocationsGroup->AddChild(fGlobalLocationView);
     529}
     530
     531/*---*/
     532
     533void
     534NetworkView::_InstallInDeskbar()
     535{
     536    char *argv[] = {const_cast<char *>("--deskbar"), NULL};
     537   
     538    status_t ret = be_roster->Launch("application/x-vnd.Haiku-NetworkStatus",
     539        1, argv);
     540       
     541        if (ret < B_OK) {
     542        BString errorMessage("Installing NetworkStatus in Deskbar failed.\n\n"
     543            "Error: ");
     544        errorMessage << strerror(ret);
     545        BAlert* alert = new BAlert("launch error", errorMessage.String(),
     546            "Ok");
     547        // asynchronous alert in order to not block replicant host
     548        // application
     549        alert->Go(NULL);
     550        }
     551}
     552
     553
     554int32
     555NetworkView::_IfInstalled()
     556{
     557    BDeskbar deskbar;
     558    bool ret = deskbar.HasItem("NetworkStatus");
     559    if(ret == false)
     560        return 0;
     561
     562    return 1;
     563}
     564
     565
     566void
     567NetworkView::_RemoveFromDeskbar()
     568{
     569    BDeskbar deskbar;
     570    deskbar.RemoveItem("NetworkStatus");
     571}
  • Jamfile

     
    11SubDir HAIKU_TOP src preferences network ;
    22
    33UsePrivateHeaders shared ;
     4UsePrivateHeaders interface ;
     5
    46UseHeaders [ FDirName $(HAIKU_TOP) src servers net ] : true ;
    57
    68Preference Network :
    7     NetworkApp.cpp
    8     NetworkWindow.cpp
    9     EthernetSettingsView.cpp
    10     settings.cpp
    11     : be root $(HAIKU_NETWORK_LIBS) $(TARGET_LIBSUPC++)
     9         NetworkApp.cpp
     10     NetworkWindow.cpp
     11         NetworkView.cpp
     12         LocationNameWindow.cpp
     13         TextEntryWindow.cpp
     14         LocationView.cpp
     15         LocationSettings.cpp
     16         NetworkTypeView.cpp
     17         EthernetSettingsView.cpp
     18         PPPoEView.cpp
     19         NetworksMenuView.cpp
     20         Settings.cpp
     21         EthernetSettings.cpp
     22         NetworkSettings.cpp
     23         PPPoESettings.cpp
     24
     25    : be root $(HAIKU_NETWORK_LIBS) libcolumnlistview.a libshared.a $(TARGET_LIBSUPC++)
    1226    : Network.rdef
    1327    ;
    1428
  • LocationSettings.cpp

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#include <arpa/inet.h>
     10#include <net/if.h>
     11#include <net/if_dl.h>
     12#include <net/if_media.h>
     13#include <net/if_types.h>
     14#include <netinet/in.h>
     15#include <sys/socket.h>
     16#include <sys/sockio.h>
     17#include <stdio.h>
     18
     19#include <AutoDeleter.h>
     20#include <Directory.h>
     21#include <Entry.h>
     22
     23#include "LocationSettings.h"
     24
     25#define PFLT_CONFIGURED "configured"
     26
     27LocationSettings::LocationSettings(BPath settingsPath)
     28    : Settings(settingsPath)
     29{
     30
     31}
     32
     33
     34LocationSettings::~LocationSettings()
     35{
     36
     37}
     38
     39status_t
     40LocationSettings::MoveNetworkFile(BPath currentPath, BPath destinationPath)
     41{
     42    BEntry file = BEntry(currentPath.Path());
     43    return file.MoveTo(new BDirectory(destinationPath.Path()), NULL, true);
     44}
     45
     46status_t
     47LocationSettings::LoadSettings()
     48{
     49    status_t ret;
     50
     51    ret = ReadSetting(PFLT_CONFIGURED, &fConfigured);
     52    if(ret < B_OK)
     53        return ret;
     54
     55    return B_OK;
     56}
     57
     58
     59status_t
     60LocationSettings::SaveSettings()
     61{
     62    status_t ret;
     63
     64    if(fConfigured == true) {
     65        ret = ReplaceSetting(PFLT_CONFIGURED, true);
     66        if(ret < B_OK)
     67            return ret;
     68    } else {
     69        ret = WriteSetting(PFLT_CONFIGURED, true);
     70        if(ret < B_OK)
     71            return ret;
     72    }
     73    return FlattenSettings();
     74}
     75
     76
     77status_t
     78LocationSettings::DeleteLocationDirectory(BPath dirPath)
     79{
     80    BEntry* entry = new BEntry(dirPath.Path(), false);
     81
     82    status_t ret = entry->Remove();
     83    if(ret < B_OK)
     84        return ret;
     85
     86    delete entry;
     87    return B_OK;
     88}
     89
     90
     91// this is used only by the constructor of the global location view
     92void
     93LocationSettings::_FindEthernetDevices(BObjectList<BString> *devicesList)
     94{
     95    int Socket = socket(AF_INET, SOCK_DGRAM, 0);
     96    // iterate over all interfaces and retrieve minimal status
     97
     98    ifconf config;
     99    config.ifc_len = sizeof(config.ifc_value);
     100    if (ioctl(Socket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
     101        return;
     102
     103    uint32 count = (uint32)config.ifc_value;
     104    if (count == 0)
     105        return;
     106
     107    void* buffer = malloc(count * sizeof(struct ifreq));
     108    if (buffer == NULL)
     109        return;
     110
     111    MemoryDeleter deleter(buffer);
     112
     113    config.ifc_len = count * sizeof(struct ifreq);
     114    config.ifc_buf = buffer;
     115    if (ioctl(Socket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
     116        return;
     117
     118    ifreq* interface = (ifreq*)buffer;
     119
     120    fInterfaces.MakeEmpty();
     121
     122    for (uint32 i = 0; i < count; i++) {
     123        if (strncmp(interface->ifr_name, "loop", 4) && interface->ifr_name[0]) {
     124            devicesList->AddItem(new BString(interface->ifr_name));
     125        }
     126
     127        interface = (ifreq*)((addr_t)interface + IF_NAMESIZE
     128            + interface->ifr_addr.sa_len);
     129    }
     130    close(Socket);
     131}
     132
     133
     134status_t
     135LocationSettings::AddNetwork(const char* name)
     136{
     137    status_t ret;
     138
     139}
     140
     141
     142status_t
     143LocationSettings::AutoConfigureAllSettings()
     144{
     145    return B_OK;
     146}
     147
     148
     149bool
     150LocationSettings::IfConfigured()
     151{
     152    if(ReadSetting(PFLT_CONFIGURED, &fConfigured) < B_OK)
     153        return false;
     154
     155    if(fConfigured == true) {
     156        return true;
     157    } else if(fConfigured == false) {
     158        return false;
     159    }
     160    return false;
     161}
  • EthernetSettingsView.cpp

     
    11/*
    2  * Copyright 2004-2008 Haiku Inc. All rights reserved.
     2 * Copyright 2004-2009 Haiku Inc. All rights reserved.
    33 * Distributed under the terms of the MIT License.
    44 *
    55 * Authors:
     
    77 *      Stephan Assmuß
    88 *      Axel Dörfler
    99 *      Hugo Santos
    10  *      Philippe Saint-Pierre
     10 *      Dario Casalinuovo, barrett666@gmail.com
    1111 */
    1212
    1313#include "EthernetSettingsView.h"
    14 #include "settings.h"
     14#include "EthernetSettings.h"
    1515
    1616#include <Application.h>
    1717#include <Alert.h>
     
    4343#include <fs_interface.h>
    4444#include <Path.h>
    4545
    46 #include <arpa/inet.h>
    47 #include <net/if.h>
    48 #include <net/if_dl.h>
    49 #include <net/if_media.h>
    50 #include <net/if_types.h>
    51 #include <netinet/in.h>
    52 #include <sys/socket.h>
    53 #include <sys/sockio.h>
    54 
    5546#include <errno.h>
    5647#include <stdio.h>
    5748#include <stdlib.h>
    5849#include <string.h>
    5950#include <unistd.h>
    6051
    61 #include <NetServer.h>
    62 
    63 #include <support/Beep.h>
    64 
    6552#include "AutoDeleter.h"
    6653
    67 
    6854static const uint32 kMsgApply = 'aply';
    6955static const uint32 kMsgRevert = 'rvrt';
    7056static const uint32 kMsgClose = 'clse';
     
    8470}
    8571
    8672
    87 //  #pragma mark -
    88 
    89 
    90 EthernetSettingsView::EthernetSettingsView()
    91     : BView("EthernetSettingsView", 0, NULL),
    92     fCurrentSettings(NULL)
     73EthernetSettingsView::EthernetSettingsView(BPath path, const char* adapterName)
     74    : NetworkTypeView("EthernetSettingsView", 0, NULL)
    9375{
    9476    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
    9577
    96     fSocket = socket(AF_INET, SOCK_DGRAM, 0);
    97     _GatherInterfaces();
     78    fCurrentSettings = new EthernetSettings(path, adapterName);
    9879
    9980    // build the GUI
    10081    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     
    10990    rootLayout->SetSpacing(inset);
    11091    layout->SetSpacing(inset, inset);
    11192
    112     BPopUpMenu* deviceMenu = new BPopUpMenu("devices");
    113     for (int32 i = 0; i < fInterfaces.CountItems(); i++) {
    114         BString& name = *fInterfaces.ItemAt(i);
    115         BString label = name;
    116         BMessage* info = new BMessage(kMsgInfo);
    117         info->AddString("interface", name.String());
    118         BMenuItem* item = new BMenuItem(label.String(), info);
    119         deviceMenu->AddItem(item);
    120     }
    121 
    12293    BPopUpMenu* modeMenu = new  BPopUpMenu("modes");
    12394    modeMenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
    12495    modeMenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
     
    12697    //BMenuItem* offItem = new BMenuItem("Disabled", NULL);
    12798    //modeMenu->AddItem(offItem);
    12899
    129     fDeviceMenuField = new BMenuField("Adapter:", deviceMenu);
    130     layout->AddItem(fDeviceMenuField->CreateLabelLayoutItem(), 0, 0);
    131     layout->AddItem(fDeviceMenuField->CreateMenuBarLayoutItem(), 1, 0);
    132 
    133100    fTypeMenuField = new BMenuField("Mode:", modeMenu);
    134101    layout->AddItem(fTypeMenuField->CreateLabelLayoutItem(), 0, 1);
    135102    layout->AddItem(fTypeMenuField->CreateMenuBarLayoutItem(), 1, 1);
     
    167134    layout->AddItem(fSecondaryDNSTextControl->CreateLabelLayoutItem(), 0, 6);
    168135    layout->AddItem(fSecondaryDNSTextControl->CreateTextViewLayoutItem(), 1, 6);
    169136
    170     fErrorMessage = new BStringView("error", "");
    171     fErrorMessage->SetAlignment(B_ALIGN_LEFT);
    172     fErrorMessage->SetFont(be_bold_font);
    173     fErrorMessage->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
    174 
    175     layout->AddView(fErrorMessage, 1, 7);
    176 
    177137    // button group (TODO: move to window, but take care of
    178138    // enabling/disabling)
    179139    BGroupView* buttonGroup = new BGroupView(B_HORIZONTAL);
     
    194154
    195155EthernetSettingsView::~EthernetSettingsView()
    196156{
    197     close(fSocket);
    198 }
    199157
    200 
    201 bool
    202 EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
    203 {
    204     // This function is used for talking direct to the stack.
    205     // It's used by _ShowConfiguration.
    206 
    207     if (strlen(name) > IF_NAMESIZE)
    208         return false;
    209 
    210     strcpy(request.ifr_name, name);
    211     return true;
    212158}
    213159
    214 
    215160void
    216 EthernetSettingsView::_GatherInterfaces()
    217 {
    218     // iterate over all interfaces and retrieve minimal status
    219 
    220     ifconf config;
    221     config.ifc_len = sizeof(config.ifc_value);
    222     if (ioctl(fSocket, SIOCGIFCOUNT, &config, sizeof(struct ifconf)) < 0)
    223         return;
    224 
    225     uint32 count = (uint32)config.ifc_value;
    226     if (count == 0)
    227         return;
    228 
    229     void* buffer = malloc(count * sizeof(struct ifreq));
    230     if (buffer == NULL)
    231         return;
    232 
    233     MemoryDeleter deleter(buffer);
    234 
    235     config.ifc_len = count * sizeof(struct ifreq);
    236     config.ifc_buf = buffer;
    237     if (ioctl(fSocket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
    238         return;
    239 
    240     ifreq* interface = (ifreq*)buffer;
    241 
    242     fInterfaces.MakeEmpty();
    243 
    244     for (uint32 i = 0; i < count; i++) {
    245         if (strncmp(interface->ifr_name, "loop", 4) && interface->ifr_name[0]) {
    246             fInterfaces.AddItem(new BString(interface->ifr_name));
    247             fSettings.AddItem(new Settings(interface->ifr_name));
    248         }
    249 
    250         interface = (ifreq*)((addr_t)interface + IF_NAMESIZE
    251             + interface->ifr_addr.sa_len);
    252     }
    253 }
    254 
    255 
    256 void
    257161EthernetSettingsView::AttachedToWindow()
    258162{
    259163    fApplyButton->SetTarget(this);
     
    263167    fGatewayTextControl->SetTarget(this);
    264168    fPrimaryDNSTextControl->SetTarget(this);
    265169    fSecondaryDNSTextControl->SetTarget(this);
    266     fDeviceMenuField->Menu()->SetTargetForItems(this);
    267170    fTypeMenuField->Menu()->SetTargetForItems(this);
    268171
    269     // display settigs of first adapter on startup
    270     _ShowConfiguration(fSettings.ItemAt(0));
     172    // display settigs of adapter on startup
     173    _ShowConfiguration();
    271174}
    272175
    273176
    274177void
    275178EthernetSettingsView::DetachedFromWindow()
    276179{
     180
    277181}
    278182
    279183
    280184void
    281 EthernetSettingsView::_ShowConfiguration(Settings* settings)
     185EthernetSettingsView::DeleteNetworkAndSettings()
    282186{
    283     fCurrentSettings = settings;
    284187
     188}
     189
     190
     191void
     192EthernetSettingsView::_ShowConfiguration()
     193{
    285194    // Clear the inputs.
    286195    fIPTextControl->SetText("");
    287196    fGatewayTextControl->SetText("");
     
    290199    fSecondaryDNSTextControl->SetText("");
    291200
    292201    bool enableControls = false;
    293     fTypeMenuField->SetEnabled(settings != NULL);
     202    fTypeMenuField->SetEnabled(fCurrentSettings != NULL);
    294203
    295     if (settings) {
    296         BMenuItem* item = fDeviceMenuField->Menu()->FindItem(
    297             settings->GetName());
    298         if (item)
    299             item->SetMarked(true);
     204    if (fCurrentSettings) {
     205        BMenuItem* item;
    300206
    301         fIPTextControl->SetText(settings->GetIP());
    302         fGatewayTextControl->SetText(settings->GetGateway());
    303         fNetMaskTextControl->SetText(settings->GetNetmask());
     207        fIPTextControl->SetText(fCurrentSettings->GetIP());
     208        fGatewayTextControl->SetText(fCurrentSettings->GetGateway());
     209        fNetMaskTextControl->SetText(fCurrentSettings->GetNetmask());
    304210
    305         if (settings->GetAutoConfigure() == true)
     211        if (fCurrentSettings->GetAutoConfigure() == true)
    306212            item = fTypeMenuField->Menu()->FindItem("DHCP");
    307213        else
    308214            item = fTypeMenuField->Menu()->FindItem("Static");
    309215        if (item)
    310216            item->SetMarked(true);
    311217
    312         enableControls = settings->GetAutoConfigure() == false;
     218        enableControls = fCurrentSettings->GetAutoConfigure() == false;
    313219
    314         if (settings->fNameservers.CountItems() >= 2) {
     220        if (fCurrentSettings->fNameservers.CountItems() >= 2) {
    315221            fSecondaryDNSTextControl->SetText(
    316                 settings->fNameservers.ItemAt(1)->String());
     222                fCurrentSettings->fNameservers.ItemAt(1)->String());
    317223        }
    318224
    319         if (settings->fNameservers.CountItems() >= 1) {
     225        if (fCurrentSettings->fNameservers.CountItems() >= 1) {
    320226            fPrimaryDNSTextControl->SetText(
    321                     settings->fNameservers.ItemAt(0)->String());
     227                    fCurrentSettings->fNameservers.ItemAt(0)->String());
    322228        }
    323229    }
    324230
     
    364270void
    365271EthernetSettingsView::_SaveConfiguration()
    366272{
     273    fCurrentSettings->SaveSettings();
    367274    _ApplyControlsToConfiguration();
    368     _SaveDNSConfiguration();
    369     _SaveAdaptersConfiguration();
    370     if (fCurrentSettings->GetAutoConfigure())
    371         _TriggerAutoConfig(fCurrentSettings->GetName());
    372275}
    373276
    374277
    375278void
    376 EthernetSettingsView::_SaveDNSConfiguration()
    377 {
    378     BFile file("/etc/resolv.conf",
    379         B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
    380     if (file.InitCheck() < B_OK) {
    381         fprintf(stderr, "failed to open /etc/resolv.conf for writing: %s\n",
    382             strerror(file.InitCheck()));
    383         return;
    384     }
    385 
    386     BString content("# Generated by Network Preflet\n");
    387     // loop over all adapters
    388     for (int i = 0; i < fSettings.CountItems(); i++) {
    389         Settings* settings = fSettings.ItemAt(i);
    390         for (int j = 0; j < settings->fNameservers.CountItems(); j++) {
    391             if (settings->fNameservers.ItemAt(j)->Length() > 0) {
    392                 content << "nameserver\t"
    393                     << settings->fNameservers.ItemAt(j)->String()
    394                     << "\n";
    395             }
    396         }
    397     }
    398 
    399     file.Write(content.String(), content.Length());
    400 }
    401 
    402 
    403 void
    404 EthernetSettingsView::_SaveAdaptersConfiguration()
    405 {
    406     BPath path;
    407     status_t status = _GetPath("interfaces", path);
    408     if (status < B_OK)
    409         return;
    410 
    411     FILE* fp = NULL;
    412     // loop over all adapters. open the settings file only once,
    413     // append the settins of each non-autoconfiguring adapter
    414     for (int i = 0; i < fSettings.CountItems(); i++) {
    415         if (fSettings.ItemAt(i)->GetAutoConfigure())
    416             continue;
    417 
    418         if (fp == NULL) {
    419             fp = fopen(path.Path(), "w");
    420             if (fp == NULL) {
    421                 fprintf(stderr, "failed to open file %s to write "
    422                     "configuration: %s\n", path.Path(), strerror(errno));
    423                 return;
    424             }
    425         }
    426 
    427         fprintf(fp, "interface %s {\n\t\taddress {\n",
    428             fSettings.ItemAt(i)->GetName());
    429         fprintf(fp, "\t\t\tfamily\tinet\n");
    430         fprintf(fp, "\t\t\taddress\t%s\n",
    431             fSettings.ItemAt(i)->GetIP());
    432         fprintf(fp, "\t\t\tgateway\t%s\n",
    433             fSettings.ItemAt(i)->GetGateway());
    434         fprintf(fp, "\t\t\tmask\t%s\n",
    435             fSettings.ItemAt(i)->GetNetmask());
    436         fprintf(fp, "\t\t}\n}\n\n");
    437     }
    438     if (fp) {
    439         printf("%s saved.\n", path.Path());
    440         fclose(fp);
    441     } else {
    442         // all configuration is DHCP, so delete interfaces file.
    443         remove(path.Path());
    444     }
    445 }
    446 
    447 
    448 status_t
    449 EthernetSettingsView::_TriggerAutoConfig(const char* device)
    450 {
    451     BMessenger networkServer(kNetServerSignature);
    452     if (!networkServer.IsValid()) {
    453         (new BAlert("error", "The net_server needs to run for the auto "
    454             "configuration!", "Ok"))->Go();
    455         return B_ERROR;
    456     }
    457 
    458     BMessage message(kMsgConfigureInterface);
    459     message.AddString("device", device);
    460     BMessage address;
    461     address.AddString("family", "inet");
    462     address.AddBool("auto_config", true);
    463     message.AddMessage("address", &address);
    464 
    465     BMessage reply;
    466     status_t status = networkServer.SendMessage(&message, &reply);
    467     if (status != B_OK) {
    468         BString errorMessage("Sending auto-config message failed: ");
    469         errorMessage << strerror(status);
    470         (new BAlert("error", errorMessage.String(), "Ok"))->Go();
    471         return status;
    472     } else if (reply.FindInt32("status", &status) == B_OK
    473             && status != B_OK) {
    474         BString errorMessage("Auto-configuring failed: ");
    475         errorMessage << strerror(status);
    476         (new BAlert("error", errorMessage.String(), "Ok"))->Go();
    477         return status;
    478     }
    479 
    480     return B_OK;
    481 }
    482 
    483 
    484 status_t
    485 EthernetSettingsView::_GetPath(const char* name, BPath& path)
    486 {
    487     if (find_directory(B_COMMON_SETTINGS_DIRECTORY, &path, true) != B_OK)
    488         return B_ERROR;
    489 
    490     path.Append("network");
    491     create_directory(path.Path(), 0755);
    492 
    493     if (name != NULL)
    494         path.Append(name);
    495     return B_OK;
    496 }
    497 
    498 
    499 bool
    500 MatchPattern(const char* string, const char* pattern)
    501 {
    502     regex_t compiled;
    503     bool result = regcomp(&compiled, pattern, REG_NOSUB | REG_EXTENDED) == 0
    504         && regexec(&compiled, string, 0, NULL, 0) == 0;
    505     regfree(&compiled);
    506 
    507     return result;
    508 }
    509 
    510 
    511 bool
    512 EthernetSettingsView::_ValidateControl(BTextControl* control)
    513 {
    514     static const char* pattern = "^(25[0-5]|2[0-4][0-9]|[01][0-9]{2}|[0-9]"
    515         "{1,2})(\\.(25[0-5]|2[0-4][0-9]|[01][0-9]{2}|[0-9]{1,2})){3}$";
    516 
    517     if (control->IsEnabled() && !MatchPattern(control->Text(), pattern)) {
    518         control->MakeFocus();
    519         BString errorMessage;
    520         errorMessage << control->Label();
    521         errorMessage.RemoveLast(":");
    522         errorMessage << " is invalid";
    523         fErrorMessage->SetText(errorMessage.String());
    524         beep();
    525         return false;
    526     }
    527     return true;
    528 }
    529 
    530 
    531 void
    532279EthernetSettingsView::MessageReceived(BMessage* message)
    533280{
    534281    switch (message->what) {
     
    542289            const char* name;
    543290            if (message->FindString("interface", &name) != B_OK)
    544291                break;
    545             for (int32 i = 0; i < fSettings.CountItems(); i++) {
    546                 Settings* settings = fSettings.ItemAt(i);
    547                 if (strcmp(settings->GetName(), name) == 0) {
    548                     _ShowConfiguration(settings);
     292                if (strcmp(fCurrentSettings->GetName(), name) == 0) {
     293                    _ShowConfiguration();
    549294                    break;
    550295                }
    551             }
    552296            break;
    553297        }
    554298        case kMsgRevert:
    555             _ShowConfiguration(fCurrentSettings);
     299            _ShowConfiguration();
    556300            fRevertButton->SetEnabled(false);
    557301            break;
    558302        case kMsgApply:
    559             if (_ValidateControl(fIPTextControl)
    560                 && _ValidateControl(fNetMaskTextControl)
    561                 && (strlen(fGatewayTextControl->Text()) == 0
    562                     || _ValidateControl(fGatewayTextControl))
    563                 && (strlen(fPrimaryDNSTextControl->Text()) == 0
    564                     || _ValidateControl(fPrimaryDNSTextControl))
    565                 && (strlen(fSecondaryDNSTextControl->Text()) == 0
    566                     || _ValidateControl(fSecondaryDNSTextControl)))
    567                 _SaveConfiguration();
     303            _SaveConfiguration();
    568304            break;
    569305        case kMsgChange:
    570             fErrorMessage->SetText("");
    571306            fApplyButton->SetEnabled(true);
    572307            break;
    573308        default:
  • NetworkTypeView.h

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario Casalinuovo, barrett666@gmail.com
     7 */
     8
     9#ifndef NETWORK_TYPE_H
     10#define NETWORK_TYPE_H
     11
     12#include <Path.h>
     13#include <String.h>
     14#include <View.h>
     15
     16class NetworkTypeView : public BView {
     17public:
     18                        NetworkTypeView(const char* name, uint32 flags, BLayout* layout);
     19        virtual         ~NetworkTypeView();
     20
     21        virtual void    MessageReceived(BMessage* message);
     22        virtual void    AttachedToWindow();
     23        virtual void    DetachedFromWindow();
     24
     25        virtual void    DeleteNetworkAndSettings();
     26        virtual void    ChangeSettingsFilePath(BPath path);
     27       
     28        bool            IfHwDevice() { return fIfDevice; }
     29        void            SetHwDevice(bool ifDevice);
     30       
     31        const char*     Name() { return fName.String(); }
     32
     33private:
     34        BString         fName;
     35        bool            fIfDevice;
     36
     37};
     38
     39#endif
     40
  • EthernetSettings.cpp

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8#include "EthernetSettings.h"
     9
     10#include <stdio.h>
     11
     12#include <AutoDeleter.h>
     13#include <Directory.h>
     14#include <FindDirectory.h>
     15#include <fs_interface.h>
     16#include <Path.h>
     17#include <SupportDefs.h>
     18
     19// network headers
     20#include <arpa/inet.h>
     21#include <net/if.h>
     22#include <net/if_dl.h>
     23#include <net/if_media.h>
     24#include <net/if_types.h>
     25#include <netinet/in.h>
     26#include <sys/socket.h>
     27#include <sys/sockio.h>
     28
     29#include <errno.h>
     30
     31/*
     32#define PFLT_IPV4 "ipv4"
     33#define PFLT_SUBNETMASK "subnetmask"
     34#define PFLT_ROUTER "router"
     35#define PFLT_IPV4_NAME "ipv4_name"
     36*/
     37#define PFLT_IPV4_AUTOCONF "ipv4_autoconf"
     38#define PFLT_CONFIGURED "configured"
     39
     40EthernetSettings::EthernetSettings(BPath settingsPath, const char *name)
     41    : Settings(settingsPath)
     42{
     43    fAuto = true;
     44    fSocket = socket(AF_INET, SOCK_DGRAM, 0);
     45    fName = name;
     46    ReadConfiguration();
     47}
     48
     49
     50EthernetSettings::~EthernetSettings()
     51{
     52    close(fSocket);
     53}
     54
     55
     56status_t
     57EthernetSettings::LoadSettings()
     58{
     59    status_t ret;
     60
     61    ret = ReadSetting(PFLT_IPV4_AUTOCONF , &fAutoIPv4);
     62    if(ret < B_OK)
     63        return ret;
     64   
     65    ret = ReadSetting(PFLT_CONFIGURED, &fConfigured);
     66    if(ret < B_OK)
     67        return ret;
     68
     69    ReadConfiguration();
     70
     71    return B_OK;
     72}
     73
     74
     75status_t
     76EthernetSettings::ApplySettings()
     77{
     78
     79    return B_OK;
     80}
     81
     82
     83status_t
     84EthernetSettings::SaveSettings()
     85{
     86    _SaveAdapterConfiguration();
     87    _SaveDNSConfiguration();
     88
     89    status_t ret;
     90
     91        if(fConfigured == true) {
     92            ret = ReplaceSetting(PFLT_IPV4_AUTOCONF , fAutoIPv4);
     93            if(ret < B_OK)
     94                return ret;
     95        } else {
     96            ret = WriteSetting(PFLT_IPV4_AUTOCONF , fAutoIPv4);
     97            if(ret < B_OK)
     98                return ret;
     99
     100            ret = WriteSetting(PFLT_CONFIGURED, true);
     101            if(ret < B_OK)
     102                return ret;
     103        }       
     104    return FlattenSettings();
     105}
     106
     107
     108
     109
     110void
     111EthernetSettings::_SaveAdapterConfiguration()
     112{
     113    BPath path;
     114    status_t status = _GetPath("interfaces", path);
     115    if (status < B_OK)
     116        return;
     117
     118    FILE* fp = NULL;
     119
     120        if (!fAuto)
     121            return;
     122
     123        if (fp == NULL) {
     124            fp = fopen(path.Path(), "w");
     125            if (fp == NULL) {
     126                fprintf(stderr, "failed to open file %s to write "
     127                    "configuration: %s\n", path.Path(), strerror(errno));
     128                return;
     129            }
     130        }
     131
     132        fprintf(fp, "interface %s {\n\t\taddress {\n",GetName());
     133        fprintf(fp, "\t\t\tfamily\tinet\n");
     134        fprintf(fp, "\t\t\taddress\t%s\n", GetIP());
     135        fprintf(fp, "\t\t\tgateway\t%s\n", GetGateway());
     136        fprintf(fp, "\t\t\tmask\t%s\n", GetNetmask());
     137        fprintf(fp, "\t\t}\n}\n\n");
     138
     139    if (fp) {
     140        printf("%s saved.\n", path.Path());
     141        fclose(fp);
     142    } else {
     143        // all configuration is DHCP, so delete interfaces file.
     144        remove(path.Path());
     145    }
     146}
     147
     148status_t
     149EthernetSettings::_GetPath(const char* name, BPath& path)
     150{
     151    if(find_directory(B_COMMON_SETTINGS_DIRECTORY, &path, true) != B_OK)
     152        return B_ERROR;
     153
     154    path.Append("network");
     155    create_directory(path.Path(), 0755);
     156
     157    if (name != NULL)
     158        path.Append(name);
     159    return B_OK;
     160}
     161
     162
     163void
     164EthernetSettings::_SaveDNSConfiguration()
     165{
     166    BFile file("/etc/resolv.conf",
     167        B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
     168    if (file.InitCheck() < B_OK) {
     169        fprintf(stderr, "failed to open /etc/resolv.conf for writing: %s\n",
     170            strerror(file.InitCheck()));
     171        return;
     172    }
     173
     174    BString content("# Generated by Network Preflet\n");
     175        for (int j = 0; j < fNameservers.CountItems(); j++) {
     176            if (fNameservers.ItemAt(j)->Length() > 0) {
     177                content << "nameserver\t"
     178                    << fNameservers.ItemAt(j)->String()
     179                    << "\n";
     180            }
     181        }
     182
     183    file.Write(content.String(), content.Length());
     184}
     185
     186
     187void
     188EthernetSettings::ReadConfiguration()
     189{
     190    ifreq request;
     191    if (!_PrepareRequest(request))
     192        return;
     193
     194    BString text = "dummy";
     195    char address[32];
     196    sockaddr_in* inetAddress = NULL;
     197
     198    // Obtain IP.   
     199    if (ioctl(fSocket, SIOCGIFADDR, &request, sizeof(request)) < 0)
     200        return;
     201
     202    inetAddress = (sockaddr_in*)&request.ifr_addr;
     203    if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
     204            sizeof(address)) == NULL) {
     205        return;
     206    }
     207   
     208    fIP = address;
     209
     210    // Obtain netmask.
     211    if (ioctl(fSocket, SIOCGIFNETMASK, &request,
     212            sizeof(request)) < 0) {
     213        return;
     214    }
     215
     216    inetAddress = (sockaddr_in*)&request.ifr_mask;
     217    if (inet_ntop(AF_INET, &inetAddress->sin_addr, address,
     218            sizeof(address)) == NULL) {
     219        return;
     220    }
     221
     222    fSubnetMask = address;
     223
     224    // Obtain gateway
     225    ifconf config;
     226    config.ifc_len = sizeof(config.ifc_value);
     227    if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
     228        return;
     229
     230    uint32 size = (uint32)config.ifc_value;
     231    if (size == 0)
     232        return;
     233
     234    void *buffer = malloc(size);
     235    if (buffer == NULL)
     236        return;
     237
     238    MemoryDeleter bufferDeleter(buffer);
     239    config.ifc_len = size;
     240    config.ifc_buf = buffer;
     241
     242    if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0)
     243        return;
     244
     245    ifreq *interface = (ifreq *)buffer;
     246    ifreq *end = (ifreq *)((uint8 *)buffer + size);
     247
     248
     249    while (interface < end) {
     250        route_entry& route = interface->ifr_route;
     251
     252        if (route.flags & RTF_GATEWAY) {
     253            inetAddress = (sockaddr_in*)route.gateway;
     254            fGateway = inet_ntoa(inetAddress->sin_addr);
     255        }
     256
     257        int32 addressSize = 0;
     258        if (route.destination != NULL)
     259            addressSize += route.destination->sa_len;
     260        if (route.mask != NULL)
     261            addressSize += route.mask->sa_len;
     262        if (route.gateway != NULL)
     263            addressSize += route.gateway->sa_len;
     264
     265        interface = (ifreq *)((addr_t)interface +
     266            IF_NAMESIZE + sizeof(route_entry) + addressSize);
     267    }
     268
     269    uint32 flags = 0;
     270    if (ioctl(fSocket, SIOCGIFFLAGS, &request, sizeof(struct ifreq)) == 0)
     271        flags = request.ifr_flags;
     272
     273    fAuto = flags & IFF_AUTO_CONFIGURED;
     274
     275    // read resolv.conf for the dns.
     276    fNameservers.MakeEmpty();
     277
     278#define MATCH(line, name) \
     279    (!strncmp(line, name, sizeof(name) - 1) && \
     280    (line[sizeof(name) - 1] == ' ' || \
     281     line[sizeof(name) - 1] == '\t'))
     282
     283
     284    register FILE *fp = fopen("/etc/resolv.conf", "r");
     285    if (fp == NULL) {
     286        fprintf(stderr, "failed to open '/etc/resolv.conf' to "
     287            "read nameservers: %s\n", strerror(errno));
     288        return;
     289    }
     290
     291    int nserv = 0;
     292    char buf[1024];
     293    register char *cp; //, **pp;
     294//  register int n;
     295    int MAXNS = 2;
     296
     297    // read the config file
     298    while (fgets(buf, sizeof(buf), fp) != NULL) {
     299        // skip comments
     300        if (*buf == ';' || *buf == '#')
     301            continue;
     302
     303        // read nameservers to query
     304        if (MATCH(buf, "nameserver") && nserv < MAXNS) {
     305//          char sbuf[2];
     306            cp = buf + sizeof("nameserver") - 1;
     307            while (*cp == ' ' || *cp == '\t')
     308                cp++;
     309            cp[strcspn(cp, ";# \t\n")] = '\0';
     310            if ((*cp != '\0') && (*cp != '\n')) {
     311                fNameservers.AddItem(new BString(cp));
     312                nserv++;
     313            }
     314        }
     315        continue;
     316    }
     317
     318    fclose(fp);
     319}
     320
     321
     322status_t
     323EthernetSettings :: AutoConfigureAllSettings()
     324{
     325
     326    SetAutoConfigure(true);
     327
     328    return B_OK;   
     329}
     330
     331
     332bool
     333EthernetSettings :: IfConfigured()
     334{
     335    if(ReadSetting(PFLT_CONFIGURED, &fConfigured) < B_OK)
     336        return false;
     337
     338    if(fConfigured == true) {
     339        return true;
     340    } else if(fConfigured == false) {
     341        return false;   
     342    }
     343    return false;
     344}
     345
     346
     347bool
     348EthernetSettings::_PrepareRequest(struct ifreq& request)
     349{
     350    //This function is used for talking direct to the stack.
     351    //It´s used by _ShowConfiguration.
     352
     353    const char* name = fName.String();
     354
     355    if (strlen(name) > IF_NAMESIZE)
     356        return false;
     357
     358    strcpy(request.ifr_name, name);
     359    return true;
     360}
  • NetworkApp.h

     
    2525       
    2626        virtual void ReadyToRun();
    2727    private:
    28         NetworkWindow   *fEthWindow;
     28        NetworkWindow   *fPrefletWindow;
    2929};
    3030
    3131
  • Settings.cpp

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8#include "Settings.h"
     9
     10#include <stdio.h>
     11
     12#include <Entry.h>
     13#include <new>
     14
     15const uint32 kMsgSettings = 'stng';
     16
     17Settings::Settings(BPath path)
     18    : BMessage(kMsgSettings), fSettingsPath(path)
     19{
     20        fSettingsFile = new(std :: nothrow) BFile(path.Path(),
     21        B_READ_WRITE | B_CREATE_FILE);
     22}
     23
     24
     25Settings::~Settings()
     26{
     27
     28}
     29
     30
     31status_t
     32Settings::OpenSettings()
     33{
     34    status_t ret = fSettingsFile->InitCheck();
     35
     36    if(ret < B_OK) {
     37        printf("Settings: initchek error %s\n", strerror(ret));
     38        return ret;
     39    }
     40
     41    ret = Unflatten(fSettingsFile);
     42   
     43    if(ret < B_OK) {
     44        printf("Settings: unflatten error %s\n", strerror(ret));
     45        return ret;
     46    }
     47   
     48    return B_OK;   
     49}
     50
     51
     52status_t
     53Settings::ReadSetting(const char* name, BString* string)
     54{
     55    const char* setting;
     56   
     57    status_t ret = FindString(name, &setting);
     58   
     59    if(ret < B_OK) {
     60        return ret;
     61    } else {
     62        string->SetTo(setting);
     63        return ret;
     64    }   
     65}
     66
     67
     68status_t
     69Settings::ReadSetting(const char* name, int32* setting)
     70{
     71    return FindInt32(name, setting);
     72}
     73
     74
     75status_t
     76Settings::ReadSetting(const char* name, bool* setting)
     77{
     78    return FindBool(name, setting);
     79}
     80
     81
     82status_t
     83Settings::WriteSetting(const char* name, const char* string)
     84{
     85    return AddString(name, string); 
     86}
     87
     88
     89status_t
     90Settings::WriteSetting(const char* name, int32 setting)
     91{
     92    return AddInt32(name, setting);
     93}
     94
     95
     96status_t
     97Settings::WriteSetting(const char* name, bool setting)
     98{
     99    return AddBool(name, setting);
     100}
     101
     102
     103status_t
     104Settings::ReplaceSetting(const char* name, const char* string)
     105{
     106    return ReplaceString(name, 0, string);
     107}
     108
     109
     110status_t
     111Settings::ReplaceSetting(const char* name, int32 setting)
     112{
     113    return ReplaceInt32(name, 0, setting);
     114}
     115
     116
     117status_t
     118Settings::ReplaceSetting(const char* name, bool setting)
     119{
     120    return ReplaceBool(name, 0, setting);
     121}
     122
     123
     124status_t
     125Settings::RemoveSetting(const char* name)
     126{
     127    return RemoveData(name);
     128}
     129
     130
     131
     132status_t
     133Settings::DeleteSettings()
     134{
     135    delete fSettingsFile;
     136    BEntry* entry = new BEntry(fSettingsPath.Path(), false);
     137    printf("Settings::DeleteSettings() the entry is %s\n", fSettingsPath.Path());
     138
     139    status_t ret = entry->Remove();
     140    if(ret < B_OK)
     141        return ret;
     142
     143    delete entry;
     144    return B_OK;
     145}
     146
     147
     148status_t
     149Settings::FlattenSettings()
     150{
     151    fSettingsFile->Seek(0, SEEK_SET);
     152    return Flatten(fSettingsFile);
     153}
  • NetworksMenuView.h

     
     1/*
     2 * Copyright 2008 - 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#ifndef ETHERNET_NETWORK_VIEW_H
     10#define ETHERNET_NETWORK_VIEW_H
     11
     12#include "NetworkTypeView.h"
     13
     14#include <ColumnListView.h>
     15#include <ObjectList.h>
     16#include <String.h>
     17
     18class BBox;
     19class BCheckBox;
     20class BStringView;
     21
     22//
     23class NRow : public BRow {
     24public:
     25                            NRow(float height);
     26
     27        void                SetAssociatedView(NetworkTypeView*);
     28        NetworkTypeView*    GetAssociatedView();
     29
     30        void                SetType(const char*);
     31        const char*         GetType();
     32
     33private:
     34        NetworkTypeView*    fAssociatedView;
     35        const char*         fType;
     36};
     37
     38
     39class NetworksMenuView : public BView {
     40public:
     41                            NetworksMenuView();
     42        virtual             ~NetworksMenuView();
     43
     44        virtual void        MessageReceived(BMessage* message);
     45        virtual void        AttachedToWindow();
     46        virtual void        DetachedFromWindow();
     47
     48        status_t            AddEthernetAdapter(BString name, BPath path);
     49        status_t            AddPPPoENetwork(BString name, BPath path);
     50//      status_t            AddVPN(Bstring name, BPath path);
     51        status_t            MoveNetwork(NetworkTypeView* network);
     52       
     53        NetworkTypeView*    GetSelectedNetwork();
     54
     55        status_t            SelectedNetworkMoved();
     56
     57        status_t            RemoveNetwork();
     58        status_t            DeleteAllNetworks();
     59private:
     60        status_t            _AddRow(const char* name);
     61        void                _HideAllChilds();
     62
     63        BBox*               fNetworksBBox;
     64        BBox*               fNetworkPreferencesBBox;
     65        BCheckBox*          fCustomizePreferencesCheckBox;
     66        BStringView*        fNetworkName;
     67        BStringView*        fNetworkStatus;
     68        BColumnListView*    fNetworksList;
     69        BGroupView*         fNtpGroup;
     70};
     71
     72#endif
  • TextEntryWindow.h

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo, Barrett666@gmail.com
     7 */
     8#ifndef NETWORK_WINDOW_H
     9#define NETWORK_WINDOW_H
     10
     11#include <Window.h>
     12
     13class BButton;
     14class BString;
     15class BTextControl;
     16class NetworkView;
     17
     18class TextEntryWindow : public BWindow {
     19public:
     20                    TextEntryWindow();
     21    virtual         ~TextEntryWindow();
     22    virtual void    MessageReceived(BMessage* mesage);
     23            bool    QuitRequested();
     24   
     25            void    Quit();
     26
     27    const char*     Go();
     28
     29private:
     30    BTextControl*   fNetworkNameControl;
     31    BString     fString;
     32    BButton*        fConfirmButton;
     33    sem_id          fTextSem;
     34};
     35
     36#endif  /* NETWORK_WINDOW_H */
  • LocationView.cpp

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *     
     8 *     
     9 */
     10
     11#include <stdio.h>
     12
     13#include <Alert.h>
     14#include <Button.h>
     15#include <GridView.h>
     16#include <GroupView.h>
     17#include <LayoutItem.h>
     18#include <Menu.h>
     19#include <MenuField.h>
     20#include <MenuItem.h>
     21#include <Path.h>
     22#include <PopUpMenu.h>
     23#include <View.h>
     24
     25#include "LocationSettings.h"
     26#include "LocationView.h"
     27#include "NetworkTypeView.h"
     28#include "TextEntryWindow.h"
     29#include "NetworksMenuView.h"
     30#include "PrefletDefs.h"
     31
     32#include <new>
     33
     34const uint32 kMsgPPPOE = 'ppoe';
     35const uint32 kMsgVPN = 'vpnc';
     36const uint32 kMsgMoveToLocation = 'mvtl';
     37const uint32 kMsgRemoveNetwork = 'rmnt';
     38
     39LocationView::LocationView(const char* locationName,
     40    BObjectList<LocationView> &list)
     41    : BView("LocationView", 0, NULL)
     42{
     43    fIfGlobal = false;
     44
     45    fSettingsPath = BPath(LOCATIONS_BASE_PATH);
     46    fLocationPath = BPath(LOCATIONS_BASE_PATH);
     47
     48    fSettingsPath.Append(locationName);
     49    fSettingsPath.Append("location");
     50
     51    fLocationPath.Append(locationName);
     52
     53//  printf("LocationView::LocationView() the location is %s %s %s \n",fLocationPath.Path(), fSettingsPath.Path(), locationName);
     54    fLocationSettings = new LocationSettings(fSettingsPath);
     55
     56    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     57
     58    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     59
     60    SetLayout(rootLayout);
     61
     62    BGroupView* firstGroup = new BGroupView(B_VERTICAL);
     63
     64    fIspMenu = new BMenu("My ISP");
     65
     66    fPreferredISP = new BMenuField("Connect On Demand :", fIspMenu);
     67
     68    firstGroup->GroupLayout()->AddView(fPreferredISP);
     69
     70    BGroupView* bboxGroup = new BGroupView(B_HORIZONTAL);
     71
     72    //
     73
     74    fNetworksMenuView = new NetworksMenuView();
     75
     76    bboxGroup->GroupLayout()->AddView(fNetworksMenuView);
     77
     78    // under the Networks Menu
     79
     80    BGroupView* useroptGroup = new BGroupView(B_HORIZONTAL);
     81
     82    fNetworkMenu = new BPopUpMenu("Network");
     83    fNetworkMenu->SetLabelFromMarked(false);
     84
     85    fNetworkMenuField = new BMenuField("Add :", fNetworkMenu); 
     86
     87    BMenuItem* fPPPoEMenu = new BMenuItem("PPPoE", new BMessage(kMsgPPPOE));
     88
     89    fNetworkMenu->AddItem(fPPPoEMenu);
     90
     91    BMenuItem* fVPNMenu = new BMenuItem("VPN", new BMessage(kMsgVPN));
     92
     93    fNetworkMenu->AddItem(fVPNMenu);
     94
     95    useroptGroup->GroupLayout()->AddView(fNetworkMenuField);
     96
     97    fMoveToMenu = new BPopUpMenu("Select a Location");
     98    fMoveToMenu->SetLabelFromMarked(false);
     99
     100    fMoveToMenuField = new BMenuField("Move To :", fMoveToMenu);
     101
     102    useroptGroup->GroupLayout()->AddView(fMoveToMenuField);
     103
     104    fRemoveButton = new BButton("Remove", new BMessage(kMsgRemoveNetwork));
     105    useroptGroup->GroupLayout()->AddView(fRemoveButton);
     106   
     107    _PopulateMoveToMenu(list);
     108
     109    rootLayout->AddView(firstGroup);
     110    rootLayout->AddView(bboxGroup);
     111    rootLayout->AddView(useroptGroup);
     112}
     113
     114
     115// global location constructor
     116LocationView::LocationView()
     117    : BView("GlobalLocation", 0, NULL)
     118{
     119    fLocationPath = BPath(LOCATIONS_BASE_PATH);
     120    fSettingsPath = BPath(LOCATIONS_BASE_PATH);
     121    fSettingsPath.Append("global/location");
     122    fLocationPath.Append("global");
     123   
     124    fIfGlobal = true;
     125
     126    fLocationSettings = new LocationSettings(fSettingsPath);
     127
     128    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     129
     130    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     131
     132    SetLayout(rootLayout);
     133
     134    BGroupView* firstGroup = new BGroupView(B_VERTICAL);
     135
     136    fIspMenu = new BMenu("My ISP");
     137
     138    fPreferredISP = new BMenuField("Connect On Demand :", fIspMenu);
     139
     140    firstGroup->GroupLayout()->AddView(fPreferredISP);
     141
     142    BGroupView* bboxGroup = new BGroupView(B_HORIZONTAL);
     143
     144    //
     145
     146    fNetworksMenuView = new NetworksMenuView();
     147
     148    bboxGroup->GroupLayout()->AddView(fNetworksMenuView);
     149
     150    // under the Networks Menu
     151
     152    BGroupView* useroptGroup = new BGroupView(B_HORIZONTAL);
     153
     154    fNetworkMenu = new BPopUpMenu("Network");
     155    fNetworkMenu->SetLabelFromMarked(false);
     156
     157    fNetworkMenuField = new BMenuField("Add :", fNetworkMenu); 
     158
     159    BMenuItem* fPPPoEMenu = new BMenuItem("PPPoE", new BMessage(kMsgPPPOE));
     160
     161    fNetworkMenu->AddItem(fPPPoEMenu);
     162
     163    BMenuItem* fVPNMenu = new BMenuItem("VPN", new BMessage(kMsgPPPOE));
     164
     165    fNetworkMenu->AddItem(fVPNMenu);
     166
     167    useroptGroup->GroupLayout()->AddView(fNetworkMenuField);
     168
     169    fMoveToMenu = new BPopUpMenu("Select a Location");
     170    fMoveToMenu->SetLabelFromMarked(false);
     171
     172    fMoveToMenuField = new BMenuField("Move To :", fMoveToMenu);
     173
     174    useroptGroup->GroupLayout()->AddView(fMoveToMenuField);
     175
     176    fRemoveButton = new BButton("Remove", new BMessage(kMsgRemoveNetwork));                               
     177    useroptGroup->GroupLayout()->AddView(fRemoveButton);
     178   
     179    _FindAndAddEthernetInterfaces();
     180
     181    rootLayout->AddView(firstGroup);
     182    rootLayout->AddView(bboxGroup);
     183    rootLayout->AddView(useroptGroup);
     184}
     185
     186
     187LocationView::~LocationView()
     188{
     189
     190}
     191
     192
     193void
     194LocationView::AttachedToWindow()
     195{
     196    fPreferredISP->Menu()->SetTargetForItems(this);
     197    fNetworkMenuField->Menu()->SetTargetForItems(this);
     198    fMoveToMenuField->Menu()->SetTargetForItems(this);
     199    fRemoveButton->SetTarget(this);
     200}
     201
     202
     203void
     204LocationView::DetachedFromWindow()
     205{
     206    fPreferredISP->Menu()->SetTargetForItems(this);
     207    fNetworkMenuField->Menu()->SetTargetForItems(this);
     208    fMoveToMenuField->Menu()->SetTargetForItems(this);
     209    fRemoveButton->SetTarget(this);
     210}
     211
     212
     213void
     214LocationView::SetGlobal(bool ifGlobal)
     215{
     216    fIfGlobal = ifGlobal;
     217}
     218
     219
     220bool
     221LocationView::IfGlobal()
     222{
     223    return fIfGlobal;
     224}
     225
     226
     227status_t
     228LocationView::DeleteLocationAndNetworks()
     229{
     230    fNetworksMenuView->DeleteAllNetworks();
     231    fLocationSettings->DeleteSettings();
     232    fLocationSettings->DeleteLocationDirectory(fLocationPath);
     233
     234    return B_OK;
     235}
     236
     237
     238void
     239LocationView::LocationAdded(const char* locationName, LocationView* newLocation)
     240{
     241    BMessage* message = new BMessage(kMsgMoveToLocation);
     242    message->AddPointer("location_pointer", (const void*) newLocation);
     243    BMenuItem* item = new BMenuItem(locationName, message);
     244    fMoveToMenu->AddItem(item);
     245    fMoveToMenu->SetTargetForItems(this);
     246}
     247
     248
     249void
     250LocationView::LocationRemoved(const char* locationName)
     251{
     252    BMenuItem* item = fMoveToMenu->FindItem(locationName);
     253    fMoveToMenu->RemoveItem(item);
     254}
     255
     256
     257void
     258LocationView::SetName(const char* locationName)
     259{
     260    fName = BString(locationName);
     261}
     262
     263
     264const char*
     265LocationView::GetName()
     266{
     267    return fName.String();
     268}
     269
     270
     271status_t
     272LocationView::MoveNetworkHere(NetworkTypeView* network)
     273{
     274    printf("LocationView::MoveNetworkHere\n");
     275    return fNetworksMenuView->MoveNetwork(network);
     276}
     277
     278const char*
     279LocationView::GetPath()
     280{
     281    return fLocationPath.Path();
     282}
     283
     284
     285void
     286LocationView::MessageReceived(BMessage* message)
     287{
     288    switch(message->what) {
     289
     290        case kMsgRemoveNetwork:
     291        {
     292            fNetworksMenuView->RemoveNetwork();
     293        }
     294        break;
     295
     296        case kMsgPPPOE:
     297        {
     298            TextEntryWindow* window = new TextEntryWindow();
     299            if(window == NULL)
     300                break;
     301
     302            const char* text = window->Go();
     303            if(text == NULL)
     304                break;
     305
     306            BString string = BString(text);
     307
     308            fNetworksMenuView->AddPPPoENetwork(string,
     309                fLocationPath);
     310        }
     311        break;
     312
     313        case kMsgMoveToLocation:
     314        {
     315            LocationView* location = NULL;
     316            status_t ret = 0;
     317
     318            ret = message->FindPointer("location_pointer", (void**) &location);
     319            if(ret < B_OK) {
     320                printf("LocationView: FindPointer %s\n", strerror(ret));
     321                break;
     322            }
     323            BPath path = BPath(fLocationPath.Path());
     324
     325            NetworkTypeView* view = fNetworksMenuView->GetSelectedNetwork();
     326            if(view == NULL) {
     327                printf("LocationView::NetworkTypeView* view is NULL\n");
     328                break;
     329            }
     330           
     331            if(view->IfHwDevice()) {
     332                BAlert* alert = new BAlert("Warning!",
     333                "The Hardware devices cannot be moved!\n",
     334                "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
     335                alert->Go();
     336                break; 
     337            }
     338
     339            path.Append(view->Name());
     340           
     341            ret = fLocationSettings->MoveNetworkFile(path,
     342                location->GetPath());
     343            if(ret < B_OK) {
     344                printf("LocationView::MessageReceived MoveNetworkFile %s \n",
     345                    strerror(ret));
     346                break;
     347            }
     348           
     349            path = location->GetPath();
     350            path.Append(view->Name());
     351           
     352            view->ChangeSettingsFilePath(path);
     353
     354            fNetworksMenuView->SelectedNetworkMoved();
     355            location->MoveNetworkHere(view);
     356        }
     357        break;
     358
     359    default:
     360        BView::MessageReceived(message);
     361    }
     362}
     363
     364
     365void
     366LocationView::_PopulateMoveToMenu(BObjectList<LocationView> &list)
     367{
     368    int count = list.CountItems();
     369    int i;
     370    LocationView* location;
     371
     372    for(i = 0; i < count; i++) {
     373        location = list.ItemAt(i);
     374        LocationAdded(location->GetName(), location);
     375    }
     376}
     377
     378
     379void
     380LocationView::_FindAndAddEthernetInterfaces()
     381{
     382    BObjectList<BString> adaptersList;
     383    fLocationSettings->_FindEthernetDevices(&adaptersList);
     384    int count = adaptersList.CountItems();
     385    int i;
     386
     387    for(i = 0; i < count; i++) {
     388        BString& adapter = *adaptersList.ItemAt(i);
     389        BPath adapterPath = BPath(fLocationPath);
     390        fNetworksMenuView->AddEthernetAdapter(adapter, fLocationPath);
     391    }
     392}
  • PPPoESettings.cpp

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8#include "PPPoESettings.h"
     9
     10PPPoESettings::PPPoESettings(BPath settingsPath)
     11    : Settings(settingsPath)
     12{
     13
     14}
     15
     16
     17PPPoESettings::~PPPoESettings()
     18{
     19
     20}
     21
     22
     23status_t
     24PPPoESettings::LoadSettings()
     25{
     26    return B_OK;
     27}
     28
     29
     30status_t
     31PPPoESettings::ApplySettings()
     32{
     33
     34    return B_OK;
     35}
     36
     37
     38status_t
     39PPPoESettings::SaveSettings()
     40{
     41
     42    return B_OK;
     43}
     44
     45void
     46PPPoESettings::ChangeSettingsFilePath(BPath path)
     47{
     48        fSettingsFile->SetTo(path.Path(), B_READ_WRITE
     49            | B_CREATE_FILE);
     50
     51        fSettingsPath = path;
     52}
  • settings.h

     
    1 /*
    2  * Copyright 2004-2007 Haiku Inc. All rights reserved.
    3  * Distributed under the terms of the MIT License.
    4  *
    5  * Author:
    6  *      Andre Alves Garzia, andre@andregarzia.com
    7  */
    8 #ifndef SETTINGS_H
    9 #define SETTINGS_H
    10 
    11 #include <ObjectList.h>
    12 #include <String.h>
    13 
    14 class Settings {
    15     public:
    16         Settings(const char* name);
    17         virtual ~Settings();
    18 
    19         void SetName(BString name);
    20         void SetIP(BString ip) {fIP = ip; }
    21         void SetGateway(BString ip) {fGateway = ip; }
    22         void SetNetmask(BString ip) {fNetmask = ip; }
    23         void SetAutoConfigure(bool t) {fAuto = t; }
    24        
    25         const char* GetIP()  {return fIP.String(); }
    26         const char* GetGateway()  {return fGateway.String(); }     
    27         const char* GetNetmask()  {return fNetmask.String(); }
    28         const char* GetName()  {return fName.String(); }
    29         bool GetAutoConfigure() {return fAuto; }
    30         BObjectList<BString> fNameservers;
    31         void ReadConfiguration();
    32 
    33        
    34     private:
    35         bool _PrepareRequest(struct ifreq& request);
    36         BString fIP;
    37         BString fGateway;
    38         BString fNetmask;
    39         BString fName;
    40         int fSocket;
    41         bool fAuto;
    42 };
    43 
    44 #endif /* SETTINGS_H */
  • LocationNameWindow.h

     
     1/*
     2 * Copyright 2004-2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo, Barrett666@gmail.com
     7 */
     8#ifndef NETWORK_WINDOW_H
     9#define NETWORK_WINDOW_H
     10
     11#include <Window.h>
     12
     13class BButton;
     14class BTextControl;
     15class NetworkView;
     16
     17class LocationNameWindow : public BWindow {
     18public:
     19                    LocationNameWindow(NetworkView* network);
     20    virtual         ~LocationNameWindow();
     21    virtual void    MessageReceived(BMessage* mesage);
     22
     23private:
     24    BTextControl*   fLocationNameControl;
     25    BButton*        fConfirmButton;
     26   
     27    NetworkView*    fNetworkView;
     28};
     29
     30#endif  /* NETWORK_WINDOW_H */
  • PPPoEView.cpp

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *
     8 *
     9 */
     10
     11#include "PPPoEView.h"
     12
     13#include <stdio.h>
     14
     15#include <GroupLayout.h>
     16#include <GroupView.h>
     17#include <Path.h>
     18#include <String.h>
     19#include <StringView.h>
     20#include <View.h>
     21
     22PPPoEView::PPPoEView(BPath path, BString networkName)
     23    : NetworkTypeView(networkName.String(), 0, NULL)
     24{
     25    fSettingsPath = BPath(path);
     26    fNetworkName = BString(networkName);
     27
     28    fSettings = new PPPoESettings(fSettingsPath);
     29
     30    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     31    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     32    SetLayout(rootLayout);
     33
     34    BGroupView* firstGroup = new BGroupView(B_HORIZONTAL);
     35
     36    BStringView* fStringView = new BStringView("strview", path.Path(),
     37        B_WILL_DRAW); // only for debugging
     38
     39    firstGroup->GroupLayout()->AddView(fStringView);
     40    rootLayout->AddView(firstGroup);
     41}
     42
     43
     44PPPoEView::~PPPoEView()
     45{
     46
     47}
     48
     49
     50void
     51PPPoEView::AttachedToWindow()
     52{
     53
     54}
     55
     56
     57void
     58PPPoEView::DetachedFromWindow()
     59{
     60
     61}
     62
     63
     64void
     65PPPoEView::DeleteNetworkAndSettings()
     66{
     67    fSettings->DeleteSettings();
     68}
     69
     70
     71void
     72PPPoEView::ChangeSettingsFilePath(BPath path)
     73{
     74    fSettingsPath = path;
     75    fSettings->ChangeSettingsFilePath(path);
     76    printf("PPPoEView::ChangeSettingsFilePath %s\n", fSettingsPath.Path());
     77}
     78
     79
     80void
     81PPPoEView::MessageReceived(BMessage* message)
     82{
     83    switch (message->what) {
     84
     85        default:
     86            BView::MessageReceived(message);
     87    }
     88}
  • NetworkSettings.h

     
     1/*
     2 * Copyright 2008 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#ifndef NETWORKS_SETTINGS_H
     10#define NETWORKS_SETTINGS_H
     11
     12#include <ObjectList.h>
     13#include <String.h>
     14
     15#include "Settings.h"
     16
     17class NetworkSettings : public Settings {
     18public:
     19                NetworkSettings(BPath settingsPath);
     20    virtual     ~NetworkSettings();
     21   
     22    status_t    MakeBaseDir();
     23
     24    status_t    LoadSettings();
     25    status_t    SaveSettings();
     26
     27    status_t    AutoConfigureAllSettings();
     28    bool        IfConfigured();
     29
     30    void        SetComputerName(const char* pcname) { fComputerName.SetTo(pcname); }
     31    const char* GetComputerName() { return fComputerName.String(); }
     32
     33private:
     34    const char* _ReadComputerName();
     35    void        _SaveComputerName();
     36
     37    BString     fComputerName;
     38    BPath       fSettingsPath;
     39    bool        fConfigured;
     40};
     41#endif /* NETWORKS_SETTINGS_H */
  • NetworkTypeView.cpp

     
     1/*
     2 * Copyright 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *
     8 *
     9 */
     10
     11#include <Message.h>
     12
     13
     14#include "NetworkTypeView.h"
     15
     16NetworkTypeView::NetworkTypeView(const char* name, uint32 flags, BLayout* layout)
     17    : BView(name, flags, layout)
     18{
     19    fName = BString(name);
     20    fIfDevice = false;
     21}
     22
     23
     24NetworkTypeView::~NetworkTypeView()
     25{
     26
     27}
     28
     29
     30void
     31NetworkTypeView::AttachedToWindow()
     32{
     33
     34}
     35
     36
     37void
     38NetworkTypeView::DetachedFromWindow()
     39{
     40
     41}
     42
     43
     44void
     45NetworkTypeView::DeleteNetworkAndSettings()
     46{
     47// non-pure virtual : )
     48}
     49
     50
     51
     52void
     53NetworkTypeView::ChangeSettingsFilePath(BPath path)
     54{
     55
     56}
     57
     58
     59void
     60NetworkTypeView::SetHwDevice(bool ifDevice)
     61{
     62    fIfDevice = ifDevice;
     63}
     64
     65
     66void
     67NetworkTypeView::MessageReceived(BMessage* message)
     68{
     69    switch (message->what) {
     70
     71        default:
     72            BView::MessageReceived(message);
     73    }
     74}
  • NetworkWindow.h

     
    1111
    1212#include <Window.h>
    1313
    14 #include "EthernetSettingsView.h"
     14#include "NetworkView.h"
    1515
    1616
    1717class NetworkWindow : public BWindow {
     
    2222    virtual void    MessageReceived(BMessage* mesage);
    2323
    2424private:
    25     EthernetSettingsView* fEthernetView;
     25    NetworkView*    fNetworkView;
    2626};
    2727
    2828#endif  /* NETWORK_WINDOW_H */
  • NetworkView.h

     
     1/*
     2 * Copyright 2008-2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario "Barrett" Casalinuovo - barrett666@gmail.com
     7 *     
     8 *     
     9 */
     10#ifndef NETWORK_VIEW_H
     11#define NETWORK_VIEW_H
     12
     13#include <ObjectList.h>
     14
     15class BBox;
     16class BCheckBox;
     17class BGroupView;
     18class BMenuField;
     19class BPopUpMenu;
     20class BTextControl;
     21class BView;
     22
     23class LocationView;
     24class NetworkSettings;
     25
     26class NetworkView : public BView {
     27  public:
     28                        NetworkView();
     29                        NetworkView(BMessage*);
     30        virtual         ~NetworkView();
     31
     32        void            MessageReceived(BMessage* message);
     33
     34        virtual void    AttachedToWindow();
     35        virtual void    DetachedFromWindow();
     36
     37  private:
     38        void            _InstallInDeskbar();
     39        int32           _IfInstalled();
     40        void            _RemoveFromDeskbar();
     41
     42        void            _BuildLocationsMenu();
     43        void            _BuildGlobalLocation();
     44
     45        status_t        _AddNewLocation(const char* locationName);
     46        status_t        _RestoreLocation(const char* locationName);
     47        status_t        _RemoveLocation();
     48        void            _HideAllLocations();
     49       
     50        status_t        _FindAndAddLocations();
     51
     52        void            _NotifyLocationRemoved(const char* locationName);
     53        void            _NotifyLocationAdded(const char*, LocationView*);
     54
     55        status_t        _SaveConfiguration();
     56        status_t        _LoadConfiguration();
     57        void            _SelectGlobalLocation();
     58   
     59  private:
     60        LocationView*   fCurrentLocation;
     61        BMenuItem*      fCurrentLocationMenu;
     62       
     63        BTextControl*   fComputerNameTextControl;
     64
     65        BMenuField*     fLocationsMenuField;
     66        BPopUpMenu*     fLocationsMenu;
     67       
     68        BGroupView*     fLocationsGroup;
     69
     70        BCheckBox*      fShowNetworkInDeskbar;
     71        BButton*        fRevertButton;
     72
     73        LocationView*   fGlobalLocationView;
     74        BMenuItem*      fGlobalMenuItem;
     75
     76        NetworkSettings*    fNetworkSettings;
     77        BObjectList<LocationView>   fLocationsList;
     78};
     79
     80#endif
  • NetworkApp.cpp

     
    2424void
    2525NetworkApp::ReadyToRun()
    2626{
    27     fEthWindow = new NetworkWindow();
    28     fEthWindow->Show();
     27    fPrefletWindow = new NetworkWindow();
     28    fPrefletWindow->Show();
    2929}
    3030
    3131int
  • NetworksMenuView.cpp

     
     1/*
     2 * Copyright 2008 - 2009 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Casalinuovo Dario, barrett666@gmail.com
     7 */
     8
     9#include <stdio.h>
     10
     11#include <Alert.h>
     12#include <Box.h>
     13#include <CheckBox.h>
     14#include <ColumnTypes.h>
     15#include <ColumnListView.h>
     16#include <GridView.h>
     17#include <GroupView.h>
     18#include <LayoutItem.h>
     19#include <StringView.h>
     20
     21#include "EthernetSettingsView.h"
     22#include "NetworksMenuView.h"
     23#include "PPPoEView.h"
     24
     25const uint32 kMsgCustomizePreferences = 'cprf';
     26const uint32 kMsgSelectedNetwork = 'slnw';
     27
     28NetworksMenuView::NetworksMenuView()
     29    : BView("EthernetNetworkView", 0, NULL)
     30{
     31    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     32
     33    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     34   
     35    SetLayout(rootLayout);
     36
     37    BGroupView* bboxGroup = new BGroupView(B_HORIZONTAL);
     38
     39    fNetworksBBox = new BBox("Networks");
     40    fNetworksBBox->SetLabel("Networks");
     41
     42    fNetworksList = new BColumnListView(BRect(5,15,164,175), "Networks List",
     43        B_FOLLOW_LEFT, B_WILL_DRAW, B_PLAIN_BORDER, false);
     44
     45    fNetworksList->AddColumn(new BStringColumn("Name", 100, 50, 500,
     46        B_TRUNCATE_MIDDLE), 0);
     47
     48    fNetworksList->AddColumn(new BStringColumn("State", 100, 50, 500,
     49        B_TRUNCATE_MIDDLE), 0);
     50
     51    fNetworksList->SetSelectionMessage(new BMessage(kMsgSelectedNetwork));
     52
     53    fNetworksBBox->AddChild(fNetworksList);
     54
     55    bboxGroup->GroupLayout()->AddView(fNetworksBBox);
     56
     57    fNtpGroup = new BGroupView(B_VERTICAL);
     58
     59    fNetworkPreferencesBBox = new BBox("Network Preferences");
     60    fNetworkPreferencesBBox->SetLabel("Network Preferences");
     61/* 
     62    fCustomizePreferencesCheckBox = new BCheckBox(
     63        "Customize Preferences for this location",
     64        new BMessage(kMsgCustomizePreferences));
     65
     66    fNetworkPreferencesBBox->AddChild(fCustomizePreferencesCheckBox);
     67
     68    fNetworkName = new BStringView("NetworkName","Name :");
     69    fNetworkStatus = new BStringView("NetworkStatus","Status :");
     70
     71    fNetworkPreferencesBBox->AddChild(fNetworkName);
     72    fNetworkPreferencesBBox->AddChild(fNetworkStatus);
     73*/
     74
     75    fNetworkPreferencesBBox->AddChild(fNtpGroup);
     76
     77    bboxGroup->GroupLayout()->AddView(fNetworkPreferencesBBox);
     78
     79    rootLayout->AddView(bboxGroup);
     80}
     81
     82
     83NetworksMenuView::~NetworksMenuView()
     84{
     85
     86}
     87
     88
     89status_t
     90NetworksMenuView::AddEthernetAdapter(BString adapterName, BPath fBasePath)
     91{
     92    _HideAllChilds();
     93    NRow* row = new NRow(20);
     94
     95    BString name = BString(adapterName);
     96    name.Remove(0,9);
     97    fBasePath.Append("/");
     98    fBasePath.Append(name.String());
     99
     100    EthernetSettingsView* ethernetView = new EthernetSettingsView(
     101        fBasePath, adapterName.String());
     102   
     103    ethernetView->SetHwDevice(true);
     104   
     105    fNtpGroup->AddChild(ethernetView);
     106    row->SetAssociatedView(ethernetView);
     107
     108    BStringField* strFeld = new BStringField(name.String());
     109    row->SetField(strFeld, 0);
     110    fNetworksList->AddRow(row, 1);
     111
     112    return B_OK;
     113}
     114
     115
     116status_t
     117NetworksMenuView::AddPPPoENetwork(BString adapterName, BPath fBasePath)
     118{
     119    _HideAllChilds();
     120    NRow* row = new NRow(20);
     121
     122    fBasePath.Append(adapterName.String());
     123
     124    PPPoEView* pppoeView = new PPPoEView(fBasePath, adapterName);
     125    fNtpGroup->AddChild(pppoeView);
     126
     127    row->SetAssociatedView(pppoeView);
     128
     129    BStringField* strFeld = new BStringField(adapterName.String());
     130    row->SetField(strFeld, 0);
     131    fNetworksList->AddRow(row, 1);
     132
     133    return B_OK;
     134}
     135
     136
     137status_t
     138NetworksMenuView::MoveNetwork(NetworkTypeView* network)
     139{
     140    printf("NetworksMenuView::MoveNetwork\n");
     141    if(network == NULL)
     142        return B_ERROR;
     143
     144    _HideAllChilds();
     145    NRow* row = new NRow(20);
     146
     147    fNtpGroup->AddChild(network);
     148    row->SetAssociatedView(network);
     149
     150    BStringField* strFeld = new BStringField(network->Name());
     151    row->SetField(strFeld, 0);
     152    fNetworksList->AddRow(row, 1);
     153
     154    return B_OK;
     155}
     156
     157
     158status_t
     159NetworksMenuView::DeleteAllNetworks()
     160{
     161    int i, count;   
     162    if(!fNetworksList)
     163        return B_OK;
     164
     165    count = fNetworksList->CountRows();
     166    for(i = 0; i < count; i++) {
     167        NRow* row = dynamic_cast<NRow*>(fNetworksList->RowAt(i));
     168        NetworkTypeView* view = row->GetAssociatedView();
     169        view->DeleteNetworkAndSettings();
     170    }
     171
     172    return B_OK;
     173}
     174
     175
     176status_t
     177NetworksMenuView::RemoveNetwork()
     178{
     179    NRow* selectedRow = dynamic_cast<NRow*>(
     180        fNetworksList->CurrentSelection());
     181    if(selectedRow == NULL)
     182        return B_ERROR;
     183   
     184    NetworkTypeView* associatedView = selectedRow->GetAssociatedView();
     185    if(associatedView == NULL)
     186        return B_ERROR;
     187
     188    if(associatedView->IfHwDevice()) {
     189        BAlert* alert = new BAlert("Warning!",
     190            "The Hardware devices cannot be deleted!\n",
     191            "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT);
     192        alert->Go();
     193        return B_OK;   
     194    }
     195
     196    fNtpGroup->RemoveChild(associatedView);
     197
     198    fNetworksList->RemoveRow(selectedRow);
     199
     200    BView* currentView = fNtpGroup->ChildAt(0);
     201
     202    if(currentView != NULL) {
     203        fNtpGroup->AddChild(currentView);
     204    } else {
     205        return B_ERROR;
     206    }
     207
     208    associatedView->DeleteNetworkAndSettings();
     209    delete associatedView;
     210
     211    return B_OK;
     212}
     213
     214
     215status_t
     216NetworksMenuView::SelectedNetworkMoved()
     217{
     218    NRow* selectedRow = dynamic_cast<NRow*>(
     219        fNetworksList->CurrentSelection());
     220    if(selectedRow == NULL)
     221        return B_ERROR;
     222
     223    NetworkTypeView* associatedView = selectedRow->GetAssociatedView();
     224
     225    printf("NetworksMenuView::SelectedNetworkMoved %p\n", associatedView);
     226
     227    if(associatedView == NULL)
     228        return B_ERROR;
     229
     230    fNtpGroup->RemoveChild(associatedView);
     231
     232    fNetworksList->RemoveRow(selectedRow);
     233
     234    BView* currentView = fNtpGroup->ChildAt(0);
     235
     236    if(currentView != NULL) {
     237        fNtpGroup->AddChild(currentView);
     238    } else {
     239        return B_ERROR;
     240    }
     241
     242    return B_OK;
     243}
     244
     245NetworkTypeView*
     246NetworksMenuView::GetSelectedNetwork()
     247{
     248    NRow* selectedRow = dynamic_cast<NRow*>(
     249        fNetworksList->CurrentSelection());
     250    if(selectedRow == NULL)
     251        return NULL;
     252
     253    return selectedRow->GetAssociatedView();
     254}
     255
     256void
     257NetworksMenuView::AttachedToWindow()
     258{
     259    fNetworksList->SetTarget(this);
     260    //fCustomizePreferencesCheckBox->SetTarget(this);
     261}
     262
     263
     264void
     265NetworksMenuView :: DetachedFromWindow()
     266{
     267
     268}
     269
     270
     271void
     272NetworksMenuView :: MessageReceived(BMessage* message)
     273{
     274    switch(message->what) {
     275
     276        case kMsgSelectedNetwork:
     277        {       
     278            BRow* selected = fNetworksList->CurrentSelection();
     279
     280            if(selected == NULL)
     281                break;
     282
     283            NRow* selectedRow = (NRow*)selected;
     284
     285            if(selected == NULL)
     286                break;
     287
     288            BView* associatedView = selectedRow->GetAssociatedView();
     289           
     290            _HideAllChilds();
     291
     292            if(associatedView == NULL)
     293                break;
     294
     295            fNtpGroup->AddChild(associatedView);
     296        }
     297        break;
     298    }
     299}
     300
     301
     302void
     303NetworksMenuView::_HideAllChilds()
     304{
     305    int32 childrens = fNtpGroup->CountChildren();
     306    int i;
     307
     308    for(i = 0; i < childrens; i++) {   
     309        BView* currview = fNtpGroup->ChildAt(i);
     310
     311        if(currview != NULL) {
     312            fNtpGroup->RemoveChild(currview);
     313        } else {
     314            break;
     315        }
     316    }
     317}
     318
     319//
     320
     321NRow::NRow(float height)
     322    : BRow(height)
     323{
     324
     325}
     326
     327
     328void
     329NRow::SetAssociatedView(NetworkTypeView* view)
     330{
     331    fAssociatedView = view;
     332}
     333
     334
     335NetworkTypeView*
     336NRow::GetAssociatedView()
     337{
     338    return fAssociatedView;
     339}
     340
     341
     342void
     343NRow::SetType(const char* type)
     344{
     345    fType = type;
     346}
     347
     348
     349const char*
     350NRow::GetType()
     351{
     352    return fType;
     353}