Example #1
0
        public override void Execute(params object[] args)
        {
            using (var one = new OneNote())
            {
                var section = one.GetSection();
                var ns      = one.GetNamespace(section);

                // find all level 1 pages not collapsed and immediately followed by level 2 page

                var pages =
                    from e in section.Elements(ns + "Page")
                    let n = e.NextNode
                            where n != null &&
                            e.Attribute("pageLevel").Value.Equals("1") &&
                            !e.Attributes("isCollapsed").Any(x => x.Value.Equals("true")) &&
                            n.NodeType == XmlNodeType.Element && ((XElement)n).Attribute("pageLevel").Value.Equals("2")
                            select e;

                if (pages?.Count() > 0)
                {
                    logger.WriteLine($"found {pages.Count()} expanded pages");

                    foreach (var page in pages)
                    {
                        page.Add(new XAttribute("isCollapsed", "true"));
                    }

                    one.UpdateHierarchy(section);
                }
                else
                {
                    logger.WriteLine($"found 0 expanded pages");
                }
            }
        }
Example #2
0
        public override void 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 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;

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

            logger.StopClock();
            logger.WriteTime("removed page numbering");
        }
Example #3
0
        public override void Execute(params object[] args)
        {
            using (var dialog = new PageNumberingDialog())
            {
                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 ProgressDialog())
                            {
                                progress.SetMaximum(pages.Count);
                                progress.Show(owner);

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

                                progress.Close();
                            }

                            logger.StopClock();
                            logger.WriteTime("numbered pages");
                        }
                    }
                }
            }
        }
Example #4
0
        public override void Execute(params object[] args)
        {
            using (var one = new OneNote())
            {
                var notebook = one.GetNotebook();
                if (notebook != null)
                {
                    pattern = new Regex(@"^(\(\d+\)\s).+");

                    if (ApplyNumbering(notebook, one.GetNamespace(notebook)) > 0)
                    {
                        one.UpdateHierarchy(notebook);
                    }
                }
            }
        }
Example #5
0
        /// <summary>
        /// Analyze the current page selections and determined if at least two pages are selected.
        /// </summary>
        /// <param name="control">The menu item invoking the action.</param>
        /// <returns>True if two or more pages are selected.</returns>
        public bool GetMultiPageContext(IRibbonControl control)
        {
            //Logger.Current.WriteLine($"GetMultiPageContext {control.Id}");

            using (var one = new OneNote())
            {
                var section = one.GetSection();
                var sns     = one.GetNamespace(section);

                var count =
                    section.Elements(sns + "Page")
                    .Count(e => e.Attributes("selected").Any(a => a.Value.Equals("all")));

                return(count > 1);
            }
        }
Example #6
0
        /*
         * private static void SortPages(SortDialog.Sortings sorting, SortDialog.Directions direction)
         * {
         *      using (var manager = new ApplicationManager())
         *      {
         *              var section = manager.CurrentSection();
         *              var ns = section.GetNamespaceOfPrefix("one");
         *
         *              var tree = new List<PageNode>();
         *              var list = section.Elements(ns + "Page").ToList();
         *              BuildTree(tree, list, 0, 0);
         *      }
         * }
         *
         *
         * private static int BuildTree(List<PageNode> tree, List<XElement> elements, int level, int index)
         * {
         *      if (index >= elements.Count)
         *      {
         *              return index;
         *      }
         *
         *      var element = elements[index];
         *      var pageLevel = int.Parse(element.Attribute("pageLevel").Value);
         *
         *      if (pageLevel < level)
         *      {
         *              return index;
         *      }
         *
         *      if (pageLevel > level)
         *      {
         *              index = BuildTree(tree, elements, pageLevel, index + 1);
         *      }
         *
         *      return index;
         * }
         */


        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Sections

        private void SortSections(
            SortDialog.Sortings sorting, SortDialog.Directions direction, bool pinNotes)
        {
            #region Notes

            /*
             * <one:Notebook name="Personal" nickname="Personal" ID="">
             *   <one:Section name="Notes" ID="" />
             *   <one:Section name="Automobiles" ID="" />
             *   <one:SectionGroup name="OneNote_RecycleBin" ID="" isRecycleBin="true">
             *     <one:Section name="Deleted Pages" ID="" isInRecycleBin="true" isDeletedPages="true" />
             *   </one:SectionGroup>
             * </one:Notebook>
             *
             * Sort top-level sections only; ignore section groups.
             */
            #endregion Notes

            logger.StartClock();

            using (var one = new OneNote())
            {
                // get the current notebook with its sections
                var notebook = one.GetNotebook();

                if (notebook == null)
                {
                    return;
                }

                var ns = one.GetNamespace(notebook);

                var key = sorting == SortDialog.Sortings.ByName
                                        ? "name"
                                        : "lastModifiedTime";

                SortSection(notebook, ns,
                            key, direction == SortDialog.Directions.Ascending, pinNotes);

                //logger.WriteLine(notebook.ToString());
                one.UpdateHierarchy(notebook);
            }

            logger.WriteTime(nameof(SortSections));
        }
