1 //
2 // C++ Implementation: fmpaths
3 //
4 // Description:
5 //
6 //
7 // Author: Pierre Marchand <pierremarc@oep-h.com>, (C) 2008
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12 #include "fmpaths.h"
13 #include "typotek.h"
14 
15 #include <QApplication>
16 // #include <QDebug>
17 
18 FMPaths *FMPaths::instance = 0;
getThis()19 FMPaths * FMPaths::getThis()
20 {
21 	if(!instance)
22 		instance = new FMPaths;
23 	return instance;
24 }
25 
TranslationsDir()26 QString FMPaths::TranslationsDir()
27 {
28 	if(getThis()->FMPathsDB.contains("TranslationsDir"))
29 		return getThis()->FMPathsDB["TranslationsDir"];
30 	QString dirsep(QDir::separator());
31 #ifdef PLATFORM_APPLE
32 	QString QMDirPath = QApplication::applicationDirPath();
33 	QMDirPath +=  dirsep + ".." + dirsep + "Resources" + dirsep + "Locales" + dirsep;
34 #elif _WIN32
35 	QString QMDirPath = QApplication::applicationDirPath();
36 	QMDirPath +=  dirsep + "share" + dirsep + "qm" + dirsep;
37 #else
38 	QString QMDirPath = PREFIX;
39 	QMDirPath +=  dirsep + "share" + dirsep + "fontmatrix" + dirsep + "qm" + dirsep;
40 #endif
41 	getThis()->FMPathsDB["TranslationsDir"] = QMDirPath;
42 	return QMDirPath;
43 }
44 
45 
HelpDir()46 QString FMPaths::HelpDir()
47 {
48 	if(getThis()->FMPathsDB.contains("HelpDir"))
49 		return getThis()->FMPathsDB["HelpDir"];
50 	QString hf;
51 	QString dirsep(QDir::separator());
52 #ifdef PLATFORM_APPLE
53 	hf = LocalizedDirPath( QApplication::applicationDirPath() + dirsep + "help" + dirsep );
54 #elif _WIN32
55 	hf = LocalizedDirPath(QApplication::applicationDirPath() + dirsep + "help" + dirsep );
56 #else
57 	hf = LocalizedDirPath( PREFIX + dirsep + "share" + dirsep + "fontmatrix" + dirsep + "help" + dirsep );
58 #endif
59 	getThis()->FMPathsDB["HelpDir"] = hf;
60 	return getThis()->FMPathsDB["HelpDir"];
61 }
62 
ResourcesDir()63 QString FMPaths::ResourcesDir()
64 {
65 	if(getThis()->FMPathsDB.contains("ResourcesDir"))
66 		return getThis()->FMPathsDB["ResourcesDir"];
67 	QString dirsep(QDir::separator());
68 #ifdef PLATFORM_APPLE
69 	QString QMDirPath = QApplication::applicationDirPath();
70 	QMDirPath +=  dirsep + ".." + dirsep + "Resources" + dirsep ;
71 #elif _WIN32
72 	QString QMDirPath = QApplication::applicationDirPath();
73 	QMDirPath +=  dirsep + "share" + dirsep + "resources" + dirsep;
74 #else
75 	QString QMDirPath = PREFIX;
76 	QMDirPath +=  dirsep + "share" + dirsep + "fontmatrix" + dirsep + "resources" + dirsep;
77 #endif
78 	getThis()->FMPathsDB["ResourcesDir"] = QMDirPath;
79 	return getThis()->FMPathsDB["ResourcesDir"];
80 }
81 
ScriptsDir()82 QString FMPaths::ScriptsDir()
83 {
84 	QString sep(QDir::separator());
85 	return typotek::getInstance()->getOwnDir().absolutePath() + sep + "Scripts"+ sep;
86 }
87 
SamplesDir()88 QString FMPaths::SamplesDir()
89 {
90 	QString sep(QDir::separator());
91 	return typotek::getInstance()->getOwnDir().absolutePath() + sep + "Samples"+ sep;
92 }
93 
FiltersDir()94 QString FMPaths::FiltersDir()
95 {
96 	QString sep(QDir::separator());
97 	QDir bdir(typotek::getInstance()->getOwnDir().absolutePath());
98 	if (!bdir.exists(QString("Filters")))
99 		bdir.mkdir(QString("Filters"));
100 	return typotek::getInstance()->getOwnDir().absolutePath() + sep + "Filters"+ sep;
101 }
102 
LocalizedDirPath(const QString & base,const QString & fallback)103 QString FMPaths::LocalizedDirPath(const QString & base, const QString& fallback )
104 {
105 	QString sep("_");
106 	QStringList l_c(QLocale::system().name().split(sep));
107 	QString langcode( l_c.first() );
108 	QString countrycode(l_c.last());
109 
110 	QStringList names;
111 	if((!langcode.isEmpty()) || (!countrycode.isEmpty()))
112 	{
113 		names << base + langcode + sep + countrycode ;
114 		names << base + langcode  ;
115 	}
116 	names << base + fallback  ;
117 	names << base  ;
118 
119 	foreach(QString t, names)
120 	{
121 		QDir d(t);
122 		if( d.exists() )
123 		{
124 			return d.absolutePath() + QString(QDir::separator()) ;
125 		}
126 	}
127 
128 	return QString();
129 }
130 
LocalizedFilePath(const QString & base,const QString & ext,const QString & fallback)131 QString FMPaths::LocalizedFilePath(const QString & base, const QString & ext, const QString& fallback)
132 {
133 	QString sep("_");
134 	QStringList l_c(QLocale::system().name().split(sep));
135 	QString langcode( l_c.first() );
136 	QString countrycode(l_c.last());
137 
138 	QStringList names;
139 	if((!langcode.isEmpty()) || (!countrycode.isEmpty()))
140 	{
141 		names << base + langcode + sep + countrycode + ext ;
142 		names << base + langcode + ext ;
143 	}
144 	names << base + fallback + ext ;
145 	names << base + ext ;
146 
147 	foreach(QString t, names)
148 	{
149 		if( QFile::exists(t) )
150 		{
151 			return t;
152 		}
153 	}
154 
155 	return QString();
156 }
157 
158 
159