You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackgroundService.php 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020 Julius Härtl <jus@bitgrid.net>
  5. *
  6. * @author Jan C. Borchardt <hey@jancborchardt.net>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Christopher Ng <chrng8@gmail.com>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\Theming\Service;
  27. use InvalidArgumentException;
  28. use OC\User\NoUserException;
  29. use OCA\Theming\AppInfo\Application;
  30. use OCA\Theming\ThemingDefaults;
  31. use OCP\Files\File;
  32. use OCP\Files\IAppData;
  33. use OCP\Files\IRootFolder;
  34. use OCP\Files\NotFoundException;
  35. use OCP\Files\NotPermittedException;
  36. use OCP\Files\SimpleFS\ISimpleFile;
  37. use OCP\Files\SimpleFS\ISimpleFolder;
  38. use OCP\IConfig;
  39. use OCP\Lock\LockedException;
  40. use OCP\PreConditionNotMetException;
  41. class BackgroundService {
  42. // true when the background is bright and need dark icons
  43. public const THEMING_MODE_DARK = 'dark';
  44. public const DEFAULT_COLOR = '#0082c9';
  45. public const DEFAULT_ACCESSIBLE_COLOR = '#006aa3';
  46. public const BACKGROUND_SHIPPED = 'shipped';
  47. public const BACKGROUND_CUSTOM = 'custom';
  48. public const BACKGROUND_DEFAULT = 'default';
  49. public const BACKGROUND_DISABLED = 'disabled';
  50. public const DEFAULT_BACKGROUND = 'kamil-porembinski-clouds.jpg';
  51. public const SHIPPED_BACKGROUNDS = [
  52. 'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
  53. 'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
  54. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:%D0%A7%D0%B5%D1%88%D1%83%D0%B9%D0%BA%D0%B8_%D0%BA%D1%80%D1%8B%D0%BB%D0%B0_%D0%B1%D0%B0%D0%B1%D0%BE%D1%87%D0%BA%D0%B8.jpg',
  55. 'primary_color' => '#a53c17',
  56. ],
  57. 'bernie-cetonia-aurata-take-off-composition.jpg' => [
  58. 'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
  59. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
  60. 'theming' => self::THEMING_MODE_DARK,
  61. 'primary_color' => '#56633d',
  62. ],
  63. 'dejan-krsmanovic-ribbed-red-metal.jpg' => [
  64. 'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
  65. 'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
  66. 'primary_color' => '#9c4236',
  67. ],
  68. 'eduardo-neves-pedra-azul.jpg' => [
  69. 'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
  70. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
  71. 'primary_color' => '#4f6071',
  72. ],
  73. 'european-space-agency-barents-bloom.jpg' => [
  74. 'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
  75. 'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
  76. 'primary_color' => '#396475',
  77. ],
  78. 'hannes-fritz-flippity-floppity.jpg' => [
  79. 'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
  80. 'attribution_url' => 'http://hannes.photos/flippity-floppity',
  81. 'primary_color' => '#98415a',
  82. ],
  83. 'hannes-fritz-roulette.jpg' => [
  84. 'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
  85. 'attribution_url' => 'http://hannes.photos/roulette',
  86. 'primary_color' => '#845334',
  87. ],
  88. 'hannes-fritz-sea-spray.jpg' => [
  89. 'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
  90. 'attribution_url' => 'http://hannes.photos/sea-spray',
  91. 'primary_color' => '#4f6071',
  92. ],
  93. 'kamil-porembinski-clouds.jpg' => [
  94. 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
  95. 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
  96. 'primary_color' => self::DEFAULT_COLOR,
  97. ],
  98. 'bernard-spragg-new-zealand-fern.jpg' => [
  99. 'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
  100. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
  101. 'primary_color' => '#316b26',
  102. ],
  103. 'rawpixel-pink-tapioca-bubbles.jpg' => [
  104. 'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
  105. 'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
  106. 'theming' => self::THEMING_MODE_DARK,
  107. 'primary_color' => '#7b4e7e',
  108. ],
  109. 'nasa-waxing-crescent-moon.jpg' => [
  110. 'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
  111. 'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
  112. 'primary_color' => '#005ac1',
  113. ],
  114. 'tommy-chau-already.jpg' => [
  115. 'attribution' => 'Cityscape (Tommy Chau, CC BY)',
  116. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
  117. 'primary_color' => '#6a2af4',
  118. ],
  119. 'tommy-chau-lion-rock-hill.jpg' => [
  120. 'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
  121. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
  122. 'theming' => self::THEMING_MODE_DARK,
  123. 'primary_color' => '#7f4f70',
  124. ],
  125. 'lali-masriera-yellow-bricks.jpg' => [
  126. 'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
  127. 'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
  128. 'theming' => self::THEMING_MODE_DARK,
  129. 'primary_color' => '#7f5700',
  130. ],
  131. ];
  132. private IRootFolder $rootFolder;
  133. private IAppData $appData;
  134. private IConfig $config;
  135. private string $userId;
  136. private ThemingDefaults $themingDefaults;
  137. public function __construct(IRootFolder $rootFolder,
  138. IAppData $appData,
  139. IConfig $config,
  140. ?string $userId,
  141. ThemingDefaults $themingDefaults) {
  142. if ($userId === null) {
  143. return;
  144. }
  145. $this->rootFolder = $rootFolder;
  146. $this->config = $config;
  147. $this->userId = $userId;
  148. $this->appData = $appData;
  149. $this->themingDefaults = $themingDefaults;
  150. }
  151. public function setDefaultBackground(): void {
  152. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_image');
  153. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $this->themingDefaults->getDefaultColorPrimary());
  154. }
  155. /**
  156. * @param $path
  157. * @throws NotFoundException
  158. * @throws NotPermittedException
  159. * @throws LockedException
  160. * @throws PreConditionNotMetException
  161. * @throws NoUserException
  162. */
  163. public function setFileBackground($path): void {
  164. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM);
  165. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  166. /** @var File $file */
  167. $file = $userFolder->get($path);
  168. $image = new \OCP\Image();
  169. if ($image->loadFromFileHandle($file->fopen('r')) === false) {
  170. throw new InvalidArgumentException('Invalid image file');
  171. }
  172. $this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
  173. }
  174. public function setShippedBackground($fileName): void {
  175. if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
  176. throw new InvalidArgumentException('The given file name is invalid');
  177. }
  178. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', $fileName);
  179. $this->setColorBackground(self::SHIPPED_BACKGROUNDS[$fileName]['primary_color']);
  180. }
  181. public function setColorBackground(string $color): void {
  182. if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  183. throw new InvalidArgumentException('The given color is invalid');
  184. }
  185. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $color);
  186. }
  187. public function deleteBackgroundImage(): void {
  188. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DISABLED);
  189. }
  190. public function getBackground(): ?ISimpleFile {
  191. $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT);
  192. if ($background === self::BACKGROUND_CUSTOM) {
  193. try {
  194. return $this->getAppDataFolder()->getFile('background.jpg');
  195. } catch (NotFoundException | NotPermittedException $e) {
  196. return null;
  197. }
  198. }
  199. return null;
  200. }
  201. /**
  202. * Storing the data in appdata/theming/users/USERID
  203. *
  204. * @return ISimpleFolder
  205. * @throws NotPermittedException
  206. */
  207. private function getAppDataFolder(): ISimpleFolder {
  208. try {
  209. $rootFolder = $this->appData->getFolder('users');
  210. } catch (NotFoundException $e) {
  211. $rootFolder = $this->appData->newFolder('users');
  212. }
  213. try {
  214. return $rootFolder->getFolder($this->userId);
  215. } catch (NotFoundException $e) {
  216. return $rootFolder->newFolder($this->userId);
  217. }
  218. }
  219. }