1<?php
2
3declare(strict_types=1);
4
5/**
6 * @copyright Copyright (c) 2016, ownCloud, Inc.
7 *
8 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
9 * @author Daniel Kesselberg <mail@danielkesselberg.de>
10 * @author Joas Schilling <coding@schilljs.com>
11 * @author John Molakvoæ <skjnldsv@protonmail.com>
12 * @author Julius Härtl <jus@bitgrid.net>
13 * @author Morris Jobke <hey@morrisjobke.de>
14 * @author Robin Appelman <robin@icewind.nl>
15 * @author Roeland Jago Douma <roeland@famdouma.nl>
16 * @author Tobias Kaminsky <tobias@kaminsky.me>
17 * @author Vincent Petry <vincent@nextcloud.com>
18 *
19 * @license AGPL-3.0
20 *
21 * This code is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU Affero General Public License, version 3,
23 * as published by the Free Software Foundation.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU Affero General Public License for more details.
29 *
30 * You should have received a copy of the GNU Affero General Public License, version 3,
31 * along with this program. If not, see <http://www.gnu.org/licenses/>
32 *
33 */
34namespace OCA\Files\AppInfo;
35
36use Closure;
37use OC\Search\Provider\File;
38use OCA\Files\Capabilities;
39use OCA\Files\Collaboration\Resources\Listener;
40use OCA\Files\Collaboration\Resources\ResourceProvider;
41use OCA\Files\Controller\ApiController;
42use OCA\Files\Event\LoadAdditionalScriptsEvent;
43use OCA\Files\Event\LoadSidebar;
44use OCA\Files\Listener\LegacyLoadAdditionalScriptsAdapter;
45use OCA\Files\Listener\LoadSidebarListener;
46use OCA\Files\Notification\Notifier;
47use OCA\Files\Search\FilesSearchProvider;
48use OCA\Files\Service\TagService;
49use OCP\Activity\IManager as IActivityManager;
50use OCP\AppFramework\App;
51use OCP\AppFramework\Bootstrap\IBootContext;
52use OCP\AppFramework\Bootstrap\IBootstrap;
53use OCP\AppFramework\Bootstrap\IRegistrationContext;
54use OCP\Collaboration\Resources\IProviderManager;
55use OCP\IConfig;
56use OCP\IL10N;
57use OCP\IPreview;
58use OCP\ISearch;
59use OCP\IRequest;
60use OCP\IServerContainer;
61use OCP\ITagManager;
62use OCP\IUserSession;
63use OCP\Share\IManager as IShareManager;
64use OCP\Util;
65use Psr\Container\ContainerInterface;
66
67class Application extends App implements IBootstrap {
68	public const APP_ID = 'files';
69
70	public function __construct(array $urlParams = []) {
71		parent::__construct(self::APP_ID, $urlParams);
72	}
73
74	public function register(IRegistrationContext $context): void {
75		/**
76		 * Controllers
77		 */
78		$context->registerService('APIController', function (ContainerInterface $c) {
79			/** @var IServerContainer $server */
80			$server = $c->get(IServerContainer::class);
81
82			return new ApiController(
83				$c->get('AppName'),
84				$c->get(IRequest::class),
85				$c->get(IUserSession::class),
86				$c->get(TagService::class),
87				$c->get(IPreview::class),
88				$c->get(IShareManager::class),
89				$c->get(IConfig::class),
90				$server->getUserFolder()
91			);
92		});
93
94		/**
95		 * Services
96		 */
97		$context->registerService(TagService::class, function (ContainerInterface $c) {
98			/** @var IServerContainer $server */
99			$server = $c->get(IServerContainer::class);
100
101			return new TagService(
102				$c->get(IUserSession::class),
103				$c->get(IActivityManager::class),
104				$c->get(ITagManager::class)->load(self::APP_ID),
105				$server->getUserFolder(),
106				$server->getEventDispatcher()
107			);
108		});
109
110		/*
111		 * Register capabilities
112		 */
113		$context->registerCapability(Capabilities::class);
114
115		$context->registerEventListener(LoadAdditionalScriptsEvent::class, LegacyLoadAdditionalScriptsAdapter::class);
116		$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
117
118		$context->registerSearchProvider(FilesSearchProvider::class);
119
120		$context->registerNotifierService(Notifier::class);
121	}
122
123	public function boot(IBootContext $context): void {
124		$context->injectFn(Closure::fromCallable([$this, 'registerCollaboration']));
125		$context->injectFn([Listener::class, 'register']);
126		$context->injectFn(Closure::fromCallable([$this, 'registerSearchProvider']));
127		$this->registerTemplates();
128		$context->injectFn(Closure::fromCallable([$this, 'registerNavigation']));
129		$this->registerHooks();
130	}
131
132	private function registerCollaboration(IProviderManager $providerManager): void {
133		$providerManager->registerResourceProvider(ResourceProvider::class);
134	}
135
136	private function registerSearchProvider(ISearch $search): void {
137		$search->registerProvider(File::class, ['apps' => ['files']]);
138	}
139
140	private function registerTemplates(): void {
141		$templateManager = \OC_Helper::getFileTemplateManager();
142		$templateManager->registerTemplate('application/vnd.oasis.opendocument.presentation', 'core/templates/filetemplates/template.odp');
143		$templateManager->registerTemplate('application/vnd.oasis.opendocument.text', 'core/templates/filetemplates/template.odt');
144		$templateManager->registerTemplate('application/vnd.oasis.opendocument.spreadsheet', 'core/templates/filetemplates/template.ods');
145	}
146
147	private function registerNavigation(IL10N $l10n): void {
148		\OCA\Files\App::getNavigationManager()->add(function () use ($l10n) {
149			return [
150				'id' => 'files',
151				'appname' => 'files',
152				'script' => 'list.php',
153				'order' => 0,
154				'name' => $l10n->t('All files')
155			];
156		});
157		\OCA\Files\App::getNavigationManager()->add(function () use ($l10n) {
158			return [
159				'id' => 'recent',
160				'appname' => 'files',
161				'script' => 'recentlist.php',
162				'order' => 2,
163				'name' => $l10n->t('Recent')
164			];
165		});
166		\OCA\Files\App::getNavigationManager()->add(function () use ($l10n) {
167			return [
168				'id' => 'favorites',
169				'appname' => 'files',
170				'script' => 'simplelist.php',
171				'order' => 5,
172				'name' => $l10n->t('Favorites'),
173				'expandedState' => 'show_Quick_Access'
174			];
175		});
176	}
177
178	private function registerHooks(): void {
179		Util::connectHook('\OCP\Config', 'js', '\OCA\Files\App', 'extendJsConfig');
180	}
181}
182