Ticket #3314: deskbarnetw.diff

File deskbarnetw.diff, 8.2 KB (added by Barrett, 13 years ago)

updated patch for ticket #3314 and little improvements.

  • src/preferences/network/NetworkWindow.cpp

     
    44 *
    55 * Author:
    66 *      Andre Alves Garzia, andre@andregarzia.com
     7 *      Dario Casalinuovo
    78 */
    8 
    9 
    109#include "NetworkWindow.h"
    1110
    1211#include <Application.h>
     
    1413#include <GroupLayout.h>
    1514#include <Locale.h>
    1615
    17 #include "EthernetSettingsView.h"
     16#include <stdio.h>
    1817
     18#include "NetworkView.h"
    1919
    2020#undef B_TRANSLATE_CONTEXT
    2121#define B_TRANSLATE_CONTEXT "NetworkWindow"
    2222
    23 
    2423NetworkWindow::NetworkWindow()
    25     :
    26     BWindow(BRect(50, 50, 269, 302), B_TRANSLATE("Network"), B_TITLED_WINDOW,
    27         B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
    28         | B_AUTO_UPDATE_SIZE_LIMITS | B_QUIT_ON_WINDOW_CLOSE)
     24    : BWindow(BRect(50, 50, 300, 400), "Network", B_TITLED_WINDOW,
     25        B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE
     26        | B_AUTO_UPDATE_SIZE_LIMITS |  B_QUIT_ON_WINDOW_CLOSE)
    2927{
    3028    SetLayout(new BGroupLayout(B_HORIZONTAL));
    31     GetLayout()->AddView(new EthernetSettingsView());
     29    fNetworkView = new NetworkView();
     30    GetLayout()->AddView(fNetworkView);
    3231}
  • src/preferences/network/NetworkView.cpp

     
     1/*
     2 * Copyright 2008-2010 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario Casalinuovo     
     7 *     
     8 */
     9#include "NetworkView.h"
     10
     11#include <Alert.h>
     12#include <Box.h>
     13#include <Button.h>
     14#include <CheckBox.h>
     15#include <Deskbar.h>
     16#include <File.h>
     17#include <GridView.h>
     18#include <GroupView.h>
     19#include <LayoutItem.h>
     20#include <SpaceLayoutItem.h>
     21#include <Roster.h>
     22#include <FindDirectory.h>
     23#include <TextControl.h>
     24#include <View.h>
     25
     26#include <stdio.h>
     27#include <stdlib.h>
     28
     29static const uint32 kMsgInstallInDeskbar = 'iidb';
     30static const uint32 kMsgSetName = 'stnm';
     31
     32NetworkView::NetworkView()
     33    : BView("NetworkView", 0, NULL)
     34{
     35    SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
     36    BGroupLayout* rootLayout = new BGroupLayout(B_VERTICAL);
     37    SetLayout(rootLayout);
     38    BGridView* informationsGroup = new BGridView();
     39    BGridLayout* layout = informationsGroup->GridLayout();
     40
     41    // insets
     42
     43    float inset = ceilf(be_plain_font->Size() * 0.7);
     44    rootLayout->SetInsets(inset, inset, inset, inset);
     45    rootLayout->SetSpacing(inset);
     46    layout->SetSpacing(inset, inset);
     47
     48    fComputerNameTextControl = new BTextControl("Computer Name :", "",
     49        new BMessage(kMsgSetName));
     50
     51    layout->AddItem(fComputerNameTextControl->CreateLabelLayoutItem(),
     52        0, 1.5);
     53    layout->AddItem(fComputerNameTextControl->CreateTextViewLayoutItem(),
     54        1, 1.5);
     55
     56    fComputerNameTextControl->SetText(_ReadComputerName());
     57
     58    BGroupView* guiGroup = new BGroupView(B_VERTICAL);
     59    fPreferencesBox = new BBox("Preferences", B_WILL_DRAW,
     60        B_FANCY_BORDER, guiGroup);
     61
     62    EthernetSettingsView* fEthernetView = new EthernetSettingsView();
     63    guiGroup->GroupLayout()->AddView(fEthernetView);
     64
     65    fShowNetworkInDeskbar = new BCheckBox("Show Network icon in Deskbar.",
     66        new BMessage(kMsgInstallInDeskbar));
     67    fShowNetworkInDeskbar->SetValue(_IsInstalled());
     68
     69    rootLayout->AddView(informationsGroup);
     70    rootLayout->AddView(fPreferencesBox);
     71    rootLayout->AddView(fShowNetworkInDeskbar);
     72}
     73
     74
     75NetworkView::~NetworkView()
     76{
     77    BString str = fComputerNameTextControl->Text();
     78    if(str != fComputerName)
     79        _SaveComputerName(str.String());
     80}
     81
     82
     83void
     84NetworkView::AttachedToWindow()
     85{
     86    fShowNetworkInDeskbar->SetTarget(this);
     87    fComputerNameTextControl->SetTarget(this);
     88}
     89
     90
     91void
     92NetworkView::MessageReceived(BMessage* message)
     93{
     94    switch (message->what) {
     95   
     96        case kMsgInstallInDeskbar:
     97        {
     98            if (fShowNetworkInDeskbar->Value() == B_CONTROL_ON) {
     99                _InstallInDeskbar();
     100            } else if (fShowNetworkInDeskbar->Value() == B_CONTROL_OFF) {
     101                _RemoveFromDeskbar();   
     102            }
     103            break;
     104        }   
     105
     106        case kMsgSetName:
     107        {
     108            _SaveComputerName(fComputerNameTextControl->Text());
     109            fComputerNameTextControl->MakeFocus(false);
     110            break;
     111        }
     112       
     113        default:
     114            BView::MessageReceived(message);
     115    }
     116}
     117
     118
     119// TODO: The methods _ReadComputerName() and _SaveComputerName()
     120// should be removed from here
     121const char*
     122NetworkView::_ReadComputerName()
     123{
     124    BFile hostname;
     125    fComputerName = BString();
     126    char buf[128];
     127    ssize_t ret;
     128
     129    find_directory(B_COMMON_SETTINGS_DIRECTORY,
     130        &fHostNamePath, true);
     131
     132    fHostNamePath.Append("network/hostname");
     133    hostname = BFile(fHostNamePath.Path(),
     134        B_READ_ONLY);
     135   
     136    if (hostname.InitCheck() != B_OK)
     137        fprintf(stderr, "failed to open the file %s: %s\n",
     138            fHostNamePath.Path(), strerror(hostname.InitCheck()));
     139   
     140    ret = hostname.Read((void*) buf, 128);
     141
     142    if (ret < 0)
     143        fprintf(stderr, "failed to read the file %s: %s\n",
     144            fHostNamePath.Path(), strerror(hostname.InitCheck()));
     145
     146    if (ret > 0)
     147        buf[ret] = '\0';
     148
     149    fComputerName.Append(buf);
     150
     151    return fComputerName.String();
     152}
     153
     154
     155void
     156NetworkView::_SaveComputerName(const char* name)
     157{
     158    BFile file(fHostNamePath.Path(),
     159        B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
     160   
     161    if (file.InitCheck() != B_OK)
     162        fprintf(stderr, "failed to open the file %s: %s\n",
     163            fHostNamePath.Path(), strerror(file.InitCheck()));
     164
     165    file.Write(name, strlen(name));
     166}
     167
     168
     169void
     170NetworkView::_InstallInDeskbar()
     171{
     172    char* argv[] = {const_cast<char *>("--deskbar"), NULL};
     173
     174    status_t ret = be_roster->Launch(
     175        "application/x-vnd.Haiku-NetworkStatus", 1, argv);
     176
     177        if (ret != B_OK) {
     178            BString errorMessage(
     179                "Installing NetworkStatus in Deskbar failed.\n\n"
     180                    "Error: ");
     181            errorMessage << strerror(ret);
     182            BAlert* alert = new BAlert("launch error",
     183                errorMessage.String(), "Ok");
     184            alert->Go(NULL);
     185        }
     186}
     187
     188
     189int32
     190NetworkView::_IsInstalled()
     191{
     192    BDeskbar deskbar;
     193    bool ret = deskbar.HasItem("NetworkStatus");
     194    if (ret == false)
     195        return 0;
     196
     197    return 1;
     198}
     199
     200
     201void
     202NetworkView::_RemoveFromDeskbar()
     203{
     204    BDeskbar deskbar;
     205    deskbar.RemoveItem("NetworkStatus");
     206}
  • src/preferences/network/Jamfile

     
    66Preference Network :
    77    NetworkApp.cpp
    88    NetworkWindow.cpp
     9    NetworkView.cpp
    910    EthernetSettingsView.cpp
    1011    Settings.cpp
    1112
  • src/preferences/network/NetworkWindow.h

     
    1111
    1212#include <Window.h>
    1313
     14#include "NetworkView.h"
    1415
     16
    1517class NetworkWindow : public BWindow {
    1618public:
    17                                 NetworkWindow();
     19                    NetworkWindow();
     20
     21private:
     22    NetworkView*    fNetworkView;
    1823};
    1924
    20 
    21 #endif  // NETWORK_WINDOW_H
     25#endif  /* NETWORK_WINDOW_H */
  • src/preferences/network/NetworkView.h

     
     1/*
     2 * Copyright 2008-2010 Haiku Inc. All rights reserved.
     3 * Distributed under the terms of the MIT License.
     4 *
     5 * Author:
     6 *      Dario Casalinuovo
     7 *     
     8 *     
     9 */
     10#ifndef NETWORK_VIEW_H
     11#define NETWORK_VIEW_H
     12
     13#include <Path.h>
     14#include <String.h>
     15#include <View.h>
     16
     17#include "EthernetSettingsView.h"
     18
     19class BBox;
     20class BButton;
     21class BCheckBox;
     22class BPath;
     23class BTextControl;
     24
     25
     26class NetworkView : public BView {
     27public:
     28                    NetworkView();
     29                    NetworkView(BMessage*);
     30    virtual         ~NetworkView();
     31
     32    void            MessageReceived(BMessage* message);
     33
     34    virtual void    AttachedToWindow();
     35
     36private:
     37    void            _InstallInDeskbar();
     38    int32           _IsInstalled();
     39    void            _RemoveFromDeskbar();
     40   
     41    const char*     _ReadComputerName();
     42    void            _SaveComputerName(const char* name);
     43
     44    BTextControl*   fComputerNameTextControl;
     45    BBox*           fPreferencesBox;
     46    BCheckBox*      fShowNetworkInDeskbar;
     47
     48    BPath           fHostNamePath;
     49    BString         fComputerName;
     50
     51    EthernetSettingsView* fEthernetView;
     52};
     53
     54#endif