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
|
angular.module('contactsApp')
.factory('AddressBookService', function(DavClient, DavService, SettingsService, AddressBook, $q) {
var addressBooks = [];
var loadPromise = undefined;
var observerCallbacks = [];
var notifyObservers = function(eventName) {
var ev = {
event: eventName,
addressBooks: addressBooks
};
angular.forEach(observerCallbacks, function(callback) {
callback(ev);
});
};
var loadAll = function() {
if (addressBooks.length > 0) {
return $q.when(addressBooks);
}
if (_.isUndefined(loadPromise)) {
loadPromise = DavService.then(function(account) {
loadPromise = undefined;
addressBooks = account.addressBooks.map(function(addressBook) {
return new AddressBook(addressBook);
});
});
}
return loadPromise;
};
return {
registerObserverCallback: function(callback) {
observerCallbacks.push(callback);
},
getAll: function() {
return loadAll().then(function() {
return addressBooks;
});
},
getGroups: function () {
return this.getAll().then(function(addressBooks) {
return addressBooks.map(function (element) {
return element.groups;
}).reduce(function(a, b) {
return a.concat(b);
});
});
},
getDefaultAddressBook: function() {
return addressBooks[0];
},
getAddressBook: function(displayName) {
return DavService.then(function(account) {
return DavClient.getAddressBook({displayName:displayName, url:account.homeUrl}).then(function(res) {
var addressBook = new AddressBook({
account: account,
ctag: res[0].props.getctag,
url: account.homeUrl+displayName+'/',
data: res[0],
displayName: res[0].props.displayname,
resourcetype: res[0].props.resourcetype,
syncToken: res[0].props.syncToken
});
return addressBook;
});
});
},
create: function(displayName) {
return DavService.then(function(account) {
return DavClient.createAddressBook({displayName:displayName, url:account.homeUrl});
});
},
delete: function(addressBook) {
return DavService.then(function() {
return DavClient.deleteAddressBook(addressBook).then(function() {
var index = addressBooks.indexOf(addressBook);
addressBooks.splice(index, 1);
notifyObservers('delete');
});
});
},
rename: function(addressBook, displayName) {
return DavService.then(function(account) {
return DavClient.renameAddressBook(addressBook, {displayName:displayName, url:account.homeUrl});
});
},
get: function(displayName) {
return this.getAll().then(function(addressBooks) {
return addressBooks.filter(function (element) {
return element.displayName === displayName;
})[0];
});
},
sync: function(addressBook) {
return DavClient.syncAddressBook(addressBook);
},
share: function(addressBook, shareType, shareWith, writable, existingShare) {
var xmlDoc = document.implementation.createDocument('', '', null);
var oShare = xmlDoc.createElement('o:share');
oShare.setAttribute('xmlns:d', 'DAV:');
oShare.setAttribute('xmlns:o', 'http://owncloud.org/ns');
xmlDoc.appendChild(oShare);
var oSet = xmlDoc.createElement('o:set');
oShare.appendChild(oSet);
var dHref = xmlDoc.createElement('d:href');
if (shareType === OC.Share.SHARE_TYPE_USER) {
dHref.textContent = 'principal:principals/users/';
} else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
dHref.textContent = 'principal:principals/groups/';
}
dHref.textContent += shareWith;
oSet.appendChild(dHref);
var oSummary = xmlDoc.createElement('o:summary');
oSummary.textContent = t('contacts', '{addressbook} shared by {owner}', {
addressbook: addressBook.displayName,
owner: addressBook.owner
});
oSet.appendChild(oSummary);
if (writable) {
var oRW = xmlDoc.createElement('o:read-write');
oSet.appendChild(oRW);
}
var body = oShare.outerHTML;
return DavClient.xhr.send(
dav.request.basic({method: 'POST', data: body}),
addressBook.url
).then(function(response) {
if (response.status === 200) {
if (!existingShare) {
if (shareType === OC.Share.SHARE_TYPE_USER) {
addressBook.sharedWith.users.push({
id: shareWith,
displayname: shareWith,
writable: writable
});
} else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
addressBook.sharedWith.groups.push({
id: shareWith,
displayname: shareWith,
writable: writable
});
}
}
}
});
},
unshare: function(addressBook, shareType, shareWith) {
var xmlDoc = document.implementation.createDocument('', '', null);
var oShare = xmlDoc.createElement('o:share');
oShare.setAttribute('xmlns:d', 'DAV:');
oShare.setAttribute('xmlns:o', 'http://owncloud.org/ns');
xmlDoc.appendChild(oShare);
var oRemove = xmlDoc.createElement('o:remove');
oShare.appendChild(oRemove);
var dHref = xmlDoc.createElement('d:href');
if (shareType === OC.Share.SHARE_TYPE_USER) {
dHref.textContent = 'principal:principals/users/';
} else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
dHref.textContent = 'principal:principals/groups/';
}
dHref.textContent += shareWith;
oRemove.appendChild(dHref);
var body = oShare.outerHTML;
return DavClient.xhr.send(
dav.request.basic({method: 'POST', data: body}),
addressBook.url
).then(function(response) {
if (response.status === 200) {
if (shareType === OC.Share.SHARE_TYPE_USER) {
addressBook.sharedWith.users = addressBook.sharedWith.users.filter(function(user) {
return user.id !== shareWith;
});
} else if (shareType === OC.Share.SHARE_TYPE_GROUP) {
addressBook.sharedWith.groups = addressBook.sharedWith.groups.filter(
|