fw4spl
SPreferences.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 "preferences/SPreferences.hpp"
8 
9 #include <fwAtomConversion/convert.hpp>
10 
11 #include <fwAtomsBoostIO/Reader.hpp>
12 #include <fwAtomsBoostIO/types.hpp>
13 #include <fwAtomsBoostIO/Writer.hpp>
14 
15 #include <fwData/Composite.hpp>
16 
17 #include <fwDataCamp/visitor/RecursiveLock.hpp>
18 
19 #include <fwPreferences/helper.hpp>
20 
21 #include <fwRuntime/ConfigurationElement.hpp>
22 
23 #include <fwServices/macros.hpp>
24 
25 #include <fwZip/ReadDirArchive.hpp>
26 #include <fwZip/WriteDirArchive.hpp>
27 
28 #include <boost/filesystem.hpp>
29 
30 namespace preferences
31 {
32 
34 
35 //-----------------------------------------------------------------------------
36 
38 {
39  this->load();
40 }
41 
42 //-----------------------------------------------------------------------------
43 
45 {
46  this->save();
47 }
48 
49 //-----------------------------------------------------------------------------
50 
52 {
53  this->save();
54 }
55 
56 //-----------------------------------------------------------------------------
57 
59 {
60  std::vector < ::fwRuntime::ConfigurationElement::sptr > vectConfig = m_configuration->find("filename");
61  SLM_ASSERT("The <filename> element must exist.", !vectConfig.empty() );
62  m_prefFile = vectConfig.at(0)->getValue();
63 }
64 
65 //-----------------------------------------------------------------------------
66 
67 void SPreferences::load()
68 {
69  if (::boost::filesystem::is_regular_file(m_prefFile))
70  {
71  const ::boost::filesystem::path folderPath = m_prefFile.parent_path();
72  const ::boost::filesystem::path filename = m_prefFile.filename();
73 
74  ::fwData::Object::sptr data = this->getInOut< ::fwData::Object >(::fwPreferences::s_PREFERENCES_KEY);
75 
76  if (!data)
77  {
78  FW_DEPRECATED_KEY(::fwPreferences::s_PREFERENCES_KEY, "inout", "18.0");
79  data = this->getObject< ::fwData::Object >();
80  }
81 
82  // Read atom
83  ::fwZip::IReadArchive::sptr readArchive = ::fwZip::ReadDirArchive::New(folderPath.string());
85  try
86  {
87  ::fwAtoms::Object::sptr atom = ::fwAtoms::Object::dynamicCast( reader.read( readArchive, filename ) );
88 
89  ::fwData::Object::sptr newData = ::fwAtomConversion::convert(atom,
91  data->shallowCopy(newData);
92  }
93  catch(...)
94  {
95  SLM_ERROR("Failed to load preference file '"+m_prefFile.string()+"'.");
96  }
97  }
98 }
99 
100 //-----------------------------------------------------------------------------
101 
102 void SPreferences::save()
103 {
104  const ::boost::filesystem::path folderPath = m_prefFile.parent_path();
105  const ::boost::filesystem::path filename = m_prefFile.filename();
106 
107  ::fwData::Object::sptr obj = this->getInOut< ::fwData::Object >(::fwPreferences::s_PREFERENCES_KEY);
108 
109  if (!obj)
110  {
111  FW_DEPRECATED_KEY(::fwPreferences::s_PREFERENCES_KEY, "inout", "18.0");
112  obj = this->getObject< ::fwData::Object >();
113  }
114 
115  // Mutex data lock
116  ::fwDataCamp::visitor::RecursiveLock recursiveLock(obj);
117 
118  // Convert data to atom
119  ::fwAtoms::Object::sptr atom = ::fwAtomConversion::convert(obj);
120  // Write atom
121  ::fwZip::IWriteArchive::sptr writeArchive = ::fwZip::WriteDirArchive::New(folderPath.string());
122  ::fwAtomsBoostIO::FormatType format = ::fwAtomsBoostIO::JSON;
123 
124  if(::boost::filesystem::exists(m_prefFile) && ::boost::filesystem::is_regular_file(m_prefFile)
125  && !(::boost::filesystem::status(m_prefFile).permissions() & ::boost::filesystem::owner_write) )
126  {
127  SLM_ERROR("SPreference need write access to the file '"+m_prefFile.string()+"'."
128  "Please, change file permission.");
129  }
130  else
131  {
132  ::fwAtomsBoostIO::Writer(atom).write( writeArchive, filename, format );
133  }
134 }
135 
136 //-----------------------------------------------------------------------------
137 
138 } //namespace preferences
#define FW_DEPRECATED_KEY(newKey, access, version)
Use this macro when deprecating a service key to warn the developer.
Definition: spyLog.hpp:366
Preferences service API. This class represents the base interface for preferences services...
::boost::filesystem::path m_prefFile
preference file
This policy changes data&#39;s uuid if it already exists.
Definition: AtomVisitor.hpp:55
Service to load and save preferences.
#define SLM_ERROR(message)
Definition: spyLog.hpp:272
virtual PREFERENCES_API void updating() override
Updates service and saves preferences.
The namespace preferences contains the service to manage the application&#39;s preferences (window size/p...
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
This class is an helper to lock an object and all its children recursively.
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
This class defines a composite object.
virtual PREFERENCES_API void configuring() override
Configure the service before starting. Apply the configuration to service.
virtual PREFERENCES_API void stopping() override
Stops service and saves preferences.
virtual PREFERENCES_API void starting() override
Starts service and loads preferences if exists.