7 #include "fwPreferences/helper.hpp" 9 #include <fwData/Composite.hpp> 10 #include <fwData/String.hpp> 12 #include <fwRuntime/profile/Profile.hpp> 14 #include <fwServices/macros.hpp> 15 #include <fwServices/registry/ObjectService.hpp> 17 #include <fwTools/Os.hpp> 22 const std::string s_PREFERENCES_KEY =
"preferences";
26 bool setPreference(
const std::string& key,
const std::string& value)
28 bool isModified =
false;
31 ::fwData::Composite::sptr prefs = getPreferences();
34 ::fwData::Composite::IteratorType iterPref = prefs->find(key);
35 if ( iterPref != prefs->end() )
37 ::fwData::String::sptr
preferences = ::fwData::String::dynamicCast(iterPref->second);
38 preferences->value() = value;
42 (*prefs)[key] = ::fwData::String::New(value);
51 std::string getPreference(
const std::string& preferenceKey)
55 ::fwData::Composite::sptr prefs = getPreferences();
58 ::fwData::Composite::IteratorType iterPref = prefs->find( preferenceKey );
59 if ( iterPref != prefs->end() )
61 ::fwData::String::sptr prefString = ::fwData::String::dynamicCast(iterPref->second);
62 value = prefString->value();
70 ::boost::filesystem::path getPreferencesFile()
72 namespace bfile = ::boost::filesystem;
74 ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::profile::getCurrentProfile();
75 FW_RAISE_IF(
"No current profile set.", !profile);
77 const std::string appName = profile->getName();
78 const bfile::path appPrefDir = ::fwTools::os::getUserDataDir(
"fw4spl", appName,
true);
79 const bfile::path appPrefFile = appPrefDir /
"preferences.json";
81 FW_RAISE_IF(
"Unable to define user data directory", appPrefDir.empty());
83 if (!bfile::exists(appPrefDir))
85 bfile::create_directories(appPrefDir);
88 FW_RAISE_IF(
"Preferences file '"+appPrefFile.string()+
"' already exists and is not a regular file.",
89 bfile::exists(appPrefFile) && !bfile::is_regular_file(appPrefFile));
97 ::fwPreferences::IPreferences::sptr getPreferencesSrv()
99 ::fwPreferences::IPreferences::sptr srv;
100 const auto preferencesServicesList = ::fwServices::OSR::getServices(
"::fwPreferences::IPreferences");
102 if(!preferencesServicesList.empty())
104 ::fwServices::IService::sptr prefService = *preferencesServicesList.begin();
105 srv = ::fwPreferences::IPreferences::dynamicCast(prefService);
107 SLM_DEBUG_IF(
"The preferences service is not found, the preferences can not be used", !srv);
114 ::fwData::Composite::sptr getPreferences()
116 ::fwData::Composite::sptr prefs;
118 const auto prefService = getPreferencesSrv();
131 void savePreferences()
133 const auto prefService = getPreferencesSrv();
134 SLM_WARN_IF(
"The preferences service is not found, the preferences can not be saved", !prefService);
135 SLM_WARN_IF(
"The preferences service is not started, the preferences can not be saved", !prefService->isStarted());
136 if(prefService && prefService->isStarted())
138 prefService->update();
#define SLM_DEBUG_IF(message, cond)
The namespace preferences contains the service to manage the application's preferences (window size/p...
This class defines a composite object.
#define SLM_WARN_IF(message, cond)