Example #1
0
        // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
        // Pages

        private void SortPages(SortBy sorting, bool ascending, bool children)
        {
            #region Notes

            /*
             * <one:Page ID="" name="Notes" pageLevel="1" />
             *
             * Pages within a section are stored as a flat list of elements where indented
             * page are indicated by pageLevel; they are not recursive child elements.
             */
            #endregion Notes

            logger.StartClock();

            using (var one = new OneNote())
            {
                var section = one.GetSection();
                var ns      = section.GetNamespaceOfPrefix(OneNote.Prefix);

                var pages = section.Elements(ns + "Page").ToList();

                var tree = new List <PageNode>();
                MakePageTree(tree, pages, 0, 1);

                var cleaner = new Func <XElement, string>((e) => sorting == SortBy.Name
                                        ? AddTitleIconDialog.RemoveEmojis(e.Attribute("name").Value)
                                        : sorting == SortBy.Created
                                                ? e.Attribute("dateTime").Value
                                                : e.Attribute("lastModifiedTime").Value
                                                          );

                if (children)
                {
                    // sub-pages of currently selected page
                    var root = FindStartingNode(tree, one.CurrentPageId);
                    if (root?.Nodes.Any() == true)
                    {
                        root.Nodes = SortPageTree(root.Nodes, ascending, cleaner);
                    }
                }
                else
                {
                    // pages within section
                    tree = SortPageTree(tree, ascending, cleaner);
                }

                section.Elements().Remove();
                section.Add(FlattenPageTree(tree));
                //logger.WriteLine(section);

                one.UpdateHierarchy(section);
            }

            logger.WriteTime(nameof(SortPages));
        }
Example #2
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 => AddTitleIconDialog.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 => AddTitleIconDialog.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);
                one.UpdateHierarchy(root);
            }

            logger.WriteTime(nameof(SortPages));
        }