00001
00002 var setURLResourceValuesForKeysFromProperties = function(aURL, keys, properties)
00003 {
00004 var resourceType = [properties objectForKey:@"resourcetype"];
00005
00006 if (resourceType === CPWebDAVManagerCollectionResourceType)
00007 {
00008 [aURL setResourceValue:YES forKey:CPURLIsDirectoryKey];
00009 [aURL setResourceValue:NO forKey:CPURLIsRegularFileKey];
00010 }
00011 else if (resourceType === CPWebDAVManagerNonCollectionResourceType)
00012 {
00013 [aURL setResourceValue:NO forKey:CPURLIsDirectoryKey];
00014 [aURL setResourceValue:YES forKey:CPURLIsRegularFileKey];
00015 }
00016
00017 var displayName = [properties objectForKey:@"displayname"];
00018
00019 if (displayName !== nil)
00020 {
00021 [aURL setResourceValue:displayName forKey:CPURLNameKey];
00022 [aURL setResourceValue:displayName forKey:CPURLLocalizedNameKey];
00023 }
00024 }
00025
00026 CPWebDAVManagerCollectionResourceType = 1;
00027 CPWebDAVManagerNonCollectionResourceType = 0;
00028
00029 @implementation CPWebDAVManager : CPObject
00030 {
00031 CPDictionary _blocksForConnections;
00032 }
00033
00034 - (id)init
00035 {
00036 self = [super init];
00037
00038 if (self)
00039 _blocksForConnections = [CPDictionary dictionary];
00040
00041 return self;
00042 }
00043
00044 - (CPArray)contentsOfDirectoryAtURL:(CPURL)aURL includingPropertiesForKeys:(CPArray)keys options:(CPDirectoryEnumerationOptions)aMask block:(Function)aBlock
00045 {
00046 var properties = [],
00047 count = [keys count];
00048
00049 while (count--)
00050 properties.push(WebDAVPropertiesForURLKeys[keys[count]]);
00051
00052 var makeContents = function(aURL, response)
00053 {
00054 var contents = [],
00055 URLString = nil,
00056 URLStrings = [response keyEnumerator];
00057
00058 while (URLString = [URLStrings nextObject])
00059 {
00060 var URL = [CPURL URLWithString:URLString],
00061 properties = [response objectForKey:URLString];
00062
00063
00064 if (![[URL absoluteString] isEqual:[aURL absoluteString]])
00065 {
00066 contents.push(URL);
00067
00068 setURLResourceValuesForKeysFromProperties(URL, keys, properties);
00069 }
00070 }
00071
00072 return contents;
00073 }
00074
00075 if (!aBlock)
00076 return makeContents(aURL, response);
00077
00078 [self PROPFIND:aURL properties:properties depth:1 block:function(aURL, response)
00079 {
00080 aBlock(aURL, makeContents(aURL, response));
00081 }];
00082 }
00083
00084 - (CPDictionary)PROPFIND:(CPURL)aURL properties:(CPDictionary)properties depth:(CPString)aDepth block:(Function)aBlock
00085 {
00086 var request = [CPURLRequest requestWithURL:aURL];
00087
00088 [request setHTTPMethod:@"PROPFIND"];
00089 [request setValue:aDepth forHTTPHeaderField:@"Depth"];
00090
00091 var HTTPBody = ["<?xml version=\"1.0\"?><a:propfind xmlns:a=\"DAV:\">"],
00092 index = 0,
00093 count = properties.length;
00094
00095 for (; index < count; ++index)
00096 HTTPBody.push("<a:prop><a:", properties[index], "/></a:prop>");
00097
00098 HTTPBody.push("</a:propfind>");
00099
00100 [request setHTTPBody:HTTPBody.join("")];
00101
00102 if (!aBlock)
00103 return parsePROPFINDResponse([[CPURLConnection sendSynchronousRequest:request returningResponse:nil] rawString]);
00104
00105 else
00106 {
00107 var connection = [CPURLConnection connectionWithRequest:request delegate:self];
00108
00109 [_blocksForConnections setObject:aBlock forKey:[connection UID]];
00110 }
00111 }
00112
00113 - (void)connection:(CPURLConnection)aURLConnection didReceiveData:(CPString)aString
00114 {
00115 var block = [_blocksForConnections objectForKey:[aURLConnection UID]];
00116
00117
00118 block([aURLConnection._request URL], parsePROPFINDResponse(aString));
00119 }
00120
00121 @end
00122
00123 var WebDAVPropertiesForURLKeys = { };
00124
00125 WebDAVPropertiesForURLKeys[CPURLNameKey] = @"displayname";
00126 WebDAVPropertiesForURLKeys[CPURLLocalizedNameKey] = @"displayname";
00127 WebDAVPropertiesForURLKeys[CPURLIsRegularFileKey] = @"resourcetype";
00128 WebDAVPropertiesForURLKeys[CPURLIsDirectoryKey] = @"resourcetype";
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 var XMLDocumentFromString = function(anXMLString)
00152 {
00153 if (typeof window["ActiveXObject"] !== "undefined")
00154 {
00155 var XMLDocument = new ActiveXObject("Microsoft.XMLDOM");
00156
00157 XMLDocument.async = false;
00158 XMLDocument.loadXML(anXMLString);
00159
00160 return XMLDocument;
00161 }
00162
00163 return new DOMParser().parseFromString(anXMLString,"text/xml");
00164 }
00165
00166 var parsePROPFINDResponse = function(anXMLString)
00167 {
00168 var XMLDocument = XMLDocumentFromString(anXMLString),
00169 responses = XMLDocument.getElementsByTagNameNS("*", "response"),
00170 responseIndex = 0,
00171 responseCount = responses.length;
00172
00173 var propertiesForURLs = [CPDictionary dictionary];
00174
00175 for (; responseIndex < responseCount; ++responseIndex)
00176 {
00177 var response = responses[responseIndex],
00178 elements = response.getElementsByTagNameNS("*", "prop").item(0).childNodes,
00179 index = 0,
00180 count = elements.length,
00181 properties = [CPDictionary dictionary];
00182
00183 for (; index < count; ++index)
00184 {
00185 var element = elements[index];
00186
00187 if (element.nodeType === 8 || element.nodeType === 3)
00188 continue;
00189
00190 var nodeName = element.nodeName,
00191 colonIndex = nodeName.lastIndexOf(':');
00192
00193 if (colonIndex > -1)
00194 nodeName = nodeName.substr(colonIndex + 1);
00195
00196 if (nodeName === @"resourcetype")
00197 [properties setObject:element.firstChild ? CPWebDAVManagerCollectionResourceType : CPWebDAVManagerNonCollectionResourceType forKey:nodeName];
00198
00199 else
00200 [properties setObject:element.firstChild.nodeValue forKey:nodeName];
00201 }
00202
00203 var href = response.getElementsByTagNameNS("*", "href").item(0);
00204
00205 [propertiesForURLs setObject:properties forKey:href.firstChild.nodeValue];
00206 }
00207
00208 return propertiesForURLs;
00209 }
00210
00211 var mapURLsAndProperties = function( properties, ignoredURL)
00212 {
00213
00214 }