private:
+ enum ItemKeyType {
+ CITY,
+ COUNTRY,
+ OFFSET
+ };
+
/**
* @brief Gets time zone by its no in time_zone_ list
*/
*/
static std::vector<Timezone> time_zones_;
+ /**
+ * @brief Saves items list state using app_preferences API
+ * @details It saves every particular item(location)
+ */
+ void SaveItemsList();
+
+ /**
+ * @brief Loads items list state from data stored using app_preferences API
+ */
+ void LoadItemsList();
+
+ /**
+ * @brief Gets key identify given type(location struct field) for no'th item
+ * @details The key is used while storing data using preferences API
+ * @param type type of location struct field( see ItemKeyType enum)
+ * @param no order number of item
+ * @return created key
+ */
+ const char *GetItemKey(int type, int no);
+
+ /**
+ * @brief Saves single location using app_preferences API
+ * @param[in] item location to save
+ * @param[in] i order number of the item
+ */
+ void SaveItem(const model::Location *item, int i);
+
+ /**
+ * @brief Loads single location using app_preferences API
+ * @param[in] i order number of the item
+ *
+ * @return user location
+ */
+ const model::Location *LoadItem(int i);
+
};
} /* model */
#include <app_preference.h>
#include <algorithm>
+#include <string>
+#include <sstream>
#include "Model/WorldClock.h"
#include "Model/Location.h"
WorldClock::WorldClock()
{
+ LoadItemsList();
+
int current_tz = 1;
int ret = preference_get_int("WORLD_CLOCK_MAP_CURRENT_TIMEZONE", ¤t_tz);
if (ret != PREFERENCE_ERROR_NONE) {
WorldClock::~WorldClock()
{
+ SaveItemsList();
+
int ret = preference_set_int("WORLD_CLOCK_MAP_CURRENT_TIMEZONE", GetCurrentTimezoneNo());
if (ret != PREFERENCE_ERROR_NONE) {
DBG("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
bool WorldClock::AddItemToCustomList(const model::Location *l)
{
if (!user_locations_.empty()) {
- auto it = std::find(user_locations_.begin(), user_locations_.end(), l);
- if (it != user_locations_.end()) {
- DBG("Location already exists in custom list");
- return false;
+ for (auto it = user_locations_.begin(); it != user_locations_.end(); it++) {
+
+ if (!(*it)->name.compare(l->name) && !(*it)->country.compare(l->country)) {
+ DBG("Location already exists in custom list");
+ return false;
+ }
}
}
user_locations_.push_back(l);
return true;
}
+
+void WorldClock::SaveItemsList()
+{
+ int ret = preference_remove_all();
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_remove_all failed[%d]: %s", ret, get_error_message(ret));
+ }
+
+ ret = preference_set_int("WORLD_CLOCK_LIST_SIZE", (int)user_locations_.size());
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
+ return;
+ }
+
+ int no = 0;
+ for (auto it = user_locations_.begin(); it != user_locations_.end();) {
+ SaveItem(*it, no++);
+ delete *it;
+ it = user_locations_.erase(it);
+ }
+}
+
+void WorldClock::LoadItemsList()
+{
+ int list_size;
+ bool existing;
+
+ int ret = preference_is_existing("WORLD_CLOCK_LIST_SIZE", &existing);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_is_existing failed[%d]: %s", ret, get_error_message(ret));
+ return;
+ }
+ if (!existing) {
+ DBG("Given Key does not exist yet. Skipping!");
+ return;
+ }
+
+ ret = preference_get_int("WORLD_CLOCK_LIST_SIZE", &list_size);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_get_int failed[%d]: %s", ret, get_error_message(ret));
+ return;
+ }
+ if (!user_locations_.empty()){
+ ERR("User locations list is not empty!");
+ return;
+ }
+
+ for (int i = 0; i < list_size; i++) {
+ const Location *l = LoadItem(i);
+ if (l)
+ user_locations_.push_back(l);
+ }
+}
+
+
+const char *WorldClock::GetItemKey(int type, int no)
+{
+ static std::stringstream ss;
+ ss.clear();
+
+ ss << "ITEM_" << no << "_";
+ switch (type) {
+ case CITY:
+ ss << "CITY";
+ break;
+ case COUNTRY:
+ ss << "COUNTRY";
+ break;
+ case OFFSET:
+ ss << "OFFSET";
+ break;
+ default:
+ ERR("Invalid type!");
+ return NULL;
+ }
+
+ return ss.str().c_str();
+}
+
+void WorldClock::SaveItem(const model::Location *item, int i)
+{
+ int ret = preference_set_string(GetItemKey(CITY, i), item->name.c_str());
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_set_string failed[%d]: %s", ret, get_error_message(ret));
+ return;
+ }
+ ret = preference_set_string(GetItemKey(COUNTRY, i), item->country.c_str());
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_set_string failed[%d]: %s", ret, get_error_message(ret));
+ return;
+ }
+ ret = preference_set_int(GetItemKey(OFFSET, i), item->gmt_offset_);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_set_int failed[%d]: %s", ret, get_error_message(ret));
+ }
+}
+
+const model::Location *WorldClock::LoadItem(int i)
+{
+ model::Location *location = new Location;
+
+ char *city_name;
+ char *country_name;
+
+ int ret = preference_get_string(GetItemKey(CITY, i), &city_name);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_get_string failed[%d]: %s", ret, get_error_message(ret));
+ delete location;
+ return NULL;
+ }
+ ret = preference_get_string(GetItemKey(COUNTRY, i), &country_name);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_get_string failed[%d]: %s", ret, get_error_message(ret));
+ free(city_name);
+ delete location;
+ return NULL;
+ }
+ ret = preference_get_int(GetItemKey(OFFSET, i), &location->gmt_offset_);
+ if (ret != PREFERENCE_ERROR_NONE) {
+ ERR("preference_get_int failed[%d]: %s", ret, get_error_message(ret));
+ free(city_name);
+ free(country_name);
+ delete location;
+ return NULL;
+ }
+
+ location->name = city_name;
+ location->country = country_name;
+
+ free(city_name);
+ free(country_name);
+
+ return location;
+}