Application.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  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, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Trashbin\AppInfo;
  27. use OCA\DAV\Connector\Sabre\Principal;
  28. use OCA\Files_Trashbin\Capabilities;
  29. use OCA\Files_Trashbin\Expiration;
  30. use OCA\Files_Trashbin\Trash\ITrashManager;
  31. use OCA\Files_Trashbin\Trash\TrashManager;
  32. use OCA\Files_Trashbin\UserMigration\TrashbinMigrator;
  33. use OCP\AppFramework\App;
  34. use OCP\AppFramework\Bootstrap\IBootContext;
  35. use OCP\AppFramework\Bootstrap\IBootstrap;
  36. use OCP\AppFramework\Bootstrap\IRegistrationContext;
  37. use OCP\App\IAppManager;
  38. use OCP\ILogger;
  39. use OCP\IServerContainer;
  40. class Application extends App implements IBootstrap {
  41. public const APP_ID = 'files_trashbin';
  42. public function __construct(array $urlParams = []) {
  43. parent::__construct(self::APP_ID, $urlParams);
  44. }
  45. public function register(IRegistrationContext $context): void {
  46. $context->registerCapability(Capabilities::class);
  47. $context->registerServiceAlias('Expiration', Expiration::class);
  48. $context->registerServiceAlias(ITrashManager::class, TrashManager::class);
  49. /** Register $principalBackend for the DAV collection */
  50. $context->registerServiceAlias('principalBackend', Principal::class);
  51. $context->registerUserMigrator(TrashbinMigrator::class);
  52. }
  53. public function boot(IBootContext $context): void {
  54. $context->injectFn([$this, 'registerTrashBackends']);
  55. // create storage wrapper on setup
  56. \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage');
  57. //Listen to delete user signal
  58. \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook');
  59. //Listen to post write hook
  60. \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook');
  61. // pre and post-rename, disable trash logic for the copy+unlink case
  62. \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook');
  63. \OCA\Files\App::getNavigationManager()->add(function () {
  64. $l = \OC::$server->getL10N(self::APP_ID);
  65. return [
  66. 'id' => 'trashbin',
  67. 'appname' => self::APP_ID,
  68. 'script' => 'list.php',
  69. 'order' => 50,
  70. 'name' => $l->t('Deleted files'),
  71. 'classes' => 'pinned',
  72. ];
  73. });
  74. }
  75. public function registerTrashBackends(IServerContainer $serverContainer, ILogger $logger, IAppManager $appManager, ITrashManager $trashManager) {
  76. foreach ($appManager->getInstalledApps() as $app) {
  77. $appInfo = $appManager->getAppInfo($app);
  78. if (isset($appInfo['trash'])) {
  79. $backends = $appInfo['trash'];
  80. foreach ($backends as $backend) {
  81. $class = $backend['@value'];
  82. $for = $backend['@attributes']['for'];
  83. try {
  84. $backendObject = $serverContainer->query($class);
  85. $trashManager->registerBackend($for, $backendObject);
  86. } catch (\Exception $e) {
  87. $logger->logException($e);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }