Right to leftness stored in bible passage and module, affects html rendering and...
[kworship.git] / kworship / bible / KwBibleModuleBibleGateway.cpp
blobb0f32ab7297db1f0c28d185d1f127c1ba2f2a452
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwBibleModuleBibleGateway.cpp
22 * @brief A BibleGateway bible module.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBibleModuleBibleGateway.h"
27 #include "KwBiblePassage.h"
29 #include <KIO/NetAccess>
30 #include <KMessageBox>
31 #include <KLocale>
32 #include <dom/html_document.h>
33 #include <dom/html_element.h>
34 #include <dom/html_block.h>
36 #include <QStringList>
37 #include <QFile>
40 * Constructors + destructor
43 /// Default constructor.
44 KwBibleModuleBibleGateway::KwBibleModuleBibleGateway(int id)
45 : KwBibleModule()
47 KUrl url(QString("http://www.biblegateway.com/versions/index.php?action=getVersionInfo&vid=%1").arg(id));
49 QString tmpFile;
50 if (KIO::NetAccess::download(url, tmpFile, 0))
52 QFile file(tmpFile);
53 if (file.open(QFile::ReadOnly | QFile::Text))
55 QByteArray rawPage = file.readAll();
56 file.close();
58 DOM::HTMLDocument doc;
59 doc.loadXML(QString::fromUtf8(rawPage));
60 DOM::Element bookList = doc.getElementById("booklist");
61 bool tableFound = false;
62 if (!bookList.isNull())
64 // Get the next table
65 DOM::Node sibling = bookList.nextSibling();
66 while (!sibling.isNull() && sibling.nodeType() != DOM::Node::ELEMENT_NODE)
68 sibling = sibling.nextSibling();
70 DOM::Element tableElement(sibling);
71 if (!tableElement.isNull() && tableElement.tagName() == "table")
73 tableFound = true;
74 // Each row except header is a book
75 DOM::NodeList rows = tableElement.getElementsByTagName("tr");
76 bool firstRow = true;
77 for (unsigned int row = 0; row < rows.length(); ++row)
79 DOM::NodeList cells = DOM::Element(rows.item(row)).getElementsByTagName("td");
80 if (cells.length() == 2)
82 // First cell is the name
83 m_bookList.push_back(Book());
84 Book* book = &m_bookList[m_bookList.size()-1];
85 book->name = DOM::HTMLElement(cells.item(0)).innerText().string();
86 // Also check if text is right-to-left
87 if (firstRow)
89 setRightToLeft(DOM::HTMLElement(cells.item(0)).className().string().toLower().contains("rtl"));
90 firstRow = false;
92 // Second cell is the chapter links
93 DOM::NodeList chapterLinks = DOM::Element(cells.item(1)).getElementsByTagName("a");
94 for (unsigned int chapter = 0; chapter < chapterLinks.length(); ++chapter)
96 // Check the chapter number is right
97 DOM::HTMLElement link = chapterLinks.item(chapter);
98 bool numeric;
99 int check = link.innerText().string().toInt(&numeric);
100 if (!numeric || check != (int)chapter+1)
102 KMessageBox::error(0, i18n("Error parsing webpage: %1", i18n("Non sequential chapter list in book '%1'", book->name)));
103 break;
105 // Get the link
106 book->chapters.push_back(Chapter());
107 Chapter* chapter = &book->chapters[book->chapters.size()-1];
108 chapter->url = "http://www.biblegateway.com" + link.getAttribute("href").string();
109 chapter->fetched = false;
115 if (!tableFound)
117 // Book list reference node not found
118 KMessageBox::error(0, i18n("Error parsing webpage: %1", i18n("Book list table not found")));
122 KIO::NetAccess::removeTempFile(tmpFile);
124 else
126 KMessageBox::error(0, KIO::NetAccess::lastErrorString());
130 /// Destructor.
131 KwBibleModuleBibleGateway::~KwBibleModuleBibleGateway()
136 * Main interface
139 QString KwBibleModuleBibleGateway::name()
141 return QString();
144 QString KwBibleModuleBibleGateway::description()
146 return QString();
149 int KwBibleModuleBibleGateway::numChapters(int book)
151 if (book >= 0 && book < m_bookList.size())
153 return m_bookList[book].chapters.size();
155 return 0;
158 int KwBibleModuleBibleGateway::numVerses(int book, int chapter)
160 Chapter* chap = fetchChapter(book, chapter);
161 if (0 != chap)
163 return chap->verses.size();
165 else
167 return 0;
171 bool KwBibleModuleBibleGateway::fillPassageVerse(int bookIndex, int chapterIndex, int verseIndex, KwBiblePassage* outPassage)
173 Chapter* chapter = fetchChapter(bookIndex, chapterIndex);
174 if (0 != chapter)
176 outPassage->initVerse(bookIndex, 1+chapterIndex, 1+verseIndex,
177 chapter->verses[verseIndex].heading,
178 chapter->verses[verseIndex].content);
180 return false;
184 * Protected virtual interface
187 void KwBibleModuleBibleGateway::obtainBooks()
189 QStringList list;
190 for (int book = 0; book < m_bookList.size(); ++book)
192 list << m_bookList[book].name;
194 setBooks(list);
198 * Private functions
201 /// Ensure chapter contents are fetched.
202 KwBibleModuleBibleGateway::Chapter* KwBibleModuleBibleGateway::fetchChapter(int book, int chapter)
204 if (book >= 0 && book < m_bookList.size())
206 Book* bookObj = &m_bookList[book];
207 if (chapter >= 0 && chapter < m_bookList[book].chapters.size())
209 Chapter* chap = &bookObj->chapters[chapter];
210 if (!chap->fetched)
212 QString tmpFile;
213 if (KIO::NetAccess::download(chap->url, tmpFile, 0))
215 QFile file(tmpFile);
216 if (file.open(QFile::ReadOnly | QFile::Text))
218 QByteArray rawPage = file.readAll();
219 file.close();
221 DOM::HTMLDocument doc;
222 doc.loadXML(QString::fromUtf8(rawPage));
224 // Find all spans with class="sup"
225 DOM::NodeList sups = doc.body().getElementsByClassName("sup");
226 int verse = 0;
227 for (unsigned int i = 0; i < sups.length(); ++i)
229 DOM::HTMLElement span = sups.item(i);
230 if (span.tagName() == "span")
232 // Get the verse number and validate
233 bool numeric;
234 QString verseNumber = span.innerText().string();
235 Verse verseInfo;
236 int check = verseNumber.toInt(&numeric);
237 if (!numeric)
239 // Its not going to be a verse if it isn't numeric
240 KMessageBox::error(0, i18n("Error parsing webpage: %1", i18n("Non numeric superscript encountered: '%1'. It may correspond to a verse range.", verseNumber)));
241 continue;
243 ++verse;
244 if (check != verse)
246 KMessageBox::error(0, i18n("Error parsing webpage: %1", i18n("Non sequential verse list in chapter %1 of book '%2'. Expected verse %3 but got verse %4.", (chapter+1), bookObj->name, verse, check)));
247 break;
250 // Get any headers before it
251 DOM::Node sibling;
252 sibling = span.previousSibling();
253 while (!sibling.isNull())
255 DOM::Element siblingElement = sibling;
256 if (!siblingElement.isNull())
258 // Stop at a span class="sup"
259 if (siblingElement.tagName() == "span" && siblingElement.getAttribute("class") == "sup")
261 break;
263 // See if its an interesting heading
264 DOM::HTMLHeadingElement heading = siblingElement;
265 if (!heading.isNull())
267 verseInfo.heading = heading.toHTML() + verseInfo.heading;
270 sibling = sibling.previousSibling();
273 // Get any text after it until the next span
274 sibling = span.nextSibling();
275 while (!sibling.isNull())
277 DOM::Element siblingElement = sibling;
278 if (!siblingElement.isNull())
280 // Stop at a span class="sup"
281 if (siblingElement.tagName() == "span" && siblingElement.getAttribute("class") == "sup")
283 break;
285 // Also stop at headings
286 DOM::HTMLHeadingElement heading = siblingElement;
287 if (!heading.isNull())
289 break;
292 verseInfo.content += sibling.toHTML();
293 sibling = sibling.nextSibling();
296 chap->verses.push_back(verseInfo);
301 KIO::NetAccess::removeTempFile(tmpFile);
303 else
305 KMessageBox::error(0, KIO::NetAccess::lastErrorString());
307 chap->fetched = true;
310 return chap;
313 return 0;