summaryrefslogtreecommitdiffstats
path: root/bibletime/backend/clanguagemgr.h
blob: 2acc4c7fbe70d2026899572de92222a0d8ed86b7 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2006 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

//Qt includes
#include <qstring.h>
#include <qstringlist.h>
#include <qvaluelist.h>
#include <qmap.h>
#include <qdict.h>

/** Manages the anguages 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;
		/** Returns the translated name.
		 * @return The translated name of the language.
		 */
		inline const QString& translatedName() const;
		//always define inlines in the header file, or make them not inline.
		/** 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 A pointer to the list of alternate abbreviations
		 */
		inline const QStringList* const 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 const bool isValid() const;

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

	typedef QPtrList<CLanguageMgr::Language> LanguageList;

	typedef QDict<Language> LangMap;
	typedef QDictIterator<Language> LangMapIterator;

	/** Constructor.
	*/
	CLanguageMgr();
	/** Destructor
	*/
	virtual ~CLanguageMgr();
	/**
	* Returns the standard languages available as standard. Does nothing for Sword.
	* @return A QDict<Language> map which contains all known languages
	*/
	inline const CLanguageMgr::LangMap* const languages() const;
	/**
	* 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* const 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* const 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* const languageForTranslatedName( const QString& language ) const;
	/** Default language so we don't return NULL pointers.
	* @return Pointer to the default language
	*/
	inline const CLanguageMgr::Language* const defaultLanguage() const;

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

	mutable LangMap m_langMap;
	Language m_defaultLanguage;

	//typedef QPtrList<CLanguageMgr::Language> LanguageList;
	static LanguageList m_langList;
	static LanguageList m_cleanupLangPtrs;

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


/** Returns true if this language object is valid, i.e. has an abbrev and name. */
inline const bool CLanguageMgr::Language::isValid() const {
	return (!abbrev().isEmpty() && !name().isEmpty());
}

inline const QString& CLanguageMgr::Language::abbrev() const {
	if (m_altAbbrevs && m_abbrev.isEmpty() && m_altAbbrevs->count()) { //no standard abbrev but alternative ones
		return m_altAbbrevs->first();
	}

	return m_abbrev;
}

inline const QString& CLanguageMgr::Language::translatedName() const {
	return m_translatedName;
}

inline const CLanguageMgr::LangMap* const CLanguageMgr::languages() const {
	return &m_langMap;
}

inline const CLanguageMgr::Language* const CLanguageMgr::defaultLanguage() const {
	return &m_defaultLanguage;
}

#endif