BackgroundService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = '#00679e';
  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_IMAGE = 'kamil-porembinski-clouds.jpg';
  51. public const SHIPPED_BACKGROUNDS = [
  52. 'hannah-maclean-soft-floral.jpg' => [
  53. 'attribution' => 'Soft floral (Hannah MacLean, CC0)',
  54. 'description' => 'Abstract background picture in yellow and white color whith a flower on it',
  55. 'attribution_url' => 'https://stocksnap.io/photo/soft-floral-XOYWCCW5PA',
  56. 'theming' => self::THEMING_MODE_DARK,
  57. 'primary_color' => '#D8A06C',
  58. ],
  59. 'ted-moravec-morning-fog.jpg' => [
  60. 'attribution' => 'Morning fog (Ted Moravec, Public Domain)',
  61. 'description' => 'Background picture of a forest shrouded in fog',
  62. 'attribution_url' => 'https://flickr.com/photos/tmoravec/52392410261',
  63. 'theming' => self::THEMING_MODE_DARK,
  64. 'primary_color' => '#38A084',
  65. ],
  66. 'stefanus-martanto-setyo-husodo-underwater-ocean.jpg' => [
  67. 'attribution' => 'Underwater ocean (Stefanus Martanto Setyo Husodo, CC0)',
  68. 'description' => 'Background picture of an underwater ocean',
  69. 'attribution_url' => 'https://stocksnap.io/photo/underwater-ocean-TJA9LBH4WS',
  70. 'primary_color' => '#04577e',
  71. ],
  72. 'zoltan-voros-rhythm-and-blues.jpg' => [
  73. 'attribution' => 'Rhythm and blues (Zoltán Vörös, CC BY)',
  74. 'description' => 'Abstract background picture of sand dunes during night',
  75. 'attribution_url' => 'https://flickr.com/photos/v923z/51634409289/',
  76. 'primary_color' => '#1c243c',
  77. ],
  78. 'anatoly-mikhaltsov-butterfly-wing-scale.jpg' => [
  79. 'attribution' => 'Butterfly wing scale (Anatoly Mikhaltsov, CC BY-SA)',
  80. 'description' => 'Background picture of a red-ish butterfly wing under microscope',
  81. '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',
  82. 'primary_color' => '#a53c17',
  83. ],
  84. 'bernie-cetonia-aurata-take-off-composition.jpg' => [
  85. 'attribution' => 'Cetonia aurata take off composition (Bernie, Public Domain)',
  86. 'description' => 'Montage of a cetonia aurata bug that takes off with white background',
  87. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Cetonia_aurata_take_off_composition_05172009.jpg',
  88. 'theming' => self::THEMING_MODE_DARK,
  89. 'primary_color' => '#869171',
  90. ],
  91. 'dejan-krsmanovic-ribbed-red-metal.jpg' => [
  92. 'attribution' => 'Ribbed red metal (Dejan Krsmanovic, CC BY)',
  93. 'description' => 'Abstract background picture of red ribbed metal with two horizontal white elements on top of it',
  94. 'attribution_url' => 'https://www.flickr.com/photos/dejankrsmanovic/42971456774/',
  95. 'primary_color' => '#9c4236',
  96. ],
  97. 'eduardo-neves-pedra-azul.jpg' => [
  98. 'attribution' => 'Pedra azul milky way (Eduardo Neves, CC BY-SA)',
  99. 'description' => 'Background picture of the milky way during night with a mountain in front of it',
  100. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:Pedra_Azul_Milky_Way.jpg',
  101. 'primary_color' => '#4f6071',
  102. ],
  103. 'european-space-agency-barents-bloom.jpg' => [
  104. 'attribution' => 'Barents bloom (European Space Agency, CC BY-SA)',
  105. 'description' => 'Abstract background picture of blooming barents in blue and green colors',
  106. 'attribution_url' => 'https://www.esa.int/ESA_Multimedia/Images/2016/08/Barents_bloom',
  107. 'primary_color' => '#396475',
  108. ],
  109. 'hannes-fritz-flippity-floppity.jpg' => [
  110. 'attribution' => 'Flippity floppity (Hannes Fritz, CC BY-SA)',
  111. 'description' => 'Abstract background picture of many pairs of flip flops hanging on a wall in multiple colors',
  112. 'attribution_url' => 'http://hannes.photos/flippity-floppity',
  113. 'primary_color' => '#98415a',
  114. ],
  115. 'hannes-fritz-roulette.jpg' => [
  116. 'attribution' => 'Roulette (Hannes Fritz, CC BY-SA)',
  117. 'description' => 'Background picture of a rotating giant wheel during night',
  118. 'attribution_url' => 'http://hannes.photos/roulette',
  119. 'primary_color' => '#845334',
  120. ],
  121. 'hannes-fritz-sea-spray.jpg' => [
  122. 'attribution' => 'Sea spray (Hannes Fritz, CC BY-SA)',
  123. 'description' => 'Background picture of a stone coast with fog and sea behind it',
  124. 'attribution_url' => 'http://hannes.photos/sea-spray',
  125. 'primary_color' => '#4f6071',
  126. ],
  127. 'kamil-porembinski-clouds.jpg' => [
  128. 'attribution' => 'Clouds (Kamil Porembiński, CC BY-SA)',
  129. 'description' => 'Background picture of white clouds on in front of a blue sky',
  130. 'attribution_url' => 'https://www.flickr.com/photos/paszczak000/8715851521/',
  131. 'primary_color' => self::DEFAULT_COLOR,
  132. ],
  133. 'bernard-spragg-new-zealand-fern.jpg' => [
  134. 'attribution' => 'New zealand fern (Bernard Spragg, CC0)',
  135. 'description' => 'Abstract background picture of fern leafes',
  136. 'attribution_url' => 'https://commons.wikimedia.org/wiki/File:NZ_Fern.(Blechnum_chambersii)_(11263534936).jpg',
  137. 'primary_color' => '#316b26',
  138. ],
  139. 'rawpixel-pink-tapioca-bubbles.jpg' => [
  140. 'attribution' => 'Pink tapioca bubbles (Rawpixel, CC BY)',
  141. 'description' => 'Abstract background picture of pink tapioca bubbles',
  142. 'attribution_url' => 'https://www.flickr.com/photos/byrawpixel/27665140298/in/photostream/',
  143. 'theming' => self::THEMING_MODE_DARK,
  144. 'primary_color' => '#b17ab4',
  145. ],
  146. 'nasa-waxing-crescent-moon.jpg' => [
  147. 'attribution' => 'Waxing crescent moon (NASA, Public Domain)',
  148. 'description' => 'Background picture of glowing earth in foreground and moon in the background',
  149. 'attribution_url' => 'https://www.nasa.gov/image-feature/a-waxing-crescent-moon',
  150. 'primary_color' => '#005ac1',
  151. ],
  152. 'tommy-chau-already.jpg' => [
  153. 'attribution' => 'Cityscape (Tommy Chau, CC BY)',
  154. 'description' => 'Background picture of a skyscraper city during night',
  155. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/16910999368',
  156. 'primary_color' => '#6a2af4',
  157. ],
  158. 'tommy-chau-lion-rock-hill.jpg' => [
  159. 'attribution' => 'Lion rock hill (Tommy Chau, CC BY)',
  160. 'description' => 'Background picture of mountains during sunset or sunrise',
  161. 'attribution_url' => 'https://www.flickr.com/photos/90975693@N05/17136440246',
  162. 'theming' => self::THEMING_MODE_DARK,
  163. 'primary_color' => '#c074a9',
  164. ],
  165. 'lali-masriera-yellow-bricks.jpg' => [
  166. 'attribution' => 'Yellow bricks (Lali Masriera, CC BY)',
  167. 'description' => 'Background picture of yellow bricks with some yellow tubes',
  168. 'attribution_url' => 'https://www.flickr.com/photos/visualpanic/3982464447',
  169. 'theming' => self::THEMING_MODE_DARK,
  170. 'primary_color' => '#bc8210',
  171. ],
  172. ];
  173. private IRootFolder $rootFolder;
  174. private IAppData $appData;
  175. private IConfig $config;
  176. private string $userId;
  177. private ThemingDefaults $themingDefaults;
  178. public function __construct(IRootFolder $rootFolder,
  179. IAppData $appData,
  180. IConfig $config,
  181. ?string $userId,
  182. ThemingDefaults $themingDefaults) {
  183. if ($userId === null) {
  184. return;
  185. }
  186. $this->rootFolder = $rootFolder;
  187. $this->config = $config;
  188. $this->userId = $userId;
  189. $this->appData = $appData;
  190. $this->themingDefaults = $themingDefaults;
  191. }
  192. public function setDefaultBackground(): void {
  193. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_image');
  194. $this->config->deleteUserValue($this->userId, Application::APP_ID, 'background_color');
  195. }
  196. /**
  197. * @param $path
  198. * @throws NotFoundException
  199. * @throws NotPermittedException
  200. * @throws LockedException
  201. * @throws PreConditionNotMetException
  202. * @throws NoUserException
  203. */
  204. public function setFileBackground($path): void {
  205. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_CUSTOM);
  206. $userFolder = $this->rootFolder->getUserFolder($this->userId);
  207. /** @var File $file */
  208. $file = $userFolder->get($path);
  209. $image = new \OCP\Image();
  210. if ($image->loadFromFileHandle($file->fopen('r')) === false) {
  211. throw new InvalidArgumentException('Invalid image file');
  212. }
  213. $this->getAppDataFolder()->newFile('background.jpg', $file->fopen('r'));
  214. }
  215. public function setShippedBackground($fileName): void {
  216. if (!array_key_exists($fileName, self::SHIPPED_BACKGROUNDS)) {
  217. throw new InvalidArgumentException('The given file name is invalid');
  218. }
  219. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', $fileName);
  220. $this->setColorBackground(self::SHIPPED_BACKGROUNDS[$fileName]['primary_color']);
  221. }
  222. public function setColorBackground(string $color): void {
  223. if (!preg_match('/^#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
  224. throw new InvalidArgumentException('The given color is invalid');
  225. }
  226. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_color', $color);
  227. }
  228. public function deleteBackgroundImage(): void {
  229. $this->config->setUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DISABLED);
  230. }
  231. public function getBackground(): ?ISimpleFile {
  232. $background = $this->config->getUserValue($this->userId, Application::APP_ID, 'background_image', self::BACKGROUND_DEFAULT);
  233. if ($background === self::BACKGROUND_CUSTOM) {
  234. try {
  235. return $this->getAppDataFolder()->getFile('background.jpg');
  236. } catch (NotFoundException | NotPermittedException $e) {
  237. return null;
  238. }
  239. }
  240. return null;
  241. }
  242. /**
  243. * Storing the data in appdata/theming/users/USERID
  244. *
  245. * @return ISimpleFolder
  246. * @throws NotPermittedException
  247. */
  248. private function getAppDataFolder(): ISimpleFolder {
  249. try {
  250. $rootFolder = $this->appData->getFolder('users');
  251. } catch (NotFoundException $e) {
  252. $rootFolder = $this->appData->newFolder('users');
  253. }
  254. try {
  255. return $rootFolder->getFolder($this->userId);
  256. } catch (NotFoundException $e) {
  257. return $rootFolder->newFolder($this->userId);
  258. }
  259. }
  260. }