summaryrefslogtreecommitdiff
path: root/src/backend/managers/clanguagemgr.h
blob: de716c031aa75d8ad711e7d3efc48cb1fe295cde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#ifndef CLANGUAGEMGR_H
#define CLANGUAGEMGR_H

#include <QHash>
#include <QList>
#include <QString>
#include <QStringList>


/** Manages the languages of BibleTime and provides functions to work with them.
  * @author The BibleTime team
  */
class CLanguageMgr {

    public:
        /** Language container.
         * This class (Language) contains the information about the chosen language.
         */
        class Language {
            public:
                /** Default constructor of a language object.
                 * Uses the abbreviation parameter to lookup the
                 * language name and to be able to return the name, flag etc.
                 * Possible values for abbrev are de, en, fr, it etc.
                 */
                Language();
                /** Copy constructor.
                 */
                Language(const Language&);
                /** Constructor which takes all necessary data.
                */
                Language(const QString& abbrev, const QString& englishName, const QString& translatedName, const QStringList& altAbbrevs = QStringList());
                /** Destructor.
                 */
                ~Language();
                /** Returns the abbreviation.
                 * @return The abbreviation of the chosen language.
                 */
                inline const QString& abbrev() const {
                    if (m_abbrev.isEmpty() && m_altAbbrevs.count()) { //no standard abbrev but alternative ones
                        return m_altAbbrevs.first();
                    }
                    return m_abbrev;
                }
                /** Returns the translated name.
                 * @return The translated name of the language.
                 */
                inline const QString& translatedName() const {
                    return m_translatedName;
                }
                /** The english name of the language.
                 * @return The english name of the chosen language.
                 */
                inline const QString& name() const {
                    return m_englishName;
                }
                /** The alternative abbreviations which are avalable for this language.
                 * @return The List of alternate abbreviations
                 */
                inline const QStringList alternativeAbbrevs() const {
                    return m_altAbbrevs;
                }
                /**
                 * Returns true if this language object is valid, i.e. has an abbrev and name.
                 * @return True if the data is valid for this language.
                 */
                inline bool isValid() const {
                    return (!abbrev().isEmpty() && !name().isEmpty());
                }

            private:
                QString m_abbrev;
                QString m_englishName;
                QString m_translatedName;
                QStringList m_altAbbrevs;
        };

        typedef QList<Language*> LanguageList;
        typedef QHash<QString, const Language*> LangMap;
        typedef QHash<QString, const Language*>::const_iterator LangMapIterator;

        /** Constructor.
        */
        CLanguageMgr();
        /** Destructor
        */
        virtual ~CLanguageMgr();
        /**
        * Returns the standard languages available as standard. Does nothing for Sword.
        * @return A LangMap map which contains all known languages
        */
        inline const CLanguageMgr::LangMap* languages() const {
            return &m_langMap;
        }
        /**
        * Returns the languages which are available. The languages cover all available modules, but nothing more.
        * @return A map of all languages with modules available for them
        */
        const CLanguageMgr::LangMap& availableLanguages();
        /** Language for abbreviation.
        * @param abbrev The language abbreviation
        * @return Pointer to a language for the given string abbreviation.
        */
        const CLanguageMgr::Language* languageForAbbrev( const QString& abbrev ) const;
        /** Language for english name.
        * @param abbrev The english language name.
        * @return Pointer to a language for the given name
        */
        const CLanguageMgr::Language* languageForName( const QString& language ) const;
        /** Language for translated language name.
        * @param abbrev The translated language name
        * @return Pointer to a language for the given translated language name
        */
        const CLanguageMgr::Language* languageForTranslatedName( const QString& language ) const;
        /** Default language so we don't return NULL pointers.
        * @return Pointer to the default language
        */
        inline const CLanguageMgr::Language* defaultLanguage() const {
            return &m_defaultLanguage;
        }

    private:
        void init();
        inline const QStringList makeStringList(const QString& abbrevs) {
            return abbrevs.split( ";", QString::KeepEmptyParts, Qt::CaseSensitive );
        }

        Language m_defaultLanguage;
        mutable LanguageList m_langList;
        mutable LangMap m_langMap;
        mutable LanguageList m_cleanupLangPtrs;

        struct ModuleCache {
            unsigned int moduleCount;
            LangMap availableLanguages;
        }
        m_availableModulesCache;
};

#endif