From d37cd1e172d83dc095ca5cbd08832861a6ce15eb Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 7 Jan 2015 09:27:31 +0100 Subject: [PATCH] Network preferences: add a checkbox to control NetworkStatus Fixes #3314. Based on Barrett initial patch, reworked for the current network preferences implementation. --- .../network/NetworkSetupWindow.cpp | 56 +++++++++++++++++-- src/preferences/network/NetworkSetupWindow.h | 4 ++ 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/src/preferences/network/NetworkSetupWindow.cpp b/src/preferences/network/NetworkSetupWindow.cpp index 91227d5c26..902d46e6fe 100644 --- a/src/preferences/network/NetworkSetupWindow.cpp +++ b/src/preferences/network/NetworkSetupWindow.cpp @@ -1,9 +1,10 @@ /* - * Copyright 2004-2011 Haiku Inc. All rights reserved. + * Copyright 2004-2015 Haiku Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: * Alexander von Gluck, + * Adrien Destugues, */ @@ -12,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -61,6 +63,11 @@ NetworkSetupWindow::NetworkSetupWindow(const char *title) new BMessage(kMsgRevert)); // fRevertButton->SetEnabled(false); + BMessage* message = new BMessage(kMsgToggleReplicant); + BCheckBox* replicantStatus = new BCheckBox("replicantStatus", + B_TRANSLATE("Show interfaces status in Deskbar"), message); + replicantStatus->SetValue(_IsReplicantInstalled()); + // Build the layout SetLayout(new BGroupLayout(B_VERTICAL)); @@ -75,6 +82,7 @@ NetworkSetupWindow::NetworkSetupWindow(const char *title) .Add(fPanel) .Add(new BGroupView("panel")) .End() + .Add(replicantStatus) .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING) .Add(fRevertButton) .AddGlue() @@ -106,9 +114,9 @@ NetworkSetupWindow::QuitRequested() void -NetworkSetupWindow::MessageReceived(BMessage* msg) +NetworkSetupWindow::MessageReceived(BMessage* message) { - switch (msg->what) { + switch (message->what) { case kMsgProfileNew: break; @@ -118,7 +126,7 @@ NetworkSetupWindow::MessageReceived(BMessage* msg) bool is_default; bool is_current; - if (msg->FindString("path", &path) != B_OK) + if (message->FindString("path", &path) != B_OK) break; name.SetTo(path); @@ -139,7 +147,6 @@ NetworkSetupWindow::MessageReceived(BMessage* msg) break; } - case kMsgApply: { for (int addonIndex = 0; addonIndex < fAddonCount; addonIndex++) { NetworkSetupAddOn* addon @@ -149,8 +156,12 @@ NetworkSetupWindow::MessageReceived(BMessage* msg) break; } + case kMsgToggleReplicant: { + _ShowReplicant(message->FindInt32("be:value")); + } + default: - inherited::MessageReceived(msg); + inherited::MessageReceived(message); } } @@ -298,3 +309,36 @@ NetworkSetupWindow::_BuildShowTabView() free(search_paths); } + + +void +NetworkSetupWindow::_ShowReplicant(bool show) +{ + if (show) { + char* argv[] = {const_cast("--deskbar"), NULL}; + + status_t ret = be_roster->Launch( + "application/x-vnd.Haiku-NetworkStatus", 1, argv); + + if (ret != B_OK) { + BString errorMessage; + errorMessage.SetToFormat( + B_TRANSLATE("Installing NetworkStatus in Deskbar failed: %s"), + strerror(ret)); + BAlert* alert = new BAlert(B_TRANSLATE("launch error"), + errorMessage, B_TRANSLATE("Ok")); + alert->Go(NULL); + } + } else { + BDeskbar deskbar; + deskbar.RemoveItem("NetworkStatus"); + } +} + + +bool +NetworkSetupWindow::_IsReplicantInstalled() +{ + BDeskbar deskbar; + return deskbar.HasItem("NetworkStatus"); +} diff --git a/src/preferences/network/NetworkSetupWindow.h b/src/preferences/network/NetworkSetupWindow.h index 6d8ecb4cd9..320f4aa851 100644 --- a/src/preferences/network/NetworkSetupWindow.h +++ b/src/preferences/network/NetworkSetupWindow.h @@ -38,6 +38,7 @@ class NetworkSetupWindow : public BWindow static const uint32 kMsgProfileNew = 'newp'; static const uint32 kMsgApply = 'aply'; static const uint32 kMsgRevert = 'rvrt'; + static const uint32 kMsgToggleReplicant = 'trep'; bool QuitRequested(); void MessageReceived(BMessage* msg); @@ -46,6 +47,9 @@ class NetworkSetupWindow : public BWindow void _BuildProfilesMenu(BMenu* menu, int32 msg); void _BuildShowTabView(); + bool _IsReplicantInstalled(); + void _ShowReplicant(bool show); + BButton* fRevertButton; BButton* fApplyButton;