diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp new file mode 100644 index 000000000..900548c72 --- /dev/null +++ b/src/libcalamares/locale/TimeZone.cpp @@ -0,0 +1,83 @@ +/* === This file is part of Calamares - === + * + * Copyright 2019, Adriaan de Groot + * + * Calamares is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Calamares is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Calamares. If not, see . + */ + +#include "TimeZone.h" + +#include + +namespace CalamaresUtils +{ +namespace Locale +{ + + +CStringPair::CStringPair( CStringPair&& t ) +{ + // My pointers are initialized to nullptr + std::swap( m_human, t.m_human ); + std::swap( m_key, t.m_key ); +} + +CStringPair::CStringPair( const CStringPair& t ) + : m_human( t.m_human ? strdup( t.m_human ) : nullptr ) + , m_key( t.m_key ? strdup( t.m_key ) : nullptr ) +{ +} + +/** @brief Massage an identifier into a human-readable form + * + * Makes a copy of @p s, caller must free() it. + */ +static char* +munge( const char* s ) +{ + char* t = strdup( s ); + if ( !t ) + { + return nullptr; + } + + // replace("_"," ") in the Python script + char* p = t; + while ( *p ) + { + if ( ( *p ) == '_' ) + { + *p = ' '; + } + ++p; + } + + return t; +} + +CStringPair::CStringPair( const char* s1 ) + : m_human( s1 ? munge( s1 ) : nullptr ) + , m_key( s1 ? strdup( s1 ) : nullptr ) +{ +} + + +CStringPair::~CStringPair() +{ + free( m_human ); + free( m_key ); +} + +} // namespace Locale +} // namespace CalamaresUtils diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index b9102dff9..f3590c898 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -19,6 +19,7 @@ #ifndef LOCALE_TIMEZONE_H #define LOCALE_TIMEZONE_H +#include #include namespace CalamaresUtils @@ -39,14 +40,20 @@ namespace Locale class CStringPair { public: - explicit CStringPair(const char *s1); - CStringPair(CStringPair&& t); - CStringPair(const CStringPair&) = delete; - ~CStringPair(); + /// @brief An empty pair + CStringPair() {}; + /// @brief Given an identifier, create the pair + explicit CStringPair( const char* s1 ); + CStringPair( CStringPair&& t ); + CStringPair( const CStringPair& ); + virtual ~CStringPair(); -private: - const char* m_human = nullptr; - const char* m_key = nullptr; + /// @brief Give the localized human-readable form + virtual QString tr() const = 0; + +protected: + char* m_human = nullptr; + char* m_key = nullptr; }; /// @brief A pair of strings for timezone regions (e.g. "America") @@ -54,6 +61,9 @@ class TZRegion : public CStringPair { public: using CStringPair::CStringPair; + + // NOTE: context name must match what's used in zone-extractor.py + QString tr() const override { return QObject::tr( m_human, "tz_regions" ); } }; /// @brief A pair of strings for specific timezone names (e.g. "New_York") @@ -61,9 +71,12 @@ class TZZone : public CStringPair { public: using CStringPair::CStringPair; + + // NOTE: context name must match what's used in zone-extractor.py + QString tr() const override { return QObject::tr( m_human, "tz_names" ); } }; -} -} +} // namespace Locale +} // namespace CalamaresUtils #endif // LOCALE_TIMEZONE_H