bool CSettingList::fromValues(const std::vector<std::string> &strValues, SettingPtrList &values) const { if ((int)strValues.size() < m_minimumItems || (m_maximumItems > 0 && (int)strValues.size() > m_maximumItems)) return false; bool ret = true; int index = 0; for (std::vector<std::string>::const_iterator itValue = strValues.begin(); itValue != strValues.end(); ++itValue) { CSetting *settingValue = m_definition->Clone(StringUtils::Format("%s.%d", m_id.c_str(), index++)); if (settingValue == NULL || !settingValue->FromString(*itValue)) { delete settingValue; ret = false; break; } values.push_back(SettingPtr(settingValue)); } if (!ret) values.clear(); return ret; }
bool CSettings::SetList(const std::string &id, const std::vector<CVariant> &value) { CSetting *setting = m_settingsManager->GetSetting(id); if (setting == NULL || setting->GetType() != SettingTypeList) return false; CSettingList *listSetting = static_cast<CSettingList*>(setting); SettingPtrList newValues; bool ret = true; int index = 0; for (std::vector<CVariant>::const_iterator itValue = value.begin(); itValue != value.end(); ++itValue) { CSetting *settingValue = listSetting->GetDefinition()->Clone(StringUtils::Format("%s.%d", listSetting->GetId().c_str(), index++)); if (settingValue == NULL) return false; switch (listSetting->GetElementType()) { case SettingTypeBool: if (!itValue->isBoolean()) return false; ret = static_cast<CSettingBool*>(settingValue)->SetValue(itValue->asBoolean()); break; case SettingTypeInteger: if (!itValue->isInteger()) return false; ret = static_cast<CSettingInt*>(settingValue)->SetValue((int)itValue->asInteger()); break; case SettingTypeNumber: if (!itValue->isDouble()) return false; ret = static_cast<CSettingNumber*>(settingValue)->SetValue(itValue->asDouble()); break; case SettingTypeString: if (!itValue->isString()) return false; ret = static_cast<CSettingString*>(settingValue)->SetValue(itValue->asString()); break; default: ret = false; break; } if (!ret) { delete settingValue; return false; } newValues.push_back(SettingPtr(settingValue)); } return listSetting->SetValue(newValues); }
void CSettingList::Reset() { CExclusiveLock lock(m_critical); SettingPtrList values; for (SettingPtrList::const_iterator it = m_defaults.begin(); it != m_defaults.end(); ++it) values.push_back(SettingPtr((*it)->Clone((*it)->GetId()))); SetValue(values); }
std::string CSettingList::toString(const SettingPtrList &values) const { std::vector<std::string> strValues; for (SettingPtrList::const_iterator it = values.begin(); it != values.end(); ++it) { if (*it != NULL) strValues.push_back((*it)->ToString()); } return StringUtils::Join(strValues, m_delimiter); }
void CSettingList::SetDefault(const SettingPtrList &values) { CExclusiveLock lock(m_critical); m_defaults.clear(); m_defaults.insert(m_defaults.begin(), values.begin(), values.end()); if (!m_changed) { m_values.clear(); for (SettingPtrList::const_iterator it = m_defaults.begin(); it != m_defaults.end(); ++it) m_values.push_back(SettingPtr((*it)->Clone((*it)->GetId()))); } }
void CSettingList::copy(const SettingPtrList &srcValues, SettingPtrList &dstValues) { dstValues.clear(); for (SettingPtrList::const_iterator itValue = srcValues.begin(); itValue != srcValues.end(); ++itValue) { if (*itValue == NULL) continue; CSetting *valueCopy = (*itValue)->Clone((*itValue)->GetId()); if (valueCopy == NULL) continue; dstValues.push_back(SettingPtr(valueCopy)); } }
bool CSettingList::Equals(const std::string &value) const { SettingPtrList values; if (!fromString(value, values) || values.size() != m_values.size()) return false; bool ret = true; for (size_t index = 0; index < values.size(); index++) { if (!m_values[index]->Equals(values[index]->ToString())) { ret = false; break; } } return ret; }
bool CSettingList::SetValue(const SettingPtrList &values) { CExclusiveLock lock(m_critical); if ((int)values.size() < m_minimumItems || (m_maximumItems > 0 && (int)values.size() > m_maximumItems)) return false; bool equal = values.size() == m_values.size(); for (size_t index = 0; index < values.size(); index++) { if (values[index]->GetType() != GetElementType()) return false; if (equal && !values[index]->Equals(m_values[index]->ToString())) equal = false; } if (equal) return true; SettingPtrList oldValues = m_values; m_values.clear(); m_values.insert(m_values.begin(), values.begin(), values.end()); if (!OnSettingChanging(this)) { m_values = oldValues; // the setting couldn't be changed because one of the // callback handlers failed the OnSettingChanging() // callback so we need to let all the callback handlers // know that the setting hasn't changed OnSettingChanging(this); return false; } m_changed = (toString(m_values) != toString(m_defaults)); OnSettingChanged(this); return true; }