Example #1
0
        private void ExportMany(OneNote one, List <string> pageIDs)
        {
            OneNote.ExportFormat format;
            string path;

            using (var dialog = new ExportDialog(pageIDs.Count))
            {
                if (dialog.ShowDialog(owner) != DialogResult.OK)
                {
                    return;
                }

                path   = dialog.FolderPath;
                format = dialog.Format;
            }

            string ext = null;

            switch (format)
            {
            case OneNote.ExportFormat.HTML: ext = ".htm"; break;

            case OneNote.ExportFormat.PDF: ext = ".pdf"; break;

            case OneNote.ExportFormat.Word: ext = ".docx"; break;

            case OneNote.ExportFormat.XML: ext = ".xml"; break;

            case OneNote.ExportFormat.OneNote: ext = ".one"; break;
            }

            string formatName = format.ToString();

            using (var progress = new UI.ProgressDialog())
            {
                progress.SetMaximum(pageIDs.Count);
                progress.Show(owner);

                foreach (var pageID in pageIDs)
                {
                    var page     = one.GetPage(pageID, OneNote.PageDetail.BinaryData);
                    var filename = Path.Combine(path, page.Title.Replace(' ', '_') + ext);

                    progress.SetMessage(filename);
                    progress.Increment();

                    if (format == OneNote.ExportFormat.XML)
                    {
                        SaveAsXML(page.Root, filename);
                    }
                    else
                    {
                        SaveAs(one, page.PageId, filename, format, formatName);
                    }
                }
            }

            UIHelper.ShowMessage(string.Format(Resx.SaveAsMany_Success, pageIDs.Count, path));
        }
        public override async Task Execute(params object[] args)
        {
            logger.StartClock();

            using (var one = new OneNote())
            {
                var section = one.GetSection();
                if (section != null)
                {
                    var ns = one.GetNamespace(section);

                    var pageIds = section.Elements(ns + "Page")
                                  .Select(e => e.Attribute("ID").Value)
                                  .ToList();

                    using (var progress = new UI.ProgressDialog())
                    {
                        progress.SetMaximum(pageIds.Count);
                        progress.Show(owner);

                        foreach (var pageId in pageIds)
                        {
                            var page = one.GetPage(pageId, OneNote.PageDetail.Basic);
                            var name = page.Root.Attribute("name").Value;

                            progress.SetMessage(string.Format(
                                                    Properties.Resources.RemovingPageNumber_Message, name));

                            progress.Increment();

                            if (string.IsNullOrEmpty(name))
                            {
                                continue;
                            }

                            if (RemoveNumbering(name, out string clean))
                            {
                                page.Root
                                .Element(ns + "Title")
                                .Element(ns + "OE")
                                .Element(ns + "T")
                                .GetCData().Value = clean;

                                await one.Update(page);
                            }
                        }
                    }
                }
            }

            logger.StopClock();
            logger.WriteTime("removed page numbering");
        }
        public override async Task Execute(params object[] args)
        {
            using (var dialog = new NumberPagesDialog())
            {
                if (dialog.ShowDialog(owner) == DialogResult.OK)
                {
                    using (one = new OneNote())
                    {
                        var section = one.GetSection();
                        ns = one.GetNamespace(section);

                        var pages = section.Elements(ns + "Page")
                                    .Select(e => new PageBasics
                        {
                            ID    = e.Attribute("ID").Value,
                            Name  = e.Attribute("name").Value,
                            Level = int.Parse(e.Attribute("pageLevel").Value)
                        })
                                    .ToList();

                        if (pages?.Count > 0)
                        {
                            logger.StartClock();

                            var index = 0;

                            if (dialog.CleanupNumbering)
                            {
                                cleaner = new RemovePageNumbersCommand();
                            }

                            using (progress = new UI.ProgressDialog())
                            {
                                progress.SetMaximum(pages.Count);
                                progress.Show(owner);

                                await ApplyNumbering(
                                    pages, index, pages[0].Level,
                                    dialog.NumericNumbering, string.Empty);

                                progress.Close();
                            }

                            logger.StopClock();
                            logger.WriteTime("numbered pages");
                        }
                    }
                }
            }
        }
