Exemplo n.º 1
0
		private void OnLiveClientGetCompleted(object sender, LiveOperationCompletedEventArgs e)
		{
			// Cancels the timeout timer.
			CancelTimeoutTimer();
			
			// No result? Nothing to do.
			if (e.Result == null)
			{
				EndSyncDownloads(); 
				return;
			}

			if ("root".Equals(e.UserState))
			{
				// Sync Step 2: We are getting results for the root folder.
				// We need to enumerate through all file entries and find the first
				// folder whose name is "Geowigo".
				// Then, we will ask for file enumerations of this folder.
				// If no folder is found, the sync is over.

				// Enumerates through all the file entries.
				List<object> data = (List<object>)e.Result["data"];
				foreach (IDictionary<string, object> content in data)
				{
					// Is it a folder?
					if ("folder".Equals(content["type"]))
					{
						// Is its name "Geowigo"?
						if ("Geowigo".Equals((string)content["name"], StringComparison.InvariantCultureIgnoreCase))
						{
							// Sync Step 3. Asks for the list of files in this folder.

							// Stores the folder id.
							_geowigoFolderId = (string)content["id"];

							// Asks for the list of files.
							_liveClient.GetAsync((string)content["id"] + "/files", "geowigo");

							// Starts the timeout timer.
							StartTimeoutTimer(GetRequestTimeoutTimeSpan);

							// Nothing more to do.
							return;
						}
					}
				}
				
				// If we are here, it means that the Geowigo folder was not found.
				// The sync ends.
				EndSyncDownloads();
				return;
			}
			else if ("geowigo".Equals(e.UserState))
			{
				// Sync Step 4: We are getting results for the Geowigo folder.
				// We need to enumerate through all files and download each GWC
				// or GWS file in the background.

				List<object> data = (List<object>)e.Result["data"];

				// Enumerates through all the file entries.
				List<SkyDriveFile> cartFiles = new List<SkyDriveFile>();
				List<SkyDriveFile> extraFiles = new List<SkyDriveFile>();
				foreach (IDictionary<string, object> content in data)
				{
					// Is it a cartridge file?
					string name = (string)content["name"];
					string lname = name.ToLower();
					object type = content["type"];
					if ("file".Equals(type))
					{
						if (lname.EndsWith(".gwc"))
						{
							// Adds the file to the list of cartridges.
							cartFiles.Add(new SkyDriveFile((string)content["id"], name, IsoStoreCartridgesPath));
						}
						else if (lname.EndsWith(".gws"))
						{
							// Adds the file to the list of extra files.
							extraFiles.Add(new SkyDriveFile((string)content["id"], name, IsoStoreCartridgeContentPath));
						}
					}
					else if ("folder".Equals(type) && UploadFolderName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
					{
						// We found the uploads folder.
						// Stores its id.
						_uploadsFolderId = (string)content["id"];
					}
				}

				// Creates the list of cartridges in the isostore that do not exist 
				// on SkyDrive anymore.
				// These will be removed. Extra files are not removed even if they are not
				// on SkyDrive anymore.
				List<string> isoStoreFiles;
				List<string> toRemoveFiles;
				List<string> isoStoreExtraFiles;
				using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
				{
					isoStoreExtraFiles = isf.GetAllFiles(IsoStoreCartridgeContentPath + "/*.gws");
					
					isoStoreFiles =
						isf
						.GetFileNames(GetIsoStorePath("*.gwc", IsoStoreCartridgesPath))
						.Select(s => IsoStoreCartridgesPath + "/" + s)
						.ToList();

					toRemoveFiles =
						isoStoreFiles
						.Where(s => !cartFiles.Any(sd => sd.Name == System.IO.Path.GetFileName(s)))
						.ToList();
				}

				// Clears from the list of extra files to download those which
				// are already somewhere in the isolated storage.
				foreach (SkyDriveFile ef in extraFiles.ToList())
				{
					if (isoStoreExtraFiles.Any(s => s.EndsWith("/" + ef.Name, StringComparison.InvariantCultureIgnoreCase)))
					{
						// The file needn't be downloaded.
						extraFiles.Remove(ef);
					}
				} 

				// Creates the list of cartridges and extra files that are on SkyDrive but
				// not in the isolated storage.
				List<SkyDriveFile> toDlFiles = cartFiles
					.Where(sd => !isoStoreFiles.Contains(GetIsoStorePath(sd.Name, sd.DownloadDirectory)))
					.Union(extraFiles)
					.ToList();

				// Bakes an event for when the sync will be over.
				lock (_syncRoot)
				{
					_syncEventArgs = new CartridgeProviderSyncEventArgs()
					{
						AddedFiles = toDlFiles
							.Select(sd => GetIsoStorePath(sd.Name, sd.DownloadDirectory))
							.ToList(),
						ToRemoveFiles = toRemoveFiles
					};
				}

				// Sends a progress event for removing the files marked
				// to be removed.
				if (toRemoveFiles.Count > 0)
				{
					RaiseSyncProgress(toRemoveFiles: toRemoveFiles);
				}

				// Starts downloading all new files, or terminate
				// the sync if no new file is to be downloaded.
				if (toDlFiles.Count > 0)
				{
					toDlFiles.ForEach(sd => BeginDownloadFile(sd));
				}
				else
				{
					EndSyncDownloads();
				}
			}
		}
Exemplo n.º 2
0
		private void EndSync()
		{
			CartridgeProviderSyncEventArgs e;

			// Marks the sync as completed.
			lock (_syncRoot)
			{
				e = _syncEventArgs;
				_syncEventArgs = null;
			}
			IsSyncing = false;

			// Notifies that the sync is finished.
			if (e != null)
			{
				if (SyncCompleted != null)
				{
					SyncCompleted(this, e);
				}
			}
		}