| 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 | |
| 29 | static const uint32 kMsgInstallInDeskbar = 'iidb'; |
| 30 | static const uint32 kMsgSetName = 'stnm'; |
| 31 | |
| 32 | NetworkView::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 | |
| 75 | NetworkView::~NetworkView() |
| 76 | { |
| 77 | BString str = fComputerNameTextControl->Text(); |
| 78 | if(str != fComputerName) |
| 79 | _SaveComputerName(str.String()); |
| 80 | } |
| 81 | |
| 82 | |
| 83 | void |
| 84 | NetworkView::AttachedToWindow() |
| 85 | { |
| 86 | fShowNetworkInDeskbar->SetTarget(this); |
| 87 | fComputerNameTextControl->SetTarget(this); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | void |
| 92 | NetworkView::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 |
| 121 | const char* |
| 122 | NetworkView::_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 | |
| 155 | void |
| 156 | NetworkView::_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 | |
| 169 | void |
| 170 | NetworkView::_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 | |
| 189 | int32 |
| 190 | NetworkView::_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 | |
| 201 | void |
| 202 | NetworkView::_RemoveFromDeskbar() |
| 203 | { |
| 204 | BDeskbar deskbar; |
| 205 | deskbar.RemoveItem("NetworkStatus"); |
| 206 | } |