Example #1
0
        private bool FillInBookChildren(TreeNode bookNode)
        {
            IScrBook book    = bookNode.Tag as IScrBook;
            int      bookNum = (book == null) ? (int)bookNode.Tag : book.CanonicalNum;
            Form     owner   = FindForm();

            while (owner != null && !owner.Visible)
            {
                owner = owner.Owner;
            }
            if (owner == null)
            {
                owner = Form.ActiveForm ?? Application.OpenForms[0];
            }

            if (book == null || (m_associatedPtText != null && m_associatedPtText.BookPresent(book.CanonicalNum) &&
                                 !m_associatedPtText.IsCheckSumCurrent(book.CanonicalNum, book.ImportedCheckSum)))
            {
                // The book for this node is out-of-date with the Paratext book data
                IScrBook importedBook = m_bookImporter.Import(bookNum, owner, false);
                if (importedBook != null)
                {
                    bookNode.Tag = book = importedBook;
                }
                if (book == null)
                {
                    return(false);
                }
            }

            if (m_associatedPtText != null)
            {
                ScrText btProject = ParatextHelper.GetBtsForProject(m_associatedPtText).FirstOrDefault();
                if (btProject != null && !btProject.IsCheckSumCurrent(book.CanonicalNum,
                                                                      book.ImportedBtCheckSum.get_String(book.Cache.DefaultAnalWs).Text))
                {
                    // The BT for this book node is out-of-date with the Paratext BT data
                    m_bookImporter.Import(bookNum, owner, true);
                }
            }

            bookNode.Nodes.Clear();             // Gets rid of dummy.
            //IScrBook book = (IScrBook)bookNode.Tag;
            // Add Title node.
            if (book.TitleOA != null)
            {
                TreeNode titleNode =
                    new TreeNode(ResourceHelper.GetResourceString("kstidScriptureTitle"));
                titleNode.Name = book.TitleOA.ToString();
                titleNode.Tag  = book.TitleOA;
                bookNode.Nodes.Add(titleNode);
            }

            // Add Sections.
            foreach (IScrSection section in book.SectionsOS)
            {
                string chapterVerseBridge = m_scr.ChapterVerseBridgeAsString(section);
                if (section.HeadingOA != null)
                {
                    // Include the heading text if it's not empty.  See LT-8764.
                    int cTotal = 0;
                    foreach (IScrTxtPara para in section.HeadingOA.ParagraphsOS)
                    {
                        cTotal += para.Contents.Length;
                    }
                    if (cTotal > 0)
                    {
                        string   sFmt = ResourceHelper.GetResourceString("kstidSectionHeading");
                        TreeNode node = new TreeNode(String.Format(sFmt, chapterVerseBridge));
                        node.Name = String.Format(sFmt, section);
                        node.Tag  = section.HeadingOA;                        // expect an StText
                        bookNode.Nodes.Add(node);
                    }
                }
                TreeNode sectionNode = new TreeNode(chapterVerseBridge);
                sectionNode.Name = section.ToString();
                sectionNode.Tag  = section.ContentOA;                // expect an StText
                bookNode.Nodes.Add(sectionNode);
            }

            // Add Footnotes in reverse order, so we can insert them in the proper order.
            List <IScrFootnote> footnotes = new List <IScrFootnote>(book.FootnotesOS);

            footnotes.Reverse();
            foreach (IScrFootnote scrFootnote in footnotes)
            {
                // insert under the relevant section, if any (LTB-408)
                IScrSection containingSection;
                IStText     containingTitle = null;
                if (scrFootnote.TryGetContainingSection(out containingSection) ||
                    scrFootnote.TryGetContainingTitle(out containingTitle))
                {
                    string   nodeName     = m_scr.ContainingRefAsString(scrFootnote);
                    TreeNode footnoteNode = new TreeNode(nodeName);
                    footnoteNode.Tag  = scrFootnote;
                    footnoteNode.Name = "Footnote";

                    // see if we can lookup the node of this section.
                    int nodeIndex = bookNode.Nodes.IndexOfKey(
                        containingSection != null ? containingSection.ToString() : containingTitle.ToString());
                    //TreeNode[] sectionNodes = bookNode.Nodes.Find(hvoSection.ToString(), false);
                    //if (sectionNodes != null && sectionNodes.Length > 0)
                    if (nodeIndex >= 0)
                    {
                        bookNode.Nodes.Insert(nodeIndex + 1, footnoteNode);
                    }
                    else
                    {
                        bookNode.Nodes.Add(footnoteNode);                               // insert at end.
                    }
                }
            }
            return(true);
        }