Example #4
0
        public async Task IndexPages(List <string> pageIds)
        {
            string indexId = null;

            using (var progress = new UI.ProgressDialog())
            {
                progress.SetMaximum(pageIds.Count);
                progress.Show(owner);

                // create a new page to get a new ID
                one.CreatePage(sectionId, out indexId);
                var indexPage = one.GetPage(indexId);

                indexPage.Title = "Page Index";

                var container = indexPage.EnsureContentContainer();

                foreach (var pageId in pageIds)
                {
                    // get the page to copy
                    var page = one.GetPage(pageId);
                    var ns   = page.Namespace;

                    progress.SetMessage(page.Title);
                    progress.Increment();

                    var link = one.GetHyperlink(page.PageId, string.Empty);

                    container.Add(new XElement(ns + "OE",
                                               new XElement(ns + "T",
                                                            new XCData($"<a href=\"{link}\">{page.Title}</a>"))
                                               ));
                }

                await one.Update(indexPage);
            }

            // navigate after progress dialog is closed otherwise it will hang!
            if (indexId != null)
            {
                await one.NavigateTo(indexId);
            }
        }
Example #5
0
        public async Task CopyPages(List <string> pageIds)
        {
            string lastId = null;

            using (var progress = new UI.ProgressDialog())
            {
                progress.SetMaximum(pageIds.Count);
                progress.Show(owner);

                foreach (var pageId in pageIds)
                {
                    if (one.GetParent(pageId) == sectionId)
                    {
                        continue;
                    }

                    // get the page to copy
                    var page = one.GetPage(pageId);
                    progress.SetMessage(page.Title);

                    // create a new page to get a new ID
                    one.CreatePage(sectionId, out var newPageId);

                    // set the page ID to the new page's ID
                    page.Root.Attribute("ID").Value = newPageId;
                    // remove all objectID values and let OneNote generate new IDs
                    page.Root.Descendants().Attributes("objectID").Remove();
                    await one.Update(page);

                    lastId = newPageId;

                    progress.Increment();
                }
            }

            // navigate after progress dialog is closed otherwise it will hang!
            if (lastId != null)
            {
                await one.NavigateTo(lastId);
            }
        }
Example #6
0
        private async Task Toggle(bool pageOnly, bool showTimestamps)
        {
            using (var one = new OneNote())
            {
                if (pageOnly)
                {
                    var page = one.GetPage();
                    await SetTimestampVisibility(one, page, showTimestamps);
                }
                else
                {
                    var section = one.GetSection();
                    if (section != null)
                    {
                        var ns = one.GetNamespace(section);

                        var pageIds = section.Elements(ns + "Page")
                                      .Select(e => e.Attribute("ID").Value)
                                      .ToList();

                        using (var progress = new UI.ProgressDialog())
                        {
                            progress.SetMaximum(pageIds.Count);
                            progress.Show(owner);

                            foreach (var pageId in pageIds)
                            {
                                var page = one.GetPage(pageId, OneNote.PageDetail.Basic);
                                var name = page.Root.Attribute("name").Value;

                                progress.SetMessage(name);
                                progress.Increment();

                                await SetTimestampVisibility(one, page, showTimestamps);
                            }
                        }
                    }
                }
            }
        }
Example #7
0
        public override async Task Execute(params object[] args)
        {
            using (one = new OneNote())
            {
                var section = one.GetSection();
                var ns      = one.GetNamespace(section);

                var infos = section.Elements(ns + "Page")
                            .Select(e => new PageInfo
                {
                    ID   = e.Attribute("ID").Value,
                    Name = e.Attribute("name").Value,
                    Date = e.Attribute("dateTime").Value
                })
                            .ToList();

                if (infos?.Count > 0)
                {
                    logger.StartClock();

                    using (progress = new UI.ProgressDialog())
                    {
                        progress.SetMaximum(infos.Count);
                        progress.Show(owner);

                        foreach (var info in infos)
                        {
                            progress.SetMessage(info.Name);
                            await StampPage(info);
                        }

                        progress.Close();
                    }

                    logger.StopClock();
                    logger.WriteTime("datestamp pages");
                }
            }
        }
