fw4spl
fwPreferences/src/fwPreferences/helper.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2014-2018.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwPreferences/helper.hpp"
8 
9 #include <fwData/Composite.hpp>
10 #include <fwData/String.hpp>
11 
12 #include <fwRuntime/profile/Profile.hpp>
13 
14 #include <fwServices/macros.hpp>
15 #include <fwServices/registry/ObjectService.hpp>
16 
17 #include <fwTools/Os.hpp>
18 
19 namespace fwPreferences
20 {
21 
22 const std::string s_PREFERENCES_KEY = "preferences";
23 
24 //----------------------------------------------------------------------------
25 
26 bool setPreference(const std::string& key, const std::string& value)
27 {
28  bool isModified = false;
29  // Check preferences
30 
31  ::fwData::Composite::sptr prefs = getPreferences();
32  if(prefs)
33  {
34  ::fwData::Composite::IteratorType iterPref = prefs->find(key);
35  if ( iterPref != prefs->end() )
36  {
37  ::fwData::String::sptr preferences = ::fwData::String::dynamicCast(iterPref->second);
38  preferences->value() = value;
39  }
40  else
41  {
42  (*prefs)[key] = ::fwData::String::New(value);
43  }
44  isModified = true;
45  }
46  return isModified;
47 }
48 
49 //----------------------------------------------------------------------------
50 
51 std::string getPreference(const std::string& preferenceKey)
52 {
53  std::string value;
54  // Check preferences
55  ::fwData::Composite::sptr prefs = getPreferences();
56  if(prefs)
57  {
58  ::fwData::Composite::IteratorType iterPref = prefs->find( preferenceKey );
59  if ( iterPref != prefs->end() )
60  {
61  ::fwData::String::sptr prefString = ::fwData::String::dynamicCast(iterPref->second);
62  value = prefString->value();
63  }
64  }
65  return value;
66 }
67 
68 //-----------------------------------------------------------------------------
69 
70 ::boost::filesystem::path getPreferencesFile()
71 {
72  namespace bfile = ::boost::filesystem;
73 
74  ::fwRuntime::profile::Profile::sptr profile = ::fwRuntime::profile::getCurrentProfile();
75  FW_RAISE_IF("No current profile set.", !profile);
76 
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";
80 
81  FW_RAISE_IF("Unable to define user data directory", appPrefDir.empty());
82 
83  if (!bfile::exists(appPrefDir))
84  {
85  bfile::create_directories(appPrefDir);
86  }
87 
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));
90 
91  return appPrefFile;
92 }
93 
94 //-----------------------------------------------------------------------------
95 
96 // returns the preferences service (or nullptr if is does not exist). This method is not exposed.
97 ::fwPreferences::IPreferences::sptr getPreferencesSrv()
98 {
99  ::fwPreferences::IPreferences::sptr srv;
100  const auto preferencesServicesList = ::fwServices::OSR::getServices("::fwPreferences::IPreferences");
101 
102  if(!preferencesServicesList.empty())
103  {
104  ::fwServices::IService::sptr prefService = *preferencesServicesList.begin();
105  srv = ::fwPreferences::IPreferences::dynamicCast(prefService);
106  }
107  SLM_DEBUG_IF("The preferences service is not found, the preferences can not be used", !srv);
108 
109  return srv;
110 }
111 
112 //-----------------------------------------------------------------------------
113 
114 ::fwData::Composite::sptr getPreferences()
115 {
116  ::fwData::Composite::sptr prefs;
117 
118  const auto prefService = getPreferencesSrv();
119 
120  if(prefService)
121  {
122  prefs = prefService->getInOut< ::fwData::Composite >(s_PREFERENCES_KEY);
123  }
124  SLM_DEBUG_IF("The preferences are not found", !prefs);
125 
126  return prefs;
127 }
128 
129 //-----------------------------------------------------------------------------
130 
131 void savePreferences()
132 {
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())
137  {
138  prefService->update();
139  }
140 }
141 
142 //-----------------------------------------------------------------------------
143 
144 } // namespace fwPreferences
#define SLM_DEBUG_IF(message, cond)
Definition: spyLog.hpp:243
The namespace preferences contains the service to manage the application&#39;s preferences (window size/p...
This class defines a composite object.
#define SLM_WARN_IF(message, cond)
Definition: spyLog.hpp:265