Example #7
0
        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Notebooks

        private void SortNotebooks(SortDialog.Sortings sorting, SortDialog.Directions direction)
        {
            #region Notes

            /*
             * <one:Notebook name="Personal" nickname="Personal" ID="" path="" />
             */
            #endregion Notes

            logger.StartClock();

            using (var one = new OneNote())
            {
                var root = one.GetNotebooks();
                var ns   = one.GetNamespace(root);

                // nickname is display name whereas name is the folder name
                var key = sorting == SortDialog.Sortings.ByName
                                        ? "nickname"
                                        : "lastModifiedTime";

                IEnumerable <XElement> books;
                if (direction == SortDialog.Directions.Descending)
                {
                    books = root.Elements(ns + "Notebook")
                            .OrderByDescending(s => s.Attribute(key).Value);
                }
                else
                {
                    books = root.Elements(ns + "Notebook")
                            .OrderBy(s => s.Attribute(key).Value);
                }

                root.ReplaceNodes(books);

                //logger.WriteLine(root.ToString());
                one.UpdateHierarchy(root);
            }

            logger.WriteTime(nameof(SortNotebooks));
        }
Example #8
0
        private void Toggle(bool pageOnly, bool showTimestamps)
        {
            using (var one = new OneNote())
            {
                if (pageOnly)
                {
                    var page = one.GetPage();
                    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 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();

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

                var pageIDs = section.Elements(ns + "Page")
                              .Where(e => e.Attribute("selected")?.Value == "all")
                              .Select(e => e.Attribute("ID").Value)
                              .ToList();

                if (pageIDs.Count > 1)
                {
                    ExportMany(one, pageIDs);
                }
                else
                {
                    var page = one.GetPage();
                    ExportOne(one, page);
                }
            }
        }
Example #10
0
        private void MovePages(string sectionId, OneNote one)
        {
            var sections = new Dictionary <string, XElement>();
            var section  = one.GetSection(sectionId);
            var ns       = one.GetNamespace(section);

            var updated = false;

            using (var progress = new 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);
            }
        }
