diff options
Diffstat (limited to 'src/backend/managers/cswordbackend.cpp')
-rw-r--r-- | src/backend/managers/cswordbackend.cpp | 233 |
1 files changed, 132 insertions, 101 deletions
diff --git a/src/backend/managers/cswordbackend.cpp b/src/backend/managers/cswordbackend.cpp index 6420627..ee74e4a 100644 --- a/src/backend/managers/cswordbackend.cpp +++ b/src/backend/managers/cswordbackend.cpp @@ -7,43 +7,40 @@ * **********/ -#include "cswordbackend.h" +#include "backend/managers/cswordbackend.h" -#include "backend/rendering/centrydisplay.h" -#include "backend/rendering/cbookdisplay.h" -#include "backend/rendering/cchapterdisplay.h" +#include <QDebug> +#include <QDir> +#include <QFileInfo> +#include <QSet> +#include <QString> +#include <QTextCodec> +#include "backend/config/cbtconfig.h" #include "backend/drivers/cswordbiblemoduleinfo.h" +#include "backend/drivers/cswordbookmoduleinfo.h" #include "backend/drivers/cswordcommentarymoduleinfo.h" #include "backend/drivers/cswordlexiconmoduleinfo.h" -#include "backend/drivers/cswordbookmoduleinfo.h" -#include "backend/filters/bt_thmlhtml.h" -#include "backend/filters/bt_thmlplain.h" -#include "backend/filters/bt_osishtml.h" #include "backend/filters/bt_gbfhtml.h" +#include "backend/filters/bt_osishtml.h" +#include "backend/filters/bt_teihtml.h" #include "backend/filters/bt_plainhtml.h" +#include "backend/filters/bt_thmlhtml.h" +#include "backend/filters/bt_thmlplain.h" #include "backend/filters/osismorphsegmentation.h" +#include "backend/rendering/cbookdisplay.h" +#include "backend/rendering/cchapterdisplay.h" +#include "backend/rendering/centrydisplay.h" +#include "util/directory.h" -#include "backend/config/cbtconfig.h" - -#include "util/directoryutil.h" - -#include <dirent.h> - -//Qt -#include <QString> -#include <QDir> -#include <QFileInfo> -#include <QSet> -#include <QDebug> - -//Sword -#include <swdisp.h> -#include <swfiltermgr.h> +// Sword includes: #include <encfiltmgr.h> -#include <rtfhtml.h> #include <filemgr.h> -#include <utilstr.h> +#include <rtfhtml.h> +#include <swdisp.h> +#include <swfiltermgr.h> #include <swfilter.h> +#include <utilstr.h> + using namespace Filters; using namespace Rendering; @@ -56,6 +53,7 @@ CSwordBackend::CSwordBackend() m_filters.plain = new BT_PLAINHTML(); m_filters.thml = new BT_ThMLHTML(); m_filters.osis = new BT_OSISHTML(); + m_filters.tei = new BT_TEIHTML(); m_displays.entry = new CEntryDisplay(); m_displays.chapter = new CChapterDisplay(); @@ -70,6 +68,7 @@ CSwordBackend::CSwordBackend(const QString& path, const bool augmentHome) m_filters.plain = new BT_PLAINHTML(); m_filters.thml = new BT_ThMLHTML(); m_filters.osis = new BT_OSISHTML(); + m_filters.tei = new BT_TEIHTML(); m_displays.entry = new CEntryDisplay(); m_displays.chapter = new CChapterDisplay(); @@ -85,6 +84,7 @@ CSwordBackend::~CSwordBackend() { delete m_filters.plain; delete m_filters.thml; delete m_filters.osis; + delete m_filters.tei; delete m_displays.book; delete m_displays.chapter; @@ -194,20 +194,24 @@ void CSwordBackend::AddRenderFilters(sword::SWModule *module, sword::ConfigEntMa sourceformat = ((entry = section.find("SourceType")) != section.end()) ? (*entry).second : (sword::SWBuf) ""; moduleDriver = ((entry = section.find("ModDrv")) != section.end()) ? (*entry).second : (sword::SWBuf) ""; - if (sourceformat == "GBF") { - module->AddRenderFilter(m_filters.gbf); - noDriver = false; - } - else if (sourceformat == "PLAIN") { - module->AddRenderFilter(m_filters.plain); + if (sourceformat == "OSIS") { + module->AddRenderFilter(m_filters.osis); noDriver = false; } else if (sourceformat == "ThML") { module->AddRenderFilter(m_filters.thml); noDriver = false; } - else if (sourceformat == "OSIS") { - module->AddRenderFilter(m_filters.osis); + else if (sourceformat == "TEI") { + module->AddRenderFilter(m_filters.tei); + noDriver = false; + } + else if (sourceformat == "GBF") { + module->AddRenderFilter(m_filters.gbf); + noDriver = false; + } + else if (sourceformat == "PLAIN") { + module->AddRenderFilter(m_filters.plain); noDriver = false; } @@ -325,30 +329,20 @@ CSwordModuleInfo* CSwordBackend::findModuleByPointer(const CSwordModuleInfo* con /** Returns our local config object to store the cipher keys etc. locally for each user. The values of the config are merged with the global config. */ bool CSwordBackend::moduleConfig(const QString& module, sword::SWConfig& moduleConfig) { - sword::SectionMap::iterator section; - DIR *dir = opendir(configPath); - - struct dirent *ent; + sword::SectionMap::iterator section; + QDir dir(QString::fromUtf8(configPath)); bool foundConfig = false; - QString modFile; - - if (dir) { // find and update .conf file - rewinddir(dir); - while ((ent = readdir(dir)) && !foundConfig) { - if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) { - modFile = QString(configPath); - modFile.append("/"); - modFile.append( QString::fromLocal8Bit(ent->d_name) ); + QFileInfoList list = dir.entryInfoList(); + if (dir.isReadable()) { + for (int i = 0; i < list.size(); ++i) { + QFileInfo fileInfo = list.at(i); - moduleConfig = sword::SWConfig( modFile.toLocal8Bit().constData() ); - section = moduleConfig.Sections.find( module.toLocal8Bit().constData() ); - foundConfig = ( section != moduleConfig.Sections.end() ); - } + moduleConfig = sword::SWConfig( fileInfo.absoluteFilePath().toLocal8Bit().constData() ); + section = moduleConfig.Sections.find( module.toLocal8Bit().constData() ); + foundConfig = ( section != moduleConfig.Sections.end() ); } - - closedir(dir); } else { //try to read mods.conf moduleConfig = sword::SWConfig("");//global config @@ -367,25 +361,17 @@ bool CSwordBackend::moduleConfig(const QString& module, sword::SWConfig& moduleC if (!foundConfig && configType != 2) { //search in $HOME/.sword/ - QString myPath = util::filesystem::DirectoryUtil::getUserHomeDir().absolutePath(); - myPath.append("/.sword/mods.d"); - dir = opendir(myPath.toUtf8().constData()); - - if (dir) { - rewinddir(dir); - - while ((ent = readdir(dir)) && !foundConfig) { - if ((strcmp(ent->d_name, ".")) && (strcmp(ent->d_name, ".."))) { - modFile = myPath; - modFile.append('/'); - modFile.append(ent->d_name); - moduleConfig = sword::SWConfig( modFile.toLocal8Bit().constData() ); - section = moduleConfig.Sections.find( module.toLocal8Bit().constData() ); - foundConfig = ( section != moduleConfig.Sections.end() ); - } - } + QString myPath = util::directory::getUserHomeSwordModsDir().absolutePath(); + dir.setPath(myPath); - closedir(dir); + QFileInfoList list = dir.entryInfoList(); + if (dir.isReadable()) { + for (int i = 0; i < list.size(); ++i) { + QFileInfo fileInfo = list.at(i); + moduleConfig = sword::SWConfig( fileInfo.absoluteFilePath().toLocal8Bit().constData() ); + section = moduleConfig.Sections.find( module.toLocal8Bit().constData() ); + foundConfig = ( section != moduleConfig.Sections.end() ); + } } } @@ -526,46 +512,91 @@ void CSwordBackend::reloadModules(SetupChangedReason reason) { initModules(reason); } -const QStringList CSwordBackend::swordDirList() { - QSet<QString> ret; - const QString home = util::filesystem::DirectoryUtil::getUserHomeDir().absolutePath(); +// Get one or more shared sword config (sword.conf) files +QStringList CSwordBackend::getSharedSwordConfigFiles() const +{ + QStringList configPath; +#ifdef Q_WS_WIN + // %ALLUSERSPROFILE%\Sword\sword.conf + QString tmp = util::directory::getSharedSwordDir().filePath("sword.conf"); + QString globalPath = util::directory::convertDirSeparators(QString(getenv("SWORD_PATH"))); + configPath << globalPath.append("/Sword/sword.conf"); +#else + // /etc/sword.conf, /usr/local/etc/sword.conf + configPath = QString(globalConfPath).split(":"); +#endif + return configPath; +} - //return a list of used Sword dirs. Useful for the installer - QString configPath = QString("%1/.sword/sword.conf").arg(home); +// Get the private sword directory +QString CSwordBackend::getPrivateSwordConfigPath() const +{ + return util::directory::getUserHomeSwordDir().absolutePath(); +} - if (!QFile(configPath).exists()) { - configPath = globalConfPath; //e.g. /etc/sword.conf, /usr/local/etc/sword.conf - } +QString CSwordBackend::getPrivateSwordConfigFile() const +{ + QString file(getPrivateSwordConfigPath() + "/sword.conf"); + return util::directory::convertDirSeparators(file); +} - QStringList configs = configPath.split(":"); +// Return a list of used Sword dirs. Useful for the installer. +QStringList CSwordBackend::swordDirList() const { + namespace DU = util::directory; + typedef QStringList::const_iterator SLCI; + typedef sword::ConfigEntMap::const_iterator CEMCI; + + // Get the set of sword directories that could contain modules: + QSet<QString> swordDirSet; + QStringList configs; + + if (QFile(getPrivateSwordConfigFile()).exists()) { + // Use the private sword.conf file: + configs << getPrivateSwordConfigFile(); + } else { + /* + Did not find private sword.conf, will use shared sword.conf files to + build the private one. Once the private sword.conf exist, the shared + ones will not be searched again. + */ + configs = getSharedSwordConfigFiles(); + +#ifdef Q_WS_WIN + /* + On Windows, add the shared sword directory to the set so the new + private sword.conf will have it. The user could decide to delete this + shared path and it will not automatically come back. + */ + swordDirSet << DU::convertDirSeparators(QString(getenv("SWORD_PATH"))); +#endif + } - for (QStringList::const_iterator it = configs.begin(); it != configs.end(); ++it) { + // Search the sword.conf file(s) for sword directories that could contain modules + for (SLCI it(configs.begin()); it != configs.end(); it++) { if (!QFileInfo(*it).exists()) { continue; } - //get all DataPath and AugmentPath entries from the config file and add them to the list - sword::SWConfig conf( (*it).toUtf8().constData() ); - ret << conf["Install"]["DataPath"].c_str(); - sword::ConfigEntMap group = conf["Install"]; - sword::ConfigEntMap::iterator start = group.equal_range("AugmentPath").first; - sword::ConfigEntMap::iterator end = group.equal_range("AugmentPath").second; - - for (sword::ConfigEntMap::const_iterator it = start; it != end; ++it) { - ret << QDir(it->second.c_str()).absolutePath(); //added augment path + /* + Get all DataPath and AugmentPath entries from the config file and add + them to the list: + */ + sword::SWConfig conf((*it).toUtf8().constData()); + swordDirSet << QDir(QTextCodec::codecForLocale()->toUnicode(conf["Install"]["DataPath"].c_str())).absolutePath(); + + sword::ConfigEntMap group(conf["Install"]); + const sword::ConfigEntMap::iterator start(group.equal_range("AugmentPath").first); + const sword::ConfigEntMap::iterator end(group.equal_range("AugmentPath").second); + + for (CEMCI it(start); it != end; it++) { + QDir(QTextCodec::codecForLocale()->toUnicode(it->second.c_str())).absolutePath(); + // Added augment path: + swordDirSet << QDir(QTextCodec::codecForLocale()->toUnicode(it->second.c_str())).absolutePath(); } } - if (!home.isEmpty()) { - // This is added to the set if not there already. Notice that - // this prevents duplication only if the QDir::absolutePath() returns - // string without the prepended "/". - ret << home + "/.sword"; - } - - return ret.values(); -} + // Add the private sword path to the set if not there already: + swordDirSet << getPrivateSwordConfigPath(); -void CSwordBackend::notifyChange(SetupChangedReason reason) { - emit sigSwordSetupChanged(reason); + return swordDirSet.values(); } |