Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix(theming): Allow to specify a userId manually in BackgroundService
Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Sep 10, 2024
commit 4efd39ec23efbc38f064af7d22fb74c91e3100f7
59 changes: 41 additions & 18 deletions apps/theming/lib/Service/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,15 @@ public function __construct(
) {
}

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->userId;
if ($this->userId === null) {
throw new RuntimeException('No currently logged-in user');
}

$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');
}

/**
Expand All @@ -227,31 +232,49 @@ public function setFileBackground($path): void {

/** @var File $file */
$file = $userFolder->get($path);
$handle = $file->fopen('r');
if ($handle === false) {
throw new InvalidArgumentException('Invalid image file');
}
$this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));

$this->recalculateMeanColor();
}

public function recalculateMeanColor(?string $userId = null): void {
$userId = $userId ?? $this->userId;
$image = new \OCP\Image();

if ($image->loadFromFileHandle($file->fopen('r')) === false) {
$handle = $this->getAppDataFolder($userId)->getFile('background.jpg')->read();
if ($handle === false || $image->loadFromFileHandle($handle) === false) {
throw new InvalidArgumentException('Invalid image file');
}

$meanColor = $this->calculateMeanColor($image);
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) {
/**
* 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->userId;
if ($userId === null) {
throw new RuntimeException('No currently logged-in user');
}
if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
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']);
$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']);
}

/**
Expand Down Expand Up @@ -366,19 +389,19 @@ function toHex(int $channel): string {
/**
* 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 {
try {
$rootFolder = $this->appData->getFolder('users');
} catch (NotFoundException $e) {
$rootFolder = $this->appData->newFolder('users');
}
try {
return $rootFolder->getFolder($this->userId);
return $rootFolder->getFolder($userId);
} catch (NotFoundException $e) {
return $rootFolder->newFolder($this->userId);
return $rootFolder->newFolder($userId);
}
}
}
6 changes: 3 additions & 3 deletions apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ public function getColorBackground(): string {

// user-defined background color
if (!empty($user)) {
$userPrimaryColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_color', '');
if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userPrimaryColor)) {
return $userPrimaryColor;
$userBackgroundColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_color', '');
if (preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $userBackgroundColor)) {
return $userBackgroundColor;
}
}

Expand Down