Exemple #1
0
        private async void buttonRemoveSelectedPages_Click(object sender, EventArgs e)
        {
            SetUIControlEnabled(false);

            TreeNode selectedTreeNode    = null;
            bool     isEveryPageSelected = CheckIfEveryPageIsSelected(out selectedTreeNode);

            if (isEveryPageSelected)
            {
                MessageBox.Show("WARNING: Data might be lost!\r\n\r\n" + "You have selected every page in the same group.\r\n" + string.Format("Name: {0}.\r\n", selectedTreeNode.Text) + "\r\n\r\nThe removal operation has been canceled.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                SetUIControlEnabled(true);
                treeViewHierarchy.SelectedNode = selectedTreeNode;
                treeViewHierarchy.Focus();
            }
            else
            {
                List <Tuple <string, string> > pagesBeingRemoved = PrepareRemovalOperation();
                if (pagesBeingRemoved.Count > 0)
                {
                    if (MessageBox.Show("Are you sure to remove the selected pages?\r\n" + string.Format("The number of the selected pages: {0}", pagesBeingRemoved.Count) + "\r\n\r\nPlease **BACKUP** OneNote notebooks!", "Confirm", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        isRemovingPages         = true;
                        cancellationTokenSource = new CancellationTokenSource();
                        List <Tuple <string, string, bool> > resultRemovePages = await Task.Run(() =>
                        {
                            return(accessor.RemovePages(pagesBeingRemoved, new Progress <Tuple <int, int, int, string> >(progress => UpdateProgressRemovePages(progress)), cancellationTokenSource.Token));
                        }, cancellationTokenSource.Token);

                        isRemovingPages = false;

                        HtmlReportGenerator report = new HtmlReportGenerator();
                        string generatedHtmlFile;
                        report.GenerateReportForRemovalOperation(resultRemovePages, out generatedHtmlFile);
                        System.Diagnostics.Process.Start(generatedHtmlFile);

                        ResetUIResultScanPages();
                        toolStripStatusLabelScan.Text = "Remove Completed";
                        UpdateProgressBar(100, 100);
                        SetUIControlEnabled(true);
                    }
                    else
                    {
                        SetUIControlEnabled(true);
                    }
                }
                else
                {
                    SetUIControlEnabled(true);
                }
            }
        }
Exemple #2
0
        private async void cleanUpUsingJSONToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetUIControlEnabled(false);
            Dictionary <string, List <Tuple <string, string> > > duplicatesGroups = accessor.GetDuplicatesGroups();

            if (duplicatesGroups != null)
            {
                string warningMessage = "** DANGEROUS FEATURE **" + "\r\n\r\n" +
                                        "It removes pages that are found in the JSON file." + "\r\n" +
                                        "Please make sure the original notebook is closed in order to prevent data loss." + "\r\n\r\n" +
                                        "Do you really want to continue?";
                if (MessageBox.Show(warningMessage, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    OpenFileDialog ofd = new OpenFileDialog();
                    ofd.Filter = "JSON files (*.json)|*.json";
                    ofd.Title  = "Save JSON";
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        string jsonText = "";
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(ofd.FileName, Encoding.UTF8, true))
                        {
                            jsonText = sr.ReadToEnd();
                        }
                        Dictionary <string, List <Tuple <string, string> > > archivedPages = new Dictionary <string, List <Tuple <string, string> > >();
                        archivedPages = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText, archivedPages.GetType()) as Dictionary <string, List <Tuple <string, string> > >;
                        HashSet <string> knownHashes = new HashSet <string>(archivedPages.Keys);

                        List <Tuple <string, string> > pagesBeingRemoved = new List <Tuple <string, string> >();
                        foreach (KeyValuePair <string, List <Tuple <string, string> > > groupInfo in duplicatesGroups)
                        {
                            string sha256sum = groupInfo.Key;
                            if (knownHashes.Contains(sha256sum))
                            {
                                foreach (Tuple <String, String> pageInfo in groupInfo.Value)
                                {
                                    pagesBeingRemoved.Add(Tuple.Create(pageInfo.Item1, pageInfo.Item2));
                                }
                            }
                        }

                        isRemovingPages         = true;
                        cancellationTokenSource = new CancellationTokenSource();
                        List <Tuple <string, string, bool> > resultRemovePages = await Task.Run(() =>
                        {
                            return(accessor.RemovePages(pagesBeingRemoved, new Progress <Tuple <int, int, int, string> >(progress => UpdateProgressRemovePages(progress)), cancellationTokenSource.Token));
                        }, cancellationTokenSource.Token);

                        isRemovingPages = false;

                        HtmlReportGenerator report = new HtmlReportGenerator();
                        string generatedHtmlFile;
                        report.GenerateReportForRemovalOperation(resultRemovePages, out generatedHtmlFile);
                        System.Diagnostics.Process.Start(generatedHtmlFile);

                        ResetUIResultScanPages();
                        toolStripStatusLabelScan.Text = "Remove Completed";
                        UpdateProgressBar(100, 100);
                    }
                }
            }
            SetUIControlEnabled(true);
        }