settings_set.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:11k
- /*
- * ===========================================================================
- * PRODUCTION $Log: settings_set.cpp,v $
- * PRODUCTION Revision 1000.1 2004/06/01 20:46:07 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: settings_set.cpp,v 1000.1 2004/06/01 20:46:07 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Author: Robert G. Smith
- *
- * File Description:
- * CSettingsSet is an adapter of the PluginConfigCache. It is meant to be
- * used as a base class for individual plugins' configuration classes, and
- * provides an interface to the PluginConfigCache for plugins.
- * It constrains our access to the PluginConfigCache to a particular type.
- *
- */
- #include <ncbi_pch.hpp>
- #include <gui/config/settings_set.hpp>
- BEGIN_NCBI_SCOPE
- const string CSettingsSet::sm_StartupStyleName("default");
- CSettingsSet::CSettingsSet(objects::CPluginConfigCache* config_cache,
- const string& type,
- const AutoPtr<IFactoryDefaultSettings>& fds,
- const string& typedesc,
- const string& delim
- )
- : m_FactoryDefault(fds),
- m_ConfigCache(config_cache),
- m_Type(type),
- m_TypeDescription(typedesc),
- m_KeyDelim(delim)
- {
- if (m_TypeDescription.empty()) {
- m_TypeDescription = m_Type;
- }
- SetCurrentStyleName(sm_StartupStyleName);
- }
- CSettingsSet::~CSettingsSet()
- {
- }
- const string& CSettingsSet::GetKeyDelimiter() const
- {
- return m_KeyDelim;
- }
- const string& CSettingsSet::GetType(void) const
- {
- return m_Type;
- }
- const string& CSettingsSet::GetCurrentStyleName(void) const
- {
- return m_CurrentStyleName;
- }
- void CSettingsSet::SetCurrentStyleName(const string& new_style)
- {
- if ( m_CurrentStyleName != new_style) {
- m_CurrentStyleName = new_style;
- if (m_ConfigCache.NotEmpty() && m_ConfigCache->HasPCV(m_Type, m_CurrentStyleName) ) {
- m_CurrentSavedSet.Reset(& m_ConfigCache->SetPCV(m_Type, m_CurrentStyleName));
- } else {
- m_CurrentSavedSet.Reset();
- }
- CurrentSettingsModified(false);
- }
- }
- bool CSettingsSet::CurrentSettingsModified(void) const
- {
- return m_CurrentSetModified;
- }
- void CSettingsSet::CurrentSettingsModified(bool modified)
- {
- m_CurrentSetModified = modified;
- }
- const string& CSettingsSet::GetTypeDescription(void) const
- {
- return m_TypeDescription;
- }
- void CSettingsSet::SetTypeDescription(const string& desc)
- {
- m_TypeDescription = desc;
- }
- list<string> CSettingsSet::GetStyleNames() const
- {
- list <string> ret_list;
- if ( m_ConfigCache.NotEmpty() ) {
- ret_list = m_ConfigCache->GetStyleNamesByType(m_Type);
- ret_list.remove(sm_StartupStyleName);
- ret_list.sort();
- }
- // Make sure the default name is always at the top of the list
- // whether or not it exists in the PCC.
- ret_list.push_front(sm_StartupStyleName);
- return ret_list;
- }
- string CSettingsSet::AddStyle(void)
- {
- objects::CPluginConfigCache& pcc = SetConfigCache();
- string new_name("New Default Values");
- new_name = pcc.MakeUniqueStyle(m_Type, new_name);
- pcc.SetPCV(m_Type, new_name);
- return new_name;
- }
- string CSettingsSet::DuplicateStyle(const string& style)
- {
- objects::CPluginConfigCache& pcc = SetConfigCache();
- // make new unique name.
- string new_name(style + " copy");
- new_name = pcc.MakeUniqueStyle(m_Type, new_name);
-
- // Some style names (default) may not actually exist
- // so there is nothing to duplicate.
- if (pcc.HasPCV(m_Type, style)) {
- pcc.DupPCV(m_Type, style, new_name);
- } else {
- pcc.SetPCV(m_Type, new_name);
- }
- return new_name;
- }
- string CSettingsSet::RenameStyle(const string& old_style, const string& new_style)
- {
- if (old_style == sm_StartupStyleName ||
- new_style.empty() ||
- old_style == new_style ) {
- return kEmptyStr;
- }
-
- objects::CPluginConfigCache& pcc = SetConfigCache();
- objects::CPluginConfigValues& pcv = pcc.SetPCV(m_Type, old_style);
- string new_name = pcc.MakeUniqueStyle(m_Type, new_style);
- pcv.SetId().SetStyle(new_name);
- return new_name;
- }
- bool CSettingsSet::CanRenameStyle(const string& style)
- {
- if (style == sm_StartupStyleName)
- return false;
-
- return SetConfigCache().HasPCV(m_Type, style);
- }
- bool CSettingsSet::DeleteStyle(const string& style)
- {
- if (style == sm_StartupStyleName) {
- return false;
- }
- SetConfigCache().DeletePCV(m_Type, style);
- return true;
- }
- string CSettingsSet::Get(const string& key, ELoadValueSource data_src) const
- {
- if (data_src == eLoad_Current &&
- m_ConfigCache.NotEmpty() &&
- m_CurrentSavedSet.NotEmpty() ) {
- try { // keys never saved with a value get Factory Defaults.
- return m_ConfigCache->GetStringByKey(*m_CurrentSavedSet, key, m_KeyDelim);
- }
- catch (const CSerialException&) {
- }
- }
- return m_FactoryDefault->Get( key );
- }
- void CSettingsSet::Set(const string& key, const string& value)
- {
- CurrentSettingsModified(true);
-
- SetCurrentSavedSet().AddKeyString(key, value, m_KeyDelim);
- }
- void CSettingsSet::Set(const string& typekey, const string& key, const string& value)
- {
- CurrentSettingsModified(true);
-
- SetConfigCache().AddKeyString( SetCurrentSavedSet(), typekey, key, value, m_KeyDelim);
- }
- bool CSettingsSet::Delete(const string& key)
- {
- if (SetConfigCache().DelKeyvalue(SetCurrentSavedSet(), key, m_KeyDelim)) {
- CurrentSettingsModified(true);
- return true;
- }
- return false;
- }
- objects::CPluginConfigCache& CSettingsSet::SetConfigCache(void)
- {
- if ( m_ConfigCache.Empty() ) {
- m_ConfigCache.Reset( new objects::CPluginConfigCache );
- }
- return *m_ConfigCache;
- }
- objects::CPluginConfigValues& CSettingsSet::SetCurrentSavedSet(void)
- {
- if ( m_CurrentSavedSet.Empty() ) {
- m_CurrentSavedSet.Reset(& (SetConfigCache().SetPCV(m_Type, m_CurrentStyleName)) );
- }
- return *m_CurrentSavedSet;
- }
- // Array access methods.
- const string kArrayCountSubKey("array");
- string CSettingsSet::ArrayHeaderKey(const string& array_key) const
- {
- return array_key + GetKeyDelimiter() + kArrayCountSubKey;
- }
- string CSettingsSet::ArrayItemKey(const string& array_key, unsigned int i, const string& item_key) const
- {
- string array_item_key = array_key + GetKeyDelimiter() + NStr::UIntToString(i);
- if ( ! item_key.empty()) {
- array_item_key += GetKeyDelimiter() + item_key;
- }
- return array_item_key;
- }
-
- unsigned int CSettingsSet::GetArraySize(const string& array_key, ELoadValueSource src) const
- {
- string array_size_str = Get(ArrayHeaderKey(array_key), src);
- return NStr::StringToUInt(array_size_str);
- }
- string CSettingsSet::GetArrayItem(const string& array_key, unsigned int i, const string& item_key, ELoadValueSource data_src) const
- {
- try {
- return Get(ArrayItemKey(array_key, i, item_key), data_src);
- }
- catch (const CConfigException& e1) {
- /// no array item i in the PCC or in the Factory Defaults.
- /// try array item 0 as a default.
- if (e1.GetErrCode() == CConfigException::eNoDefaultValue) {
- try {
- return Get(ArrayItemKey(array_key, 0, item_key), data_src);
- }
- catch (const CConfigException&) {
- // no item 0 either.
- // rethrow the original exception.
- throw e1;
- }
- }
- throw;
- }
- _ASSERT(false); // can't reach here.
- return kEmptyStr; // satisfy compilers.
- }
- void CSettingsSet::SetArraySize(const string& array_key, unsigned int array_size)
- {
- Set(ArrayHeaderKey(array_key), NStr::UIntToString(array_size));
- }
- void CSettingsSet::SetArrayItem(const string& array_key, unsigned int i, const string& item_key, const string& value)
- {
- Set(ArrayItemKey(array_key, i, item_key), value);
- }
- string CFactoryDefaultSettings::Get(const string& key) const {
- TFDInput::const_iterator i = m_vals.find(key);
- if (i == m_vals.end()) {
- NCBI_THROW(CConfigException, eNoDefaultValue, "key: " + key);
- }
- return i->second;
- }
- END_NCBI_SCOPE
- /*
- * ===========================================================================
- *
- * $Log: settings_set.cpp,v $
- * Revision 1000.1 2004/06/01 20:46:07 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.2
- *
- * Revision 1.2 2004/05/21 22:27:41 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.1 2004/02/12 14:57:45 rsmith
- * initial checkin. Moved from gui/config/test
- *
- * Revision 1.8 2004/02/02 18:44:18 rsmith
- * add description to CSettingsSet, constructor and descendants.
- *
- * Revision 1.7 2003/12/30 15:01:07 dicuccio
- * Fixed compiler warnings on MSVC
- *
- * Revision 1.6 2003/12/29 14:40:57 rsmith
- * Add array utility methods. Get returns string not const string&.
- * Get always throws on failure, does not return special values.
- *
- * Revision 1.5 2003/11/21 12:49:12 rsmith
- * Add ability to delete values by key.
- *
- * Revision 1.4 2003/10/28 19:02:17 dicuccio
- * Changed ctor parameter for config cache from CRef<> to raw pointer
- *
- * Revision 1.3 2003/10/24 14:39:39 rsmith
- * Add more doxygen comments.
- *
- * Revision 1.2 2003/10/17 19:42:54 rsmith
- * make an abstract interface for factory defaults, and hence store them as a pointer in CSettingsSet.
- *
- * Revision 1.1 2003/10/10 17:43:42 rsmith
- * moved from gui/core to gui/config
- *
- * Revision 1.1 2003/09/26 18:15:31 rsmith
- * plugin configration data and dialog
- *
- *
- * ===========================================================================
- */