Application.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Citharel <nextcloud@tcit.fr>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Tobia De Koninck <tobia@ledfan.be>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\AppInfo;
  30. use OC\AppFramework\Utility\SimpleContainer;
  31. use OCA\DAV\CalDAV\Activity\Backend;
  32. use OCA\DAV\CalDAV\Activity\Provider\Event;
  33. use OCA\DAV\CalDAV\BirthdayService;
  34. use OCA\DAV\CalDAV\CalendarManager;
  35. use OCA\DAV\CalDAV\Reminder\Backend as ReminderBackend;
  36. use OCA\DAV\CalDAV\Reminder\NotificationProvider\AudioProvider;
  37. use OCA\DAV\CalDAV\Reminder\NotificationProvider\EmailProvider;
  38. use OCA\DAV\CalDAV\Reminder\NotificationProvider\PushProvider;
  39. use OCA\DAV\CalDAV\Reminder\NotificationProviderManager;
  40. use OCA\DAV\CalDAV\Reminder\Notifier;
  41. use OCA\DAV\CalDAV\Reminder\ReminderService;
  42. use OCA\DAV\Capabilities;
  43. use OCA\DAV\CardDAV\ContactsManager;
  44. use OCA\DAV\CardDAV\PhotoCache;
  45. use OCA\DAV\CardDAV\SyncService;
  46. use OCA\DAV\HookManager;
  47. use OCP\AppFramework\App;
  48. use OCP\Calendar\IManager as ICalendarManager;
  49. use OCP\Contacts\IManager as IContactsManager;
  50. use OCP\IUser;
  51. use Symfony\Component\EventDispatcher\GenericEvent;
  52. class Application extends App {
  53. const APP_ID = 'dav';
  54. /**
  55. * Application constructor.
  56. */
  57. public function __construct() {
  58. parent::__construct('dav');
  59. $container = $this->getContainer();
  60. $server = $container->getServer();
  61. $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) {
  62. return new PhotoCache(
  63. $server->getAppDataDir('dav-photocache'),
  64. $server->getLogger()
  65. );
  66. });
  67. /*
  68. * Register capabilities
  69. */
  70. $container->registerCapability(Capabilities::class);
  71. }
  72. /**
  73. * @param IContactsManager $contactsManager
  74. * @param string $userID
  75. */
  76. public function setupContactsProvider(IContactsManager $contactsManager, $userID) {
  77. /** @var ContactsManager $cm */
  78. $cm = $this->getContainer()->query(ContactsManager::class);
  79. $urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
  80. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  81. }
  82. /**
  83. * @param IContactsManager $contactsManager
  84. */
  85. public function setupSystemContactsProvider(IContactsManager $contactsManager) {
  86. /** @var ContactsManager $cm */
  87. $cm = $this->getContainer()->query(ContactsManager::class);
  88. $urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
  89. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  90. }
  91. /**
  92. * @param ICalendarManager $calendarManager
  93. * @param string $userId
  94. */
  95. public function setupCalendarProvider(ICalendarManager $calendarManager, $userId) {
  96. $cm = $this->getContainer()->query(CalendarManager::class);
  97. $cm->setupCalendarProvider($calendarManager, $userId);
  98. }
  99. public function registerHooks() {
  100. /** @var HookManager $hm */
  101. $hm = $this->getContainer()->query(HookManager::class);
  102. $hm->setup();
  103. $dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  104. // first time login event setup
  105. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
  106. if ($event instanceof GenericEvent) {
  107. $hm->firstLogin($event->getSubject());
  108. }
  109. });
  110. $birthdayListener = function ($event) {
  111. if ($event instanceof GenericEvent) {
  112. /** @var BirthdayService $b */
  113. $b = $this->getContainer()->query(BirthdayService::class);
  114. $b->onCardChanged(
  115. $event->getArgument('addressBookId'),
  116. $event->getArgument('cardUri'),
  117. $event->getArgument('cardData')
  118. );
  119. }
  120. };
  121. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $birthdayListener);
  122. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $birthdayListener);
  123. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function ($event) {
  124. if ($event instanceof GenericEvent) {
  125. /** @var BirthdayService $b */
  126. $b = $this->getContainer()->query(BirthdayService::class);
  127. $b->onCardDeleted(
  128. $event->getArgument('addressBookId'),
  129. $event->getArgument('cardUri')
  130. );
  131. }
  132. });
  133. $clearPhotoCache = function($event) {
  134. if ($event instanceof GenericEvent) {
  135. /** @var PhotoCache $p */
  136. $p = $this->getContainer()->query(PhotoCache::class);
  137. $p->delete(
  138. $event->getArgument('addressBookId'),
  139. $event->getArgument('cardUri')
  140. );
  141. }
  142. };
  143. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $clearPhotoCache);
  144. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', $clearPhotoCache);
  145. $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) {
  146. $user = $event->getSubject();
  147. $syncService = $this->getContainer()->query(SyncService::class);
  148. $syncService->updateUser($user);
  149. });
  150. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function(GenericEvent $event) {
  151. /** @var Backend $backend */
  152. $backend = $this->getContainer()->query(Backend::class);
  153. $backend->onCalendarAdd(
  154. $event->getArgument('calendarData')
  155. );
  156. });
  157. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function(GenericEvent $event) {
  158. /** @var Backend $backend */
  159. $backend = $this->getContainer()->query(Backend::class);
  160. $backend->onCalendarUpdate(
  161. $event->getArgument('calendarData'),
  162. $event->getArgument('shares'),
  163. $event->getArgument('propertyMutations')
  164. );
  165. });
  166. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function(GenericEvent $event) {
  167. /** @var Backend $backend */
  168. $backend = $this->getContainer()->query(Backend::class);
  169. $backend->onCalendarDelete(
  170. $event->getArgument('calendarData'),
  171. $event->getArgument('shares')
  172. );
  173. $reminderBackend = $this->getContainer()->query(ReminderBackend::class);
  174. $reminderBackend->cleanRemindersForCalendar(
  175. $event->getArgument('calendarId')
  176. );
  177. });
  178. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function(GenericEvent $event) {
  179. /** @var Backend $backend */
  180. $backend = $this->getContainer()->query(Backend::class);
  181. $backend->onCalendarUpdateShares(
  182. $event->getArgument('calendarData'),
  183. $event->getArgument('shares'),
  184. $event->getArgument('add'),
  185. $event->getArgument('remove')
  186. );
  187. // Here we should recalculate if reminders should be sent to new or old sharees
  188. });
  189. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', function(GenericEvent $event) {
  190. /** @var Backend $backend */
  191. $backend = $this->getContainer()->query(Backend::class);
  192. $backend->onCalendarPublication(
  193. $event->getArgument('calendarData'),
  194. $event->getArgument('public')
  195. );
  196. });
  197. $listener = function(GenericEvent $event, $eventName) {
  198. /** @var Backend $backend */
  199. $backend = $this->getContainer()->query(Backend::class);
  200. $subject = Event::SUBJECT_OBJECT_ADD;
  201. if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') {
  202. $subject = Event::SUBJECT_OBJECT_UPDATE;
  203. } else if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') {
  204. $subject = Event::SUBJECT_OBJECT_DELETE;
  205. }
  206. $backend->onTouchCalendarObject(
  207. $subject,
  208. $event->getArgument('calendarData'),
  209. $event->getArgument('shares'),
  210. $event->getArgument('objectData')
  211. );
  212. /** @var ReminderService $reminderBackend */
  213. $reminderService = $this->getContainer()->query(ReminderService::class);
  214. $reminderService->onTouchCalendarObject(
  215. $eventName,
  216. $event->getArgument('objectData')
  217. );
  218. };
  219. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener);
  220. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener);
  221. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener);
  222. }
  223. public function getSyncService() {
  224. return $this->getContainer()->query(SyncService::class);
  225. }
  226. public function registerNotifier():void {
  227. $this->getContainer()
  228. ->getServer()
  229. ->getNotificationManager()
  230. ->registerNotifierService(Notifier::class);
  231. }
  232. public function registerCalendarReminders():void {
  233. try {
  234. /** @var NotificationProviderManager $notificationProviderManager */
  235. $notificationProviderManager = $this->getContainer()->query(NotificationProviderManager::class);
  236. $notificationProviderManager->registerProvider(AudioProvider::class);
  237. $notificationProviderManager->registerProvider(EmailProvider::class);
  238. $notificationProviderManager->registerProvider(PushProvider::class);
  239. } catch(\Exception $ex) {
  240. $this->getContainer()->getServer()->getLogger()->logException($ex);
  241. }
  242. }
  243. }