private void EndSync() { Log("Sync ends."); // Clears tokens. _activeCancellationTokenSources.ForEach(cts => cts.Dispose()); _activeCancellationTokenSources.Clear(); 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); } } }
private void OnLiveClientGetRootChildrenCompleted(System.Threading.Tasks.Task <LiveOperationResult> task) { if (!CheckTaskCompleted(task, "Get root children failed.")) { EndSyncDownloads(); return; } LiveOperationResult res = task.Result; // No result? Nothing to do. if (res.Result == null) { Log("Get root children returned no results."); EndSyncDownloads(); return; } // Sync Step 2: We are getting results for the app root folder. // We need to enumerate through all file entries to download GWC and GWS files. List <object> data = (List <object>)res.Result["data"]; // Enumerates through all the file entries. List <OneDriveFile> cartFiles = new List <OneDriveFile>(); List <OneDriveFile> extraFiles = new List <OneDriveFile>(); 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"]; try { Log("Found " + type + " called " + lname); } catch (Exception) { } if ("file".Equals(type)) { if (lname.EndsWith(".gwc")) { // Adds the file to the list of cartridges. Log("Marked as cartridge to download: " + name); cartFiles.Add(new OneDriveFile((string)content["id"], name, IsoStoreCartridgesPath)); } else if (lname.EndsWith(".gws")) { // Adds the file to the list of extra files. Log("Marked as extra file to download: " + name); extraFiles.Add(new OneDriveFile((string)content["id"], name, IsoStoreCartridgeContentPath)); } } } // Creates the list of cartridges in the isostore that do not exist on OneDrive anymore. // These will be removed. Extra files are not removed even if they are not on OneDrive 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 (OneDriveFile ef in extraFiles.ToList()) { if (isoStoreExtraFiles.Any(s => s.EndsWith("/" + ef.Name, StringComparison.InvariantCultureIgnoreCase))) { // The file needn't be downloaded. Log("Unmarked download because it exists locally: " + ef); extraFiles.Remove(ef); } } // Creates the list of cartridges and extra files that are on OneDrive but // not in the isolated storage. List <OneDriveFile> toDlFiles = cartFiles .Where(sd => !isoStoreFiles.Contains(GetIsoStorePath(sd.Name, sd.DownloadDirectory))) .Union(extraFiles) .ToList(); foreach (OneDriveFile file in toDlFiles) { Log("Scheduled to download: " + file); } Log("Total count of scheduled downloads: " + toDlFiles.Count); foreach (string fileName in toRemoveFiles) { Log("Scheduled to remove locally: " + fileName); } Log("Total count of scheduled local removals: " + toRemoveFiles.Count); // 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(); } }
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); } } }
private void OnLiveClientGetCompleted(object sender, LiveOperationCompletedEventArgs e) { // Cancels the timeout timer. CancelTimeoutTimer(); // No result? Nothing to do. if (e.Result == null) { EndSync(); 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. _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. EndSync(); 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 // file in the background. // Enumerates through all the file entries. List<SkyDriveFile> cartFiles = new List<SkyDriveFile>(); List<object> data = (List<object>)e.Result["data"]; foreach (IDictionary<string, object> content in data) { // Is it a cartridge file? string name = (string)content["name"]; if ("file".Equals(content["type"]) && name.ToLower().EndsWith(".gwc")) { // Adds the file to the list. string id = (string)content["id"]; lock (_syncRoot) { cartFiles.Add(new SkyDriveFile(id, name)); } } } // Creates the list of files in the isostore that do not exist // on SkyDrive anymore. List<string> isoStoreFiles; List<string> toRemoveFiles; using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { isoStoreFiles = isf .GetFileNames(GetIsoStorePath("*.gwc")) .Select(s => IsoStoreCartridgesPath + "/" + s) .ToList(); toRemoveFiles = isoStoreFiles .Where(s => !cartFiles.Any(sd => sd.Name == System.IO.Path.GetFileName(s))) .ToList(); } // Creates the list of cartridges that are on SkyDrive but // not in the isolated storage. List<SkyDriveFile> toDlFiles = cartFiles .Where(sd => !isoStoreFiles.Contains(GetIsoStorePath(sd.Name))) .ToList(); // Bakes an event for when the sync will be over. lock (_syncRoot) { _syncEventArgs = new CartridgeProviderSyncEventArgs() { AddedFiles = toDlFiles .Select(sd => GetIsoStorePath(sd.Name)) .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 => BeginDownloadCartridge(sd)); } else { EndSync(); } } }
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. try { string type = content.ContainsKey("type") ? ((content["type"] as string) ?? "<null>") : "<unknown>"; string name = content.ContainsKey("name") ? ((content["name"] as string) ?? "<null>") : "<unknown>"; Log("Found " + type + " called " + name); } catch (Exception ex) { Log("Exception while logging discovered entry: " + ex.Message); } 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. Log("Found Geowigo folder. Starts GetAsync."); _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. Log("No Geowigo folder found in root."); 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"]; try { Log("Found " + type + " called " + lname); } catch (Exception) { } if ("file".Equals(type)) { if (lname.EndsWith(".gwc")) { // Adds the file to the list of cartridges. Log("Marked as cartridge to download: " + name); cartFiles.Add(new SkyDriveFile((string)content["id"], name, IsoStoreCartridgesPath)); } else if (lname.EndsWith(".gws")) { // Adds the file to the list of extra files. Log("Marked as extra file to download: " + name); 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. Log("Found Uploads folder."); _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. Log("Unmarked download because it exists locally: " + ef); 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(); foreach (SkyDriveFile file in toDlFiles) { Log("Scheduled to download: " + file); } Log("Total count of scheduled downloads: " + toDlFiles.Count); foreach (string fileName in toRemoveFiles) { Log("Scheduled to remove locally: " + fileName); } Log("Total count of scheduled local removals: " + toRemoveFiles.Count); // 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(); } } }