Example #8
0
        private void Export(List <string> pageIDs)
        {
            OneNote.ExportFormat format;
            string path;
            bool   withAttachments;
            bool   useUnderscores;

            // dialog...

            using (var dialog = new ExportDialog(pageIDs.Count))
            {
                if (dialog.ShowDialog(owner) != DialogResult.OK)
                {
                    return;
                }

                path            = dialog.FolderPath;
                format          = dialog.Format;
                withAttachments = dialog.WithAttachments;
                useUnderscores  = dialog.UseUnderscores;
            }

            // prepare...

            string ext = null;

            switch (format)
            {
            case OneNote.ExportFormat.HTML: ext = ".htm"; break;

            case OneNote.ExportFormat.PDF: ext = ".pdf"; break;

            case OneNote.ExportFormat.Word: ext = ".docx"; break;

            case OneNote.ExportFormat.XML: ext = ".xml"; break;

            case OneNote.ExportFormat.Markdown: ext = ".md"; break;

            case OneNote.ExportFormat.OneNote: ext = ".one"; break;
            }

            // export...

            using (var progress = new UI.ProgressDialog())
            {
                progress.SetMaximum(pageIDs.Count);
                progress.Show(owner);

                var archivist = new Archivist(one);

                foreach (var pageID in pageIDs)
                {
                    var page = one.GetPage(pageID, OneNote.PageDetail.BinaryData);

                    var title = useUnderscores
                                                ? PathFactory.CleanFileName(page.Title).Replace(' ', '_')
                                                : page.Title;

                    var filename = Path.Combine(path, title + ext);

                    progress.SetMessage(filename);
                    progress.Increment();

                    if (format == OneNote.ExportFormat.HTML)
                    {
                        if (withAttachments)
                        {
                            archivist.ExportHTML(page, ref filename);
                        }
                        else
                        {
                            archivist.Export(page.PageId, filename, OneNote.ExportFormat.HTML);
                        }
                    }
                    else if (format == OneNote.ExportFormat.XML)
                    {
                        archivist.ExportXML(page.Root, filename, withAttachments);
                    }
                    else if (format == OneNote.ExportFormat.Markdown)
                    {
                        archivist.ExportMarkdown(page, filename, withAttachments);
                    }
                    else
                    {
                        archivist.Export(page.PageId, filename, format, withAttachments);
                    }
                }
            }

            SaveDefaultPath(path);

            UIHelper.ShowMessage(string.Format(Resx.SaveAsMany_Success, pageIDs.Count, path));
        }
Example #9
0
        public async Task MovePages(List <string> pageIds)
        {
            var sections = new Dictionary <string, XElement>();
            var section  = one.GetSection(sectionId);
            var ns       = one.GetNamespace(section);

            var updated = false;

            using (var progress = new UI.ProgressDialog())
            {
                progress.SetMaximum(pageIds.Count);
                progress.Show(owner);

                foreach (var pageId in pageIds)
                {
                    // find the section that currently owns the page
                    var parentId = one.GetParent(pageId);
                    if (parentId == sectionId)
                    {
                        continue;
                    }

                    // load the owning section
                    XElement parent;
                    if (sections.ContainsKey(parentId))
                    {
                        parent = sections[parentId];
                    }
                    else
                    {
                        parent = one.GetSection(parentId);
                        sections.Add(parentId, parent);
                    }

                    // get the Page reference within the owing section
                    var element = parent.Elements(ns + "Page")
                                  .FirstOrDefault(e => e.Attribute("ID").Value == pageId);

                    if (element != null)
                    {
                        progress.SetMessage(element.Attribute("name").Value);

                        // remove page from current owner
                        element.Remove();

                        // remove misc attributes; OneNote will recreate them
                        element.Attributes()
                        .Where(a => a.Name != "ID" && a.Name != "name")
                        .Remove();

                        // add page to target section
                        section.Add(element);

                        updated = true;
                    }

                    progress.Increment();
                }
            }

            // updated at least one
            if (updated)
            {
                // update each source section
                foreach (var s in sections.Values)
                {
                    one.UpdateHierarchy(s);
                }

                sections.Clear();

                // update target section
                one.UpdateHierarchy(section);

                // navigate after progress dialog is closed otherwise it will hang!
                await one.NavigateTo(sectionId);
            }
        }
