fw4spl
IWriter.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-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 #include "fwIO/IWriter.hpp"
7 
8 #include <fwCom/Slots.hpp>
9 #include <fwCom/Slots.hxx>
10 
11 #include <fwCore/base.hpp>
12 
13 #include <fwServices/macros.hpp>
14 
15 namespace fwIO
16 {
17 
18 // Public slot
19 const ::fwCom::Slots::SlotKeyType IWriter::s_SET_FILE_FOLDER = "setFileFolder";
20 
21 // Private slot
22 static const ::fwCom::Slots::SlotKeyType s_CONFIGURE_WITH_IHM = "configureWithIHM";
23 
24 //-----------------------------------------------------------------------------
25 
26 IWriter::IWriter() noexcept
27 {
28  newSlot(s_CONFIGURE_WITH_IHM, &IWriter::configureWithIHM, this);
29  newSlot(s_SET_FILE_FOLDER, &IWriter::setFileFolder, this);
30 }
31 
32 //-----------------------------------------------------------------------------
33 
34 IWriter::~IWriter() noexcept
35 {
36 }
37 
38 //-----------------------------------------------------------------------------
39 
40 const ::boost::filesystem::path& IWriter::getFile() const
41 {
42  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILE));
43  FW_RAISE_IF("Exactly one file must be define in location", m_locations.size() != 1);
44  return m_locations.front();
45 }
46 
47 //-----------------------------------------------------------------------------
48 
49 void IWriter::setFile( const ::boost::filesystem::path& file)
50 {
51  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILE));
52  m_locations.clear();
53  m_locations.push_back(file);
54 }
55 
56 //-----------------------------------------------------------------------------
57 
58 const ::fwIO::LocationsType& IWriter::getFiles() const
59 {
60  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILES));
61  FW_RAISE_IF("At least one file must be define in location", m_locations.empty() );
62  return m_locations;
63 }
64 
65 //-----------------------------------------------------------------------------
66 
67 void IWriter::setFiles(const ::fwIO::LocationsType& files)
68 {
69  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILES));
70  m_locations = files;
71 }
72 
73 //-----------------------------------------------------------------------------
74 
75 const ::boost::filesystem::path& IWriter::getFolder() const
76 {
77  FW_RAISE_IF("This reader doesn't manage folders", !(this->getIOPathType() & ::fwIO::FOLDER));
78  FW_RAISE_IF("Exactly one folder must be define in location", m_locations.size() != 1 );
79  return m_locations.front();
80 }
81 
82 //-----------------------------------------------------------------------------
83 
84 void IWriter::setFolder(const ::boost::filesystem::path& folder)
85 {
86  FW_RAISE_IF("This reader doesn't manage folders", !(this->getIOPathType() & ::fwIO::FOLDER));
87  m_locations.clear();
88  m_locations.push_back(folder);
89 }
90 
91 //-----------------------------------------------------------------------------
92 
93 void IWriter::setFileFolder(boost::filesystem::path folder)
94 {
95  FW_RAISE_IF("This reader doesn't manage file or files",
96  !(this->getIOPathType() & ::fwIO::FILE) && !(this->getIOPathType() & ::fwIO::FILES));
97 
98  for(auto& file : m_locations)
99  {
100  file = file.filename();
101  file = folder / file;
102  }
103 }
104 
105 //-----------------------------------------------------------------------------
106 
107 const ::fwIO::LocationsType& IWriter::getLocations() const
108 {
109  FW_RAISE_IF("At least one path must be define in location", m_locations.empty() );
110  return m_locations;
111 }
112 
113 //-----------------------------------------------------------------------------
114 
116 {
117  m_locations.clear();
118 }
119 
120 //-----------------------------------------------------------------------------
121 
123 {
124  SLM_ASSERT("Generic configuring method is only available for io service that uses paths.",
125  !( this->getIOPathType() & ::fwIO::TYPE_NOT_DEFINED ) );
126 
127  SLM_ASSERT("This writer does not manage folders and a folder path is given in the configuration",
128  ( this->getIOPathType() & ::fwIO::FOLDER ) ||
129  (m_configuration->find("folder").size() == 0));
130 
131  SLM_ASSERT("This writer does not manages files and a file path is given in the configuration",
132  ( this->getIOPathType() & ::fwIO::FILE || this->getIOPathType() & ::fwIO::FILES ) ||
133  (m_configuration->find("file").size() == 0));
134 
135  ::fwRuntime::ConfigurationElement::sptr titleConfig = m_configuration->findConfigurationElement("windowTitle");
136  m_windowTitle = titleConfig ? titleConfig->getValue() : "";
137 
138  if ( this->getIOPathType() & ::fwIO::FILE )
139  {
140  FW_RAISE_IF("This reader cannot manages FILE and FILES.", this->getIOPathType() & ::fwIO::FILES );
141  std::vector< ::fwRuntime::ConfigurationElement::sptr > config = m_configuration->find("file");
142  FW_RAISE_IF("No more than one file must be defined in the configuration", config.size() > 1 );
143  if (config.size() == 1)
144  {
145  std::string file = config.at(0)->getValue();
146  this->setFile(::boost::filesystem::path(file));
147  }
148  }
149 
150  if ( this->getIOPathType() & ::fwIO::FILES )
151  {
152  FW_RAISE_IF("This reader cannot manages FILE and FILES.", this->getIOPathType() & ::fwIO::FILE );
153  std::vector< ::fwRuntime::ConfigurationElement::sptr > config = m_configuration->find("file");
154  ::fwIO::LocationsType locations;
155  for(::fwRuntime::ConfigurationElement::sptr elt : config)
156  {
157  std::string location = elt->getValue();
158  locations.push_back(::boost::filesystem::path(location));
159  }
160  this->setFiles(locations);
161  }
162 
163  if ( this->getIOPathType() & ::fwIO::FOLDER )
164  {
165  std::vector< ::fwRuntime::ConfigurationElement::sptr > config = m_configuration->find("folder");
166  FW_RAISE_IF("No more than one folder must be defined in configuration", config.size() > 1 );
167  if (config.size() == 1)
168  {
169  std::string folder = config.at(0)->getValue();
170  this->setFolder(::boost::filesystem::path(folder));
171  }
172  }
173 }
174 
175 //-----------------------------------------------------------------------------
176 
178 {
179  return ::fwIO::TYPE_NOT_DEFINED;
180 }
181 
182 //-----------------------------------------------------------------------------
183 
185 {
186  return m_locations.size() > 0;
187 }
188 
189 //-----------------------------------------------------------------------------
190 
191 }
FWIO_API void setFiles(const ::fwIO::LocationsType &files)
Sets file paths.
Definition: IWriter.cpp:67
virtual FWIO_API void configureWithIHM()=0
Configure the image path (by default does nothing).
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IWriter.cpp:49
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IWriter.cpp:122
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IWriter.cpp:40
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IWriter.cpp:115
#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
FWIO_API void setFileFolder(::boost::filesystem::path folder)
Slot: Sets the folder when a path is configured in FILE or FILES mode This is ignored if a path is no...
Definition: IWriter.cpp:93
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IWriter.cpp:75
::fwRuntime::ConfigurationElement::sptr m_configuration
Configuration element used to configure service internal state using a generic XML like structure TOD...
Definition: IService.hpp:670
FWIO_APIconst::fwIO::LocationsType & getFiles() const
Returns file paths set by the user or set during service configuration.
Definition: IWriter.cpp:58
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
The namespace fwIO contains the base interface for reader and writer services.
Definition: ioTypes.hpp:16
FWIO_APIconst::fwIO::LocationsType & getLocations() const
Returns file/files/folder paths set by the user or set during service configuration.
Definition: IWriter.cpp:107
FWIO_API void setFolder(const ::boost::filesystem::path &folder)
Sets folder path.
Definition: IWriter.cpp:84
std::vector< ::boost::filesystem::path > LocationsType
List of paths managed by io services.
Definition: ioTypes.hpp:28
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IWriter.cpp:184
virtual FWIO_API::fwIO::IOPathType getIOPathType() const
This method must be implemented by concrete service writers that use path file system to write data...
Definition: IWriter.cpp:177
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IWriter.hpp:171