Example #11
0
        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Pages

        private void SortPages(SortDialog.Sortings sorting, SortDialog.Directions direction)
        {
            #region Notes

            /*
             * <one:Page ID="" name="Notes" pageLevel="1" />
             *
             * Pages within a section are stored as a flat list of elements with
             * indented pages indicated by pageLevel only - they are not recursive children.
             * So the code below must group child pages with their parent so all parents
             * can be sorted correctly. Children are not sorted.
             */
            #endregion Notes

            logger.StartClock();

            using (var one = new OneNote())
            {
                var root = one.GetSection();
                var ns   = one.GetNamespace(root);

                var pages = new List <PageNode>();

                foreach (var child in root.Elements(ns + "Page"))
                {
                    if (child.Attribute("pageLevel").Value == "1")
                    {
                        // found the next parent page
                        pages.Add(new PageNode(child));
                    }
                    else
                    {
                        // grouping child pages with the top parent
                        pages[pages.Count - 1].Children.Add(child);
                    }
                }

                if (direction == SortDialog.Directions.Descending)
                {
                    if (sorting == SortDialog.Sortings.ByName)
                    {
                        pages = pages.OrderByDescending(
                            p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList();
                    }
                    else
                    {
                        var key = sorting == SortDialog.Sortings.ByCreated
                                                        ? "dateTime" : "lastModifiedTime";

                        pages = pages.OrderByDescending(
                            p => p.Page.Attribute(key).Value).ToList();
                    }
                }
                else
                {
                    if (sorting == SortDialog.Sortings.ByName)
                    {
                        pages = pages.OrderBy(
                            p => EmojiDialog.RemoveEmojis(p.Page.Attribute("name").Value)).ToList();
                    }
                    else
                    {
                        var key = sorting == SortDialog.Sortings.ByCreated
                                                        ? "dateTime" : "lastModifiedTime";

                        pages = pages.OrderBy(
                            p => p.Page.Attribute(key).Value).ToList();
                    }
                }

                root.RemoveNodes();

                // recreate flat list
                foreach (var page in pages)
                {
                    root.Add(page.Page);

                    foreach (var child in page.Children)
                    {
                        root.Add(child);
                    }
                }

                //logger.WriteLine(root.ToString());
                one.UpdateHierarchy(root);
            }

            logger.WriteTime(nameof(SortPages));
        }
Example #12
0
        public override void Execute(params object[] args)
        {
            using (var one = new OneNote())
            {
                var section = one.GetSection();
                ns = one.GetNamespace(section);

                // find first selected - active page

                var active =
                    section.Elements(ns + "Page")
                    .FirstOrDefault(e => e.Attributes("isCurrentlyViewed").Any(a => a.Value.Equals("true")));

                if (active == null)
                {
                    UIHelper.ShowInfo(one.Window, "At least two pages must be selected to merge");
                    return;
                }

                var selections =
                    section.Elements(ns + "Page")
                    .Where(e =>
                           !e.Attributes("isCurrentlyViewed").Any() &&
                           e.Attributes("selected").Any(a => a.Value.Equals("all")));

                if (active == null)
                {
                    UIHelper.ShowInfo(one.Window, "At least two pages must be selected to merge");
                    return;
                }


                // get first selected (active) page and reference its quick styles, outline, size

                page = one.GetPage(active.Attribute("ID").Value);

                quickmap = page.GetQuickStyleMap();

                var offset = GetPageBottomOffset();

                // track running bottom as we add new outlines
                var maxOffset = offset;

                // find maximum z-offset
                var z = page.Root.Elements(ns + "Outline").Elements(ns + "Position")
                        .Attributes("z").Max(a => int.Parse(a.Value)) + 1;

                // merge each of the subsequently selected pages into the active page

                foreach (var selection in selections.ToList())
                {
                    var childPage = one.GetPage(selection.Attribute("ID").Value);

                    var map = page.MergeQuickStyles(childPage);

                    var childOutlines = childPage.Root.Elements(ns + "Outline");
                    if (childOutlines == null || !childOutlines.Any())
                    {
                        break;
                    }

                    var topOffset = childOutlines.Elements(ns + "Position")
                                    .Min(p => double.Parse(p.Attribute("y").Value, CultureInfo.InvariantCulture));

                    foreach (var childOutline in childOutlines)
                    {
                        // adjust position relative to new parent page outlines
                        var position = childOutline.Elements(ns + "Position").FirstOrDefault();
                        var y        = double.Parse(position.Attribute("y").Value, CultureInfo.InvariantCulture)
                                       - topOffset + offset + OutlineMargin;

                        position.Attribute("y").Value = y.ToString("#0.0", CultureInfo.InvariantCulture);

                        // keep track of lowest bottom
                        var size   = childOutline.Elements(ns + "Size").FirstOrDefault();
                        var bottom = y + double.Parse(size.Attribute("height").Value, CultureInfo.InvariantCulture);
                        if (bottom > maxOffset)
                        {
                            maxOffset = bottom;
                        }

                        position.Attribute("z").Value = z.ToString();
                        z++;

                        // remove its IDs so the page can apply its own
                        childOutline.Attributes("objectID").Remove();
                        childOutline.Descendants().Attributes("objectID").Remove();

                        page.ApplyStyleMapping(map, childOutline);

                        page.Root.Add(childOutline);
                    }

                    if (maxOffset > offset)
                    {
                        offset = maxOffset;
                    }
                }

                // update page and section hierarchy

                one.Update(page);

                foreach (var selection in selections)
                {
                    one.DeleteHierarchy(selection.Attribute("ID").Value);
                }
            }
        }
Example #13
0
        private void PowerPointImporter(string filepath, bool append, bool split)
        {
            string outpath;

            using (var powerpoint = new PowerPoint())
            {
                outpath = powerpoint.ConvertFileToImages(filepath);
            }

            if (outpath == null)
            {
                logger.WriteLine($"failed to create output path");
                return;
            }

            if (split)
            {
                using (var one = new OneNote())
                {
                    var section   = one.CreateSection(Path.GetFileNameWithoutExtension(filepath));
                    var sectionId = section.Attribute("ID").Value;
                    var ns        = one.GetNamespace(section);

                    one.NavigateTo(sectionId);

                    int i = 1;
                    foreach (var file in Directory.GetFiles(outpath, "*.jpg"))
                    {
                        one.CreatePage(sectionId, out var pageId);
                        var page = one.GetPage(pageId);
                        page.Title = $"Slide {i}";
                        var container = page.EnsureContentContainer();

                        LoadImage(container, ns, file);

                        one.Update(page);

                        i++;
                    }

                    logger.WriteLine("created section");
                }
            }
            else
            {
                using (var one = new OneNote())
                {
                    Page page;
                    if (append)
                    {
                        page = one.GetPage();
                    }
                    else
                    {
                        one.CreatePage(one.CurrentSectionId, out var pageId);
                        page       = one.GetPage(pageId);
                        page.Title = Path.GetFileName(filepath);
                    }

                    var container = page.EnsureContentContainer();

                    foreach (var file in Directory.GetFiles(outpath, "*.jpg"))
                    {
                        using (var image = Image.FromFile(file))
                        {
                            LoadImage(container, page.Namespace, file);
                        }
                    }

                    one.Update(page);

                    if (!append)
                    {
                        one.NavigateTo(page.PageId);
                    }
                }
            }

            try
            {
                Directory.Delete(outpath, true);
            }
            catch (Exception exc)
            {
                logger.WriteLine($"Error cleaning up {outpath}", exc);
            }
        }