API  1.0.0
CPWebDAVManager.j
Go to the documentation of this file.
1 /*
2  * CPWebDAVManager.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 var setURLResourceValuesForKeysFromProperties = function(aURL, keys, properties)
25 {
26  var resourceType = [properties objectForKey:@"resourcetype"];
27 
28  if (resourceType === CPWebDAVManagerCollectionResourceType)
29  {
30  [aURL setResourceValue:YES forKey:CPURLIsDirectoryKey];
31  [aURL setResourceValue:NO forKey:CPURLIsRegularFileKey];
32  }
33  else if (resourceType === CPWebDAVManagerNonCollectionResourceType)
34  {
35  [aURL setResourceValue:NO forKey:CPURLIsDirectoryKey];
36  [aURL setResourceValue:YES forKey:CPURLIsRegularFileKey];
37  }
38 
39  var displayName = [properties objectForKey:@"displayname"];
40 
41  if (displayName !== nil)
42  {
43  [aURL setResourceValue:displayName forKey:CPURLNameKey];
44  [aURL setResourceValue:displayName forKey:CPURLLocalizedNameKey];
45  }
46 };
47 
50 
51 @implementation CPWebDAVManager : CPObject
52 {
53  CPDictionary _blocksForConnections;
54 }
55 
56 - (id)init
57 {
58  self = [super init];
59 
60  if (self)
61  _blocksForConnections = @{};
62 
63  return self;
64 }
65 
66 - (CPArray)contentsOfDirectoryAtURL:(CPURL)aURL includingPropertiesForKeys:(CPArray)keys options:(CPDirectoryEnumerationOptions)aMask block:(Function)aBlock
67 {
68  var properties = [],
69  count = [keys count];
70 
71  while (count--)
72  properties.push(WebDAVPropertiesForURLKeys[keys[count]]);
73 
74  var makeContents = function(aURL, response)
75  {
76  var contents = [],
77  URLString = nil,
78  URLStrings = [response keyEnumerator];
79 
80  while ((URLString = [URLStrings nextObject]) !== nil)
81  {
82  var URL = [CPURL URLWithString:URLString],
83  properties = [response objectForKey:URLString];
84 
85  // FIXME: We need better way of comparing URLs.
86  if (![[URL absoluteString] isEqual:[aURL absoluteString]])
87  {
88  contents.push(URL);
89 
90  setURLResourceValuesForKeysFromProperties(URL, keys, properties);
91  }
92  }
93 
94  return contents;
95  };
96 
97  if (!aBlock)
98  return makeContents(aURL, [self PROPFIND:aURL properties:properties depth:1 block:nil]);
99 
100  [self PROPFIND:aURL properties:properties depth:1 block:function(aURL, response)
101  {
102  aBlock(aURL, makeContents(aURL, response));
103  }];
104 }
105 
106 - (CPDictionary)PROPFIND:(CPURL)aURL properties:(CPDictionary)properties depth:(CPString)aDepth block:(Function)aBlock
107 {
108  var request = [CPURLRequest requestWithURL:aURL];
109 
110  [request setHTTPMethod:@"PROPFIND"];
111  [request setValue:aDepth forHTTPHeaderField:@"Depth"];
112 
113  var HTTPBody = ["<?xml version=\"1.0\"?><a:propfind xmlns:a=\"DAV:\">"],
114  index = 0,
115  count = properties.length;
116 
117  for (; index < count; ++index)
118  HTTPBody.push("<a:prop><a:", properties[index], "/></a:prop>");
119 
120  HTTPBody.push("</a:propfind>");
121 
122  [request setHTTPBody:HTTPBody.join("")];
123 
124  if (!aBlock)
125  return parsePROPFINDResponse([[CPURLConnection sendSynchronousRequest:request returningResponse:nil] rawString]);
126 
127  else
128  {
129  var connection = [CPURLConnection connectionWithRequest:request delegate:self];
130 
131  [_blocksForConnections setObject:aBlock forKey:[connection UID]];
132  }
133 }
134 
135 - (void)connection:(CPURLConnection)aURLConnection didReceiveData:(CPString)aString
136 {
137  var block = [_blocksForConnections objectForKey:[aURLConnection UID]];
138 
139  // FIXME: get the request...
140  block([aURLConnection._request URL], parsePROPFINDResponse(aString));
141 }
142 
143 @end
144 
146 
147 WebDAVPropertiesForURLKeys[CPURLNameKey] = @"displayname";
148 WebDAVPropertiesForURLKeys[CPURLLocalizedNameKey] = @"displayname";
149 WebDAVPropertiesForURLKeys[CPURLIsRegularFileKey] = @"resourcetype";
150 WebDAVPropertiesForURLKeys[CPURLIsDirectoryKey] = @"resourcetype";
151 //CPURLIsSymbolicLinkKey = @"CPURLIsSymbolicLinkKey";
152 //CPURLIsVolumeKey = @"CPURLIsVolumeKey";
153 //CPURLIsPackageKey = @"CPURLIsPackageKey";
154 //CPURLIsSystemImmutableKey = @"CPURLIsSystemImmutableKey";
155 //CPURLIsUserImmutableKey = @"CPURLIsUserImmutableKey";
156 //CPURLIsHiddenKey = @"CPURLIsHiddenKey";
157 //CPURLHasHiddenExtensionKey = @"CPURLHasHiddenExtensionKey";
158 //CPURLCreationDateKey = @"CPURLCreationDateKey";
159 //CPURLContentAccessDateKey = @"CPURLContentAccessDateKey";
160 //CPURLContentModificationDateKey = @"CPURLContentModificationDateKey";
161 //CPURLAttributeModificationDateKey = @"CPURLAttributeModificationDateKey";
162 //CPURLLinkCountKey = @"CPURLLinkCountKey";
163 //CPURLParentDirectoryURLKey = @"CPURLParentDirectoryURLKey";
164 //CPURLVolumeURLKey = @"CPURLVolumeURLKey";
165 //CPURLTypeIdentifierKey = @"CPURLTypeIdentifierKey";
166 //CPURLLocalizedTypeDescriptionKey = @"CPURLLocalizedTypeDescriptionKey";
167 //CPURLLabelNumberKey = @"CPURLLabelNumberKey";
168 //CPURLLabelColorKey = @"CPURLLabelColorKey";
169 //CPURLLocalizedLabelKey = @"CPURLLocalizedLabelKey";
170 //CPURLEffectiveIconKey = @"CPURLEffectiveIconKey";
171 //CPURLCustomIconKey = @"CPURLCustomIconKey";
172 
173 var XMLDocumentFromString = function(anXMLString)
174 {
175  if (typeof window["ActiveXObject"] !== "undefined")
176  {
177  var XMLDocument = new ActiveXObject("Microsoft.XMLDOM");
178 
179  XMLDocument.async = false;
180  XMLDocument.loadXML(anXMLString);
181 
182  return XMLDocument;
183  }
184 
185  return new DOMParser().parseFromString(anXMLString,"text/xml");
186 };
187 
188 var parsePROPFINDResponse = function(anXMLString)
189 {
190  var XMLDocument = XMLDocumentFromString(anXMLString),
191  responses = XMLDocument.getElementsByTagNameNS("*", "response"),
192  responseIndex = 0,
193  responseCount = responses.length,
194  propertiesForURLs = @{};
195 
196  for (; responseIndex < responseCount; ++responseIndex)
197  {
198  var response = responses[responseIndex],
199  elements = response.getElementsByTagNameNS("*", "prop").item(0).childNodes,
200  index = 0,
201  count = elements.length,
202  properties = @{};
203 
204  for (; index < count; ++index)
205  {
206  var element = elements[index];
207 
208  if (element.nodeType === 8 || element.nodeType === 3)
209  continue;
210 
211  var nodeName = element.nodeName,
212  colonIndex = nodeName.lastIndexOf(':');
213 
214  if (colonIndex > -1)
215  nodeName = nodeName.substr(colonIndex + 1);
216 
217  if (nodeName === @"resourcetype")
218  [properties setObject:element.firstChild ? CPWebDAVManagerCollectionResourceType : CPWebDAVManagerNonCollectionResourceType forKey:nodeName];
219  else
220  [properties setObject:element.firstChild.nodeValue forKey:nodeName];
221  }
222 
223  var href = response.getElementsByTagNameNS("*", "href").item(0);
224 
225  [propertiesForURLs setObject:properties forKey:href.firstChild.nodeValue];
226  }
227 
228  return propertiesForURLs;
229 };
230 
231 var mapURLsAndProperties = function(/*CPDictionary*/ properties, /*CPURL*/ ignoredURL)
232 {
233 
234 };
var WebDAVPropertiesForURLKeys
id init()
Definition: CALayer.j:126
var isEqual
CPDictionary PROPFIND:properties:depth:block:(CPURL aURL, [properties] CPDictionary properties, [depth] CPString aDepth, [block] Function aBlock)
CGImage contents()
Definition: CALayer.j:419
Provides loading of a URL request.
var setURLResourceValuesForKeysFromProperties
A mutable key-value pair collection.
Definition: CPDictionary.h:2
id requestWithURL:(CPURL aURL)
Definition: CPURLRequest.j:56
var mapURLsAndProperties
An immutable string (collection of characters).
Definition: CPString.h:2
CPWebDAVManagerNonCollectionResourceType
CPURLConnection connectionWithRequest:delegate:(CPURLRequest aRequest, [delegate] id aDelegate)
id init()
Definition: CPObject.j:145
var XMLDocumentFromString
Contains data obtained during a request made with CPURLConnection.
Definition: CPURLRequest.h:2
id URLWithString:(CPString URLString)
Definition: CPURL.j:78
var parsePROPFINDResponse
Definition: CPURL.h:2
void setObject:forKey:(id anObject, [forKey] id aKey)
Definition: CPDictionary.j:589
CPString UID()
Definition: CPObject.j:552
CPWebDAVManagerCollectionResourceType