예제 #1
0
        private void HandleCharOrFolder(TreeNode dragNode, TreeNode dropNode)
        {
            #region Handle copying files from char to folder
            Thread notifyThread = new Thread(new ParameterizedThreadStart(ShowNotifyForm));
            TagInfo ti = new TagInfo(String.Empty, "Processing book...");

            TagInfo tIdrop = dropNode.Tag as TagInfo;
            TagInfo tIdrag = dragNode.Tag as TagInfo;

            CMacroFile[] draglist = new CMacroFile[0];

            LogMessage.Log("DragDrop: Creating list of valid files");
            for (int i = 0; i < MacroFiles.Count; i++)
            {
                #region Locate all files matching the dragNode's folder path
                if ((MacroFiles[i] != null) &&
                    (MacroFiles[i].fName != null) && 
                    (MacroFiles[i].fName.Contains(tIdrag.Text)))
                {
                    Array.Resize(ref draglist, draglist.Length + 1);
                    draglist[draglist.Length - 1] = MacroFiles[i];
                }
                #endregion
            }

            if (draglist.Length > 0)
            {
                #region If there are valid files for dragging
                CMacroFile tmp_drop = null;
                exitLoop = false;

                List<TagInfo> ttlFileList = new List<TagInfo>();
                for (int i = 0; i < draglist.Length; i++)
                {
                    // remove the common directory, to merge and create a path

                    String PathToRemove = String.Empty;
                    String FileWithFolder = draglist[i].fName;
                    String newpath = String.Empty;
                    try
                    {
                        // draglist[i].fName
                        PathToRemove = Path.GetDirectoryName(tIdrag.Text.Trim('\\'));
                        FileWithFolder = FileWithFolder.Remove(0, PathToRemove.Length);
                        // Attempt to throw an error here.
                        newpath = Path.GetFullPath(tIdrop.Text.Trim('\\') + "\\" + FileWithFolder.Trim('\\'));

                        #region Check for valid TTL files here
                        String newttlfile = String.Empty;
                        String oldttlfile = String.Empty;

                        try
                        {
                            newttlfile = Path.GetDirectoryName(newpath) + "\\mcr.ttl"; // (tIdrop.Text.Trim('\\') + "\\mcr.ttl");
                            oldttlfile = Path.GetDirectoryName(draglist[i].fName) + "\\mcr.ttl";
                        }
                        catch (PathTooLongException e)
                        {
                            // File.Exists(String.Empty) always returns false
                            // This will catch an PathTooLongException errors and ignore transfer
                            LogMessage.LogF("..HandleCharOrFolder(): TTL file path is too long {0}, skipping", e.Message);
                        }
                        catch (Exception e)
                        {
                            LogMessage.LogF("..HandleCharOrFolder(): Unknown exception {0}, ignoring", e.Message);
                        }

                        if (newttlfile != oldttlfile)
                        {
                            #region If future ttlfile is within system limits on Path Length and is not the SAME FILE
                            int oldcnt = 0;
                            for (oldcnt = 0; oldcnt < ttlFileList.Count; oldcnt++)
                            {
                                TagInfo tmpTTL = ttlFileList[oldcnt];
                                CBook cbTTL = tmpTTL.Object1 as CBook;
                                if (cbTTL.fName == oldttlfile)
                                    break;
                            }
                            if (oldcnt >= ttlFileList.Count) // not been handled yet
                            {
                                #region If not handled yet
                                CBook cbold = null, cbnew = null;
                                for (int bcnt = 0; bcnt < BookList.Count; bcnt++)
                                {
                                    #region Locate any existing book for either file
                                    if (BookList[bcnt].fName == oldttlfile)
                                    {
                                        BookList[bcnt].Restore();
                                        cbold = BookList[bcnt];
                                    }
                                    else if (BookList[bcnt].fName == newttlfile)
                                    {
                                        BookList[bcnt].Restore();
                                        cbnew = BookList[bcnt];
                                    }
                                    #endregion
                                }

                                if ((cbold == null) && (File.Exists(oldttlfile)))
                                {
                                    #region If didn't find the drag mcr.ttl file and it does exist, Load it
                                    cbold = new CBook(oldttlfile);
                                    BookList.Add(cbold);
                                    #endregion
                                }

                                if ((cbnew == null) && (File.Exists(newttlfile)))
                                {
                                    #region If didn't find the drop mcr.ttl file and it does exist, Load it
                                    cbnew = new CBook(newttlfile);
                                    BookList.Add(cbnew);
                                    #endregion
                                }

                                if ((cbold != null) && (cbnew != null))
                                {
                                    #region If old ttl file and new ttlfile info was found in memory
                                    ttlFileList.Add(new TagInfo("Overwrite_TTL", cbold, cbnew));
                                    #endregion
                                }
                                else if ((cbold != null) && (cbnew == null))
                                {
                                    #region If old ttl file was found but new ttl file was not
                                    cbnew = new CBook(cbold, newttlfile);
                                    ttlFileList.Add(new TagInfo("Copy_TTL", Path.GetDirectoryName(newttlfile), cbold, cbnew));
                                    #endregion
                                }
                                #endregion
                            }
                            #endregion
                        }
                        #endregion
                    }
                    catch (PathTooLongException error)
                    {
                        LogMessage.Log("While processing book files, encountered Path Too Long error -- {0}", error.Message);
                    }
                    catch (Exception error)
                    {
                        LogMessage.Log("While processing book files, encountered Unexpected error -- {0}", error.Message);
                    }
                } // end for loop

                #region Test for list
                if (ttlFileList.Count != 0)
                {
                    if (ttlFileList.Count == 1)
                    {
                        #region If only one book file is being processed, show generic MessageBox instead
                        String path = ttlFileList[0].Text;
                        CBook cbold = ttlFileList[0].Object1 as CBook;
                        CBook cbnew = ttlFileList[0].Object2 as CBook;
                        if (ttlFileList[0].Type == "Overwrite_TTL")
                        {
                            DialogResult dr = MessageBox.Show(
                                    String.Format("Book name file found in both directories,\r\nSource: {0}\r\nDestination: {1}\r\n\r\nOverwite the destination Book File?", Utilities.EllipsifyPath(cbold.fName, 60), Utilities.EllipsifyPath(cbnew.fName, 60)),
                                    "Overwrite?",
                                    MessageBoxButtons.YesNoCancel,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button2);
                            if (dr == DialogResult.Yes)
                            {
                                cbnew.CopyFrom(cbold);
                            }
                            else if (dr == DialogResult.Cancel)
                            {
                                LogMessage.Log("..Cancelled drop");
                                return;
                            }
                        }
                        else if (ttlFileList[0].Type == "Copy_TTL")
                        {
                            DialogResult dr = MessageBox.Show(
                                    String.Format("Book name file found in source directory:\r\n'{0}',\r\n\r\nCopy to the destination directory:\r\n'{1}'?", Utilities.EllipsifyPath(cbold.fName, 60), Utilities.EllipsifyPath(path, 60)),
                                    "Copy?",
                                    MessageBoxButtons.YesNoCancel,
                                    MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button2);
                            if (dr == DialogResult.Yes)
                            {
                                BookList.Add(cbnew);
                            }
                            else if (dr == DialogResult.Cancel)
                            {
                                LogMessage.Log("..Cancelled drop");
                                return;
                            }
                        }
                        #endregion
                    }
                    else
                    {
                        #region Else go with the CheckedListBox
                        ExitAndSaveBox esb = new ExitAndSaveBox("Confirmation box...", "Select what actions to take:", "Confirm", "No To All", "Cancel Operation");
                        esb.checkedListBox1.Items.Clear();
                        foreach (TagInfo x in ttlFileList)
                            esb.checkedListBox1.Items.Add(x, true);
                        bool ExitTTLLoop = false;
                        do
                        {
                            DialogResult dr = esb.ShowDialog(this);
                            if (dr == DialogResult.Cancel)
                            {
                                LogMessage.Log("..HandleSwapOrCopy(): Copy cancelled");
                                return;
                            }
                            else if (dr == DialogResult.No)
                                ExitTTLLoop = true;
                            else if (dr == DialogResult.Yes)
                            {
                                if (esb.checkedListBox1.CheckedItems.Count == 0)
                                    MessageBox.Show("If you're going to select to save, you must select something!", "Select 'No To All' to ignore this.");
                                else
                                {
                                    foreach (TagInfo x in esb.checkedListBox1.CheckedItems)
                                    {
                                        CBook cbold = x.Object1 as CBook;
                                        CBook cbnew = x.Object2 as CBook;
                                        String type = x.Type;
                                        if (type == "Copy_TTL")
                                        {
                                            BookList.Add(cbnew);
                                        }
                                        else if (type == "Overwrite_TTL")
                                        {
                                            cbnew.CopyFrom(cbold);
                                        }
                                    }
                                    ExitTTLLoop = true;
                                }
                            }

                        } while (ExitTTLLoop == false);
                        #endregion
                    }
                }
                #endregion
                #region Show Notify Form
                if (draglist.Length > FILES_TO_HIDE)
                {
                    ti.Object1 = draglist.Length as Object;
                    notifyThread.Start((object)ti);
                }
                #endregion

                String[] errorFiles = new String[0];
                String errorString = String.Empty;
                for (int i = 0; (i < draglist.Length) && (exitLoop == false); i++)
                {
                    #region Loop through draglist Copying files
                    #region Update NotifyForm UI
                    if (draglist.Length > FILES_TO_HIDE)
                    {
                        UpdateNotifyUI(Utilities.EllipsifyPath(draglist[i].fName), i);
                        Thread.Sleep(25);
                    }
                    #endregion
                    String PathToRemove = String.Empty;
                    String FileWithFolder = draglist[i].fName;
                    String newpath = String.Empty;

                    try
                    {
                        #region Copy files or Create new files here
                        // draglist[i].fName
                        PathToRemove = Path.GetDirectoryName(tIdrag.Text.Trim('\\'));
                        FileWithFolder = FileWithFolder.Remove(0, PathToRemove.Length);
                        // Attempt to throw an error here.
                        newpath = Path.GetFullPath(tIdrop.Text.Trim('\\') + "\\" + FileWithFolder.Trim('\\'));

                        tmp_drop = FindMacroFileExactByFileName(newpath);
                        if (tmp_drop != null)
                        {
                            tmp_drop.CopyFrom(draglist[i]);
                        }
                        else
                        {
                            NewFileInMemory(draglist[i], newpath);
                        }
                        #endregion
                    }
                    catch (PathTooLongException e)
                    {
                        #region Setup for error processing for Too long filenames
                        LogMessage.Log("..HandleCharOrFolder(): Path is too long, could not copy: {0}, skipping -- Error: {1}", newpath, e.Message);
                        Array.Resize(ref errorFiles, errorFiles.Length + 1);
                        errorFiles[errorFiles.Length - 1] = Utilities.EllipsifyPath(newpath);
                        errorString += errorFiles[errorFiles.Length - 1] + "\r\n";
                        #endregion
                    }
                    catch (Exception e)
                    {
                        #region Generic Error
                        LogMessage.LogF("..HandleCharOrFolder(): Unexpected error -- {0}, ignoring.", e.Message);
                        Array.Resize(ref errorFiles, errorFiles.Length + 1);
                        errorFiles[errorFiles.Length - 1] = Utilities.EllipsifyPath(newpath);
                        errorString += errorFiles[errorFiles.Length - 1] + "\r\n";
                        #endregion
                    }
                    #endregion
                }

                #region Close Notify Form
                if (draglist.Length > FILES_TO_HIDE)
                {
                    UpdateNotifyProgress(notifyForm.NotifyBarMax);
                    Thread.Sleep(200);
                    CloseNotifyForm();
                }
                #endregion

                #region Notify of any errors encountered
                if ((errorFiles.Length > 0) && (errorString != String.Empty))
                {
                    MessageBox.Show("The following " + errorFiles.Length + " files can not be saved (Path to file is TOO long):\r\n" + errorString, "Error: Path(s) too long on " + errorFiles.Length + " files!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                #endregion
                #endregion
            }
            else
            {
                LogMessage.Log("...Unable to determine list of files in char directory {0}", tIdrag.Text);
                MessageBox.Show("Unable to complete the request, I'm sorry.", "Drag & Drop Error!");
            }
            #endregion
        }
예제 #2
0
        private FormClosingEventArgs SaveAllChanges(bool FilesOnly, bool Exiting)
        {
            if (MacroFiles.Count < 1)
            {
                if (!Exiting)
                    MessageBox.Show("You must open a folder or file first in order to save anything at all!", "Nothing to save.");
                return new FormClosingEventArgs(CloseReason.None, false);
            }

            ExitAndSaveBox esb;
            if (!Exiting)
                esb = new ExitAndSaveBox(
                String.Format("Save which {0}?", FilesOnly ? "macro files" : "changes"),
                String.Format("Select which {0} to save:", FilesOnly ? "files" : "changes"), "Save!", String.Empty, "Forget It!");
            else esb = new ExitAndSaveBox();

            this.SetWaitCursor();
            bool Error = false, OnlyOneChange = false;
            bool foundBook = false, foundFolder = false, foundFile = false;
            int original_drc_length = DeleteRenameChanges.Count;
            TagInfo[] original;

            original = DeleteRenameChanges.ToArray();

            DeleteRenameChanges.Clear();
            // Folder deletes are assigned by system, keep them.
            // Prune anything else, because we'll rebuild it here.
            for (int i = 0; i < original.Length; i++)
            {
                TagInfo t = original[i];
                if (t.Type == "Delete_Folder")
                {
                    foundFolder = true;
                    DeleteRenameChanges.Add(t);
                }
            }
            // reassign the array b/c it's goign to replace itself in the even that it's FilesOnly or we cancel.
            original = DeleteRenameChanges.ToArray();

            if (!FilesOnly)
            {
                esb.selectFoldersOnlyToolStripMenuItem.Visible = foundFolder;

                foreach (CBook cb in BookList)
                {
                    if (cb.IsDeleted)
                    {
                        if (File.Exists(cb.fName))
                        {
                            bool foundExistingBook = false;
                            foreach (TagInfo drcTemp in DeleteRenameChanges)
                            {
                                if ((drcTemp.Type == "Delete_File") && (drcTemp.Text == cb.fName))
                                {
                                    foundExistingBook = true; // is already in the list to be deleted, don't duplicate it.
                                    break;
                                }
                            }
                            if (!foundExistingBook) // not already in the list marked for deletion
                                DeleteRenameChanges.Add(new TagInfo("Delete_File", cb.fName));
                            foundBook = true;
                        }
                    }
                    else if (cb.Changed)
                    {
                        bool foundExistingBook = false;
                        foreach (TagInfo drcTemp in DeleteRenameChanges)
                        {
                            if ((drcTemp.Type == "Save_TTL") && (cb == drcTemp.Object1))
                            {
                                foundExistingBook = true;
                                break;
                            }
                        }
                        if (!foundExistingBook)
                            DeleteRenameChanges.Add(new TagInfo("Save_TTL", (Object)cb));
                        foundBook = true;
                    }
                }

                esb.selectBooksOnlyToolStripMenuItem.Visible = foundBook;
            }
            else
            {
                esb.selectMacroFilesOnlyToolStripMenuItem.Visible = false;
                esb.toolStripSeparator1.Visible = false;
                DeleteRenameChanges.Clear();
            }

            foreach (CMacroFile x in MacroFiles)
            {
                if (x.IsDeleted)
                {
                    if (File.Exists(x.fName))
                    {
                        DeleteRenameChanges.Add(new TagInfo("Delete_File", x.fName));
                        foundFile = true;
                    }
                }
                else if (x.Changed == true)
                {
                    DeleteRenameChanges.Add(new TagInfo("Save_File", x));
                    foundFile = true;
                }
            }

            if (!FilesOnly)
            {
                esb.selectMacroFilesOnlyToolStripMenuItem.Visible = foundFile;
                esb.toolStripSeparator1.Visible = (foundFolder || foundFile || foundBook);
            }
            else
            {
                esb.selectMacroFilesOnlyToolStripMenuItem.Visible = false;
                esb.toolStripSeparator1.Visible = false;
            }

            if (DeleteRenameChanges.Count == 1)
            {
                OnlyOneChange = true;
                if (DeleteRenameChanges[0].Type == "Skip")
                    DeleteRenameChanges.Clear();
            }

            if (DeleteRenameChanges.Count >= 1)
            {
                #region If there's more than 1 item to save, show CheckedListBox
                DialogResult dr;

                if (!OnlyOneChange)
                {
                    esb.checkedListBox1.Items.Clear();
                    if (this.WindowState == FormWindowState.Minimized)
                        esb.StartPosition = FormStartPosition.CenterScreen;

                    foreach (TagInfo tiLoop in DeleteRenameChanges)
                    {
                        #region for each item in the DRC array, add it to the CheckedListBox
                        if (tiLoop.Type != "Skip")
                        {
                            // don't add Deletion of Folders or Files
                            // if the directory doesn't exist
                            // Files should be handled above, but I may not have
                            // counted on some old code lying around somewhere.
                            if ((tiLoop.Type == "Delete_Folder") &&
                                !Directory.Exists(tiLoop.Text))
                                continue;
                            else if ((tiLoop.Text == "Delete_File") &&
                                !File.Exists(tiLoop.Text))
                                continue;
                            esb.checkedListBox1.Items.Add(tiLoop, true);
                        }
                        #endregion
                    }

                    LogMessage.LogF("..Save All {0} Requested", FilesOnly ? "Files" : "Changes");
                }

                exitLoop = false;

                while (exitLoop == false)
                {
                    if (!OnlyOneChange)
                        dr = esb.ShowDialog();
                    else
                    {
                        dr = MessageBox.Show(DeleteRenameChanges[0].ToString(),
                                String.Format("Save {0}{1}?", FilesOnly ? "file" : "change", Exiting ? " before exiting" : ""), 
                                Exiting ? MessageBoxButtons.YesNoCancel : MessageBoxButtons.YesNo, 
                                MessageBoxIcon.None, 
                                Exiting ? MessageBoxDefaultButton.Button3 : MessageBoxDefaultButton.Button2);
                    }
                    exitLoop = true;
                    if (dr == DialogResult.Cancel)
                    {
                        LogMessage.LogF("...Save{0}{1} cancelled", (!OnlyOneChange && FilesOnly) ? " All Files" : (!OnlyOneChange && !FilesOnly) ? " All Changes" : "", Exiting ? " & Exit" : "");

                        // Remove the newly added Files that need to be saved.
                        if (FilesOnly)
                        {
                            // in FilesOnly case we build our own DRC list, restore original
                            DeleteRenameChanges.Clear();
                            for (int xi = 0; xi < original.Length; xi++)
                                DeleteRenameChanges.Add(original[xi]);
                        }
                        else
                        {
                            if (OnlyOneChange)
                                DeleteRenameChanges.Clear();
                            else DeleteRenameChanges.RemoveRange(original_drc_length, DeleteRenameChanges.Count - original_drc_length);
                        }
                        this.RestoreCursor();
                        return new FormClosingEventArgs(CloseReason.None, true);
                    }
                    else if (dr == DialogResult.Yes)
                    {
                        #region If we choose to Save
                        if (!OnlyOneChange && (esb.checkedListBox1.CheckedItems.Count <= 0))
                        {
                            exitLoop = false;
                            MessageBox.Show(String.Format("You need to pick something if you want to Save {0}{1}!", (FilesOnly) ? "All Files" : "All Changes", (Exiting) ? " & Exit" : ""),
                                String.Format("Select {0} instead.", (Exiting) ? "'Just Exit' or 'Cancel!'" : "Forget It!"));
                            continue;
                        }

                        if (OnlyOneChange)
                        {
                            Error = SaveTagInfo(DeleteRenameChanges[0]);
                            if (!Error)
                            {
                                DeleteRenameChanges.Clear();
                            }
                        }
                        else if (!OnlyOneChange && (esb.checkedListBox1.CheckedItems.Count > 0))
                        {
                            foreach (object x in esb.checkedListBox1.CheckedItems)
                            {
                                Error = SaveTagInfo(x as TagInfo);
                                if (!Error && DeleteRenameChanges.Contains(x as TagInfo))
                                {
                                    DeleteRenameChanges.Remove(x as TagInfo);
                                }
                            }
                        }

                        if (Error && XMLToDo.Count >= 1)
                        {
                            Error = ProcessXMLToDo();
                        }
                        LogMessage.LogF("...Save{0} Completed {1}{2}.", (!OnlyOneChange && FilesOnly) ? " All Files" : (!OnlyOneChange && !FilesOnly) ?  " All Changes" : "", 
                                        (Error == true) ? "With Errors" : "Successfully", Exiting ? ", Exiting" : "");
                    }
                        #endregion
                    else
                    {
                        LogMessage.LogF("...Save{0} Declined{1}.", (!OnlyOneChange && FilesOnly) ? " All Files" : (!OnlyOneChange && !FilesOnly) ? " All Changes" : "", (Exiting) ? ", Exiting" : "");
                    }
                }
                #endregion
            }
            else if (DeleteRenameChanges.Count == 0)
            {
                String Message = String.Format("No {0} need saving{1}.", FilesOnly ? "Macrofiles" : "Changes", Exiting ? ", exiting" : "");
                if (!Exiting)
                    MessageBox.Show(Message, String.Format("No {0} to save.", FilesOnly ? "files" : "changes"));
                LogMessage.Log(Message);
            }
            this.RestoreCursor();
            if (FilesOnly) // If Only files, restore previous not done stuff (books, delete folders, create folders/files, etc)
            {
                DeleteRenameChanges.Clear(); // Clear my additions.
                for (int xi = 0; xi < original.Length; xi++)
                    DeleteRenameChanges.Add(original[xi]);  // Restore deleted/skipped folders
            }
            return new FormClosingEventArgs(CloseReason.None, false);
        }