/// <summary> /// Will start a backup process for field books. /// </summary> /// <returns></returns> public async Task BackupFieldBook() { //Set progress ring _progressRingActive = true; _progressRingVisibility = true; RaisePropertyChanged("ProgressRingActive"); RaisePropertyChanged("ProgressRingVisibility"); //Variables List <StorageFile> FilesToBackup = new List <StorageFile>(); FileServices fs = new FileServices(); //Iterate through field book folder files //Make sure something is selected and that user is not trying to delete the only project left if (_projectCollection != null && _selectedProjectIndex != -1) { FieldBooks selectedBook = _projectCollection[_selectedProjectIndex]; StorageFolder fieldBook = await StorageFolder.GetFolderFromPathAsync(selectedBook.ProjectPath); //Get a list of files from field book IReadOnlyList <StorageFile> fieldBookPhotosRO = await fieldBook.GetFilesAsync(); //calculate new name for output database in the archive string uCode = currentLocalSettings.Containers[ApplicationLiterals.LocalSettingMainContainer].Values[Dictionaries.DatabaseLiterals.FieldUserInfoUCode].ToString(); FileServices fService = new FileServices(); string newName = fService.CalculateDBCopyName(uCode) + ".sqlite"; StorageFile databaseToRename = null; //Build list of files to add to archive foreach (StorageFile files in fieldBookPhotosRO) { //Get databases if (files.Name.ToLower().Contains(".sqlite") && files.Name.Contains(Dictionaries.DatabaseLiterals.DBName)) { databaseToRename = files; } else if (!files.Name.Contains("zip")) { FilesToBackup.Add(files); } } //Copy and rename database if (databaseToRename != null) { StorageFile newFile = await databaseToRename.CopyAsync(fieldBook, newName); FilesToBackup.Add(newFile); //Zip and Copy var loadLocalization = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); await fs.SaveArchiveCopy(FilesToBackup, selectedBook.ProjectPath, selectedBook.metadataForProject.UserCode, selectedBook.metadataForProject.ProjectName + "_"); await newFile.DeleteAsync(); } } //Unset progress ring _progressRingActive = false; _progressRingVisibility = false; RaisePropertyChanged("ProgressRingActive"); RaisePropertyChanged("ProgressRingVisibility"); return; }
/// <summary> /// Will save a field work copy, from local state folder to the my document folder of current user and will prompt a message when done. /// </summary> public async void QuickBackupAsync() { //Variables List <StorageFile> FilesToBackup = new List <StorageFile>(); FileServices fs = new FileServices(); int photoCount = 0; bool hasPhotos = false; // Will prevent warning message of no backuped photo showing when no photos were taken on the device DateTimeOffset youngestPhoto = DateTimeOffset.MinValue; //Default for current DateTimeOffset inMemoryYoungestPhoto = DateTimeOffset.MinValue; string projectName = string.Empty; if (localSetting.GetSettingValue(Dictionaries.ApplicationLiterals.KeywordBackupPhotoYoungest) != null) { inMemoryYoungestPhoto = (DateTimeOffset)localSetting.GetSettingValue(Dictionaries.ApplicationLiterals.KeywordBackupPhotoYoungest); } if (localSetting.GetSettingValue(Dictionaries.DatabaseLiterals.FieldUserInfoPName) != null) { projectName = localSetting.GetSettingValue(Dictionaries.DatabaseLiterals.FieldUserInfoPName).ToString(); } //calculate new name for output database in the archive string uCode = currentLocalSettings.Containers[ApplicationLiterals.LocalSettingMainContainer].Values[Dictionaries.DatabaseLiterals.FieldUserInfoUCode].ToString(); FileServices fService = new FileServices(); string newName = fService.CalculateDBCopyName(uCode) + ".sqlite"; //Iterate through field book folder files string _fieldBooLocalPath = localSetting.GetSettingValue(Dictionaries.ApplicationLiterals.KeywordFieldProject).ToString(); StorageFolder fieldBook = await StorageFolder.GetFolderFromPathAsync(_fieldBooLocalPath); //Get a list of files from field book IReadOnlyList <StorageFile> fieldBookPhotosRO = await fieldBook.GetFilesAsync(); //Get a list of photos only foreach (StorageFile files in fieldBookPhotosRO) { //Get databases if (files.Name.ToLower().Contains(".sqlite") && files.Name.Contains(Dictionaries.DatabaseLiterals.DBName)) { FilesToBackup.Add(files); } //Get photos if (files.Name.ToLower().Contains(".jpg")) { hasPhotos = true; if (files.DateCreated >= inMemoryYoungestPhoto) { //Copy all photos FilesToBackup.Add(files); //Keep youngest photo for futur backup if (files.DateCreated > youngestPhoto) { youngestPhoto = files.DateCreated; } photoCount++; } } } //If any photos needs to be copied, else show warning if (FilesToBackup.Count > 1) { //Copy database and rename it List <StorageFile> newList = new List <StorageFile>(); foreach (StorageFile sf in FilesToBackup) { if (sf.Name.Contains(Dictionaries.DatabaseLiterals.DBName)) { StorageFile newFile = await sf.CopyAsync(fieldBook, newName); newList.Add(newFile); } else { newList.Add(sf); } } //Zip string outputArchivePath = await fs.AddFilesToZip(newList); //Keep youngest photo in memory localSetting.SetSettingValue(Dictionaries.ApplicationLiterals.KeywordBackupPhotoYoungest, youngestPhoto); //Copy await fs.SaveArchiveCopy(newList, "", "", projectName + "_"); //Delete renamed copy foreach (StorageFile fsToDelete in newList) { if (fsToDelete.Name.Contains(newName)) { await fsToDelete.DeleteAsync(); break; } } } else if (FilesToBackup.Count == 1 && FilesToBackup[0].Name.Contains(".sqlite")) { //Do not zip, only output sqlite try { await fs.SaveDBCopy(); } catch (Exception) { //Show error message var loadLocalization = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); ContentDialog endProcessDialog = new ContentDialog() { Title = loadLocalization.GetString("SaveDBDialogTitle"), Content = loadLocalization.GetString("SaveDBDialogContentError"), PrimaryButtonText = loadLocalization.GetString("LoadDataButtonProcessEndMessageOk") }; endProcessDialog.Style = (Style)Application.Current.Resources["DeleteDialog"]; ContentDialogResult cdr = await endProcessDialog.ShowAsync(); } } else if (FilesToBackup.Count == 0) { //Show empty archive warning var loadLocalization = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); ContentDialog warningNoPhotoBackup = new ContentDialog() { Title = loadLocalization.GetString("ShellBackupEmptyTitle"), Content = loadLocalization.GetString("ShellBackupEmptyMessage/Text"), PrimaryButtonText = loadLocalization.GetString("GenericDialog_ButtonOK") }; warningNoPhotoBackup.Style = (Style)Application.Current.Resources["WarningDialog"]; ContentDialogResult cdr = await warningNoPhotoBackup.ShowAsync(); } //Show empty backed up photo warning if (photoCount == 0 && hasPhotos) { var loadLocalization = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView(); ContentDialog warningNoPhotoBackup = new ContentDialog() { Title = loadLocalization.GetString("ShellBackupPhotoEmptyTitle"), Content = loadLocalization.GetString("ShellBackupPhotoEmptyMessage/Text"), PrimaryButtonText = loadLocalization.GetString("GenericDialog_ButtonOK") }; warningNoPhotoBackup.Style = (Style)Application.Current.Resources["WarningDialog"]; ContentDialogResult cdr = await warningNoPhotoBackup.ShowAsync(); } }