Application.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\DAV\AppInfo;
  27. use OC\AppFramework\Utility\SimpleContainer;
  28. use OCA\DAV\CalDAV\Activity\Backend;
  29. use OCA\DAV\CalDAV\Activity\Provider\Event;
  30. use OCA\DAV\CalDAV\BirthdayService;
  31. use OCA\DAV\CalDAV\CalendarManager;
  32. use OCA\DAV\Capabilities;
  33. use OCA\DAV\CardDAV\ContactsManager;
  34. use OCA\DAV\CardDAV\PhotoCache;
  35. use OCA\DAV\CardDAV\SyncService;
  36. use OCA\DAV\HookManager;
  37. use \OCP\AppFramework\App;
  38. use OCP\Contacts\IManager as IContactsManager;
  39. use OCP\Calendar\IManager as ICalendarManager;
  40. use OCP\IUser;
  41. use Symfony\Component\EventDispatcher\GenericEvent;
  42. class Application extends App {
  43. /**
  44. * Application constructor.
  45. */
  46. public function __construct() {
  47. parent::__construct('dav');
  48. $container = $this->getContainer();
  49. $server = $container->getServer();
  50. $container->registerService(PhotoCache::class, function(SimpleContainer $s) use ($server) {
  51. return new PhotoCache(
  52. $server->getAppDataDir('dav-photocache'),
  53. $server->getLogger()
  54. );
  55. });
  56. /*
  57. * Register capabilities
  58. */
  59. $container->registerCapability(Capabilities::class);
  60. }
  61. /**
  62. * @param IContactsManager $contactsManager
  63. * @param string $userID
  64. */
  65. public function setupContactsProvider(IContactsManager $contactsManager, $userID) {
  66. /** @var ContactsManager $cm */
  67. $cm = $this->getContainer()->query(ContactsManager::class);
  68. $urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
  69. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  70. }
  71. /**
  72. * @param IManager $contactsManager
  73. */
  74. public function setupSystemContactsProvider(IContactsManager $contactsManager) {
  75. /** @var ContactsManager $cm */
  76. $cm = $this->getContainer()->query(ContactsManager::class);
  77. $urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
  78. $cm->setupSystemContactsProvider($contactsManager, $urlGenerator);
  79. }
  80. /**
  81. * @param ICalendarManager $calendarManager
  82. * @param string $userId
  83. */
  84. public function setupCalendarProvider(ICalendarManager $calendarManager, $userId) {
  85. $cm = $this->getContainer()->query(CalendarManager::class);
  86. $cm->setupCalendarProvider($calendarManager, $userId);
  87. }
  88. public function registerHooks() {
  89. /** @var HookManager $hm */
  90. $hm = $this->getContainer()->query(HookManager::class);
  91. $hm->setup();
  92. $dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  93. // first time login event setup
  94. $dispatcher->addListener(IUser::class . '::firstLogin', function ($event) use ($hm) {
  95. if ($event instanceof GenericEvent) {
  96. $hm->firstLogin($event->getSubject());
  97. }
  98. });
  99. // carddav/caldav sync event setup
  100. $listener = function($event) {
  101. if ($event instanceof GenericEvent) {
  102. /** @var BirthdayService $b */
  103. $b = $this->getContainer()->query(BirthdayService::class);
  104. $b->onCardChanged(
  105. $event->getArgument('addressBookId'),
  106. $event->getArgument('cardUri'),
  107. $event->getArgument('cardData')
  108. );
  109. }
  110. };
  111. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener);
  112. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener);
  113. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) {
  114. if ($event instanceof GenericEvent) {
  115. /** @var BirthdayService $b */
  116. $b = $this->getContainer()->query(BirthdayService::class);
  117. $b->onCardDeleted(
  118. $event->getArgument('addressBookId'),
  119. $event->getArgument('cardUri')
  120. );
  121. }
  122. });
  123. $clearPhotoCache = function($event) {
  124. if ($event instanceof GenericEvent) {
  125. /** @var PhotoCache $p */
  126. $p = $this->getContainer()->query(PhotoCache::class);
  127. $p->delete(
  128. $event->getArgument('addressBookId'),
  129. $event->getArgument('cardUri')
  130. );
  131. }
  132. };
  133. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $clearPhotoCache);
  134. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', $clearPhotoCache);
  135. $dispatcher->addListener('OC\AccountManager::userUpdated', function(GenericEvent $event) {
  136. $user = $event->getSubject();
  137. $syncService = $this->getContainer()->query(SyncService::class);
  138. $syncService->updateUser($user);
  139. });
  140. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendar', function(GenericEvent $event) {
  141. /** @var Backend $backend */
  142. $backend = $this->getContainer()->query(Backend::class);
  143. $backend->onCalendarAdd(
  144. $event->getArgument('calendarData')
  145. );
  146. });
  147. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendar', function(GenericEvent $event) {
  148. /** @var Backend $backend */
  149. $backend = $this->getContainer()->query(Backend::class);
  150. $backend->onCalendarUpdate(
  151. $event->getArgument('calendarData'),
  152. $event->getArgument('shares'),
  153. $event->getArgument('propertyMutations')
  154. );
  155. });
  156. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendar', function(GenericEvent $event) {
  157. /** @var Backend $backend */
  158. $backend = $this->getContainer()->query(Backend::class);
  159. $backend->onCalendarDelete(
  160. $event->getArgument('calendarData'),
  161. $event->getArgument('shares')
  162. );
  163. });
  164. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateShares', function(GenericEvent $event) {
  165. /** @var Backend $backend */
  166. $backend = $this->getContainer()->query(Backend::class);
  167. $backend->onCalendarUpdateShares(
  168. $event->getArgument('calendarData'),
  169. $event->getArgument('shares'),
  170. $event->getArgument('add'),
  171. $event->getArgument('remove')
  172. );
  173. });
  174. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::publishCalendar', function(GenericEvent $event) {
  175. /** @var Backend $backend */
  176. $backend = $this->getContainer()->query(Backend::class);
  177. $backend->onCalendarPublication(
  178. $event->getArgument('calendarData'),
  179. $event->getArgument('public')
  180. );
  181. });
  182. $listener = function(GenericEvent $event, $eventName) {
  183. /** @var Backend $backend */
  184. $backend = $this->getContainer()->query(Backend::class);
  185. $subject = Event::SUBJECT_OBJECT_ADD;
  186. if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject') {
  187. $subject = Event::SUBJECT_OBJECT_UPDATE;
  188. } else if ($eventName === '\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject') {
  189. $subject = Event::SUBJECT_OBJECT_DELETE;
  190. }
  191. $backend->onTouchCalendarObject(
  192. $subject,
  193. $event->getArgument('calendarData'),
  194. $event->getArgument('shares'),
  195. $event->getArgument('objectData')
  196. );
  197. };
  198. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createCalendarObject', $listener);
  199. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::updateCalendarObject', $listener);
  200. $dispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteCalendarObject', $listener);
  201. }
  202. public function getSyncService() {
  203. return $this->getContainer()->query(SyncService::class);
  204. }
  205. }