Application.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Thomas Müller <thomas.mueller@tmit.eu>
  6. *
  7. * @copyright Copyright (c) 2016, ownCloud, Inc.
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\DAV\AppInfo;
  24. use OCA\DAV\CalDAV\BirthdayService;
  25. use OCA\DAV\CalDAV\CalDavBackend;
  26. use OCA\DAV\CardDAV\CardDavBackend;
  27. use OCA\DAV\CardDAV\ContactsManager;
  28. use OCA\DAV\CardDAV\SyncJob;
  29. use OCA\DAV\CardDAV\SyncService;
  30. use OCA\DAV\Connector\Sabre\Principal;
  31. use OCA\DAV\DAV\GroupPrincipalBackend;
  32. use OCA\DAV\HookManager;
  33. use OCA\DAV\Migration\Classification;
  34. use OCA\DAV\Migration\GenerateBirthdays;
  35. use \OCP\AppFramework\App;
  36. use OCP\AppFramework\IAppContainer;
  37. use OCP\Contacts\IManager;
  38. use OCP\IUser;
  39. use Sabre\VObject\Reader;
  40. use Symfony\Component\EventDispatcher\GenericEvent;
  41. class Application extends App {
  42. /**
  43. * Application constructor.
  44. *
  45. * @param array $urlParams
  46. */
  47. public function __construct (array $urlParams=array()) {
  48. parent::__construct('dav', $urlParams);
  49. $container = $this->getContainer();
  50. $container->registerService('ContactsManager', function($c) {
  51. /** @var IAppContainer $c */
  52. return new ContactsManager(
  53. $c->query('CardDavBackend')
  54. );
  55. });
  56. $container->registerService('HookManager', function($c) {
  57. /** @var IAppContainer $c */
  58. return new HookManager(
  59. $c->getServer()->getUserManager(),
  60. $c->query('SyncService'),
  61. $c->query('CalDavBackend'),
  62. $c->query('CardDavBackend')
  63. );
  64. });
  65. $container->registerService('SyncService', function($c) {
  66. /** @var IAppContainer $c */
  67. return new SyncService(
  68. $c->query('CardDavBackend'),
  69. $c->getServer()->getUserManager(),
  70. $c->getServer()->getLogger()
  71. );
  72. });
  73. $container->registerService('CardDavBackend', function($c) {
  74. /** @var IAppContainer $c */
  75. $db = $c->getServer()->getDatabaseConnection();
  76. $dispatcher = $c->getServer()->getEventDispatcher();
  77. $principal = new Principal(
  78. $c->getServer()->getUserManager(),
  79. $c->getServer()->getGroupManager()
  80. );
  81. return new CardDavBackend($db, $principal, $dispatcher);
  82. });
  83. $container->registerService('CalDavBackend', function($c) {
  84. /** @var IAppContainer $c */
  85. $db = $c->getServer()->getDatabaseConnection();
  86. $principal = new Principal(
  87. $c->getServer()->getUserManager(),
  88. $c->getServer()->getGroupManager()
  89. );
  90. return new CalDavBackend($db, $principal);
  91. });
  92. $container->registerService('BirthdayService', function($c) {
  93. /** @var IAppContainer $c */
  94. $g = new GroupPrincipalBackend(
  95. $c->getServer()->getGroupManager()
  96. );
  97. return new BirthdayService(
  98. $c->query('CalDavBackend'),
  99. $c->query('CardDavBackend'),
  100. $g
  101. );
  102. });
  103. $container->registerService('OCA\DAV\Migration\Classification', function ($c) {
  104. /** @var IAppContainer $c */
  105. return new Classification(
  106. $c->query('CalDavBackend'),
  107. $c->getServer()->getUserManager()
  108. );
  109. });
  110. $container->registerService('OCA\DAV\Migration\GenerateBirthdays', function ($c) {
  111. /** @var IAppContainer $c */
  112. /** @var BirthdayService $b */
  113. $b = $c->query('BirthdayService');
  114. return new GenerateBirthdays(
  115. $b,
  116. $c->getServer()->getUserManager()
  117. );
  118. });
  119. }
  120. /**
  121. * @param IManager $contactsManager
  122. * @param string $userID
  123. */
  124. public function setupContactsProvider(IManager $contactsManager, $userID) {
  125. /** @var ContactsManager $cm */
  126. $cm = $this->getContainer()->query('ContactsManager');
  127. $urlGenerator = $this->getContainer()->getServer()->getURLGenerator();
  128. $cm->setupContactsProvider($contactsManager, $userID, $urlGenerator);
  129. }
  130. public function registerHooks() {
  131. /** @var HookManager $hm */
  132. $hm = $this->getContainer()->query('HookManager');
  133. $hm->setup();
  134. $listener = function($event) {
  135. if ($event instanceof GenericEvent) {
  136. /** @var BirthdayService $b */
  137. $b = $this->getContainer()->query('BirthdayService');
  138. $b->onCardChanged(
  139. $event->getArgument('addressBookId'),
  140. $event->getArgument('cardUri'),
  141. $event->getArgument('cardData')
  142. );
  143. }
  144. };
  145. $dispatcher = $this->getContainer()->getServer()->getEventDispatcher();
  146. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::createCard', $listener);
  147. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::updateCard', $listener);
  148. $dispatcher->addListener('\OCA\DAV\CardDAV\CardDavBackend::deleteCard', function($event) {
  149. if ($event instanceof GenericEvent) {
  150. /** @var BirthdayService $b */
  151. $b = $this->getContainer()->query('BirthdayService');
  152. $b->onCardDeleted(
  153. $event->getArgument('addressBookId'),
  154. $event->getArgument('cardUri')
  155. );
  156. }
  157. });
  158. }
  159. public function getSyncService() {
  160. return $this->getContainer()->query('SyncService');
  161. }
  162. }