Example #2
0
        /// ------------------------------------------------------------------------------------
        /// <summary>
        /// Fill in the children for a particular book. This is typically done when it gets
        /// expanded or when we need a list including the children. Separating it from the
        /// initial load saves a lot of time when we have a long list of books.
        /// </summary>
        /// <param name="bookNode">The book node.</param>
        /// <returns><c>true</c> if the dummy node was replaced by real child node(s)</returns>
        /// <remarks>protected virtual for unit tests</remarks>
        /// ------------------------------------------------------------------------------------
        protected virtual bool FillInBookChildren(TreeNode bookNode)
        {
            var book       = bookNode.Tag as IScrBook;
            var bookNum    = book?.CanonicalNum ?? (int)bookNode.Tag;
            var owningForm = FindForm();

            while (owningForm != null && !owningForm.Visible)
            {
                owningForm = owningForm.Owner;
            }
            if (owningForm == null)
            {
                owningForm = Form.ActiveForm ?? Application.OpenForms[0];
            }

            if (m_associatedPtText != null)
            {
                // Update main text, if possible/needed.
                if (m_associatedPtText.BookPresent(bookNum))
                {
                    // PT has it.
                    // If we don't have it, OR if our copy is stale, then get updated copy.
                    if (book == null || !m_associatedPtText.IsCheckSumCurrent(bookNum, book.ImportedCheckSum))
                    {
                        // Get new/fresh version from PT.
                        var importedBook = ImportBook(owningForm, bookNum);
                        if (importedBook != null)
                        {
                            book         = importedBook;
                            bookNode.Tag = importedBook;
                        }
                    }
                }
                if (book == null)
                {
                    // No book, so don't fret about a back translation
                    return(false);
                }

                // Update back translation
                IScrText btProject = ParatextHelper.GetBtsForProject(m_associatedPtText).FirstOrDefault();
                if (btProject != null && btProject.BookPresent(book.CanonicalNum) && !btProject.IsCheckSumCurrent(book.CanonicalNum, book.ImportedBtCheckSum.get_String(book.Cache.DefaultAnalWs).Text))
                {
                    // The BT for this book node is out-of-date with the Paratext BT data
                    ImportBackTranslation(owningForm, bookNum, btProject);
                }
            }

            bookNode.Nodes.Clear();             // Gets rid of dummy.
            // Add Title node.
            if (book.TitleOA != null)
            {
                var titleNode = new TreeNode(ResourceHelper.GetResourceString("kstidScriptureTitle"))
                {
                    Name = book.TitleOA.ToString(),
                    Tag  = book.TitleOA
                };
                bookNode.Nodes.Add(titleNode);
            }

            // Add Sections.
            foreach (var section in book.SectionsOS)
            {
                var chapterVerseBridge = m_scr.ChapterVerseBridgeAsString(section);
                // Include the heading text if it's not empty.  See LT-8764.
                var cTotal = section.HeadingOA?.ParagraphsOS.Cast <IScrTxtPara>().Sum(para => para.Contents.Length);
                if (cTotal > 0)
                {
                    var sFmt = ResourceHelper.GetResourceString("kstidSectionHeading");
                    var node = new TreeNode(string.Format(sFmt, chapterVerseBridge))
                    {
                        Name = string.Format(sFmt, section),
                        Tag  = section.HeadingOA                        // expect an StText
                    };
                    bookNode.Nodes.Add(node);
                }
                var sectionNode = new TreeNode(chapterVerseBridge)
                {
                    Name = section.ToString(),
                    Tag  = section.ContentOA                    // expect an StText
                };
                bookNode.Nodes.Add(sectionNode);
            }

            // Add Footnotes in reverse order, so we can insert them in the proper order.
            var footnotes = new List <IScrFootnote>(book.FootnotesOS);

            footnotes.Reverse();
            foreach (var scrFootnote in footnotes)
            {
                // insert under the relevant section, if any (LTB-408)
                IScrSection containingSection;
                IStText     containingTitle = null;
                if (!scrFootnote.TryGetContainingSection(out containingSection) && !scrFootnote.TryGetContainingTitle(out containingTitle))
                {
                    continue;
                }
                var nodeName     = m_scr.ContainingRefAsString(scrFootnote);
                var footnoteNode = new TreeNode(nodeName)
                {
                    Tag  = scrFootnote,
                    Name = "Footnote"
                };

                // see if we can lookup the node of this section.
                var nodeIndex = bookNode.Nodes.IndexOfKey(containingSection?.ToString() ?? containingTitle.ToString());
                if (nodeIndex >= 0)
                {
                    bookNode.Nodes.Insert(nodeIndex + 1, footnoteNode);
                }
                else
                {
                    bookNode.Nodes.Add(footnoteNode);                           // insert at end.
                }
            }
            return(true);
        }