application.php 4.9 KB

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