Skip to content

Commit

Permalink
API key can be specified through configuration file
Browse files Browse the repository at this point in the history
Refs: #44
  • Loading branch information
orontee committed Sep 27, 2023
1 parent 9c4f29f commit c2c92d7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- API key can be specified through configuration file
[#44](https://github.com/orontee/taranis/issues/44)

- Automatic refresh every hour
[#26](https://github.com/orontee/taranis/issues/26)

Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ When the application is running, press the location text at the top of
the screen to enter a custom location (the _town, country_ format is
expected).

## Configuration

The application stores the user preferences (unit system, default
location, etc.) in a configuration file writen under
`system/config/taranis.cfg`.

Note that the Openweather API key distributed with the application is
rate limited. It's possible to specify a custom key using the
`api_key` keyword in the configuration file (See
[OpenWeather](https://openweathermap.org) for instructions on how to
register and generate API keys).

## Contributing

Instructions to install an unreleased version, debug, etc. can be
Expand Down
40 changes: 25 additions & 15 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,44 @@ class App {
void load_config() {
Config config;

const auto config_unit_system = static_cast<UnitSystem>(
const auto api_key_from_config = config.read_string("api_key", "");
const auto is_api_key_obsolete =
(not api_key_from_config.empty() and
api_key_from_config != this->service->get_api_key());
if (is_api_key_obsolete) {
this->service->set_api_key(api_key_from_config);
}

const auto unit_system_from_config = static_cast<UnitSystem>(
config.read_int("unit_system"s, UnitSystem::metric));
const bool is_unit_system_obsolete =
(config_unit_system != this->model->unit_system);
(unit_system_from_config != this->model->unit_system);
if (is_unit_system_obsolete) {
this->model->unit_system = config_unit_system;
this->model->unit_system = unit_system_from_config;
}

const auto config_town = config.read_string("location_town"s, "Paris"s);
const auto config_country =
const auto town_from_config =
config.read_string("location_town"s, "Paris"s);
const auto country_from_config =
config.read_string("location_country"s, "France"s);

const bool is_town_or_country_obsolete =
(config_town != this->model->location.town) or
(config_country != this->model->location.country);
(town_from_config != this->model->location.town) or
(country_from_config != this->model->location.country);

if (is_town_or_country_obsolete) {
this->model->location.town = config_town;
this->model->location.country = config_country;
this->model->location.town = town_from_config;
this->model->location.country = country_from_config;
this->clear_model_weather_conditions();
}

const auto config_display_daily_forecast =
const auto display_daily_forecast_from_config =
config.read_bool("display_daily_forecast"s, false);
const bool is_display_daily_forecast_obsolete =
(config_display_daily_forecast != this->model->display_daily_forecast);
(display_daily_forecast_from_config !=
this->model->display_daily_forecast);
if (is_display_daily_forecast_obsolete) {
this->model->display_daily_forecast = config_display_daily_forecast;
this->model->display_daily_forecast = display_daily_forecast_from_config;
}

const std::string current_system_language = currentLang();
Expand All @@ -175,9 +185,9 @@ class App {
initialize_translations();
}

const bool is_data_obsolete = is_unit_system_obsolete or
is_town_or_country_obsolete or
is_language_obsolete;
const bool is_data_obsolete =
is_api_key_obsolete or is_unit_system_obsolete or
is_town_or_country_obsolete or is_language_obsolete;
// temperatures, wind speed and weather description are computed
// by the backend thus unit system or language change implies that
// data are obsolete
Expand Down

0 comments on commit c2c92d7

Please sign in to comment.