ThemesService.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\Theming\Service;
  24. use OCA\Theming\AppInfo\Application;
  25. use OCA\Theming\ITheme;
  26. use OCA\Theming\Themes\DarkHighContrastTheme;
  27. use OCA\Theming\Themes\DarkTheme;
  28. use OCA\Theming\Themes\DefaultTheme;
  29. use OCA\Theming\Themes\DyslexiaFont;
  30. use OCA\Theming\Themes\HighContrastTheme;
  31. use OCA\Theming\Themes\LightTheme;
  32. use OCP\IConfig;
  33. use OCP\IUser;
  34. use OCP\IUserSession;
  35. class ThemesService {
  36. private IUserSession $userSession;
  37. private IConfig $config;
  38. /** @var ITheme[] */
  39. private array $themesProviders;
  40. public function __construct(IUserSession $userSession,
  41. IConfig $config,
  42. DefaultTheme $defaultTheme,
  43. LightTheme $lightTheme,
  44. DarkTheme $darkTheme,
  45. HighContrastTheme $highContrastTheme,
  46. DarkHighContrastTheme $darkHighContrastTheme,
  47. DyslexiaFont $dyslexiaFont) {
  48. $this->userSession = $userSession;
  49. $this->config = $config;
  50. // Register themes
  51. $this->themesProviders = [
  52. $defaultTheme->getId() => $defaultTheme,
  53. $lightTheme->getId() => $lightTheme,
  54. $darkTheme->getId() => $darkTheme,
  55. $highContrastTheme->getId() => $highContrastTheme,
  56. $darkHighContrastTheme->getId() => $darkHighContrastTheme,
  57. $dyslexiaFont->getId() => $dyslexiaFont,
  58. ];
  59. }
  60. /**
  61. * Get the list of all registered themes
  62. *
  63. * @return ITheme[]
  64. */
  65. public function getThemes(): array {
  66. return $this->themesProviders;
  67. }
  68. /**
  69. * Enable a theme for the logged-in user
  70. *
  71. * @param ITheme $theme the theme to enable
  72. * @return string[] the enabled themes
  73. */
  74. public function enableTheme(ITheme $theme): array {
  75. $themesIds = $this->getEnabledThemes();
  76. // If already enabled, ignore
  77. if (in_array($theme->getId(), $themesIds)) {
  78. return $themesIds;
  79. }
  80. /** @var ITheme[] */
  81. $themes = array_filter(array_map(function ($themeId) {
  82. return $this->getThemes()[$themeId];
  83. }, $themesIds));
  84. // Filtering all themes with the same type
  85. $filteredThemes = array_filter($themes, function (ITheme $t) use ($theme) {
  86. return $theme->getType() === $t->getType();
  87. });
  88. // Retrieve IDs only
  89. /** @var string[] */
  90. $filteredThemesIds = array_map(function (ITheme $t) {
  91. return $t->getId();
  92. }, array_values($filteredThemes));
  93. $enabledThemes = array_merge(array_diff($themesIds, $filteredThemesIds), [$theme->getId()]);
  94. $this->setEnabledThemes($enabledThemes);
  95. return $enabledThemes;
  96. }
  97. /**
  98. * Disable a theme for the logged-in user
  99. *
  100. * @param ITheme $theme the theme to disable
  101. * @return string[] the enabled themes
  102. */
  103. public function disableTheme(ITheme $theme): array {
  104. $themesIds = $this->getEnabledThemes();
  105. // If enabled, removing it
  106. if (in_array($theme->getId(), $themesIds)) {
  107. $enabledThemes = array_diff($themesIds, [$theme->getId()]);
  108. $this->setEnabledThemes($enabledThemes);
  109. return $enabledThemes;
  110. }
  111. return $themesIds;
  112. }
  113. /**
  114. * Check whether a theme is enabled or not
  115. * for the logged-in user
  116. *
  117. * @return bool
  118. */
  119. public function isEnabled(ITheme $theme): bool {
  120. $user = $this->userSession->getUser();
  121. if ($user instanceof IUser) {
  122. // Using keys as it's faster
  123. $themes = $this->getEnabledThemes();
  124. return in_array($theme->getId(), $themes);
  125. }
  126. return false;
  127. }
  128. /**
  129. * Get the list of all enabled themes IDs
  130. * for the logged-in user
  131. *
  132. * @return string[]
  133. */
  134. public function getEnabledThemes(): array {
  135. $user = $this->userSession->getUser();
  136. if ($user === null) {
  137. return [];
  138. }
  139. $enforcedTheme = $this->config->getSystemValueString('enforce_theme', '');
  140. $enabledThemes = json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]'));
  141. if ($enforcedTheme !== '') {
  142. return array_merge([$enforcedTheme], $enabledThemes);
  143. }
  144. try {
  145. return $enabledThemes;
  146. } catch (\Exception $e) {
  147. return [];
  148. }
  149. }
  150. /**
  151. * Set the list of enabled themes
  152. * for the logged-in user
  153. *
  154. * @param string[] $themes the list of enabled themes IDs
  155. */
  156. private function setEnabledThemes(array $themes): void {
  157. $user = $this->userSession->getUser();
  158. $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_values(array_unique($themes))));
  159. }
  160. }