Example #10
0
        public override async Task Execute(params object[] args)
        {
            using (one = new OneNote())
            {
                (backupPath, defaultPath, _) = one.GetFolders();

                if (!Directory.Exists(backupPath))
                {
                    UIHelper.ShowError(owner, Resx.AnalyzeCommand_NoBackups);
                    return;
                }

                using (var dialog = new AnalyzeDialog())
                {
                    if (dialog.ShowDialog(owner) != DialogResult.OK)
                    {
                        return;
                    }

                    showNotebookSummary = dialog.IncludeNotebookSummary;
                    showSectionSummary  = dialog.IncludeSectionSummary;
                    pageDetail          = dialog.Detail;
                    thumbnailSize       = dialog.ThumbnailSize;
                }

                one.CreatePage(one.CurrentSectionId, out var pageId);
                var page = one.GetPage(pageId);
                page.Title = Resx.AnalyzeCommand_Title;
                page.SetMeta(MetaNames.AnalysisReport, "true");

                ns = page.Namespace;
                PageNamespace.Set(ns);

                heading1Index = page.GetQuickStyle(Styles.StandardStyles.Heading1).Index;
                heading2Index = page.GetQuickStyle(Styles.StandardStyles.Heading2).Index;

                using (progress = new UI.ProgressDialog())
                {
                    progress.SetMaximum(5);
                    progress.Show(owner);

                    var container = page.EnsureContentContainer();
                    var notebooks = await one.GetNotebooks();

                    var prev = false;

                    if (showNotebookSummary)
                    {
                        ReportNotebooks(container, notebooks);
                        ReportOrphans(container, notebooks);
                        ReportCache(container);
                        prev = true;
                    }

                    if (showSectionSummary)
                    {
                        if (prev)
                        {
                            WriteHorizontalLine(page, container);
                        }

                        await ReportSections(container, notebooks);

                        prev = true;
                    }

                    if (pageDetail == AnalysisDetail.Current)
                    {
                        if (prev)
                        {
                            WriteHorizontalLine(page, container);
                        }

                        ReportPages(container, one.GetSection(), null, pageId);
                    }
                    else if (pageDetail == AnalysisDetail.All)
                    {
                        if (prev)
                        {
                            WriteHorizontalLine(page, container);
                        }

                        ReportAllPages(container, await one.GetNotebook(), null, pageId);
                    }

                    progress.SetMessage("Updating report...");
                    await one.Update(page);
                }

                await one.NavigateTo(pageId);
            }
        }
Example #11
0
        private async Task Callback(string targetId)
        {
            if (string.IsNullOrEmpty(targetId))
            {
                // cancelled
                return;
            }

            logger.Start($"..target folder {targetId}");

            try
            {
                using (var one = new OneNote())
                {
                    // user might choose a sectiongroup or a notebook; GetSection will get either
                    var target = one.GetSection(targetId);
                    if (target == null)
                    {
                        logger.WriteLine("invalid target section");
                        return;
                    }

                    // source folder will be in current notebook
                    var notebook = await one.GetNotebook(OneNote.Scope.Pages);

                    var ns = one.GetNamespace(notebook);

                    // use current page to ascend back to closest folder to handle nesting...
                    var element = notebook.Descendants(ns + "Page")
                                  .FirstOrDefault(e => e.Attribute("ID").Value == one.CurrentPageId);

                    var folder = element.FirstAncestor(ns + "SectionGroup");
                    if (folder == null)
                    {
                        logger.WriteLine("error finding ancestor folder");
                        return;
                    }

                    if (folder.DescendantsAndSelf().Any(e => e.Attribute("ID").Value == targetId))
                    {
                        logger.WriteLine("cannot copy a folder into itself or one of its children");

                        MessageBox.Show(
                            Resx.CopyFolderCommand_InvalidTarget,
                            Resx.OneMoreTab_Label,
                            MessageBoxButtons.OK, MessageBoxIcon.Information,
                            MessageBoxDefaultButton.Button1,
                            MessageBoxOptions.DefaultDesktopOnly);

                        return;
                    }

                    logger.WriteLine(
                        $"copying folder {folder.Attribute("name").Value} " +
                        $"to {target.Attribute("name").Value}");

                    // clone structure of folder; this does not assign ID values
                    var clone = CloneFolder(folder, ns);

                    // update target so OneNote will apply new ID values
                    target.Add(clone);
                    one.UpdateHierarchy(target);

                    // re-fetch target to find newly assigned ID values
                    var upTarget = one.GetSection(targetId);

                    var cloneID = upTarget.Elements()
                                  .Where(e => !e.Attributes().Any(a => a.Name == "isRecycleBin"))
                                  .Select(e => e.Attribute("ID").Value)
                                  .Except(
                        target.Elements()
                        .Where(e => e.Attributes().Any(a => a.Name == "ID") &&
                               !e.Attributes().Any(a => a.Name == "isRecycleBin"))
                        .Select(e => e.Attribute("ID").Value)
                        ).FirstOrDefault();

                    clone = upTarget.Elements().FirstOrDefault(e => e.Attribute("ID").Value == cloneID);

                    using (progress = new UI.ProgressDialog())
                    {
                        progress.SetMaximum(folder.Descendants(ns + "Page").Count());
                        progress.Show(owner);

                        // now with a new SectionGroup with a valid ID, copy all pages into it
                        await CopyPages(folder, clone, one, ns);
                    }
                }
            }
            catch (Exception exc)
            {
                logger.WriteLine(exc);
            }
            finally
            {
                logger.End();
            }
        }