From dac0a06f142eec685f7ca3d4990fb97cd3de98aa Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Tue, 25 Apr 2023 19:21:15 +0700 Subject: [PATCH 01/13] An obvious refactoring - split the code and data --- data/mainRes.qrc | 3 + data/timezone.tab | 69 +++++++++++++++++++++++ src/core/StelLocationMgr.cpp | 105 +++++++++++++---------------------- src/core/StelLocationMgr.hpp | 1 + 4 files changed, 111 insertions(+), 67 deletions(-) create mode 100644 data/timezone.tab diff --git a/data/mainRes.qrc b/data/mainRes.qrc index 36645d488c7af..f7c902cd71f61 100644 --- a/data/mainRes.qrc +++ b/data/mainRes.qrc @@ -2,6 +2,9 @@ icon.bmp + + timezone.tab + shaders/xyYToRGB.glsl shaders/preethamAtmosphere.vert diff --git a/data/timezone.tab b/data/timezone.tab new file mode 100644 index 0000000000000..7802d0fe125fa --- /dev/null +++ b/data/timezone.tab @@ -0,0 +1,69 @@ +# The first entry is the DB name, the second is as we display it in the program. + +# Reported April 25, 2023. (#3200) +America/Yellowknife America/Edmonton +# Seen February 06, 2023. +America/Ciudad_Juarez America/Ojinaga +# Seen November 11, 2022. +America/Thunder_Bay America/Toronto +Europe/Uzhgorod Europe/Kyiv +# Seen on October 27, 2022. TZ Removed? +Europe/Zaporozhye Europe/Kyiv +# Seen on September 26, 2022. Officially renamed. +Europe/Kyiv Europe/Kiev +Europe/Kiev Europe/Kyiv +# Seen in an unrelated bug report, 2022-08-30 +Asia/Pyongyang UTC+09:00 +# reported in SF forum on 2017-03-27 +Europe/Minsk UTC+03:00 +Europe/Samara UTC+04:00 +America/Cancun UTC-05:00 +Asia/Kamchatka UTC+12:00 +# Missing on Qt5.7/Win10 as of 2017-03-18. +Europe/Astrakhan UTC+04:00 +Europe/Ulyanovsk UTC+04:00 +Europe/Kirov UTC+03:00 +Asia/Hebron Asia/Jerusalem +# or use UTC+2:00? (political issue...) +Asia/Gaza Asia/Jerusalem +Asia/Kolkata Asia/Calcutta +Asia/Kathmandu Asia/Katmandu +Asia/Tomsk Asia/Novosibirsk +Asia/Barnaul UTC+07:00 +Asia/Ho_Chi_Minh Asia/Saigon +Asia/Hovd UTC+07:00 +America/Argentina/Buenos_Aires America/Buenos_Aires +America/Argentina/Jujuy America/Jujuy +America/Argentina/Mendoza America/Mendoza +America/Argentina/Catamarca America/Catamarca +America/Argentina/Cordoba America/Cordoba +America/Indiana/Indianapolis America/Indianapolis +America/Kentucky/Louisville America/Louisville +# Small Canadian island. +America/Miquelon UTC-03:00 +Africa/Asmara Africa/Asmera +Atlantic/Faroe Atlantic/Faeroe +Pacific/Pohnpei Pacific/Ponape +Pacific/Norfolk UTC+11:00 +Pacific/Pitcairn UTC-08:00 +# Missing on Qt5.5.1/Ubuntu 16.04.1 LTE as of 2017-03-18: +# NOTE: We must add these following zones for lookup in both ways: When the binary file is being created for publication on Linux, Rangoon/Yangon is being translated. +# UTC+6:30 Yangon missing on Ubuntu/Qt5.5.1. +Asia/Rangoon Asia/Yangon +# This can translate from the binary location file back to the zone name as known on Windows. +Asia/Yangon Asia/Rangoon +# Missing on Qt5.9.5/Ubuntu 18.04.4 +America/Godthab UTC-03:00 +# Missing on Qt5.12.10/Win7 +# no DST; https://www.zeitverschiebung.net/en/timezone/asia--qostanay +Asia/Qostanay UTC+06:00 +# no DST; https://www.zeitverschiebung.net/en/timezone/europe--saratov +Europe/Saratov UTC+04:00 +# no DST; https://www.zeitverschiebung.net/en/timezone/asia--atyrau +Asia/Atyrau UTC+05:00 +# Asia/Nicosia has no DST, but Asia/Famagusta has DST! +Asia/Famagusta Asia/Nicosia +# no DST; https://www.zeitverschiebung.net/en/timezone/america--punta_arenas +America/Punta_Arenas UTC-03:00 +# Missing on Qt5.15.2/Win10 +America/Nuuk America/Godthab diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index abbae22677b3a..d1e58e97ff991 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -408,75 +408,11 @@ void NMEALookupHelper::nmeaTimeout() StelLocationMgr::StelLocationMgr() : nmeaHelper(Q_NULLPTR), libGpsHelper(Q_NULLPTR) { - // initialize the static QMap first if necessary. - // The first entry is the DB name, the second is as we display it in the program. - if (locationDBToIANAtranslations.isEmpty()) - { - // Reported April 25, 2023. (#3200) - locationDBToIANAtranslations.insert("America/Yellowknife","America/Edmonton"); - // Seen February 06, 2023. - locationDBToIANAtranslations.insert("America/Ciudad_Juarez","America/Ojinaga"); - // Seen November 11, 2022. - locationDBToIANAtranslations.insert("America/Thunder_Bay","America/Toronto"); - locationDBToIANAtranslations.insert("Europe/Uzhgorod", "Europe/Kyiv"); - // Seen on October 27, 2022. TZ Removed? - locationDBToIANAtranslations.insert("Europe/Zaporozhye","Europe/Kyiv"); - // Seen on September 26, 2022. Officially renamed. - locationDBToIANAtranslations.insert("Europe/Kyiv", "Europe/Kiev"); - locationDBToIANAtranslations.insert("Europe/Kiev", "Europe/Kyiv"); - // Seen in an unrelated bug report, 2022-08-30 - locationDBToIANAtranslations.insert("Asia/Pyongyang", "UTC+09:00"); - // reported in SF forum on 2017-03-27 - locationDBToIANAtranslations.insert("Europe/Minsk", "UTC+03:00"); - locationDBToIANAtranslations.insert("Europe/Samara", "UTC+04:00"); - locationDBToIANAtranslations.insert("America/Cancun", "UTC-05:00"); - locationDBToIANAtranslations.insert("Asia/Kamchatka", "UTC+12:00"); - // Missing on Qt5.7/Win10 as of 2017-03-18. - locationDBToIANAtranslations.insert("Europe/Astrakhan", "UTC+04:00"); - locationDBToIANAtranslations.insert("Europe/Ulyanovsk", "UTC+04:00"); - locationDBToIANAtranslations.insert("Europe/Kirov", "UTC+03:00"); - locationDBToIANAtranslations.insert("Asia/Hebron", "Asia/Jerusalem"); - locationDBToIANAtranslations.insert("Asia/Gaza", "Asia/Jerusalem"); // or use UTC+2:00? (political issue...) - locationDBToIANAtranslations.insert("Asia/Kolkata", "Asia/Calcutta"); - locationDBToIANAtranslations.insert("Asia/Kathmandu", "Asia/Katmandu"); - locationDBToIANAtranslations.insert("Asia/Tomsk", "Asia/Novosibirsk"); - locationDBToIANAtranslations.insert("Asia/Barnaul", "UTC+07:00"); - locationDBToIANAtranslations.insert("Asia/Ho_Chi_Minh", "Asia/Saigon"); - locationDBToIANAtranslations.insert("Asia/Hovd", "UTC+07:00"); - locationDBToIANAtranslations.insert("America/Argentina/Buenos_Aires", "America/Buenos_Aires"); - locationDBToIANAtranslations.insert("America/Argentina/Jujuy", "America/Jujuy"); - locationDBToIANAtranslations.insert("America/Argentina/Mendoza", "America/Mendoza"); - locationDBToIANAtranslations.insert("America/Argentina/Catamarca", "America/Catamarca"); - locationDBToIANAtranslations.insert("America/Argentina/Cordoba", "America/Cordoba"); - locationDBToIANAtranslations.insert("America/Indiana/Indianapolis", "America/Indianapolis"); - locationDBToIANAtranslations.insert("America/Kentucky/Louisville", "America/Louisville"); - locationDBToIANAtranslations.insert("America/Miquelon", "UTC-03:00"); // Small Canadian island. - locationDBToIANAtranslations.insert("Africa/Asmara", "Africa/Asmera"); - locationDBToIANAtranslations.insert("Atlantic/Faroe", "Atlantic/Faeroe"); - locationDBToIANAtranslations.insert("Pacific/Pohnpei", "Pacific/Ponape"); - locationDBToIANAtranslations.insert("Pacific/Norfolk", "UTC+11:00"); - locationDBToIANAtranslations.insert("Pacific/Pitcairn", "UTC-08:00"); - // Missing on Qt5.5.1/Ubuntu 16.04.1 LTE as of 2017-03-18: - // NOTE: We must add these following zones for lookup in both ways: When the binary file is being created for publication on Linux, Rangoon/Yangon is being translated. - locationDBToIANAtranslations.insert("Asia/Rangoon", "Asia/Yangon"); // UTC+6:30 Yangon missing on Ubuntu/Qt5.5.1. - locationDBToIANAtranslations.insert("Asia/Yangon", "Asia/Rangoon"); // This can translate from the binary location file back to the zone name as known on Windows. - locationDBToIANAtranslations.insert( "", "UTC"); - // Missing on Qt5.9.5/Ubuntu 18.04.4 - locationDBToIANAtranslations.insert("America/Godthab", "UTC-03:00"); - // Missing on Qt5.12.10/Win7 - locationDBToIANAtranslations.insert("Asia/Qostanay", "UTC+06:00"); // no DST; https://www.zeitverschiebung.net/en/timezone/asia--qostanay - locationDBToIANAtranslations.insert("Europe/Saratov", "UTC+04:00"); // no DST; https://www.zeitverschiebung.net/en/timezone/europe--saratov - locationDBToIANAtranslations.insert("Asia/Atyrau", "UTC+05:00"); // no DST; https://www.zeitverschiebung.net/en/timezone/asia--atyrau - locationDBToIANAtranslations.insert("Asia/Famagusta", "Asia/Nicosia"); // Asia/Nicosia has no DST, but Asia/Famagusta has DST! - locationDBToIANAtranslations.insert("America/Punta_Arenas", "UTC-03:00"); // no DST; https://www.zeitverschiebung.net/en/timezone/america--punta_arenas - // Missing on Qt5.15.2/Win10 - locationDBToIANAtranslations.insert("America/Nuuk", "America/Godthab"); - // N.B. Further missing TZ names will be printed out in the log.txt. Resolve these by adding into this list. - // TODO later: create a text file in user data directory, and auto-update it weekly. - } - QSettings* conf = StelApp::getInstance().getSettings(); + // N.B. Further missing TZ names will be printed out in the log.txt. Resolve these by adding into data/timezone.tab file. + loadTimeZones(); + loadCountries(); loadRegions(); // The line below allows to re-generate the location file, you still need to gunzip it manually afterward. @@ -1246,6 +1182,41 @@ void StelLocationMgr::loadCountries() countryNameToCodeMap.insert("Taiwan (Provice of China)", "tw"); } +void StelLocationMgr::loadTimeZones() +{ + QFile tzFile(":/data/timezone.tab"); + if(tzFile.open(QFile::ReadOnly | QFile::Text)) + { + if (locationDBToIANAtranslations.isEmpty()) + { + QString line; + int readOk=0; + locationDBToIANAtranslations.insert("", "UTC"); + while(!tzFile.atEnd()) + { + line = QString::fromUtf8(tzFile.readLine()); + if (line.startsWith("//") || line.startsWith("#") || line.isEmpty()) + continue; + + if (!line.isEmpty()) + { + #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0)) + QStringList list=line.split("\t", Qt::KeepEmptyParts); + #else + QStringList list=line.split("\t", QString::KeepEmptyParts); + #endif + + // The first entry is the DB name, the second is as we display it in the program. + locationDBToIANAtranslations.insert(list.at(0).trimmed().toLocal8Bit(), list.at(1).trimmed().toLocal8Bit()); + readOk++; + } + } + qDebug() << "Loaded" << readOk << "fixes for time zones"; + } + tzFile.close(); + } +} + void StelLocationMgr::loadRegions() { QFile geoFile(StelFileMgr::findFile("data/regions-geoscheme.tab")); diff --git a/src/core/StelLocationMgr.hpp b/src/core/StelLocationMgr.hpp index 93b5fd70bf43f..9ab89597d41b4 100644 --- a/src/core/StelLocationMgr.hpp +++ b/src/core/StelLocationMgr.hpp @@ -160,6 +160,7 @@ private slots: private: void loadRegions(); void loadCountries(); + void loadTimeZones(); void generateBinaryLocationFile(const QString& txtFile, bool isUserLocation, const QString& binFile) const; //! Load cities from a file From c616ed43b95d95364097d83a17f322416f80df38 Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Tue, 25 Apr 2023 19:27:38 +0700 Subject: [PATCH 02/13] Fix header of fine --- data/timezone.tab | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/data/timezone.tab b/data/timezone.tab index 7802d0fe125fa..4158e9eb4dd33 100644 --- a/data/timezone.tab +++ b/data/timezone.tab @@ -1,4 +1,12 @@ -# The first entry is the DB name, the second is as we display it in the program. +# Missing and wrong time zones +# +# This file contains a table of missing and/or wrong time zones. +# Columns are separated by a single tab. Lines beginning with '#' are comments. +# All text uses UTF-8 encoding. The columns of the table are as follows: +# +# 1. DB name +# 2. Name as we display it in the program +# # Reported April 25, 2023. (#3200) America/Yellowknife America/Edmonton From d305bcf03b5ad4e583accc3ffe5ef8380553a84b Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Thu, 27 Apr 2023 14:16:59 +0700 Subject: [PATCH 03/13] Add a special word --- data/timezone.tab | 1 + 1 file changed, 1 insertion(+) diff --git a/data/timezone.tab b/data/timezone.tab index 4158e9eb4dd33..b4d69ed157f35 100644 --- a/data/timezone.tab +++ b/data/timezone.tab @@ -75,3 +75,4 @@ Asia/Famagusta Asia/Nicosia America/Punta_Arenas UTC-03:00 # Missing on Qt5.15.2/Win10 America/Nuuk America/Godthab +#:EOF \ No newline at end of file From 4d5363c02ce203683bcccb13ff86405e4b84dac5 Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Thu, 27 Apr 2023 20:42:44 +0700 Subject: [PATCH 04/13] Move TZF file into user directory --- src/core/StelLocationMgr.cpp | 57 +++++++++++++++++++++--------------- src/core/StelLocationMgr.hpp | 1 + 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index d1e58e97ff991..6921ba1a6b79c 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -42,7 +42,7 @@ #include TimezoneNameMap StelLocationMgr::locationDBToIANAtranslations; - +QString StelLocationMgr::tzfFileName = "data/timezone.tab"; QList StelLocationMgr::regions; QMap StelLocationMgr::countryCodeToRegionMap; QMap StelLocationMgr::countryNameToCodeMap; @@ -1184,35 +1184,46 @@ void StelLocationMgr::loadCountries() void StelLocationMgr::loadTimeZones() { - QFile tzFile(":/data/timezone.tab"); + QString tzFilePath = StelFileMgr::findFile(tzfFileName, StelFileMgr::File); + if (tzFilePath.isEmpty()) + { + tzFilePath = StelFileMgr::findFile(tzfFileName, StelFileMgr::New); + // Create a default TZF (time zone fixes) file + QFile tzSrc(":/data/timezone.tab"); + if (!tzSrc.copy(tzFilePath)) + { + qWarning() << "Cannot copy time zones file to " + QDir::toNativeSeparators(tzFilePath); + return; + } + } + QFile tzFile(tzFilePath); + if(tzFile.open(QFile::ReadOnly | QFile::Text)) { - if (locationDBToIANAtranslations.isEmpty()) + locationDBToIANAtranslations.clear(); + QString line; + int readOk=0; + locationDBToIANAtranslations.insert("", "UTC"); + while(!tzFile.atEnd()) { - QString line; - int readOk=0; - locationDBToIANAtranslations.insert("", "UTC"); - while(!tzFile.atEnd()) - { - line = QString::fromUtf8(tzFile.readLine()); - if (line.startsWith("//") || line.startsWith("#") || line.isEmpty()) - continue; + line = QString::fromUtf8(tzFile.readLine()); + if (line.startsWith("//") || line.startsWith("#") || line.isEmpty()) + continue; - if (!line.isEmpty()) - { - #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0)) - QStringList list=line.split("\t", Qt::KeepEmptyParts); - #else - QStringList list=line.split("\t", QString::KeepEmptyParts); - #endif + if (!line.isEmpty()) + { + #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0)) + QStringList list=line.split("\t", Qt::KeepEmptyParts); + #else + QStringList list=line.split("\t", QString::KeepEmptyParts); + #endif - // The first entry is the DB name, the second is as we display it in the program. - locationDBToIANAtranslations.insert(list.at(0).trimmed().toLocal8Bit(), list.at(1).trimmed().toLocal8Bit()); - readOk++; - } + // The first entry is the DB name, the second is as we display it in the program. + locationDBToIANAtranslations.insert(list.at(0).trimmed().toLocal8Bit(), list.at(1).trimmed().toLocal8Bit()); + readOk++; } - qDebug() << "Loaded" << readOk << "fixes for time zones"; } + qDebug() << "Loaded" << readOk << "fixes for time zones"; tzFile.close(); } } diff --git a/src/core/StelLocationMgr.hpp b/src/core/StelLocationMgr.hpp index 9ab89597d41b4..b63f1f03d65f2 100644 --- a/src/core/StelLocationMgr.hpp +++ b/src/core/StelLocationMgr.hpp @@ -183,6 +183,7 @@ private slots: static QList regions; static QMap countryCodeToRegionMap; static QMap countryNameToCodeMap; + static QString tzfFileName; StelLocation lastResortLocation; From e02e570a59ddd3c1ec83407b78c88d710980724f Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 13:38:04 +0700 Subject: [PATCH 05/13] Fix crash --- src/core/StelLocationMgr.cpp | 21 ++++++++++++--------- src/core/StelLocationMgr.hpp | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index 6921ba1a6b79c..36395f30d81b1 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -411,7 +411,7 @@ StelLocationMgr::StelLocationMgr() QSettings* conf = StelApp::getInstance().getSettings(); // N.B. Further missing TZ names will be printed out in the log.txt. Resolve these by adding into data/timezone.tab file. - loadTimeZones(); + loadTimeZoneFixes(); loadCountries(); loadRegions(); @@ -1182,17 +1182,17 @@ void StelLocationMgr::loadCountries() countryNameToCodeMap.insert("Taiwan (Provice of China)", "tw"); } -void StelLocationMgr::loadTimeZones() +void StelLocationMgr::loadTimeZoneFixes() { - QString tzFilePath = StelFileMgr::findFile(tzfFileName, StelFileMgr::File); + QString tzFilePath = StelFileMgr::findFile(tzfFileName); if (tzFilePath.isEmpty()) { tzFilePath = StelFileMgr::findFile(tzfFileName, StelFileMgr::New); - // Create a default TZF (time zone fixes) file + // Create a default TZF (timezone fixes) file QFile tzSrc(":/data/timezone.tab"); if (!tzSrc.copy(tzFilePath)) { - qWarning() << "Cannot copy time zones file to " + QDir::toNativeSeparators(tzFilePath); + qWarning() << "Cannot copy timezone fixes to " + QDir::toNativeSeparators(tzFilePath); return; } } @@ -1202,8 +1202,8 @@ void StelLocationMgr::loadTimeZones() { locationDBToIANAtranslations.clear(); QString line; - int readOk=0; - locationDBToIANAtranslations.insert("", "UTC"); + locationDBToIANAtranslations.insert("", "UTC"); // a first fix + int readOk = 1; while(!tzFile.atEnd()) { line = QString::fromUtf8(tzFile.readLine()); @@ -1219,8 +1219,11 @@ void StelLocationMgr::loadTimeZones() #endif // The first entry is the DB name, the second is as we display it in the program. - locationDBToIANAtranslations.insert(list.at(0).trimmed().toLocal8Bit(), list.at(1).trimmed().toLocal8Bit()); - readOk++; + if (list.count()==2) // to avoid crashes + { + locationDBToIANAtranslations.insert(list.at(0).trimmed().toUtf8(), list.at(1).trimmed().toUtf8()); + readOk++; + } } } qDebug() << "Loaded" << readOk << "fixes for time zones"; diff --git a/src/core/StelLocationMgr.hpp b/src/core/StelLocationMgr.hpp index b63f1f03d65f2..62623c92c659e 100644 --- a/src/core/StelLocationMgr.hpp +++ b/src/core/StelLocationMgr.hpp @@ -160,7 +160,7 @@ private slots: private: void loadRegions(); void loadCountries(); - void loadTimeZones(); + void loadTimeZoneFixes(); void generateBinaryLocationFile(const QString& txtFile, bool isUserLocation, const QString& binFile) const; //! Load cities from a file From fa69fd5158fcff4be3385956671266933c6ffd5f Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 18:55:57 +0700 Subject: [PATCH 06/13] Let's use regular expression --- src/core/StelLocationMgr.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index 36395f30d81b1..1f87e2ec92c45 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -1195,6 +1195,9 @@ void StelLocationMgr::loadTimeZoneFixes() qWarning() << "Cannot copy timezone fixes to " + QDir::toNativeSeparators(tzFilePath); return; } + QFile dest(tzFilePath); + // fix file permissions + dest.setPermissions(dest.permissions() | QFile::WriteOwner); } QFile tzFile(tzFilePath); @@ -1204,6 +1207,7 @@ void StelLocationMgr::loadTimeZoneFixes() QString line; locationDBToIANAtranslations.insert("", "UTC"); // a first fix int readOk = 1; + static const QRegularExpression dataRx("^([\\w\\/]+)\\t([\\w\\/\\+\\-:]+)\\s*(.*)$"); while(!tzFile.atEnd()) { line = QString::fromUtf8(tzFile.readLine()); @@ -1212,16 +1216,11 @@ void StelLocationMgr::loadTimeZoneFixes() if (!line.isEmpty()) { - #if (QT_VERSION>=QT_VERSION_CHECK(5, 14, 0)) - QStringList list=line.split("\t", Qt::KeepEmptyParts); - #else - QStringList list=line.split("\t", QString::KeepEmptyParts); - #endif - - // The first entry is the DB name, the second is as we display it in the program. - if (list.count()==2) // to avoid crashes + QRegularExpressionMatch dataMatch=dataRx.match(line); + if (dataMatch.hasMatch()) { - locationDBToIANAtranslations.insert(list.at(0).trimmed().toUtf8(), list.at(1).trimmed().toUtf8()); + // The first entry is the DB name, the second is as we display it in the program. + locationDBToIANAtranslations.insert(dataMatch.captured(1).trimmed().toUtf8(), dataMatch.captured(2).trimmed().toUtf8()); readOk++; } } From 8980cb5182135a8685b8624e01f820fb767aa7b5 Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 19:19:01 +0700 Subject: [PATCH 07/13] simplification the code --- src/core/StelLocationMgr.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index 1f87e2ec92c45..a98bc56749484 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -1214,15 +1214,12 @@ void StelLocationMgr::loadTimeZoneFixes() if (line.startsWith("//") || line.startsWith("#") || line.isEmpty()) continue; - if (!line.isEmpty()) + QRegularExpressionMatch dataMatch = dataRx.match(line); + if (dataMatch.hasMatch()) { - QRegularExpressionMatch dataMatch=dataRx.match(line); - if (dataMatch.hasMatch()) - { - // The first entry is the DB name, the second is as we display it in the program. - locationDBToIANAtranslations.insert(dataMatch.captured(1).trimmed().toUtf8(), dataMatch.captured(2).trimmed().toUtf8()); - readOk++; - } + // The first entry is the DB name, the second is as we display it in the program. + locationDBToIANAtranslations.insert(dataMatch.captured(1).trimmed().toUtf8(), dataMatch.captured(2).trimmed().toUtf8()); + readOk++; } } qDebug() << "Loaded" << readOk << "fixes for time zones"; From 2eaf989db2818ad38e761888081020618b6cdaba Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 21:21:36 +0700 Subject: [PATCH 08/13] Fix checking updates --- src/gui/HelpDialog.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/gui/HelpDialog.cpp b/src/gui/HelpDialog.cpp index 978c4988f19a7..d1cd220d72eed 100644 --- a/src/gui/HelpDialog.cpp +++ b/src/gui/HelpDialog.cpp @@ -220,13 +220,10 @@ void HelpDialog::downloadComplete(QNetworkReply *reply) QString latestVersion = map["name"].toString(); latestVersion.replace("v","", Qt::CaseInsensitive); QStringList v = latestVersion.split("."); + if (v.count()==2) + v.append("0"); - QString appVersion; - if (v.count()==3) - appVersion = StelUtils::getApplicationVersion(); - else - appVersion = StelUtils::getApplicationPublicVersion(); - + QString appVersion = StelUtils::getApplicationVersion(); QStringList c = appVersion.split("."); int r = StelUtils::compareVersions(latestVersion, appVersion); if (r==-1 || c.count()>v.count() || c.last().contains("-")) From 22b06c2383693c6b348635e0bc2159c94f666843 Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 22:22:14 +0700 Subject: [PATCH 09/13] cosmetic fix --- plugins/Exoplanets/src/Exoplanets.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/Exoplanets/src/Exoplanets.cpp b/plugins/Exoplanets/src/Exoplanets.cpp index 3cb2149df83de..158942d53ebbe 100644 --- a/plugins/Exoplanets/src/Exoplanets.cpp +++ b/plugins/Exoplanets/src/Exoplanets.cpp @@ -965,7 +965,7 @@ void Exoplanets::updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) while (bytesTotal > 1024) { bytesReceived = static_cast(std::floor(static_cast(bytesReceived) / 1024.)); - bytesTotal = static_cast(std::floor(static_cast(bytesTotal) / 1024.)); + bytesTotal = static_cast(std::floor(static_cast(bytesTotal) / 1024.)); } currentValue = static_cast(bytesReceived); endValue = static_cast(bytesTotal); From 0e4f3ec54bd5b8e93e4e9787af9572f37b0f4026 Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Fri, 28 Apr 2023 22:23:52 +0700 Subject: [PATCH 10/13] Add updater for TZF --- src/core/StelLocationMgr.hpp | 2 + src/gui/LocationDialog.cpp | 132 +++++++++++++++++++++++++++++++++++ src/gui/LocationDialog.hpp | 28 +++++++- src/gui/locationDialogGui.ui | 119 ++++++++++++++++--------------- 4 files changed, 224 insertions(+), 57 deletions(-) diff --git a/src/core/StelLocationMgr.hpp b/src/core/StelLocationMgr.hpp index 62623c92c659e..9ca895cb909ea 100644 --- a/src/core/StelLocationMgr.hpp +++ b/src/core/StelLocationMgr.hpp @@ -104,6 +104,8 @@ class StelLocationMgr : public QObject //! Pick region name from region code static QString pickRegionFromCode(int regionCode); + static QString getTZFFileName() { return tzfFileName; } + public slots: //! Return the StelLocation for a given string //! Can match location name, or coordinates diff --git a/src/gui/LocationDialog.cpp b/src/gui/LocationDialog.cpp index babd2bfd5a785..60052a6c87f45 100644 --- a/src/gui/LocationDialog.cpp +++ b/src/gui/LocationDialog.cpp @@ -33,6 +33,7 @@ #include "StelFileMgr.hpp" #include "StelGui.hpp" #include "StelSkyCultureMgr.hpp" +#include "StelProgressController.hpp" #include #include @@ -45,6 +46,10 @@ LocationDialog::LocationDialog(QObject* parent) : StelDialog("Location", parent) , isEditingNew(false) + , updateState(CompleteNoUpdates) + , networkManager(nullptr) + , downloadReply(nullptr) + , progressBar(nullptr) , allModel(nullptr) , pickedModel(nullptr) , proxyModel(nullptr) @@ -209,6 +214,11 @@ void LocationDialog::createDialogContent() }); ui->citySearchLineEdit->setFocus(); + + // Set up download manager for checker of updates + networkManager = StelApp::getInstance().getNetworkAccessManager(); + updateState = CompleteNoUpdates; + connect(ui->updateTZFButton, SIGNAL(clicked()), this, SLOT(updateTZF())); } void LocationDialog::setDisplayFormatForSpins(bool flagDecimalDegrees) @@ -981,3 +991,125 @@ void LocationDialog::filterSitesByRegion() ui->citySearchLineEdit->setText(""); // https://wiki.qt.io/Technical_FAQ#Why_does_the_memory_keep_increasing_when_repeatedly_pasting_text_and_calling_clear.28.29_in_a_QLineEdit.3F ui->citySearchLineEdit->setFocus(); } + +void LocationDialog::updateTZF() +{ + if (updateState==LocationDialog::Updating) + { + qWarning() << "Already updating... will not start again until current update is complete."; + return; + } + + if (progressBar == Q_NULLPTR) + progressBar = StelApp::getInstance().addProgressBar(); + progressBar->setValue(0); + progressBar->setRange(0, 0); + + qDebug() << "Updating timezone fixes..."; + + QSettings* conf = StelApp::getInstance().getSettings(); + QUrl URL(conf->value("main/timezone_url", "https://www.stellarium.org/files/timezone.tab").toString()); + connect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadComplete(QNetworkReply*))); + QNetworkRequest request; + request.setUrl(URL); + request.setRawHeader("User-Agent", StelUtils::getUserAgentString().toUtf8()); +#if (QT_VERSIONget(request); + connect(downloadReply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateDownloadProgress(qint64,qint64))); + + updateState = LocationDialog::Updating; +} + +void LocationDialog::downloadComplete(QNetworkReply *reply) +{ + if (reply == nullptr) + return; + + disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadComplete(QNetworkReply*))); + deleteDownloadProgressBar(); + + if (reply->error() || reply->bytesAvailable()==0) + { + qWarning() << "Error: While trying to access" + << reply->url().toString() + << "the following error occurred:" + << reply->errorString(); + + reply->deleteLater(); + downloadReply = nullptr; + updateState = LocationDialog::DownloadError; + return; + } + + try + { + QByteArray data = reply->readAll(); + if (QString(data).endsWith(":EOF")) // Qt5-compatible check + { + QString tzfFilePath = StelFileMgr::findFile(StelLocationMgr::getTZFFileName()); + QFile tzfFile(tzfFilePath); + if (tzfFile.exists()) + tzfFile.remove(); + + if (tzfFile.open(QIODevice::WriteOnly | QIODevice::Text)) + { + tzfFile.write(data); + tzfFile.close(); + } + qWarning() << "Updating timezone fixes are complete... The new data will be apply on next startup."; + updateState = LocationDialog::CompleteUpdates; + } + else + { + qWarning() << "Downloaded timezone fixes are not completed, restore the data!"; + updateState = LocationDialog::OtherError; + } + } + catch (std::runtime_error &e) + { + qDebug() << "Cannot write timezone fixes to file:" << e.what(); + return; + } + + reply->deleteLater(); + downloadReply = nullptr; + + emit updateTZFComplete(); +} + +void LocationDialog::updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + if (progressBar == Q_NULLPTR) + return; + + int currentValue = 0; + int endValue = 0; + + if (bytesTotal > -1 && bytesReceived <= bytesTotal) + { + //Round to the greatest possible derived unit + while (bytesTotal > 1024) + { + bytesReceived = static_cast(std::floor(static_cast(bytesReceived) / 1024.)); + bytesTotal = static_cast(std::floor(static_cast(bytesTotal) / 1024.)); + } + currentValue = static_cast(bytesReceived); + endValue = static_cast(bytesTotal); + } + + progressBar->setValue(currentValue); + progressBar->setRange(0, endValue); +} + +void LocationDialog::deleteDownloadProgressBar() +{ + disconnect(this, SLOT(updateDownloadProgress(qint64,qint64))); + + if (progressBar) + { + StelApp::getInstance().removeProgressBar(progressBar); + progressBar = Q_NULLPTR; + } +} diff --git a/src/gui/LocationDialog.hpp b/src/gui/LocationDialog.hpp index 658a725dd5138..842410eb14306 100644 --- a/src/gui/LocationDialog.hpp +++ b/src/gui/LocationDialog.hpp @@ -22,6 +22,8 @@ #include #include "StelDialog.hpp" +#include +#include class Ui_locationDialogForm; class QModelIndex; @@ -33,6 +35,16 @@ class LocationDialog : public StelDialog { Q_OBJECT public: + //! @enum UpdateState + //! Used for keeping for track of the download/update status + enum UpdateState { + Updating, //!< Update in progress + CompleteNoUpdates, //!< Update completed, there we no updates + CompleteUpdates, //!< Update completed, there were updates + DownloadError, //!< Error during download phase + OtherError //!< Other error + }; + LocationDialog(QObject* parent); ~LocationDialog() override; @@ -86,7 +98,17 @@ public slots: //! Populates tooltips for GUI elements. void populateTooltips(); - + + // variables and functions for the updater + UpdateState updateState; + QNetworkAccessManager * networkManager; + QNetworkReply * downloadReply; + class StelProgressController* progressBar; + void deleteDownloadProgressBar(); + +signals: + void updateTZFComplete(void); + private slots: //! Called whenever the StelLocationMgr is updated void reloadLocations(); @@ -163,6 +185,10 @@ private slots: //! Updates the check state and the enabled/disabled status. void updateTimeZoneControls(bool useCustomTimeZone); + void updateTZF(void); + void downloadComplete(QNetworkReply * reply); + void updateDownloadProgress(qint64 bytesReceived, qint64 bytesTotal); + private: QString lastPlanet; // for caching when switching map QString customTimeZone; // for caching when switching around timezones. diff --git a/src/gui/locationDialogGui.ui b/src/gui/locationDialogGui.ui index ea15a6976fb56..2f8c06ca4fe75 100644 --- a/src/gui/locationDialogGui.ui +++ b/src/gui/locationDialogGui.ui @@ -6,7 +6,7 @@ 0 0 - 630 + 641 542 @@ -693,38 +693,13 @@ 0 - - - - - 0 - 0 - - - - - 0 - 24 - - - - - 16777215 - 24 - - - - true - - - QComboBox::NoInsert + + + + - - - - - - New Location + + Enable daylight saving time @@ -735,17 +710,17 @@ - - + + - Use custom time zone + Region: - - + + - Time zone: + Use custom time zone @@ -756,10 +731,25 @@ - - - - true + + + + + 0 + 0 + + + + + 0 + 24 + + + + + 16777215 + 24 + true @@ -797,20 +787,37 @@ - - - - Region: + + + + true + + + true + + + QComboBox::NoInsert - - - - + + + + New Location + + + + - Enable daylight saving time + Time zone: + + + + + + + Update time zones @@ -832,6 +839,11 @@
Dialog.hpp
1 + + StelCloseButton + QPushButton +
gui/StelCloseButton.hpp
+
AngleSpinBox QSpinBox @@ -842,11 +854,6 @@ QWidget
MapWidget.hpp
- - StelCloseButton - QPushButton -
gui/StelCloseButton.hpp
-
citySearchLineEdit From 42c478fcb70fdafec9996a85b8e0a4f9ec90a9fa Mon Sep 17 00:00:00 2001 From: Georg Zotti Date: Sat, 29 Apr 2023 12:31:45 +0200 Subject: [PATCH 11/13] Cosmetics --- src/core/StelLocationMgr.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index a98bc56749484..64ccebd2d075e 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -527,7 +527,7 @@ LocationMap StelLocationMgr::loadCitiesBin(const QString& fileName) } } } - if (unknownTZlist.length()>0) + if (!unknownTZlist.isEmpty()) { unknownTZlist.removeDuplicates(); qDebug() << "StelLocationMgr::loadCitiesBin(): Summary of unknown TimeZones:"; From f82ddbee2d8bbd29d1d6ef1cc6c28a573001901c Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Tue, 2 May 2023 00:14:31 +0700 Subject: [PATCH 12/13] Cosmetics --- data/timezone.tab | 1 + src/gui/LocationDialog.cpp | 22 +++++++--------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/data/timezone.tab b/data/timezone.tab index b4d69ed157f35..5783041e2906e 100644 --- a/data/timezone.tab +++ b/data/timezone.tab @@ -7,6 +7,7 @@ # 1. DB name # 2. Name as we display it in the program # +# This file is automatically retrieved on demand. Do not edit! # Reported April 25, 2023. (#3200) America/Yellowknife America/Edmonton diff --git a/src/gui/LocationDialog.cpp b/src/gui/LocationDialog.cpp index 60052a6c87f45..ce0dd9c6603fb 100644 --- a/src/gui/LocationDialog.cpp +++ b/src/gui/LocationDialog.cpp @@ -216,7 +216,8 @@ void LocationDialog::createDialogContent() ui->citySearchLineEdit->setFocus(); // Set up download manager for checker of updates - networkManager = StelApp::getInstance().getNetworkAccessManager(); + //networkManager = StelApp::getInstance().getNetworkAccessManager(); + networkManager = new QNetworkAccessManager(this); updateState = CompleteNoUpdates; connect(ui->updateTZFButton, SIGNAL(clicked()), this, SLOT(updateTZF())); } @@ -1028,7 +1029,6 @@ void LocationDialog::downloadComplete(QNetworkReply *reply) return; disconnect(networkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadComplete(QNetworkReply*))); - deleteDownloadProgressBar(); if (reply->error() || reply->bytesAvailable()==0) { @@ -1037,13 +1037,9 @@ void LocationDialog::downloadComplete(QNetworkReply *reply) << "the following error occurred:" << reply->errorString(); - reply->deleteLater(); - downloadReply = nullptr; updateState = LocationDialog::DownloadError; - return; } - - try + else { QByteArray data = reply->readAll(); if (QString(data).endsWith(":EOF")) // Qt5-compatible check @@ -1067,15 +1063,11 @@ void LocationDialog::downloadComplete(QNetworkReply *reply) updateState = LocationDialog::OtherError; } } - catch (std::runtime_error &e) - { - qDebug() << "Cannot write timezone fixes to file:" << e.what(); - return; - } + + deleteDownloadProgressBar(); reply->deleteLater(); downloadReply = nullptr; - emit updateTZFComplete(); } @@ -1092,8 +1084,8 @@ void LocationDialog::updateDownloadProgress(qint64 bytesReceived, qint64 bytesTo //Round to the greatest possible derived unit while (bytesTotal > 1024) { - bytesReceived = static_cast(std::floor(static_cast(bytesReceived) / 1024.)); - bytesTotal = static_cast(std::floor(static_cast(bytesTotal) / 1024.)); + bytesReceived /= 1024; + bytesTotal /= 1024; } currentValue = static_cast(bytesReceived); endValue = static_cast(bytesTotal); From 76ecb16b89cd9fab58f76b175989eb08dc9e003a Mon Sep 17 00:00:00 2001 From: "Alexander V. Wolf" Date: Tue, 2 May 2023 01:32:36 +0700 Subject: [PATCH 13/13] Special patch for John Simple --- src/core/StelLocationMgr.cpp | 2 ++ src/core/StelLocationMgr.hpp | 2 ++ src/gui/LocationDialog.cpp | 1 + 3 files changed, 5 insertions(+) diff --git a/src/core/StelLocationMgr.cpp b/src/core/StelLocationMgr.cpp index 64ccebd2d075e..40500a69eb629 100644 --- a/src/core/StelLocationMgr.cpp +++ b/src/core/StelLocationMgr.cpp @@ -46,6 +46,7 @@ QString StelLocationMgr::tzfFileName = "data/timezone.tab"; QList StelLocationMgr::regions; QMap StelLocationMgr::countryCodeToRegionMap; QMap StelLocationMgr::countryNameToCodeMap; +bool StelLocationMgr::unknownTZ = false; #ifdef ENABLE_GPS #ifdef ENABLE_LIBGPS @@ -530,6 +531,7 @@ LocationMap StelLocationMgr::loadCitiesBin(const QString& fileName) if (!unknownTZlist.isEmpty()) { unknownTZlist.removeDuplicates(); + unknownTZ = true; qDebug() << "StelLocationMgr::loadCitiesBin(): Summary of unknown TimeZones:"; for (const auto& tz : unknownTZlist) { diff --git a/src/core/StelLocationMgr.hpp b/src/core/StelLocationMgr.hpp index 9ca895cb909ea..f5c75588d19df 100644 --- a/src/core/StelLocationMgr.hpp +++ b/src/core/StelLocationMgr.hpp @@ -105,6 +105,7 @@ class StelLocationMgr : public QObject static QString pickRegionFromCode(int regionCode); static QString getTZFFileName() { return tzfFileName; } + static bool unknownTimezonesDetected() { return unknownTZ; } public slots: //! Return the StelLocation for a given string @@ -186,6 +187,7 @@ private slots: static QMap countryCodeToRegionMap; static QMap countryNameToCodeMap; static QString tzfFileName; + static bool unknownTZ; StelLocation lastResortLocation; diff --git a/src/gui/LocationDialog.cpp b/src/gui/LocationDialog.cpp index ce0dd9c6603fb..ab6f49e77fdd8 100644 --- a/src/gui/LocationDialog.cpp +++ b/src/gui/LocationDialog.cpp @@ -219,6 +219,7 @@ void LocationDialog::createDialogContent() //networkManager = StelApp::getInstance().getNetworkAccessManager(); networkManager = new QNetworkAccessManager(this); updateState = CompleteNoUpdates; + ui->updateTZFButton->setVisible(locMgr->unknownTimezonesDetected()); connect(ui->updateTZFButton, SIGNAL(clicked()), this, SLOT(updateTZF())); }