PageRenderTime 142ms CodeModel.GetById 137ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/SkyDriveSimpleLibrary/SkyDriveConnection.cs

#
C# | 91 lines | 72 code | 5 blank | 14 comment | 4 complexity | 3fa1154c055ae739d2d2d34a07d9911a MD5 | raw file
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using System.Net;
 5using System.Xml;
 6using System.IO;
 7
 8public class SkyDriveConnection : PassportConnection
 9{
10	/// <summary>
11	/// Create a new object to handle SkyDrive communiction.
12	/// </summary>
13	/// <param name="userName"></param>
14	/// <param name="password"></param>
15	public SkyDriveConnection(string userName, string password)
16		: base(userName, password)
17	{
18	}
19
20	/// <summary>
21	/// Fetch all WebDAV folders visible using SkyDrive web service.
22	/// </summary>
23	/// <returns></returns>
24	public SkyDriveFolder[] GetDavFolders()
25	{
26		string soap = "<?xml version=\"1.0\" encoding=\"utf-8\"?><s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">"+
27			"<s:Body><GetWebAccountInfoRequest xmlns=\"http://schemas.microsoft.com/clouddocuments\">"+
28			"<BaseRequest xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">"+
29			"<ClientAppId>Microsoft Office/14.0 (Windows NT 6.1; Microsoft Word 14.0.4536; Pro)</ClientAppId><Market>en-US</Market>"+
30			"<SkyDocsServiceVersion>v1.0</SkyDocsServiceVersion></BaseRequest>"+
31			"<GetReadWriteLibrariesOnly>true</GetReadWriteLibrariesOnly></GetWebAccountInfoRequest></s:Body></s:Envelope>";
32
33		HttpWebResponse resp = SendSoapRequest("http://docs.live.net/SkyDocsService.svc", "GetWebAccountInfo", soap);
34		XmlDocument doc = new XmlDocument();
35		doc.LoadXml(GetResponseData(resp));
36		XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
37		nsmgr.AddNamespace("ms", "http://schemas.microsoft.com/clouddocuments");
38		XmlNodeList list = doc.SelectNodes("//ms:Library", nsmgr);
39		SkyDriveFolder[] arr = new SkyDriveFolder[list.Count];
40		for (int i = 0; i<list.Count; i++)
41		{
42			XmlElement el = list[i] as XmlElement;
43			SkyDriveFolder folder = new SkyDriveFolder();
44			folder.Name = el.SelectSingleNode("ms:DisplayName", nsmgr).InnerText;
45			folder.WebDAVUrl = el.SelectSingleNode("ms:DavUrl", nsmgr).InnerText;
46			arr[i] = folder;
47		}
48		return arr;
49	}
50
51	/// <summary>
52	/// Fetch contents of a single SkyDrive folder, using the WebDAV protocol.
53	/// </summary>
54	/// <param name="folder"></param>
55	/// <returns></returns>
56	public SkyDriveItem[] GetDavFolderContent(SkyDriveFolder folder)
57	{
58		InternalWebRequest spec = new InternalWebRequest("PROPFIND", folder.WebDAVUrl);
59		spec.Headers["Depth"] = "1";
60		spec.Headers["translate"] = "f";
61		HttpWebResponse resp = SendRequest(spec);
62
63		XmlDocument doc = new XmlDocument();
64		doc.LoadXml(GetResponseData(resp));
65		XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
66		nsmgr.AddNamespace("d", "DAV:");
67		XmlNodeList list = doc.SelectNodes("//d:response", nsmgr);
68		SkyDriveItem[] arr = new SkyDriveItem[list.Count-1];
69		for (int i = 1; i < list.Count; i++)
70		{
71			XmlElement el = list[i] as XmlElement;
72			string isFolder = el.SelectSingleNode("d:propstat/d:prop/d:isFolder", nsmgr).InnerText;
73			if (isFolder == "t")
74			{
75				SkyDriveFolder item = new SkyDriveFolder();
76				item.Name = el.SelectSingleNode("d:propstat/d:prop/d:displayname", nsmgr).InnerText;
77				item.WebDAVUrl = el.SelectSingleNode("d:href", nsmgr).InnerText;
78				arr[i - 1] = item;
79			}
80			else
81			{
82				SkyDriveFile item = new SkyDriveFile();
83				item.Name = el.SelectSingleNode("d:propstat/d:prop/d:displayname", nsmgr).InnerText;
84				item.Length = long.Parse(el.SelectSingleNode("d:propstat/d:prop/d:getcontentlength", nsmgr).InnerText);
85				item.Modified = DateTime.Parse(el.SelectSingleNode("d:propstat/d:prop/d:getlastmodified", nsmgr).InnerText);
86				arr[i - 1] = item;
87			}
88		}
89		return arr;
90	}
91}