diff --git a/daemon/src/history/historyitem.cpp b/daemon/src/history/historyitem.cpp index 659e2f10befb688409b3daaa1b097298a3554450..ddf1668a53b512c835f41f5c47552df25ae016f6 100644 --- a/daemon/src/history/historyitem.cpp +++ b/daemon/src/history/historyitem.cpp @@ -53,14 +53,14 @@ HistoryItem::HistoryItem(std::string timestamp_start, CallType call_type, std::s HistoryItem::HistoryItem(std::string serialized_form) { - int indice = 0; + int index = 0; while (serialized_form.find(ITEM_SEPARATOR, 0) != std::string::npos) { size_t pos = serialized_form.find(ITEM_SEPARATOR, 0); std::string tmp = serialized_form.substr(0, pos); serialized_form.erase(0, pos + 1); - switch (indice) { + switch (index) { case 0: // The call type call_type_ = (CallType) atoi(tmp.c_str()); break; @@ -91,15 +91,15 @@ HistoryItem::HistoryItem(std::string serialized_form) case 8: // The conference ID confID_ = tmp; break; - case 9: // The time + case 9: // the time timeAdded_ = tmp; break; default: // error - ERROR("Unserialized form %d not recognized\n", indice); + ERROR("Unserialized form %d not recognized\n", index); break; } - indice ++; + ++index; } } @@ -125,7 +125,7 @@ bool HistoryItem::save(Conf::ConfigTree **history) && (*history)->setConfigTreeItem(sectionstr, "timeadded", timeAdded_); } -std::string HistoryItem::serialize() +std::string HistoryItem::serialize() const { // Replace empty string with a valid standard string value std::string name(name_); @@ -149,7 +149,7 @@ std::string HistoryItem::serialize() } -bool HistoryItem::valid_account(std::string id) +bool HistoryItem::valid_account(const std::string &id) const { return Manager::instance().accountExists(id); } diff --git a/daemon/src/history/historyitem.h b/daemon/src/history/historyitem.h index 83b136db8c17a94b2ec5a8f77797f0a0496a09b3..f54a6c463a2aa4f8804355f03c51ec9dbdc81271 100644 --- a/daemon/src/history/historyitem.h +++ b/daemon/src/history/historyitem.h @@ -73,13 +73,13 @@ class HistoryItem { bool save(Conf::ConfigTree **history); - std::string serialize(); + std::string serialize() const; private: /* * @return true if the account ID corresponds to a loaded account */ - bool valid_account(std::string); + bool valid_account(const std::string &id) const; /* * Timestamp representing the date of the call @@ -110,17 +110,17 @@ class HistoryItem { std::string account_id_; /** - * Wether or not a recording exist for this call + * Whether or not a recording exist for this call */ std::string recording_file_; /** - * The conference ID for this call (if any) - */ + * The conference ID for this call (if any) + */ std::string confID_; /** - * Time added to conference + * Time added to conference */ std::string timeAdded_; }; diff --git a/daemon/src/history/historymanager.cpp b/daemon/src/history/historymanager.cpp index 558ed30b752d6acab4e7190649c0300b6df53716..8b6863223213bc9b8a1840f5eab6f9135d0d4267 100644 --- a/daemon/src/history/historymanager.cpp +++ b/daemon/src/history/historymanager.cpp @@ -35,30 +35,10 @@ #include <cc++/file.h> #include <time.h> -namespace { -static void free_history(HistoryItemMap &history_items) -{ - HistoryItemMap::iterator iter; - - for (iter = history_items.begin(); iter != history_items.end(); ++iter) - delete *iter; - - history_items.clear(); -} -} // end anonymous namespace - -HistoryManager::HistoryManager() - : history_loaded_(false), - history_path_("") -{ -} - -HistoryManager::~HistoryManager() -{ - free_history(history_items_); -} +HistoryManager::HistoryManager() : history_loaded_(false), history_path_("") +{} -int HistoryManager::load_history(int limit, std::string path) +int HistoryManager::load_history(int limit, const std::string &path) { Conf::ConfigTree history_list; create_history_path(path); @@ -97,19 +77,19 @@ int HistoryManager::load_history_items_map(Conf::ConfigTree *history_list, int l int nb_items = 0; for (Conf::TokenList::iterator iter = sections.begin(); iter != sections.end(); ++iter) { CallType type = (CallType) getConfigInt(*iter, "type", history_list); - string timestamp_start = getConfigString(*iter, "timestamp_start", history_list); - string timestamp_stop = getConfigString(*iter, "timestamp_stop", history_list); - string name = getConfigString(*iter, "name", history_list); - string number = getConfigString(*iter, "number", history_list); - string callID = getConfigString(*iter, "id", history_list); - string accountID = getConfigString(*iter, "accountid", history_list); - string recording_file = getConfigString(*iter, "recordfile", history_list); - string confID = getConfigString(*iter, "confid", history_list); - string time_added = getConfigString(*iter, "timeadded", history_list); + string timestamp_start(getConfigString(*iter, "timestamp_start", history_list)); + string timestamp_stop(getConfigString(*iter, "timestamp_stop", history_list)); + string name(getConfigString(*iter, "name", history_list)); + string number(getConfigString(*iter, "number", history_list)); + string callID(getConfigString(*iter, "id", history_list)); + string accountID(getConfigString(*iter, "accountid", history_list)); + string recording_file(getConfigString(*iter, "recordfile", history_list)); + string confID(getConfigString(*iter, "confid", history_list)); + string timeAdded(getConfigString(*iter, "timeadded", history_list)); // Make a check on the start timestamp to know it is loadable according to CONFIG_HISTORY_LIMIT if (atoi(timestamp_start.c_str()) >= ((int) current_timestamp - history_limit)) { - HistoryItem *item = new HistoryItem(timestamp_start, type, timestamp_stop, name, number, callID, accountID, recording_file, confID, time_added); + HistoryItem item(timestamp_start, type, timestamp_stop, name, number, callID, accountID, recording_file, confID, timeAdded); add_new_history_entry(item); ++nb_items; } @@ -128,10 +108,8 @@ bool HistoryManager::save_history_to_file(Conf::ConfigTree *history_list) int HistoryManager::save_history_items_map(Conf::ConfigTree *history_list) { int items_saved = 0; - for (HistoryItemMap::iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter) { - HistoryItem *item = *iter; - - if (item and item->save(&history_list)) + for (std::vector<HistoryItem>::iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter) { + if (iter->save(&history_list)) ++items_saved; else DEBUG("can't save NULL history item\n"); @@ -140,7 +118,7 @@ int HistoryManager::save_history_items_map(Conf::ConfigTree *history_list) return items_saved; } -void HistoryManager::add_new_history_entry(HistoryItem *new_item) +void HistoryManager::add_new_history_entry(const HistoryItem &new_item) { // Add it in the map history_items_.push_back(new_item); @@ -148,7 +126,6 @@ void HistoryManager::add_new_history_entry(HistoryItem *new_item) int HistoryManager::create_history_path(std::string path) { - std::string userdata, xdg_env, xdg_data; xdg_data = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone"; @@ -158,8 +135,7 @@ int HistoryManager::create_history_path(std::string path) // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone if (XDG_DATA_HOME != NULL) { xdg_env = std::string(XDG_DATA_HOME); - (xdg_env.length() > 0) ? userdata = xdg_env - : userdata = xdg_data; + (xdg_env.length() > 0) ? userdata = xdg_env : userdata = xdg_data; } else userdata = xdg_data; @@ -186,7 +162,7 @@ HistoryManager::getConfigInt(const std::string& section, const std::string& name try { return history_list->getConfigTreeItemIntValue(section, name); } catch (const Conf::ConfigTreeItemException& e) { - throw e; + throw; } return 0; @@ -198,39 +174,30 @@ HistoryManager::getConfigString(const std::string& section, const std::string& n try { return history_list->getConfigTreeItemValue(section, name); } catch (const Conf::ConfigTreeItemException& e) { - throw e; + throw; } return ""; } -std::vector<std::string> HistoryManager::get_history_serialized() +std::vector<std::string> HistoryManager::get_history_serialized() const { std::vector<std::string> serialized; - HistoryItemMap::iterator iter; - - DEBUG("HistoryManager: Get history serialized"); - - for (iter = history_items_.begin(); iter != history_items_.end(); ++iter) { - HistoryItem *current = *iter; - - if (current) - serialized.push_back(current->serialize()); - } + for (std::vector<HistoryItem>::const_iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter) + serialized.push_back(iter->serialize()); return serialized; } -int HistoryManager::set_serialized_history(std::vector<std::string> history, int limit) +int HistoryManager::set_serialized_history(const std::vector<std::string> &history, int limit) { int history_limit; time_t current_timestamp; DEBUG("HistoryManager: Set serialized history"); - // Clear the existing history - free_history(history_items_); + history_items_.clear(); // We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT) // Get the current timestamp @@ -238,15 +205,14 @@ int HistoryManager::set_serialized_history(std::vector<std::string> history, int history_limit = get_unix_timestamp_equivalent(limit); int items_added = 0; - for (std::vector<std::string>::iterator iter = history.begin() ; iter != history.end() ; ++iter) { - HistoryItem *new_item = new HistoryItem(*iter); - int item_timestamp = atoi(new_item->get_timestamp().c_str()); + for (std::vector<std::string>::const_iterator iter = history.begin() ; iter != history.end() ; ++iter) { + HistoryItem new_item(*iter); + int item_timestamp = atoi(new_item.get_timestamp().c_str()); if (item_timestamp >= ((int) current_timestamp - history_limit)) { add_new_history_entry(new_item); ++items_added; - } else - delete new_item; + } } return items_added; diff --git a/daemon/src/history/historymanager.h b/daemon/src/history/historymanager.h index a27ae247a4b7935b7ee97402956a3c28a2aacf40..a2a82b67b898a057d82df4c78ee78582fd94eba9 100644 --- a/daemon/src/history/historymanager.h +++ b/daemon/src/history/historymanager.h @@ -38,8 +38,6 @@ #define DAY_UNIX_TIMESTAMP 86400 // Number of seconds in one day: 60 x 60 x 24 -typedef std::vector <HistoryItem *> HistoryItemMap; - class HistoryManager { public: @@ -48,17 +46,12 @@ class HistoryManager { */ HistoryManager(); - /* - * Destructor - */ - ~HistoryManager(); - /** *@param path A specific file to use; if empty, use the global one * *@return int The number of history items successfully loaded */ - int load_history(int limit, std::string path=""); + int load_history(int limit, const std::string &path=""); /** *@return bool True if the history has been successfully saved in the file @@ -103,9 +96,9 @@ class HistoryManager { return history_items_.size(); } - std::vector<std::string> get_history_serialized(); + std::vector<std::string> get_history_serialized() const; - int set_serialized_history(std::vector<std::string> history, int limit); + int set_serialized_history(const std::vector<std::string> &history, int limit); private: int get_unix_timestamp_equivalent(int days) const { @@ -124,12 +117,12 @@ class HistoryManager { /* * Add a new history item in the data structure */ - void add_new_history_entry(HistoryItem *new_item); + void add_new_history_entry(const HistoryItem &new_item); /* * Map containing the history items */ - HistoryItemMap history_items_; + std::vector<HistoryItem> history_items_; /* * History has been loaded