2019-11-25 20:51:45 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
|
|
|
*
|
|
|
|
* Copyright 2019, Adriaan de Groot <groot@kde.org>
|
|
|
|
*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef LOCALE_TIMEZONE_H
|
|
|
|
#define LOCALE_TIMEZONE_H
|
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
#include "DllMacro.h"
|
|
|
|
|
2019-12-10 14:36:53 +01:00
|
|
|
#include "utils/Logger.h"
|
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
#include <QAbstractListModel>
|
2019-11-26 11:18:35 +01:00
|
|
|
#include <QObject>
|
2019-11-25 20:51:45 +01:00
|
|
|
#include <QString>
|
|
|
|
|
2019-12-10 11:36:05 +01:00
|
|
|
#include <memory>
|
|
|
|
|
2019-11-25 20:51:45 +01:00
|
|
|
namespace CalamaresUtils
|
|
|
|
{
|
|
|
|
namespace Locale
|
|
|
|
{
|
|
|
|
|
|
|
|
/** @brief A pair of strings, one human-readable, one a key
|
|
|
|
*
|
|
|
|
* Given an identifier-like string (e.g. "New_York"), makes
|
|
|
|
* a human-readable version of that and keeps a copy of the
|
|
|
|
* identifier itself.
|
|
|
|
*
|
|
|
|
* This explicitly uses const char* instead of just being
|
|
|
|
* QPair<QString, QString> because there is API that needs
|
|
|
|
* C-style strings.
|
|
|
|
*/
|
|
|
|
class CStringPair
|
|
|
|
{
|
|
|
|
public:
|
2019-11-26 11:18:35 +01:00
|
|
|
/// @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();
|
2019-11-25 20:51:45 +01:00
|
|
|
|
2019-11-26 11:18:35 +01:00
|
|
|
/// @brief Give the localized human-readable form
|
|
|
|
virtual QString tr() const = 0;
|
2019-12-10 10:11:08 +01:00
|
|
|
QString key() const { return m_key; }
|
2019-12-09 22:20:46 +01:00
|
|
|
|
2019-12-10 15:06:22 +01:00
|
|
|
bool operator<( const CStringPair& other ) const { return m_key < other.m_key; }
|
|
|
|
|
2019-11-26 11:18:35 +01:00
|
|
|
protected:
|
|
|
|
char* m_human = nullptr;
|
2019-12-10 10:11:08 +01:00
|
|
|
QString m_key;
|
2019-11-25 20:51:45 +01:00
|
|
|
};
|
|
|
|
|
2019-12-10 15:54:43 +01:00
|
|
|
class CStringPairList : public QList< CStringPair* >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template < typename T >
|
|
|
|
T* find( const QString& key ) const
|
|
|
|
{
|
|
|
|
for ( auto* p : *this )
|
|
|
|
{
|
|
|
|
if ( p->key() == key )
|
|
|
|
{
|
|
|
|
return dynamic_cast< T* >( p );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
2019-12-10 12:45:56 +01:00
|
|
|
|
2019-11-25 20:51:45 +01:00
|
|
|
/// @brief A pair of strings for timezone regions (e.g. "America")
|
|
|
|
class TZRegion : public CStringPair
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using CStringPair::CStringPair;
|
2019-12-10 12:45:56 +01:00
|
|
|
virtual ~TZRegion();
|
2019-11-26 12:30:59 +01:00
|
|
|
QString tr() const override;
|
2019-12-10 11:54:33 +01:00
|
|
|
|
2019-12-10 15:32:15 +01:00
|
|
|
/** @brief Create list from a zone.tab-like file
|
2019-12-10 12:45:56 +01:00
|
|
|
*
|
|
|
|
* Returns a list of all the regions; each region has a list
|
2019-12-10 15:32:15 +01:00
|
|
|
* of zones within that region. Dyamically, the items in the
|
|
|
|
* returned list are TZRegions; their zones dynamically are
|
|
|
|
* TZZones even though all those lists have type CStringPairList.
|
2019-12-10 12:45:56 +01:00
|
|
|
*
|
|
|
|
* The list owns the regions, and the regions own their own list of zones.
|
|
|
|
* When getting rid of the list, remember to qDeleteAll() on it.
|
|
|
|
*/
|
2019-12-10 15:32:15 +01:00
|
|
|
static CStringPairList fromFile( const char* fileName );
|
2019-12-10 12:45:56 +01:00
|
|
|
/// @brief Calls fromFile with the standard zone.tab name
|
2019-12-10 15:32:15 +01:00
|
|
|
static CStringPairList fromZoneTab();
|
2019-12-10 12:45:56 +01:00
|
|
|
|
2019-12-10 15:54:43 +01:00
|
|
|
const CStringPairList& zones() const { return m_zones; }
|
|
|
|
|
2019-12-10 12:45:56 +01:00
|
|
|
private:
|
2019-12-10 15:32:15 +01:00
|
|
|
CStringPairList m_zones;
|
2019-11-25 20:51:45 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/// @brief A pair of strings for specific timezone names (e.g. "New_York")
|
|
|
|
class TZZone : public CStringPair
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
using CStringPair::CStringPair;
|
2019-11-26 12:30:59 +01:00
|
|
|
QString tr() const override;
|
2019-12-10 14:36:53 +01:00
|
|
|
|
|
|
|
TZZone( const char* zoneName, const QString& country, QString position );
|
|
|
|
|
|
|
|
void print( QDebug& ) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
QString m_country;
|
|
|
|
double m_latitude = 0.0, m_longitude = 0.0;
|
2019-11-25 20:51:45 +01:00
|
|
|
};
|
|
|
|
|
2019-12-10 14:36:53 +01:00
|
|
|
inline QDebug&
|
|
|
|
operator<<( QDebug& log, const TZZone& z )
|
|
|
|
{
|
|
|
|
z.print( log );
|
|
|
|
return log;
|
|
|
|
}
|
|
|
|
|
2019-12-10 15:32:15 +01:00
|
|
|
class CStringListModel : public QAbstractListModel
|
2019-12-09 21:46:10 +01:00
|
|
|
{
|
|
|
|
public:
|
2019-12-10 12:45:56 +01:00
|
|
|
/// @brief Create empty model
|
2019-12-10 15:32:15 +01:00
|
|
|
CStringListModel();
|
2019-12-10 12:45:56 +01:00
|
|
|
/// @brief Create model from list (non-owning)
|
2019-12-10 15:32:15 +01:00
|
|
|
CStringListModel( CStringPairList );
|
|
|
|
virtual ~CStringListModel() override;
|
2019-12-09 21:46:10 +01:00
|
|
|
|
|
|
|
int rowCount( const QModelIndex& parent ) const override;
|
|
|
|
|
|
|
|
QVariant data( const QModelIndex& index, int role ) const override;
|
|
|
|
|
2019-12-10 15:32:15 +01:00
|
|
|
const CStringPair* item( int index ) const;
|
2019-12-09 22:20:46 +01:00
|
|
|
|
2019-12-09 21:46:10 +01:00
|
|
|
private:
|
2019-12-10 15:32:15 +01:00
|
|
|
CStringPairList m_list;
|
2019-12-09 21:46:10 +01:00
|
|
|
};
|
|
|
|
|
2019-11-26 11:18:35 +01:00
|
|
|
} // namespace Locale
|
|
|
|
} // namespace CalamaresUtils
|
2019-11-25 20:51:45 +01:00
|
|
|
|
|
|
|
#endif // LOCALE_TIMEZONE_H
|