public static bool CreateScript(string path, string text, OverwriteType overwrite) { var directoryName = Path.GetDirectoryName(path); if (string.IsNullOrEmpty(directoryName) == false && Directory.Exists(directoryName) == false) { Directory.CreateDirectory(directoryName); } if (File.Exists(path) == false) { File.WriteAllText(path, text, Encoding.UTF8); return(true); } // TODO : 最適化? switch (overwrite) { case OverwriteType.Not: return(false); case OverwriteType.AddStart: text = text + File.ReadAllText(path, Encoding.UTF8); break; case OverwriteType.AddEnd: text = File.ReadAllText(path, Encoding.UTF8) + text; break; } File.WriteAllText(path, text, Encoding.UTF8); return(true); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Handles the Click event of the btnOverwrite control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param> /// ------------------------------------------------------------------------------------ protected void btnOverwrite_Click(object sender, EventArgs e) { foreach (ListViewItem item in SelectedBookItems) { BookMerger bookMerger = item.Tag as BookMerger; if (bookMerger == null) { continue; } IScrBook currentBook = bookMerger.BookCurr; string sDetails; List <IScrSection> sectionsToRemove; HashSet <int> missingBtWs; OverwriteType typeOfOverwrite = DetermineOverwritability(bookMerger, out sDetails, out sectionsToRemove, out missingBtWs); if (bookMerger.OriginalNumberOfDifferences != bookMerger.NumberOfDifferences) { if (!ConfirmOverwriteOfMergedVersion(currentBook.BestUIName)) { continue; } } if (typeOfOverwrite == OverwriteType.DataLoss) { // There would be data loss if the user overwrites, so inform them that overwrite // is not possible and go on to the next book. ReportDataLoss(currentBook, ScrDraftType.ImportedVersion, this, sDetails); continue; } if (missingBtWs == null || missingBtWs.Count == 0 || ConfirmBtOverwrite(currentBook, ScrDraftType.ImportedVersion, sectionsToRemove, missingBtWs, this)) { // This undo task will eventually get rolled into an outer undo task, but we // need to do this so that the low-level back translation code won't think we're // outside any undo task (because it suppresses the undo actions in that case). using (UndoableUnitOfWorkHelper undoHelper = new UndoableUnitOfWorkHelper( m_cache.ServiceLocator.GetInstance <IActionHandler>(), "Undo Overwrite", "Redo Overwrite")) { // Either no back translations might be lost, or the user has confirmed that they // want to overwrite them. if (typeOfOverwrite == OverwriteType.Partial) { PartialOverwrite(bookMerger, sectionsToRemove); } else { OnBookOverwritten(currentBook, OverwriteBook(bookMerger)); } undoHelper.RollBack = false; } SetItemStatus(item, ImportedBookStatus.Overwritten); } } }
public PermissionOverwrite(OverwriteType type, int allow, int deny) { Type = type; Allow = allow; Deny = deny; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Handles the Click event of the m_btnCopyToCurr control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event /// data.</param> /// ------------------------------------------------------------------------------------ private void m_btnCopyToCurr_Click(object sender, EventArgs e) { // If nothing or a complete archive is selected, do nothing. TreeNode node = m_treeArchives.SelectedNode; if (node == null || !(node.Tag is IScrBook)) { return; } IScrBook savedBook = (IScrBook)node.Tag; IScrBook originalBook = (IScrBook)m_scr.FindBook(savedBook.CanonicalNum); TreeNode parentNode = node.Parent; IScrDraft archive = (IScrDraft)parentNode.Tag; OverwriteType typeOfOverwrite = OverwriteType.FullNoDataLoss; List <IScrSection> sectionsToRemove = null; if (originalBook != null) { string sDetails; HashSet <int> missingBtWs; typeOfOverwrite = originalBook.DetermineOverwritability(savedBook, out sDetails, out sectionsToRemove, out missingBtWs); if (typeOfOverwrite == OverwriteType.DataLoss) { // There will be data loss if the user overwrites so we don't allow them // to continue. ImportedBooks.ReportDataLoss(originalBook, ScrDraftType.SavedVersion, this, sDetails); return; } if (missingBtWs != null && !ImportedBooks.ConfirmBtOverwrite(originalBook, ScrDraftType.SavedVersion, sectionsToRemove, missingBtWs, this)) { // The user might lose back translation(s) if they proceed and they decided // against it. return; } } string stUndo; string stRedo; TeResourceHelper.MakeUndoRedoLabels("kstidOverwriteCurrentWithSaved", out stUndo, out stRedo); using (new WaitCursor(this)) using (UndoTaskHelper undoHelper = new UndoTaskHelper(m_cache.ActionHandlerAccessor, null, stUndo, stRedo)) { if (typeOfOverwrite == OverwriteType.Partial) { // Perform an automerge of the original book and the saved version. using (BookMerger merger = new BookMerger(m_cache, m_styleSheet, savedBook)) { using (ProgressDialogWithTask progress = new ProgressDialogWithTask(this, m_cache.ThreadHelper)) { progress.Title = DlgResources.ResourceString("kstidOverwriteCaption"); progress.RunTask(merger.DoPartialOverwrite, sectionsToRemove); } if (!merger.AutoMerged) { throw new ContinuableErrorException("Partial Overwrite was not successful."); } } } else { // FWNX-677 - caused by mono-calgary patch bug-614850_Modal_614850_v6.patch List <Form> disabled_forms = new List <Form>(); if (MiscUtils.IsUnix) { lock (Application.OpenForms) { foreach (Form form in Application.OpenForms) { if (form.Enabled == false) { disabled_forms.Add(form); } } } foreach (Form form in disabled_forms) { form.Enabled = true; } } if (originalBook != null) { if (m_cache.ActionHandlerAccessor != null) { // When Undoing, we need to first resurrect the deleted book, then // put it back in the book filter...so we need a RIFF in the sequence // BEFORE the delete. ReplaceInFilterFixer fixer1 = new ReplaceInFilterFixer(originalBook, null, m_app); m_cache.ActionHandlerAccessor.AddAction(fixer1); } m_scr.ScriptureBooksOS.Remove(originalBook); } IScrBook newBook = m_scr.CopyBookToCurrent(savedBook); ReplaceInFilterFixer fixer = new ReplaceInFilterFixer(null, newBook, m_app); fixer.Redo(); if (m_cache.ActionHandlerAccessor != null) { m_cache.ActionHandlerAccessor.AddAction(fixer); } // FWNX-677 - caused by mono-calgary patch bug-614850_Modal_614850_v6.patch if (MiscUtils.IsUnix) { foreach (Form form in disabled_forms) { form.Enabled = false; } disabled_forms.Clear(); } } undoHelper.RollBack = false; } }
/// ------------------------------------------------------------------------------------ /// <summary> /// Handles the Click event of the m_btnCopyToCurr control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event /// data.</param> /// ------------------------------------------------------------------------------------ private void m_btnCopyToCurr_Click(object sender, EventArgs e) { // If nothing or a complete archive is selected, do nothing. TreeNode node = m_treeArchives.SelectedNode; if (node == null || !(node.Tag is ScrBook)) { return; } ScrBook savedBook = node.Tag as ScrBook; ScrBook originalBook = (ScrBook)m_scr.FindBook(savedBook.CanonicalNum); TreeNode parentNode = node.Parent; ScrDraft archive = parentNode.Tag as ScrDraft; OverwriteType typeOfOverwrite = OverwriteType.FullNoDataLoss; List <IScrSection> sectionsToRemove = null; if (originalBook != null) { string sDetails; List <int> missingBtWs; typeOfOverwrite = originalBook.DetermineOverwritability(savedBook, out sDetails, out sectionsToRemove, out missingBtWs); if (typeOfOverwrite == OverwriteType.DataLoss) { // There will be data loss if the user overwrites so we don't allow them // to continue. ImportedBooks.ReportDataLoss(originalBook, ScrDraftType.SavedVersion, this, sDetails); return; } if (missingBtWs != null && !ImportedBooks.ConfirmBtOverwrite(originalBook, ScrDraftType.SavedVersion, sectionsToRemove, missingBtWs, this)) { // The user might lose back translation(s) if they proceed and they decided // against it. return; } } string stUndo; string stRedo; TeResourceHelper.MakeUndoRedoLabels("kstidOverwriteCurrentWithSaved", out stUndo, out stRedo); using (new WaitCursor(this)) using (UndoTaskHelper helper = new UndoTaskHelper(m_cache.MainCacheAccessor, null, stUndo, stRedo, true)) { try { if (typeOfOverwrite == OverwriteType.Partial) { // Perform an automerge of the original book and the saved version. using (BookMerger merger = new BookMerger(m_cache, m_styleSheet, savedBook)) { using (ProgressDialogWithTask progress = new ProgressDialogWithTask(this)) { progress.Title = DlgResources.ResourceString("kstidOverwriteCaption"); progress.RunTask(true, new BackgroundTaskInvoker(merger.DoPartialOverwrite), sectionsToRemove); } if (!merger.AutoMerged) { throw new Exception("Partial Overwrite was not successful."); } } } else { if (originalBook != null) { if (m_cache.ActionHandlerAccessor != null) { // When Undoing, we need to first resurrect the deleted book, then // put it back in the book filter...so we need a RIFF in the sequence // BEFORE the delete. ReplaceInFilterFixer fixer1 = new ReplaceInFilterFixer(originalBook.Hvo, 0, m_cache); m_cache.ActionHandlerAccessor.AddAction(fixer1); } originalBook.DeleteUnderlyingObject(); } int hvoNew = (m_scr as Scripture).CopyBookToCurrent(savedBook); ReplaceInFilterFixer fixer = new ReplaceInFilterFixer(0, hvoNew, m_cache); fixer.Redo(false); if (m_cache.ActionHandlerAccessor != null) { m_cache.ActionHandlerAccessor.AddAction(fixer); } } } catch { helper.EndUndoTask = false; throw; } } }