StelLocaleMgr.cpp   StelLocaleMgr.cpp 
skipping to change at line 37 skipping to change at line 37
#include <QSettings> #include <QSettings>
#include <QString> #include <QString>
#include <QTextStream> #include <QTextStream>
#include <QFile> #include <QFile>
#include <ctime> #include <ctime>
QMap<QString, QString> StelLocaleMgr::countryCodeToStringMap; QMap<QString, QString> StelLocaleMgr::countryCodeToStringMap;
StelLocaleMgr::StelLocaleMgr() : skyTranslator("stellarium", StelFileMgr::g StelLocaleMgr::StelLocaleMgr()
etLocaleDir(), ""), GMTShift(0) : skyTranslator(NULL)
, timeFormat()
, dateFormat()
, timeZoneMode()
, GMTShift(0)
{ {
//generateCountryList(); //generateCountryList();
// Load from file // Load from file
QString path; QString path = StelFileMgr::findFile("data/countryCodes.dat");
try if (path.isEmpty())
{ {
path = StelFileMgr::findFile("data/countryCodes.dat"); qWarning() << "ERROR - could not find country code data file
} .";
catch (std::runtime_error& e)
{
qWarning() << "ERROR - could not find country code data file
." << e.what();
return; return;
} }
QFile file(path); QFile file(path);
file.open(QIODevice::ReadOnly); if(file.open(QIODevice::ReadOnly))
QDataStream in(&file); // read the data serialized from the file {
in.setVersion(QDataStream::Qt_4_5); QDataStream in(&file); // read the data serialized from the
in >> countryCodeToStringMap; file
file.close(); in.setVersion(QDataStream::Qt_4_5);
in >> countryCodeToStringMap;
file.close();
}
} }
StelLocaleMgr::~StelLocaleMgr() StelLocaleMgr::~StelLocaleMgr()
{} {
delete skyTranslator;
skyTranslator=0;
}
// Mehtod which generates and save the map between 2 letters country code a nd english country names // Mehtod which generates and save the map between 2 letters country code a nd english country names
void StelLocaleMgr::generateCountryList() void StelLocaleMgr::generateCountryList()
{ {
// Load ISO 3166-1 two-letter country codes from file (slow) // Load ISO 3166-1 two-letter country codes from file (slow)
// The format is "[code][tab][country name containing spaces][newlin e]" // The format is "[code][tab][country name containing spaces][newlin e]"
QFile textFile("data/iso3166-1-alpha-2.utf8"); QFile textFile("data/iso3166-1-alpha-2.utf8");
textFile.open(QFile::ReadOnly | QFile::Text); if(textFile.open(QFile::ReadOnly | QFile::Text))
QTextStream list(&textFile);
QString line;
while(!(line = list.readLine()).isNull())
{ {
qDebug() << line.section(QChar('\t'), 0, 0) << ":" << line.s QTextStream list(&textFile);
ection(QChar('\t'), 1, 1); QString line;
countryCodeToStringMap.insert(line.section(QChar('\t'), 0, 0 while(!(line = list.readLine()).isNull())
), line.section(QChar('\t'), 1, 1)); {
qDebug() << line.section(QChar('\t'), 0, 0) << ":" <
< line.section(QChar('\t'), 1, 1);
countryCodeToStringMap.insert(line.section(QChar('\t
'), 0, 0), line.section(QChar('\t'), 1, 1));
}
textFile.close();
} }
textFile.close();
// Save to binary file // Save to binary file
QFile binaryFile("data/countryCodes.dat"); QFile binaryFile("data/countryCodes.dat");
binaryFile.open(QIODevice::WriteOnly); if(binaryFile.open(QIODevice::WriteOnly))
QDataStream out(&binaryFile); // save the data serialized to the {
file QDataStream out(&binaryFile); // save the data serialized
out.setVersion(QDataStream::Qt_4_5); to the file
out << countryCodeToStringMap; out.setVersion(QDataStream::Qt_4_5);
binaryFile.close(); out << countryCodeToStringMap;
binaryFile.close();
}
} }
void StelLocaleMgr::init() void StelLocaleMgr::init()
{ {
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
Q_ASSERT(conf); Q_ASSERT(conf);
setSkyLanguage(conf->value("localization/sky_locale", "system").toSt setSkyLanguage(conf->value("localization/sky_locale", "system").toSt
ring()); ring(), false);
setAppLanguage(conf->value("localization/app_locale", "system").toSt setAppLanguage(conf->value("localization/app_locale", "system").toSt
ring()); ring(), false);
timeFormat = stringToSTimeFormat(conf->value("localization/time_disp lay_format", "system_default").toString()); timeFormat = stringToSTimeFormat(conf->value("localization/time_disp lay_format", "system_default").toString());
dateFormat = stringToSDateFormat(conf->value("localization/date_disp lay_format", "system_default").toString()); dateFormat = stringToSDateFormat(conf->value("localization/date_disp lay_format", "system_default").toString());
// time_zone used to be in init_location section of config, // time_zone used to be in init_location section of config,
// so use that as fallback when reading config - Rob // so use that as fallback when reading config - Rob
QString tzstr = conf->value("localization/time_zone", conf->value("i nit_location/time_zone", "system_default").toString()).toString(); QString tzstr = conf->value("localization/time_zone", conf->value("i nit_location/time_zone", "system_default").toString()).toString();
if (tzstr == "system_default") if (tzstr == "system_default")
{ {
timeZoneMode = STzSystemDefault; timeZoneMode = STzSystemDefault;
// Set the program global intern timezones variables from th e system locale // Set the program global intern timezones variables from th e system locale
#ifdef _MSC_BUILD
_tzset();
#else
tzset(); tzset();
#endif
} }
else else
{ {
if (tzstr == "gmt+x") // TODO : handle GMT+X timezones form if (tzstr == "gmt+x") // TODO : handle GMT+X timezones form
{ {
timeZoneMode = STzGMTShift; timeZoneMode = STzGMTShift;
// GMTShift = x; // GMTShift = x;
} }
else else
{ {
// We have a custom time zone name // We have a custom time zone name
timeZoneMode = STzCustom; timeZoneMode = STzCustom;
setCustomTzName(tzstr); setCustomTzName(tzstr);
} }
} }
} }
/************************************************************************* /*************************************************************************
Set the application locale. This apply to GUI, console messages etc.. Set the application locale. This apply to GUI, console messages etc..
*************************************************************************/ *************************************************************************/
void StelLocaleMgr::setAppLanguage(const QString& newAppLanguageName) void StelLocaleMgr::setAppLanguage(const QString& newAppLanguageName, bool refreshAll)
{ {
// Update the translator with new locale name // Update the translator with new locale name
StelTranslator::globalTranslator = StelTranslator("stellarium", Stel Q_ASSERT(StelTranslator::globalTranslator);
FileMgr::getLocaleDir(), newAppLanguageName); delete StelTranslator::globalTranslator;
qDebug() << "Application language is " << StelTranslator::globalTran StelTranslator::globalTranslator = new StelTranslator("stellarium",
slator.getTrueLocaleName(); newAppLanguageName);
StelApp::getInstance().updateI18n(); qDebug() << "Application language is " << StelTranslator::globalTran
slator->getTrueLocaleName();
if (refreshAll)
StelApp::getInstance().updateI18n();
} }
/************************************************************************* /*************************************************************************
Set the sky language. Set the sky language.
*************************************************************************/ *************************************************************************/
void StelLocaleMgr::setSkyLanguage(const QString& newSkyLanguageName) void StelLocaleMgr::setSkyLanguage(const QString& newSkyLanguageName, bool refreshAll)
{ {
delete skyTranslator;
// Update the translator with new locale name // Update the translator with new locale name
skyTranslator = StelTranslator("stellarium-skycultures", StelFileMgr skyTranslator = new StelTranslator("stellarium-skycultures", newSkyL
::getLocaleDir(), newSkyLanguageName); anguageName);
qDebug() << "Sky language is " << skyTranslator.getTrueLocaleName(); qDebug() << "Sky language is " << skyTranslator->getTrueLocaleName()
StelApp::getInstance().updateI18n(); ;
if (refreshAll)
StelApp::getInstance().updateI18n();
} }
/************************************************************************* /*************************************************************************
Get the language currently used for sky objects.. Get the language currently used for sky objects..
*************************************************************************/ *************************************************************************/
QString StelLocaleMgr::getSkyLanguage() const QString StelLocaleMgr::getSkyLanguage() const
{ {
return skyTranslator.getTrueLocaleName(); return skyTranslator->getTrueLocaleName();
} }
// Get the StelTranslator currently used for sky objects. // Get the StelTranslator currently used for sky objects.
StelTranslator& StelLocaleMgr::getSkyTranslator() const StelTranslator& StelLocaleMgr::getSkyTranslator() const
{ {
return skyTranslator; return *skyTranslator;
} }
StelTranslator& StelLocaleMgr::getAppStelTranslator() const const StelTranslator &StelLocaleMgr::getAppStelTranslator() const
{ {
return StelTranslator::globalTranslator; return *StelTranslator::globalTranslator;
} }
// Return the time in ISO 8601 format that is : %Y-%m-%d %H:%M:%S // Return the time in ISO 8601 format that is : %Y-%m-%d %H:%M:%S
QString StelLocaleMgr::getISO8601TimeLocal(double JD) const QString StelLocaleMgr::getISO8601TimeLocal(double JD) const
{ {
double shift = 0.0; double shift = 0.0;
if (timeZoneMode == STzGMTShift) if (timeZoneMode == STzGMTShift)
{ {
shift = GMTShift; shift = GMTShift;
} }
skipping to change at line 257 skipping to change at line 276
{ {
shift = StelUtils::getGMTShiftFromQT(JD)*0.041666666666; shift = StelUtils::getGMTShiftFromQT(JD)*0.041666666666;
} }
StelUtils::getTimeFromJulianDay(JD+shift, &hour, &minute, &second); StelUtils::getTimeFromJulianDay(JD+shift, &hour, &minute, &second);
QTime t(hour, minute, second); QTime t(hour, minute, second);
switch (timeFormat) switch (timeFormat)
{ {
case STimeSystemDefault: case STimeSystemDefault:
return t.toString(); return t.toString();
case STime24h: case STime24h:
return t.toString("hh:mm:ss"); return t.toString("hh:mm:ss");
case STime12h: case STime12h:
return t.toString("hh:mm:ss ap"); return t.toString("hh:mm:ss ap");
default: default:
qWarning() << "WARNING: unknown date format, fallbac k to system default"; qWarning() << "WARNING: unknown date format, fallbac k to system default";
return t.toString(Qt::LocaleDate); return t.toString(Qt::LocaleDate);
} }
} }
// Convert the time format enum to its associated string and reverse // Convert the time format enum to its associated string and reverse
StelLocaleMgr::STimeFormat StelLocaleMgr::stringToSTimeFormat(const QString & tf) const StelLocaleMgr::STimeFormat StelLocaleMgr::stringToSTimeFormat(const QString & tf) const
{ {
if (tf == "system_default") return STimeSystemDefault; if (tf == "system_default") return STimeSystemDefault;
if (tf == "24h") return STime24h; if (tf == "24h") return STime24h;
if (tf == "12h") return STime12h; if (tf == "12h") return STime12h;
qWarning() << "WARNING: unrecognized time_display_format : " << tf < < " system_default used."; qWarning() << "WARNING: unrecognized time_display_format : " << tf < < " system_default used.";
skipping to change at line 316 skipping to change at line 335
} }
void StelLocaleMgr::setCustomTzName(const QString& tzname) void StelLocaleMgr::setCustomTzName(const QString& tzname)
{ {
customTzName = tzname; customTzName = tzname;
timeZoneMode = STzCustom; timeZoneMode = STzCustom;
if( customTzName != "") if( customTzName != "")
{ {
// set the TZ environement variable and update c locale stuf f // set the TZ environement variable and update c locale stuf f
#ifdef _MSC_BUILD
_putenv(_strdup(qPrintable("TZ=" + customTzName)));
_tzset();
#else
putenv(strdup(qPrintable("TZ=" + customTzName))); putenv(strdup(qPrintable("TZ=" + customTzName)));
tzset(); tzset();
} #endif
}
} }
float StelLocaleMgr::getGMTShift(double JD) const float StelLocaleMgr::getGMTShift(double JD) const
{ {
if (timeZoneMode == STzGMTShift) return GMTShift; if (timeZoneMode == STzGMTShift) return GMTShift;
else return StelUtils::getGMTShiftFromQT(JD); else return StelUtils::getGMTShiftFromQT(JD);
} }
// Convert a 2 letter country code to string // Convert a 2 letter country code to string
QString StelLocaleMgr::countryCodeToString(const QString& countryCode) QString StelLocaleMgr::countryCodeToString(const QString& countryCode)
 End of changes. 28 change blocks. 
56 lines changed or deleted 81 lines changed or added

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/