Parcourir la source

Add option to hide power options from the exit dialog.

Users requested a way to disable the power options on their machines
since they have smaller kids or clueless people that might power
down or suspend the machine unintentionally. I opted to make this a
hidden option since it's mostly for power users.

This required a small refactoring since we wanted a central place
to check that setting instead of in every sub power component. I
chose to use the capabilites flags instead of doing the several
different methods, this seemed cleaner for the subclasses.
Tobias Hieta il y a 8 ans
Parent
commit
e168dac0a1

+ 5 - 0
resources/settings/settings_description.json

@@ -99,6 +99,11 @@
           ["minimized", "minimized"]
         ]
       },
+      {
+        "value": "showPowerOptions",
+        "default": true,
+        "hidden": true
+      },
       {
         // Warning: we should make this hidden or remove it later on
         "value": "useSystemVideoCodecs",

+ 12 - 0
src/power/PowerComponent.cpp

@@ -5,6 +5,7 @@
 #include "PowerComponent.h"
 #include "player/PlayerComponent.h"
 #include "input/InputComponent.h"
+#include "settings/SettingsComponent.h"
 
 #ifdef Q_OS_MAC
 #include "PowerComponentMac.h"
@@ -100,3 +101,14 @@ void PowerComponent::componentPostInitialize()
   InputComponent::Get().registerHostCommand("reboot", this, "Reboot");
   InputComponent::Get().registerHostCommand("suspend", this, "Suspend");
 }
+
+/////////////////////////////////////////////////////////////////////////////////////////
+bool PowerComponent::checkCap(PowerCapabilities capability)
+{
+  if (!SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "showPowerOptions").toBool())
+    return false;
+
+  return (getPowerCapabilities() & capability);
+}
+
+

+ 12 - 8
src/power/PowerComponent.h

@@ -11,10 +11,10 @@ public:
 
   enum PowerCapabilities
   {
-    CAP_POWER_OFF,
-    CAP_REBOOT,
-    CAP_SUSPEND,
-    CAP_RELAUNCH
+    CAP_POWER_OFF = 0x01,
+    CAP_REBOOT = 0x02,
+    CAP_SUSPEND = 0x04,
+    CAP_RELAUNCH = 0x08
   };
 
   static PowerComponent& Get();
@@ -34,10 +34,14 @@ public:
   void setFullscreenState(bool fullscreen);
 
 public Q_SLOTS:
-  virtual bool canPowerOff() { return false; }
-  virtual bool canReboot() { return false; }
-  virtual bool canSuspend() { return false; }
-  virtual bool canRelaunch() { return false; }
+  bool checkCap(PowerCapabilities capability);
+
+  bool canPowerOff() { return checkCap(CAP_POWER_OFF); }
+  bool canReboot() { return checkCap(CAP_REBOOT); }
+  bool canSuspend() { return checkCap(CAP_SUSPEND); }
+  bool canRelaunch() { return checkCap(CAP_RELAUNCH); }
+
+  virtual int getPowerCapabilities() { return CAP_RELAUNCH; }
 
   virtual bool PowerOff() { return false; }
   virtual bool Reboot() { return false; }

+ 5 - 3
src/power/PowerComponentMac.h

@@ -11,9 +11,11 @@ public:
   void doDisableScreensaver() override;
   void doEnableScreensaver() override;
 
-  virtual bool canPowerOff() override { return true; }
-  virtual bool canReboot() override { return true; }
-  virtual bool canSuspend() override { return IOPMSleepEnabled(); }
+  virtual int getPowerCapabilities() override
+  {
+    int flags = IOPMSleepEnabled() ? CAP_SUSPEND : 0;
+    return flags | CAP_POWER_OFF | CAP_REBOOT;
+  }
 
   virtual bool PowerOff() override;
   virtual bool Reboot() override;

+ 12 - 4
src/power/PowerComponentOE.h

@@ -10,10 +10,18 @@ class PowerComponentOE : public PowerComponent
     ~PowerComponentOE() {};
 
   public Q_SLOTS:
-    virtual bool canPowerOff() { return isPowerMethodAvailable("CanPowerOff"); }
-    virtual bool canReboot() { return isPowerMethodAvailable("CanReboot"); }
-    virtual bool canSuspend() { return isPowerMethodAvailable("CanSuspend"); }
-    virtual bool canRelaunch() { return true; }
+
+  virtual int getPowerCapabilities() override
+  {
+    int flags = 0;
+    if (isPowerMethodAvailable("CanPowerOff"))
+      flags |= CAP_POWER_OFF;
+    if (isPowerMethodAvailable("CanReboot"))
+      flags |= CAP_REBOOT;
+    if (isPowerMethodAvailable("CanSuspend"))
+      flags |= CAP_SUSPEND;
+    return flags;
+  }
 
     virtual bool PowerOff() { return callPowerMethod("PowerOff"); }
     virtual bool Reboot() { return callPowerMethod("Reboot"); }

+ 7 - 3
src/power/PowerComponentWin.h

@@ -11,9 +11,13 @@ public:
   virtual void doDisableScreensaver();
   virtual void doEnableScreensaver();
 
-  virtual bool canPowerOff() override { return m_hasPrivileges; }
-  virtual bool canReboot() override { return m_hasPrivileges; }
-  virtual bool canSuspend() override { return true; }
+  virtual int getPowerCapabilities() override
+  {
+    int flags = CAP_SUSPEND;
+    if (m_hasPrivileges)
+      flags |= CAP_POWER_OFF | CAP_REBOOT;
+    return flags;
+  }
 
   virtual bool PowerOff() override;
   virtual bool Reboot() override;