1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
21 * @file KwBibleManagerBibleGateway.cpp
22 * @brief A bible manager for BibleGateway.com.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwBibleManagerBibleGateway.h"
27 #include "KwBibleModuleBibleGateway.h"
29 #include <KIO/NetAccess>
30 #include <KMessageBox>
33 #include <QDomDocument>
34 #include <QDomElement>
35 #include <QDomNodeList>
38 * Constructors + destructor
41 /// Default constructor.
42 KwBibleManagerBibleGateway::KwBibleManagerBibleGateway()
48 , m_versionsByLanguage()
53 KwBibleManagerBibleGateway::~KwBibleManagerBibleGateway()
55 foreach (Version
* version
, m_versionsById
)
57 delete version
->module
;
66 QString
KwBibleManagerBibleGateway::name() const
68 return "BibleGateway.com";
71 bool KwBibleManagerBibleGateway::isRemote() const
76 KwBibleModule
* KwBibleManagerBibleGateway::module(const QString
& name
)
78 QHash
<QString
, Version
*>::iterator it
= m_versionsByName
.find(name
);
80 // Does the module with this name exist?
81 if (it
!= m_versionsByName
.end())
85 // Otherwise perhaps its a language
88 int langId
= m_languages
.indexOf(name
);
91 // Just get the first module of this language
92 const QList
<int>& moduleIds
= m_versionsByLanguage
[langId
];
93 if (!moduleIds
.isEmpty())
95 int id
= moduleIds
.first();
96 version
= m_versionsById
[id
];
102 if (0 == version
->module
)
104 version
->module
= new KwBibleModuleBibleGateway(version
->id
);
106 return version
->module
;
114 QStringList
KwBibleManagerBibleGateway::moduleNames()
117 return m_versionsByName
.keys();
120 QStringList
KwBibleManagerBibleGateway::moduleNamesInLanguage(const QString
& lang
)
123 int languageId
= m_languages
.indexOf(lang
);
127 const QList
<int>& moduleIds
= m_versionsByLanguage
[languageId
];
128 foreach (int id
, moduleIds
)
130 names
<< m_versionsById
[id
]->name
;
136 QStringList
KwBibleManagerBibleGateway::languages()
146 /// Ensure the version information is cached.
147 void KwBibleManagerBibleGateway::ensureCached()
152 if (KIO::NetAccess::download(KUrl("http://www.biblegateway.com/"), tmpFile
, 0))
155 if (file
.open(QFile::ReadOnly
| QFile::Text
))
157 QByteArray rawPage
= file
.readAll();
159 /// @todo Convert to use KDE DOM as its a bit more flexible of invalid XML
160 QString page
= QString::fromUtf8(rawPage
);
161 QRegExp
rx("<select\\s+name=\"qs_version\">.*</select>");
162 if (-1 != rx
.indexIn(page
))
165 if (dom
.setContent(rx
.cap(), false))
167 // Go through all the options (languages and versions)
169 QList
<int>* langMods
= 0;
170 QDomNodeList options
= dom
.elementsByTagName("option");
171 for (int i
= 0; i
< options
.count(); ++i
)
173 QDomElement el
= options
.at(i
).toElement();
176 // Its either a language (with class="lang") or a version
177 QString className
= el
.attribute("class");
178 QString text
= el
.text();
179 if (className
== "lang")
183 langMods
= &m_versionsByLanguage
[langId
];
185 else if (langId
>= 0)
188 int versionId
= el
.attribute("value").toInt(&ok
);
191 // Ensure there is none with this id already
192 QHash
<int, Version
*>::const_iterator it
= m_versionsById
.constFind(versionId
);
193 if (it
== m_versionsById
.constEnd())
195 Version
* version
= new Version
;
196 version
->name
= text
;
197 version
->id
= versionId
;
199 m_versionsById
[versionId
] = version
;
200 m_versionsByName
[text
] = version
;
201 *langMods
<< versionId
;
212 KIO::NetAccess::removeTempFile(tmpFile
);
216 KMessageBox::error(0, KIO::NetAccess::lastErrorString());
221 /// Clear all modules.
222 void KwBibleManagerBibleGateway::clear()
226 foreach (Version
* version
, m_versionsById
)
228 delete version
->module
;
233 m_versionsById
.clear();
234 m_versionsByName
.clear();
235 m_versionsByLanguage
.clear();