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