1 /*********
2 *
3 * In the name of the Father, and of the Son, and of the Holy Spirit.
4 *
5 * This file is part of BibleTime's source code, http://www.bibletime.info/.
6 *
7 * Copyright 1999-2016 by the BibleTime developers.
8 * The BibleTime source code is licensed under the GNU General Public License version 2.0.
9 *
10 **********/
11 
12 #ifndef CLANGUAGEMGR_H
13 #define CLANGUAGEMGR_H
14 
15 #include <QHash>
16 #include <QList>
17 #include <QString>
18 #include <QStringList>
19 
20 
21 /**
22   \brief Manages the languages and provides functions to work with them.
23   \note This is a singleton.
24 */
25 class CLanguageMgr {
26 
27     public:
28 
29         /**
30           \brief A language descriptor for CLanguageMgr.
31 
32           This class (Language) contains the information about the a language.
33         */
34         class Language {
35 
36             friend class CLanguageMgr;
37             friend class BtFontSettingsPage;
38 
39             public: /* Methods: */
40 
41                 /**
42                   \returns the abbreviation of the this language.
43                 */
abbrev()44                 inline const QString & abbrev() const {
45                     if (m_abbrev.isEmpty() && m_altAbbrevs.count()) {
46                         /* No standard abbrev but alternative ones */
47                         return m_altAbbrevs.first();
48                     }
49                     return m_abbrev;
50                 }
51 
52                 /**
53                   \returns the translated name of this language.
54                 */
translatedName()55                 inline const QString & translatedName() const {
56                     return m_translatedName;
57                 }
58 
59                 /**
60                   \returns the english name of this language.
61                 */
englishName()62                 inline const QString & englishName() const {
63                     return m_englishName;
64                 }
65 
66                 /**
67                   \returns a list of alternative abbreviations for this language.
68                 */
alternativeAbbrevs()69                 inline const QStringList & alternativeAbbrevs() const {
70                     return m_altAbbrevs;
71                 }
72 
73                 /**
74                   \returns whether this language object is valid, i.e. has an
75                            abbreviation and an english name.
76                 */
isValid()77                 inline bool isValid() const {
78                     return (!m_abbrev.isEmpty() && !m_englishName.isEmpty());
79                 }
80 
81             private: /* Methods: */
82 
Language()83                 inline Language() {}
84 
Language(const char * abbrev,const char * englishName,const QString & translatedName)85                 inline Language(const char * abbrev,
86                                 const char * englishName,
87                                 const QString & translatedName)
88                     : m_abbrev(abbrev)
89                     , m_englishName(QString::fromUtf8(englishName))
90                     , m_translatedName(translatedName) {}
91 
Language(const QString & abbrev,const QString & englishName,const QString & translatedName)92                 inline Language(const QString & abbrev,
93                                 const QString & englishName,
94                                 const QString & translatedName)
95                     : m_abbrev(abbrev)
96                     , m_englishName(englishName)
97                     , m_translatedName(translatedName) {}
98 
Language(const char * abbrev,const char * englishName,const QString & translatedName,const QStringList & altAbbrevs)99                 inline Language(const char * abbrev,
100                                 const char * englishName,
101                                 const QString & translatedName,
102                                 const QStringList & altAbbrevs)
103                     : m_abbrev(abbrev)
104                     , m_englishName(QString::fromUtf8(englishName))
105                     , m_translatedName(translatedName)
106                     , m_altAbbrevs(altAbbrevs) {}
107 
108             private: /* Fields: */
109 
110                 const QString m_abbrev;
111                 const QString m_englishName;
112                 const QString m_translatedName;
113                 const QStringList m_altAbbrevs;
114 
115         }; /* class Language { */
116 
117         using LanguageList = QList<Language *>;
118         using LangMap = QHash<QString, Language const *>;
119         using LangMapIterator = LangMap::const_iterator;
120 
121 
122         /** Returns the singleton instance, creating it if one does not exist. */
123         static CLanguageMgr *instance();
124 
125         /** Destroys the singleton instance, if one exists. */
126         static void destroyInstance();
127 
128         CLanguageMgr();
129 
130         virtual ~CLanguageMgr();
131 
132         /**
133         * Returns the standard languages available as standard. Does nothing for Sword.
134         * @return A LangMap map which contains all known languages
135         */
languages()136         inline const CLanguageMgr::LangMap* languages() const {
137             return &m_langMap;
138         }
139         /**
140         * Returns the languages which are available. The languages cover all available modules, but nothing more.
141         * @return A map of all languages with modules available for them
142         */
143         const CLanguageMgr::LangMap& availableLanguages();
144         /** Language for abbreviation.
145         * @param abbrev The language abbreviation
146         * @return Pointer to a language for the given string abbreviation.
147         */
148         const CLanguageMgr::Language* languageForAbbrev( const QString& abbrev ) const;
149 
150         /** Language for translated language name.
151         * @param abbrev The translated language name
152         * @return Pointer to a language for the given translated language name
153         */
154         const CLanguageMgr::Language* languageForTranslatedName( const QString& language ) const;
155         /** Default language so we don't return NULL pointers.
156         * @return Pointer to the default language
157         */
defaultLanguage()158         inline const CLanguageMgr::Language* defaultLanguage() const {
159             return &m_defaultLanguage;
160         }
161 
162     private:
163         void init();
makeStringList(const QString & abbrevs)164         inline const QStringList makeStringList(const QString& abbrevs) {
165             return abbrevs.split( ";", QString::KeepEmptyParts, Qt::CaseSensitive );
166         }
167 
168         Language m_defaultLanguage;
169         mutable LanguageList m_langList;
170         mutable LangMap m_langMap;
171         mutable LanguageList m_cleanupLangPtrs;
172 
173         struct ModuleCache {
174             int moduleCount;
175             LangMap availableLanguages;
176         } m_availableModulesCache;
177 
178         static CLanguageMgr *m_instance;
179 };
180 
181 #endif
182 
183