public bool Save(bool writeToDisk) { if (!Directory.Exists(txt_defaultQuizSaveFolder.Text)) { MessageBox.Show("Specified default quiz save folder path doesn't exist", "SteelQuiz", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } ConfigManager.Config.StorageConfig.DefaultQuizSaveFolder = txt_defaultQuizSaveFolder.Text; var oldPath = ConfigManager.Config.StorageConfig.QuizProgressFile; string newPath; if (txt_quizProgPath.Text == Path.GetDirectoryName(QuizCore.PROGRESS_FILE_DEFAULT)) { // quiz progress file should have the filename "QuizProgress.json" in the default folder, for compatibility reasons newPath = QuizCore.PROGRESS_FILE_DEFAULT; } else { newPath = Path.Combine(txt_quizProgPath.Text, "SteelQuizProgress.json"); } if (newPath == oldPath) { return(true); } if (!Directory.Exists(Path.GetDirectoryName(newPath))) { MessageBox.Show("Selected directory to store quiz progress data in does not exist", "SteelQuiz", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } try { QuizCore.BackupProgress(); } catch (Exception ex) { txt_quizProgPath.Text = Path.GetDirectoryName(oldPath); throw ex; } if (File.Exists(newPath) && File.Exists(oldPath)) { var conflictSolution = new QuizProgressConflict(); if (conflictSolution.ShowDialog() != DialogResult.OK) { txt_quizProgPath.Text = Path.GetDirectoryName(oldPath); return(false); } if (conflictSolution.ConflictResult == ConflictResult.MergePrioTarget) { var merge = QuizProgressMerger.Merge(newPath, oldPath, newPath); if (!merge) { return(false); } File.Delete(oldPath); } else if (conflictSolution.ConflictResult == ConflictResult.MergePrioCurrent) { var merge = QuizProgressMerger.Merge(oldPath, newPath, newPath); if (!merge) { return(false); } File.Delete(oldPath); } else if (conflictSolution.ConflictResult == ConflictResult.KeepTarget) { try { QuizCore.BackupProgress(); } catch (Exception ex) { txt_quizProgPath.Text = Path.GetDirectoryName(oldPath); throw ex; } File.Delete(oldPath); } else if (conflictSolution.ConflictResult == ConflictResult.OverwriteTarget) { try { QuizCore.BackupProgress(); } catch (Exception ex) { txt_quizProgPath.Text = Path.GetDirectoryName(oldPath); throw ex; } File.Delete(newPath); File.Move(oldPath, newPath); } } else if (File.Exists(oldPath)) { File.Move(oldPath, newPath); } ConfigManager.Config.StorageConfig.QuizProgressFile = newPath; if (writeToDisk) { ConfigManager.SaveConfig(); } QuizCore.LoadQuizAccessData(); Program.frmDashboard.PopulateQuizList(); if (Program.frmDashboard.LoadedQuiz != null) { // Reload quiz Program.frmDashboard.LoadedQuiz = QuizCore.LoadQuiz(Program.frmDashboard.LoadedQuiz.QuizIdentity.FindQuizPath()); Program.frmDashboard.UpdateQuizOverview(); } return(true); }
private bool SaveQuiz(bool saveAs = false, string customPath = null) { string path = null; if (customPath == null) { if (QuizPath == null || saveAs) { sfd_quiz.InitialDirectory = ConfigManager.Config.StorageConfig.DefaultQuizSaveFolder; int untitledCounter = 1; path = $"Untitled{ untitledCounter }.steelquiz"; while (File.Exists(Path.Combine(QuizCore.QUIZ_FOLDER_DEFAULT, path))) { ++untitledCounter; path = $"Untitled{ untitledCounter }.steelquiz"; } var sfd = sfd_quiz.ShowDialog(); if (sfd != DialogResult.OK) { return(false); } if (QuizPath != sfd_quiz.FileName) { QuizGuid = Guid.NewGuid(); // create new guid if path is not the same } path = sfd_quiz.FileName; } else { path = QuizPath; } } else { path = customPath; } UseWaitCursor = true; var quiz = ConstructQuiz(); AtomicIO.AtomicWrite(path, JsonConvert.SerializeObject(quiz, Formatting.Indented)); if (customPath == null) { QuizPath = path; ChangedSinceLastSave = false; } QuizCore.LoadQuizAccessData(); QuizCore.QuizIdentities[quiz.GUID] = new QuizIdentity(quiz.GUID, QuizPath); QuizCore.QuizAccessTimes[quiz.GUID] = DateTime.Now; QuizCore.SaveQuizAccessData(); UseWaitCursor = false; ShowNotification("Quiz has been saved", 3000); return(true); }