StelLocaleMgr.cpp   StelLocaleMgr.cpp 
skipping to change at line 30 skipping to change at line 30
#include "StelLocaleMgr.hpp" #include "StelLocaleMgr.hpp"
#include "StelApp.hpp" #include "StelApp.hpp"
#include "StelUtils.hpp" #include "StelUtils.hpp"
#include "StelFileMgr.hpp" #include "StelFileMgr.hpp"
#include <QLocale> #include <QLocale>
#include <QDebug> #include <QDebug>
#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() StelLocaleMgr::StelLocaleMgr()
: skyTranslator(NULL) : skyTranslator(NULL)
, timeFormat() , timeFormat()
, dateFormat() , dateFormat()
, timeZoneMode()
, GMTShift(0)
{ {
//generateCountryList(); core = StelApp::getInstance().getCore();
// Load from file // Load from file
QString path = StelFileMgr::findFile("data/countryCodes.dat"); QString path = StelFileMgr::findFile("data/countryCodes.dat");
if (path.isEmpty()) if (path.isEmpty())
{ {
qWarning() << "ERROR - could not find country code data file ."; qWarning() << "ERROR - could not find country code data file .";
return; return;
} }
QFile file(path); QFile file(path);
if(file.open(QIODevice::ReadOnly)) if(file.open(QIODevice::ReadOnly))
{ {
QDataStream in(&file); // read the data serialized from the file QDataStream in(&file); // read the data serialized from the file
in.setVersion(QDataStream::Qt_4_5); in.setVersion(QDataStream::Qt_5_2);
in >> countryCodeToStringMap; in >> countryCodeToStringMap;
file.close(); file.close();
} }
} }
StelLocaleMgr::~StelLocaleMgr() StelLocaleMgr::~StelLocaleMgr()
{ {
delete skyTranslator; delete skyTranslator;
skyTranslator=0; 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"); qWarning() << "Generating a country list...";
QFile textFile(StelFileMgr::findFile("data/iso3166.tab"));
if(textFile.open(QFile::ReadOnly | QFile::Text)) if(textFile.open(QFile::ReadOnly | QFile::Text))
{ {
QTextStream list(&textFile);
QString line; QString line;
while(!(line = list.readLine()).isNull()) while(!textFile.atEnd())
{ {
qDebug() << line.section(QChar('\t'), 0, 0) << ":" < line = QString::fromUtf8(textFile.readLine());
< line.section(QChar('\t'), 1, 1); if (line.startsWith("//") || line.startsWith("#"))
countryCodeToStringMap.insert(line.section(QChar('\t continue;
'), 0, 0), line.section(QChar('\t'), 1, 1));
if (!line.isEmpty())
{
QStringList list=line.split("\t", QString::K
eepEmptyParts);
QString code = list.at(0).trimmed().toLower(
);
QString country = list.at(1).trimmed().repla
ce("&", "and");
qDebug() << code << ":" << country;
countryCodeToStringMap.insert(code, country)
;
}
} }
textFile.close(); textFile.close();
} }
// Save to binary file // Save to binary file
QFile binaryFile("data/countryCodes.dat"); QFile binaryFile(StelFileMgr::findFile("data/countryCodes.dat"));
if(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 to the file
out.setVersion(QDataStream::Qt_4_5); out.setVersion(QDataStream::Qt_5_2);
out << countryCodeToStringMap; out << countryCodeToStringMap;
binaryFile.close(); binaryFile.close();
} }
} }
void StelLocaleMgr::init() void StelLocaleMgr::init()
{ {
QSettings* conf = StelApp::getInstance().getSettings(); QSettings* conf = StelApp::getInstance().getSettings();
Q_ASSERT(conf); Q_ASSERT(conf);
if (conf->value("devel/convert_countries_list", false).toBool())
generateCountryList();
setSkyLanguage(conf->value("localization/sky_locale", "system").toSt ring(), false); setSkyLanguage(conf->value("localization/sky_locale", "system").toSt ring(), false);
setAppLanguage(conf->value("localization/app_locale", "system").toSt ring(), false); setAppLanguage(conf->value("localization/app_locale", "system").toSt 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,
// 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();
if (tzstr == "system_default")
{
timeZoneMode = STzSystemDefault;
// Set the program global intern timezones variables from th
e system locale
#ifdef _MSC_BUILD
_tzset();
#else
tzset();
#endif
}
else
{
if (tzstr == "gmt+x") // TODO : handle GMT+X timezones form
{
timeZoneMode = STzGMTShift;
// GMTShift = x;
}
else
{
// We have a custom time zone name
timeZoneMode = STzCustom;
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, bool refreshAll) void StelLocaleMgr::setAppLanguage(const QString& newAppLanguageName, bool refreshAll)
{ {
// Update the translator with new locale name // Update the translator with new locale name
Q_ASSERT(StelTranslator::globalTranslator); Q_ASSERT(StelTranslator::globalTranslator);
delete StelTranslator::globalTranslator; delete StelTranslator::globalTranslator;
StelTranslator::globalTranslator = new StelTranslator("stellarium", newAppLanguageName); StelTranslator::globalTranslator = new StelTranslator("stellarium", newAppLanguageName);
qDebug() << "Application language is " << StelTranslator::globalTran slator->getTrueLocaleName(); qDebug() << "Application language is " << StelTranslator::globalTran slator->getTrueLocaleName();
if (refreshAll) if (refreshAll)
StelApp::getInstance().updateI18n(); StelApp::getInstance().updateI18n();
} }
bool StelLocaleMgr::isAppRTL() const
{
bool rtl = false;
if (QString("ar fa ckb ug ur he yi").contains(getAppLanguage()))
rtl = true;
return rtl;
}
/************************************************************************* /*************************************************************************
Set the sky language. Set the sky language.
*************************************************************************/ *************************************************************************/
void StelLocaleMgr::setSkyLanguage(const QString& newSkyLanguageName, bool refreshAll) void StelLocaleMgr::setSkyLanguage(const QString& newSkyLanguageName, bool refreshAll)
{ {
delete skyTranslator; delete skyTranslator;
// Update the translator with new locale name // Update the translator with new locale name
skyTranslator = new StelTranslator("stellarium-skycultures", newSkyL anguageName); skyTranslator = new StelTranslator("stellarium-skycultures", newSkyL anguageName);
qDebug() << "Sky language is " << skyTranslator->getTrueLocaleName() ; qDebug() << "Sky language is " << skyTranslator->getTrueLocaleName() ;
if (refreshAll) if (refreshAll)
skipping to change at line 173 skipping to change at line 165
} }
/************************************************************************* /*************************************************************************
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();
} }
bool StelLocaleMgr::isSkyRTL() const
{
bool rtl = false;
if (QString("ar fa ckb ug ur he yi").contains(getSkyLanguage()))
rtl = true;
return rtl;
}
// Get the StelTranslator currently used for sky objects. // Get the StelTranslator currently used for sky objects.
const StelTranslator& StelLocaleMgr::getSkyTranslator() const const StelTranslator& StelLocaleMgr::getSkyTranslator() const
{ {
return *skyTranslator; return *skyTranslator;
} }
const 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; return StelUtils::julianDayToISO8601String(JD + core->getUTCOffset(J
if (timeZoneMode == STzGMTShift) D)*0.041666666666);
{
shift = GMTShift;
}
else
{
shift = StelUtils::getGMTShiftFromQT(JD)*0.041666666666;
}
return StelUtils::julianDayToISO8601String(JD + shift);
} }
//! get the six ints from an ISO8601 date time, understood to be local time , make a jdate out //! get the six ints from an ISO8601 date time, understood to be local time , make a jdate out
//! of them. //! of them.
double StelLocaleMgr::getJdFromISO8601TimeLocal(const QString& t, bool* ok) const double StelLocaleMgr::getJdFromISO8601TimeLocal(const QString& t, bool* ok) const
{ {
double jd = StelUtils::getJulianDayFromISO8601String(t, ok); double jd = StelUtils::getJulianDayFromISO8601String(t, ok);
if (!*ok) if (!*ok)
{ {
qWarning() << "StelLocaleMgr::getJdFromISO8601TimeLocal: inv alid ISO8601 date. Returning JD=0"; qWarning() << "StelLocaleMgr::getJdFromISO8601TimeLocal: inv alid ISO8601 date. Returning JD=0";
return 0.0; return 0.0;
} }
// modified by shift jd -= core->getUTCOffset(jd)*0.041666666666;
if (timeZoneMode == STzGMTShift)
jd -= GMTShift;
else
jd -= StelUtils::getGMTShiftFromQT(jd)*0.041666666666;
return jd; return jd;
} }
// Return a string with the local date formated according to the dateFormat variable // Return a string with the local date formated according to the dateFormat variable
QString StelLocaleMgr::getPrintableDateLocal(double JD) const QString StelLocaleMgr::getPrintableDateLocal(double JD) const
{ {
int year, month, day, dayOfWeek; int year, month, day, dayOfWeek;
double shift = 0.0; double shift = core->getUTCOffset(JD)*0.041666666666;
if (timeZoneMode == STzGMTShift)
{
shift = GMTShift;
}
else
{
shift = StelUtils::getGMTShiftFromQT(JD)*0.041666666666;
}
StelUtils::getDateFromJulianDay(JD+shift, &year, &month, &day); StelUtils::getDateFromJulianDay(JD+shift, &year, &month, &day);
dayOfWeek = (int)floor(fmod(JD, 7)); dayOfWeek = (int)floor(fmod(JD, 7));
QString str; QString str;
switch (dateFormat) switch (dateFormat)
{ {
case SDateMMDDYYYY: case SDateMMDDYYYY:
str = QString("%1-%2-%3").arg(month,2,10,QLatin1Char ('0')).arg(day,2,10,QLatin1Char('0')).arg(year,4,10); str = QString("%1-%2-%3").arg(month,2,10,QLatin1Char ('0')).arg(day,2,10,QLatin1Char('0')).arg(year,4,10);
break; break;
case SDateDDMMYYYY: case SDateDDMMYYYY:
str = QString("%1-%2-%3").arg(day,2,10,QLatin1Char(' 0')).arg(month,2,10,QLatin1Char('0')).arg(year,4,10); str = QString("%1-%2-%3").arg(day,2,10,QLatin1Char(' 0')).arg(month,2,10,QLatin1Char('0')).arg(year,4,10);
skipping to change at line 260 skipping to change at line 239
str = StelUtils::localeDateString(year, month, day, dayOfWeek); str = StelUtils::localeDateString(year, month, day, dayOfWeek);
} }
return str; return str;
} }
// Return a string with the local time (according to timeZoneMode variable) formated // Return a string with the local time (according to timeZoneMode variable) formated
// according to the timeFormat variable // according to the timeFormat variable
QString StelLocaleMgr::getPrintableTimeLocal(double JD) const QString StelLocaleMgr::getPrintableTimeLocal(double JD) const
{ {
int hour, minute, second; int hour, minute, second;
double shift = 0.0; double shift = core->getUTCOffset(JD)*0.041666666666;
if (timeZoneMode == STzGMTShift)
{
shift = GMTShift;
}
else
{
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 time format, fallbac k to system default";
return t.toString(Qt::LocaleDate); return t.toString(Qt::LocaleDate);
} }
} }
QString StelLocaleMgr::getPrintableTimeZoneLocal(double JD) const QString StelLocaleMgr::getPrintableTimeZoneLocal(double JD) const
{ {
double shift = StelUtils::getGMTShiftFromQT(JD); if (core->getCurrentLocation().planetName=="Earth")
QTime tz = QTime(0, 0, 0).addSecs(3600*qAbs(shift)); {
if(shift<0.0f) QString timeZone = "UTC";
return "UTC-" + tz.toString("hh:mm"); QString currTZ = core->getCurrentTimeZone();
QString timeZoneST = "";
if (JD<=StelCore::TZ_ERA_BEGINNING || currTZ.contains("auto"
) || currTZ.contains("LMST"))
{
// TRANSLATORS: Local Mean Solar Time. Please use ab
breviation.
timeZoneST = qc_("LMST", "solar time");
}
if (currTZ.contains("LTST"))
{
// TRANSLATORS: Local True Solar Time. Please use ab
breviation.
timeZoneST = qc_("LTST", "solar time");
}
float shift = core->getUTCOffset(JD);
QTime tz = QTime(0, 0, 0).addSecs(3600*qAbs(shift));
if(shift<0.0f)
timeZone.append("-" + tz.toString("hh:mm"));
else
timeZone.append("+" + tz.toString("hh:mm"));
if (!timeZoneST.isEmpty())
timeZone.append(" (" + timeZoneST + ")");
return timeZone;
}
else else
return "UTC+" + tz.toString("hh:mm"); return QString();
} }
// 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.";
return STimeSystemDefault; return STimeSystemDefault;
skipping to change at line 348 skipping to change at line 346
{ {
QString dfmt; QString dfmt;
switch (dateFormat) { switch (dateFormat) {
case SDateDDMMYYYY: case SDateDDMMYYYY:
dfmt = "dd.MM.yyyy"; dfmt = "dd.MM.yyyy";
break; break;
case SDateMMDDYYYY: case SDateMMDDYYYY:
dfmt = "MM.dd.yyyy"; dfmt = "MM.dd.yyyy";
break; break;
case SDateYYYYMMDD: case SDateYYYYMMDD:
case SDateSystemDefault:
dfmt = "yyyy.MM.dd"; dfmt = "yyyy.MM.dd";
break; break;
default: default:
qWarning() << "WARNING: unknown date format, fallbac k to system default";
dfmt = "yyyy.MM.dd"; dfmt = "yyyy.MM.dd";
break; break;
} }
return dfmt; return dfmt;
} }
void StelLocaleMgr::setCustomTzName(const QString& tzname)
{
customTzName = tzname;
timeZoneMode = STzCustom;
if( customTzName != "")
{
// 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)));
tzset();
#endif
}
}
float StelLocaleMgr::getGMTShift(double JD) const
{
if (timeZoneMode == STzGMTShift) return GMTShift;
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)
{ {
QMap<QString, QString>::ConstIterator i = countryCodeToStringMap.fin d(countryCode); QMap<QString, QString>::ConstIterator i = countryCodeToStringMap.fin d(countryCode);
return (i!=countryCodeToStringMap.constEnd()) ? i.value() : QString( ); return (i!=countryCodeToStringMap.constEnd()) ? i.value() : QString( );
} }
// Return a list of all the known country names // Return a list of all the known country names
QStringList StelLocaleMgr::getAllCountryNames() QStringList StelLocaleMgr::getAllCountryNames()
{ {
 End of changes. 25 change blocks. 
108 lines changed or deleted 87 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/