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
|
(function(module, ns) {
var Errors = ns.require('request/errors');
/**
* Creates a propfind request.
*
* @param {Caldav.Connection} connection connection details.
* @param {Object} options options for propfind.
*/
function CalendarHome(connection, options) {
var key;
if (typeof(options) === 'undefined') {
options = {};
}
for (key in options) {
if (Object.hasOwnProperty.call(options, key)) {
this[key] = options[key];
}
}
this.connection = connection;
}
function findProperty(name, data, single) {
var url, results = [], prop;
for (url in data) {
if (Object.hasOwnProperty.call(data, url)) {
if (name in data[url]) {
prop = data[url][name];
if (prop.status === '200') {
results.push(data[url][name].value);
}
}
}
}
if (!results.length)
return false;
if (typeof(single) !== 'undefined' && single) {
return results[0];
}
return results;
}
CalendarHome.prototype = {
Propfind: ns.require('request/propfind'),
/**
* @return {Caldav.Xhr} The underlying xhr request so that the caller
* has a chance to abort the request.
*/
_findPrincipal: function(url, callback) {
var find = new this.Propfind(this.connection, {
url: url
});
find.prop('current-user-principal');
find.prop('principal-URL');
return find.send(function(err, data) {
var principal;
if (err) {
callback(err);
return;
}
principal = findProperty('current-user-principal', data, true);
if (!principal) {
principal = findProperty('principal-URL', data, true);
}
if ('unauthenticated' in principal) {
callback(new Errors.UnauthenticatedError());
} else if (principal.href){
callback(null, principal.href);
} else {
callback(new Errors.CaldavHttpError(404));
}
});
},
_findCalendarHome: function(url, callback) {
var details = {};
var find = new this.Propfind(this.connection, {
url: url
});
find.prop(['caldav', 'calendar-home-set']);
return find.send(function(err, data) {
if (err) {
callback(err);
return;
}
details = {
url: findProperty('calendar-home-set', data, true)
};
callback(null, details);
});
},
/**
* Starts request to find calendar home url
*
* @param {Function} callback node style where second argument
* are the details of the home calendar.
* @return {Caldav.Xhr} The underlying xhr request so that the caller
* has a chance to abort the request.
*/
send: function(callback) {
var self = this;
return self._findPrincipal(self.url, function(err, url) {
if (!url) {
callback(err);
return;
}
self._findCalendarHome(url, function(err, details) {
callback(err, details);
});
});
}
};
module.exports = CalendarHome;
}.apply(
this,
(this.Caldav) ?
[Caldav('request/calendar_home'), Caldav] :
[module, require('../caldav')]
));
|