diff options
Diffstat (limited to 'apps/theming/lib/Service')
-rw-r--r-- | apps/theming/lib/Service/BackgroundService.php | 150 | ||||
-rw-r--r-- | apps/theming/lib/Service/ThemeInjectionService.php | 36 | ||||
-rw-r--r-- | apps/theming/lib/Service/ThemesService.php | 54 |
3 files changed, 147 insertions, 93 deletions
diff --git a/apps/theming/lib/Service/BackgroundService.php b/apps/theming/lib/Service/BackgroundService.php index 52925fdf980..ee9466c3a36 100644 --- a/apps/theming/lib/Service/BackgroundService.php +++ b/apps/theming/lib/Service/BackgroundService.php @@ -18,7 +18,9 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFolder; +use OCP\IAppConfig; use OCP\IConfig; +use OCP\Image; use OCP\Lock\LockedException; use OCP\PreConditionNotMetException; use RuntimeException; @@ -44,7 +46,7 @@ class BackgroundService { */ public const BACKGROUND_COLOR = 'color'; - public const DEFAULT_BACKGROUND_IMAGE = 'kamil-porembinski-clouds.jpg'; + public const DEFAULT_BACKGROUND_IMAGE = 'jenna-kim-the-globe.webp'; /** * 'attribution': Name, artist and license @@ -54,6 +56,21 @@ class BackgroundService { * 'primary_color': Recommended primary color for this theme / image */ public const SHIPPED_BACKGROUNDS = [ + 'jenna-kim-the-globe.webp' => [ + 'attribution' => 'Globe (Jenna Kim - Nextcloud GmbH, CC-BY-SA-4.0)', + 'description' => 'Background picture of white clouds on in front of a blue sky', + 'attribution_url' => 'https://nextcloud.com/trademarks/', + 'dark_variant' => 'jenna-kim-the-globe-dark.webp', + 'background_color' => self::DEFAULT_BACKGROUND_COLOR, + 'primary_color' => self::DEFAULT_COLOR, + ], + 'kamil-porembinski-clouds.jpg' => [ + 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)', + 'description' => 'Background picture of white clouds on in front of a blue sky', + 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/', + 'background_color' => self::DEFAULT_BACKGROUND_COLOR, + 'primary_color' => self::DEFAULT_COLOR, + ], 'hannah-maclean-soft-floral.jpg' => [ 'attribution' => 'Soft floral (Hannah MacLean, CC0)', 'description' => 'Abstract background picture in yellow and white color whith a flower on it', @@ -138,13 +155,6 @@ class BackgroundService { 'background_color' => '#333f47', 'primary_color' => '#4f6071', ], - 'kamil-porembinski-clouds.jpg' => [ - 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)', - 'description' => 'Background picture of white clouds on in front of a blue sky', - 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/', - 'background_color' => self::DEFAULT_BACKGROUND_COLOR, - 'primary_color' => self::DEFAULT_COLOR, - ], 'bernard-spragg-new-zealand-fern.jpg' => [ 'attribution' => 'New zealand fern (Bernard Spragg, CC0)', 'description' => 'Abstract background picture of fern leafes', @@ -192,15 +202,18 @@ class BackgroundService { public function __construct( private IRootFolder $rootFolder, private IAppData $appData, + private IAppConfig $appConfig, private IConfig $config, private ?string $userId, ) { } - public function setDefaultBackground(): void { - $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_image'); - $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_color'); - $this->config->deleteUserValue($this->userId, Application::APP_ID, 'primary_color'); + public function setDefaultBackground(?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + + $this->config->deleteUserValue($userId, Application::APP_ID, 'background_image'); + $this->config->deleteUserValue($userId, Application::APP_ID, 'background_color'); + $this->config->deleteUserValue($userId, Application::APP_ID, 'primary_color'); } /** @@ -211,17 +224,27 @@ class BackgroundService { * @throws PreConditionNotMetException * @throws NoUserException */ - public function setFileBackground($path): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } - $userFolder = $this->rootFolder->getUserFolder($this->userId); + public function setFileBackground(string $path, ?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + $userFolder = $this->rootFolder->getUserFolder($userId); /** @var File $file */ $file = $userFolder->get($path); - $image = new \OCP\Image(); + $handle = $file->fopen('r'); + if ($handle === false) { + throw new InvalidArgumentException('Invalid image file'); + } + $this->getAppDataFolder()->newFile('background.jpg', $handle); - if ($image->loadFromFileHandle($file->fopen('r')) === false) { + $this->recalculateMeanColor(); + } + + public function recalculateMeanColor(?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + + $image = new Image(); + $handle = $this->getAppDataFolder($userId)->getFile('background.jpg')->read(); + if ($handle === false || $image->loadFromFileHandle($handle) === false) { throw new InvalidArgumentException('Invalid image file'); } @@ -229,50 +252,53 @@ class BackgroundService { if ($meanColor !== false) { $this->setColorBackground($meanColor); } - - $this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r')); - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM); + $this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM); } - public function setShippedBackground($fileName): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } - if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) { + /** + * Set background of user to a shipped background identified by the filename + * @param string $filename The shipped background filename + * @param null|string $userId The user to set - defaults to currently logged in user + * @throws RuntimeException If neither $userId is specified nor a user is logged in + * @throws InvalidArgumentException If the specified filename does not match any shipped background + */ + public function setShippedBackground(string $filename, ?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + + if (!array_key_exists($filename, self::SHIPPED_BACKGROUNDS)) { throw new InvalidArgumentException('The given file name is invalid'); } - $this->setColorBackground(self::SHIPPED_BACKGROUNDS[$fileName]['background_color']); - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', $fileName); - $this->config->setUserValue($this->userId, Application::APP_ID, 'primary_color', self::SHIPPED_BACKGROUNDS[$fileName]['primary_color']); + $this->setColorBackground(self::SHIPPED_BACKGROUNDS[$filename]['background_color'], $userId); + $this->config->setUserValue($userId, Application::APP_ID, 'background_image', $filename); + $this->config->setUserValue($userId, Application::APP_ID, 'primary_color', self::SHIPPED_BACKGROUNDS[$filename]['primary_color']); } /** * Set the background to color only + * @param string|null $userId The user to set the color - default to current logged-in user */ - public function setColorBackground(string $color): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } + public function setColorBackground(string $color, ?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) { throw new InvalidArgumentException('The given color is invalid'); } - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $color); - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); + $this->config->setUserValue($userId, Application::APP_ID, 'background_color', $color); + $this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); } - public function deleteBackgroundImage(): void { - if ($this->userId === null) { - throw new RuntimeException('No currently logged-in user'); - } - $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); + public function deleteBackgroundImage(?string $userId = null): void { + $userId = $userId ?? $this->getUserId(); + $this->config->setUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_COLOR); } - public function getBackground(): ?ISimpleFile { - $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT); + public function getBackground(?string $userId = null): ?ISimpleFile { + $userId = $userId ?? $this->getUserId(); + $background = $this->config->getUserValue($userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT); if ($background === self::BACKGROUND_CUSTOM) { try { return $this->getAppDataFolder()->getFile('background.jpg'); - } catch (NotFoundException | NotPermittedException $e) { + } catch (NotFoundException|NotPermittedException $e) { return null; } } @@ -285,14 +311,14 @@ class BackgroundService { * @param resource|string $path * @return string|null The fallback background color - if any */ - public function setGlobalBackground($path): string|null { - $image = new \OCP\Image(); + public function setGlobalBackground($path): ?string { + $image = new Image(); $handle = is_resource($path) ? $path : fopen($path, 'rb'); if ($handle && $image->loadFromFileHandle($handle) !== false) { $meanColor = $this->calculateMeanColor($image); if ($meanColor !== false) { - $this->config->setAppValue(Application::APP_ID, 'background_color', $meanColor); + $this->appConfig->setValueString(Application::APP_ID, 'background_color', $meanColor); return $meanColor; } } @@ -303,7 +329,7 @@ class BackgroundService { * Calculate mean color of an given image * It only takes the upper part into account so that a matching text color can be derived for the app menu */ - private function calculateMeanColor(\OCP\Image $image): false|string { + private function calculateMeanColor(Image $image): false|string { /** * Small helper to ensure one channel is returned as 8byte hex */ @@ -311,13 +337,13 @@ class BackgroundService { $hex = dechex($channel); return match (strlen($hex)) { 0 => '00', - 1 => '0'.$hex, + 1 => '0' . $hex, 2 => $hex, default => 'ff', }; } - $tempImage = new \OCP\Image(); + $tempImage = new Image(); // Crop to only analyze top bar $resource = $image->cropNew(0, 0, $image->width(), min(max(50, (int)($image->height() * 0.125)), $image->height())); @@ -358,19 +384,31 @@ class BackgroundService { /** * Storing the data in appdata/theming/users/USERID * - * @return ISimpleFolder + * @param string|null $userId The user to get the folder - default to current user * @throws NotPermittedException */ - private function getAppDataFolder(): ISimpleFolder { + private function getAppDataFolder(?string $userId = null): ISimpleFolder { + $userId = $userId ?? $this->getUserId(); + try { $rootFolder = $this->appData->getFolder('users'); - } catch (NotFoundException $e) { + } catch (NotFoundException) { $rootFolder = $this->appData->newFolder('users'); } try { - return $rootFolder->getFolder($this->userId); - } catch (NotFoundException $e) { - return $rootFolder->newFolder($this->userId); + return $rootFolder->getFolder($userId); + } catch (NotFoundException) { + return $rootFolder->newFolder($userId); + } + } + + /** + * @throws RuntimeException Thrown if a method that needs a user is called without any logged-in user + */ + private function getUserId(): string { + if ($this->userId === null) { + throw new RuntimeException('No currently logged-in user'); } + return $this->userId; } } diff --git a/apps/theming/lib/Service/ThemeInjectionService.php b/apps/theming/lib/Service/ThemeInjectionService.php index f65dde076bc..873d388081c 100644 --- a/apps/theming/lib/Service/ThemeInjectionService.php +++ b/apps/theming/lib/Service/ThemeInjectionService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -14,25 +15,16 @@ use OCP\IUserSession; class ThemeInjectionService { - private IURLGenerator $urlGenerator; - private ThemesService $themesService; - private DefaultTheme $defaultTheme; - private Util $util; - private IConfig $config; private ?string $userId; - public function __construct(IURLGenerator $urlGenerator, - ThemesService $themesService, - DefaultTheme $defaultTheme, - Util $util, - IConfig $config, - IUserSession $userSession) { - $this->urlGenerator = $urlGenerator; - $this->themesService = $themesService; - $this->defaultTheme = $defaultTheme; - $this->util = $util; - $this->config = $config; - + public function __construct( + private IURLGenerator $urlGenerator, + private ThemesService $themesService, + private DefaultTheme $defaultTheme, + private Util $util, + private IConfig $config, + IUserSession $userSession, + ) { if ($userSession->getUser() !== null) { $this->userId = $userSession->getUser()->getUID(); } else { @@ -52,12 +44,12 @@ class ThemeInjectionService { $this->addThemeHeaders($defaultTheme); // Themes applied by media queries - foreach($mediaThemes as $theme) { + foreach ($mediaThemes as $theme) { $this->addThemeHeaders($theme, true, $theme->getMediaQuery()); } // Themes - foreach($this->themesService->getThemes() as $theme) { + foreach ($this->themesService->getThemes() as $theme) { // Ignore default theme as already processed first if ($theme->getId() === $this->defaultTheme->getId()) { continue; @@ -99,9 +91,9 @@ class ThemeInjectionService { $metaHeaders = []; // Meta headers - foreach($this->themesService->getThemes() as $theme) { + foreach ($this->themesService->getThemes() as $theme) { if (!empty($theme->getMeta())) { - foreach($theme->getMeta() as $meta) { + foreach ($theme->getMeta() as $meta) { if (!isset($meta['name']) || !isset($meta['content'])) { continue; } @@ -114,7 +106,7 @@ class ThemeInjectionService { } } - foreach($metaHeaders as $name => $content) { + foreach ($metaHeaders as $name => $content) { \OCP\Util::addHeader('meta', [ 'name' => $name, 'content' => join(' ', array_unique($content)), diff --git a/apps/theming/lib/Service/ThemesService.php b/apps/theming/lib/Service/ThemesService.php index d6e14b6ffcb..f49524cb62c 100644 --- a/apps/theming/lib/Service/ThemesService.php +++ b/apps/theming/lib/Service/ThemesService.php @@ -1,4 +1,5 @@ <?php + /** * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later @@ -16,24 +17,23 @@ use OCA\Theming\Themes\LightTheme; use OCP\IConfig; use OCP\IUser; use OCP\IUserSession; +use Psr\Log\LoggerInterface; class ThemesService { - private IUserSession $userSession; - private IConfig $config; - /** @var ITheme[] */ private array $themesProviders; - public function __construct(IUserSession $userSession, - IConfig $config, - DefaultTheme $defaultTheme, + public function __construct( + private IUserSession $userSession, + private IConfig $config, + private LoggerInterface $logger, + private DefaultTheme $defaultTheme, LightTheme $lightTheme, - DarkTheme $darkTheme, + private DarkTheme $darkTheme, HighContrastTheme $highContrastTheme, DarkHighContrastTheme $darkHighContrastTheme, - DyslexiaFont $dyslexiaFont) { - $this->userSession = $userSession; - $this->config = $config; + DyslexiaFont $dyslexiaFont, + ) { // Register themes $this->themesProviders = [ @@ -52,6 +52,28 @@ class ThemesService { * @return ITheme[] */ public function getThemes(): array { + // Enforced theme if configured + $enforcedTheme = $this->config->getSystemValueString('enforce_theme', ''); + if ($enforcedTheme !== '') { + if (!isset($this->themesProviders[$enforcedTheme])) { + $this->logger->error('Enforced theme not found', ['theme' => $enforcedTheme]); + return $this->themesProviders; + } + + $defaultTheme = $this->themesProviders[$this->defaultTheme->getId()]; + $darkTheme = $this->themesProviders[$this->darkTheme->getId()]; + $theme = $this->themesProviders[$enforcedTheme]; + return [ + // Leave the default theme as a fallback + $defaultTheme->getId() => $defaultTheme, + // Make sure we also have the dark theme to allow apps + // to scope sections of their UI to the dark theme + $darkTheme->getId() => $darkTheme, + // Finally, the enforced theme + $theme->getId() => $theme, + ]; + } + return $this->themesProviders; } @@ -106,7 +128,7 @@ class ThemesService { $this->setEnabledThemes($enabledThemes); return $enabledThemes; } - + return $themesIds; } @@ -127,19 +149,21 @@ class ThemesService { } /** - * Get the list of all enabled themes IDs - * for the logged-in user + * Get the list of all enabled themes IDs for the current user. * * @return string[] */ public function getEnabledThemes(): array { + $enforcedTheme = $this->config->getSystemValueString('enforce_theme', ''); $user = $this->userSession->getUser(); if ($user === null) { + if ($enforcedTheme !== '') { + return [$enforcedTheme]; + } return []; } - $enforcedTheme = $this->config->getSystemValueString('enforce_theme', ''); - $enabledThemes = json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]')); + $enabledThemes = json_decode($this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '["default"]')); if ($enforcedTheme !== '') { return array_merge([$enforcedTheme], $enabledThemes); } |