From 8573ab2cd3e44a3ff5ed7783edd6d3846ea1464d Mon Sep 17 00:00:00 2001 From: Kenneth Hsu Date: Tue, 3 Oct 2017 18:53:28 -0700 Subject: [PATCH] Add ability to detect power supply source on Windows. --- .../Utility/Duplicati.Library.Utility.csproj | 2 + .../Library/Utility/Power/PowerSupply.cs | 4 ++ .../Utility/Power/WindowsPowerSupplyState.cs | 46 +++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 Duplicati/Library/Utility/Power/WindowsPowerSupplyState.cs diff --git a/Duplicati/Library/Utility/Duplicati.Library.Utility.csproj b/Duplicati/Library/Utility/Duplicati.Library.Utility.csproj index de498b25c..6d93e7fc6 100644 --- a/Duplicati/Library/Utility/Duplicati.Library.Utility.csproj +++ b/Duplicati/Library/Utility/Duplicati.Library.Utility.csproj @@ -43,6 +43,7 @@ ..\..\..\packages\FasterHashing.1.2.1\lib\net45\FasterHashing.dll + @@ -80,6 +81,7 @@ + diff --git a/Duplicati/Library/Utility/Power/PowerSupply.cs b/Duplicati/Library/Utility/Power/PowerSupply.cs index ea54167f6..ff5deadb6 100644 --- a/Duplicati/Library/Utility/Power/PowerSupply.cs +++ b/Duplicati/Library/Utility/Power/PowerSupply.cs @@ -39,6 +39,10 @@ namespace Duplicati.Library.Utility.Power { state = new LinuxPowerSupplyState(); } + else if (Utility.IsClientWindows) + { + state = new WindowsPowerSupplyState(); + } else { state = new DefaultPowerSupplyState(); diff --git a/Duplicati/Library/Utility/Power/WindowsPowerSupplyState.cs b/Duplicati/Library/Utility/Power/WindowsPowerSupplyState.cs new file mode 100644 index 000000000..c1ffa620a --- /dev/null +++ b/Duplicati/Library/Utility/Power/WindowsPowerSupplyState.cs @@ -0,0 +1,46 @@ +// Copyright (C) 2017, The Duplicati Team +// http://www.duplicati.com, info@duplicati.com +// +// This library is free software; you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as +// published by the Free Software Foundation; either version 2.1 of the +// License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +using System.Windows.Forms; + +namespace Duplicati.Library.Utility.Power +{ + public class WindowsPowerSupplyState : IPowerSupplyState + { + public PowerSupply.Source GetSource() + { + try + { + PowerLineStatus status = System.Windows.Forms.SystemInformation.PowerStatus.PowerLineStatus; + if (status == PowerLineStatus.Online) + { + return PowerSupply.Source.AC; + } + if (status == PowerLineStatus.Offline) + { + return PowerSupply.Source.Battery; + } + + return PowerSupply.Source.Unknown; + } + catch + { + return PowerSupply.Source.Unknown; + } + } + } +}