RemoteControlSettings.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "ui/RemoteControlSettings.h"
  2. #include "platform/Paths.h"
  3. #include "ui/I18n.h"
  4. #include "ui/DemoMode.h"
  5. #include <optional>
  6. namespace lynx::remoteui
  7. {
  8. namespace
  9. {
  10. std::optional<bool> requested;
  11. bool running = false;
  12. juce::String detail;
  13. std::function<void (bool)> change;
  14. juce::File settingsFile() { return platform::settingsFile ("remote-control.txt"); }
  15. }
  16. bool enabled()
  17. {
  18. if (demo::active()) // (round 190np: --demo drives LYNX through the API)
  19. return true;
  20. if (! requested)
  21. {
  22. const auto forced = juce::SystemStats::getEnvironmentVariable ("LYNX_CONTROL_ENABLED", {}).trim();
  23. requested = forced.isNotEmpty() ? forced == "1" : settingsFile().loadFileAsString().trim() == "1";
  24. }
  25. return *requested;
  26. }
  27. bool active() { return running; }
  28. juce::String status()
  29. {
  30. if (detail.isNotEmpty()) return detail;
  31. return juce::String::fromUTF8 (running ? TR_NOOP ("Remote control active — local clients can read and edit this project.")
  32. : enabled() ? TR_NOOP ("Remote control could not start. Disable and re-enable to retry.")
  33. : TR_NOOP ("Remote control is off."));
  34. }
  35. void setEnabled (bool on)
  36. {
  37. if (on == enabled()) return;
  38. if (! settingsFile().replaceWithText (on ? "1\n" : "0\n"))
  39. {
  40. if (! on)
  41. {
  42. requested = false;
  43. if (change) change (false);
  44. }
  45. detail = juce::String::fromUTF8 (TR_NOOP ("The remote control setting could not be saved."));
  46. return;
  47. }
  48. requested = on;
  49. detail = {};
  50. if (change) change (on);
  51. }
  52. void setRuntimeStatus (bool on, const juce::String& message)
  53. {
  54. running = on;
  55. detail = message;
  56. }
  57. void install (std::function<void (bool)> enable)
  58. {
  59. change = std::move (enable);
  60. if (change) change (enabled());
  61. }
  62. void shutdown()
  63. {
  64. change = {};
  65. running = false;
  66. }
  67. }