fw4spl
IReader.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 
7 #include "fwIO/IReader.hpp"
8 
9 #include <fwCom/Slots.hxx>
10 
11 #include <fwRuntime/operations.hpp>
12 
13 #include <fwServices/macros.hpp>
14 
16 
17 namespace fwIO
18 {
19 
20 // Public slot
21 const ::fwCom::Slots::SlotKeyType IReader::s_SET_FILE_FOLDER = "setFileFolder";
22 
23 // Private slot
24 static const ::fwCom::Slots::SlotKeyType s_READ_FOLDER_SLOT = "readFolder";
25 static const ::fwCom::Slots::SlotKeyType s_READ_FILE_SLOT = "readFile";
26 static const ::fwCom::Slots::SlotKeyType s_READ_FILES_SLOT = "readFiles";
27 static const ::fwCom::Slots::SlotKeyType s_CONFIGURE_WITH_IHM = "configureWithIHM";
28 
29 //-----------------------------------------------------------------------------
30 
31 IReader::IReader() noexcept
32 {
33  newSlot(s_READ_FOLDER_SLOT, &IReader::readFolder, this);
34  newSlot(s_READ_FILE_SLOT, &IReader::readFile, this);
35  newSlot(s_READ_FILES_SLOT, &IReader::readFiles, this);
36  newSlot(s_CONFIGURE_WITH_IHM, &IReader::configureWithIHM, this);
37  newSlot(s_SET_FILE_FOLDER, &IReader::setFileFolder, this);
38 }
39 
40 //-----------------------------------------------------------------------------
41 
42 IReader::~IReader() noexcept
43 {
44 }
45 
46 //-----------------------------------------------------------------------------
47 
49 {
50  return "Choose a file";
51 }
52 
53 //-----------------------------------------------------------------------------
54 
55 std::vector< std::string > IReader::getSupportedExtensions()
56 {
57  return std::vector< std::string >();
58 }
59 
60 //-----------------------------------------------------------------------------
61 
62 const ::boost::filesystem::path& IReader::getFile() const
63 {
64  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILE));
65  FW_RAISE_IF("Exactly one file must be defined in location", m_locations.size() != 1);
66  return m_locations.front();
67 }
68 
69 //-----------------------------------------------------------------------------
70 
71 void IReader::setFile( const ::boost::filesystem::path& file)
72 {
73  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILE));
74  m_locations.clear();
75  m_locations.push_back(file);
76 }
77 
78 //-----------------------------------------------------------------------------
79 
80 const ::fwIO::LocationsType& IReader::getFiles() const
81 {
82  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILES));
83  FW_RAISE_IF("At least one file must be define in location", m_locations.empty() );
84  return m_locations;
85 }
86 
87 //-----------------------------------------------------------------------------
88 
89 void IReader::setFiles(const ::fwIO::LocationsType& files)
90 {
91  FW_RAISE_IF("This reader doesn't manage files", !(this->getIOPathType() & ::fwIO::FILES));
92  m_locations = files;
93 }
94 
95 //-----------------------------------------------------------------------------
96 
97 const ::boost::filesystem::path& IReader::getFolder() const
98 {
99  FW_RAISE_IF("This reader doesn't manage folders", !(this->getIOPathType() & ::fwIO::FOLDER));
100  FW_RAISE_IF("Exactly one folder must be define in location", m_locations.size() != 1 );
101  return m_locations.front();
102 }
103 
104 //-----------------------------------------------------------------------------
105 
106 void IReader::setFolder(const ::boost::filesystem::path& folder)
107 {
108  FW_RAISE_IF("This reader doesn't manage folders", !(this->getIOPathType() & ::fwIO::FOLDER));
109  m_locations.clear();
110  m_locations.push_back(folder);
111 }
112 
113 //-----------------------------------------------------------------------------
114 
115 void IReader::setFileFolder(boost::filesystem::path folder)
116 {
117  FW_RAISE_IF("This reader doesn't manage file or files",
118  !(this->getIOPathType() & ::fwIO::FILE) && !(this->getIOPathType() & ::fwIO::FILES));
119 
120  for(auto& file : m_locations)
121  {
122  file = file.filename();
123  file = folder / file;
124  }
125 }
126 
127 //-----------------------------------------------------------------------------
128 
129 const ::fwIO::LocationsType& IReader::getLocations() const
130 {
131  FW_RAISE_IF("At least one path must be define in location", m_locations.empty() );
132  return m_locations;
133 }
134 
135 //-----------------------------------------------------------------------------
136 
138 {
139  m_locations.clear();
140 }
141 
142 //-----------------------------------------------------------------------------
143 
145 {
146  const ConfigType config = this->getConfigTree();
147 
148  SLM_ASSERT("Generic configuring method is only available for io service that use paths.",
149  !( this->getIOPathType() & ::fwIO::TYPE_NOT_DEFINED ) );
150 
151  SLM_ASSERT("This reader does not manage folders and a folder path is given in the configuration",
152  ( this->getIOPathType() & ::fwIO::FOLDER ) || (config.count("folder") == 0) );
153 
154  SLM_ASSERT("This reader does not manage files and a file path is given in the configuration",
155  ( this->getIOPathType() & ::fwIO::FILE || this->getIOPathType() & ::fwIO::FILES ) ||
156  (config.count("file") == 0));
157 
158  m_windowTitle = config.get("windowTitle", "");
159 
160  if ( this->getIOPathType() & ::fwIO::FILE )
161  {
162  FW_RAISE_IF("This reader cannot manages FILE and FILES.", this->getIOPathType() & ::fwIO::FILES );
163  FW_RAISE_IF("At least one file must be defined in configuration", config.count("file") > 1 );
164  if (config.count("file") == 1)
165  {
166  const std::string file = config.get<std::string>("file");
167  this->setFile(::boost::filesystem::path(file));
168  }
169  else if (config.count("resource") == 1)
170  {
171  const std::string resource = config.get<std::string>("resource");
172  const auto file = ::fwRuntime::getResourceFilePath(resource);
173  this->setFile(file);
174  }
175  }
176 
177  if ( this->getIOPathType() & ::fwIO::FILES )
178  {
179  FW_RAISE_IF("This reader cannot manage FILE and FILES.", this->getIOPathType() & ::fwIO::FILE );
180 
181  ::fwIO::LocationsType locations;
182 
183  const auto filesCfg = config.equal_range("file");
184  for (auto fileCfg = filesCfg.first; fileCfg != filesCfg.second; ++fileCfg)
185  {
186  const std::string location = fileCfg->second.get_value<std::string>();
187  locations.push_back(::boost::filesystem::path(location));
188  }
189 
190  const auto resourcesCfg = config.equal_range("resource");
191  for (auto resourceCfg = resourcesCfg.first; resourceCfg != resourcesCfg.second; ++resourceCfg)
192  {
193  const std::string resource = resourceCfg->second.get_value<std::string>();
194  const auto file = ::fwRuntime::getResourceFilePath(resource);
195  locations.push_back(file);
196  }
197  this->setFiles(locations);
198  }
199 
200  if ( this->getIOPathType() & ::fwIO::FOLDER )
201  {
202  FW_RAISE_IF("At least one folder must be defined in configuration", config.count("folder") > 1 );
203  if (config.count("folder") == 1)
204  {
205  const std::string folder = config.get<std::string>("folder");
206  this->setFolder(::boost::filesystem::path(folder));
207  }
208  else if (config.count("resource") == 1)
209  {
210  const std::string resource = config.get<std::string>("resource");
211  auto folder = ::fwRuntime::getBundleResourceFilePath(resource);
212  if(folder.empty())
213  {
214  // If not found in a bundle, look into libraries
215  folder = ::fwRuntime::getLibraryResourceFilePath(resource);
216  SLM_ERROR_IF("Resource '" + resource + "' has not been found in any bundle or library", folder.empty());
217  }
218 
219  this->setFolder(folder);
220  }
221  }
222 }
223 
224 //-----------------------------------------------------------------------------
225 
227 {
228  return ::fwIO::TYPE_NOT_DEFINED;
229 }
230 
231 //-----------------------------------------------------------------------------
232 
234 {
235  return m_locations.size() > 0;
236 }
237 
238 //-----------------------------------------------------------------------------
239 
240 void IReader::readFolder(::boost::filesystem::path folder)
241 {
242  this->setFolder(folder);
243  this->updating();
244 }
245 
246 //-----------------------------------------------------------------------------
247 
248 void IReader::readFile(::boost::filesystem::path file)
249 {
250  this->setFile(file);
251  this->updating();
252 }
253 
254 //-----------------------------------------------------------------------------
255 
256 void IReader::readFiles(::fwIO::LocationsType files)
257 {
258  this->setFiles(files);
259  this->updating();
260 }
261 
262 //-----------------------------------------------------------------------------
263 
264 }
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: IReader.cpp:115
std::string m_windowTitle
Title of the window that will open when the configureWithIHM slot is called.
Definition: IReader.hpp:207
FWIO_API bool hasLocationDefined() const
Returns if a location has been defined ( by the configuration process or directly by user ) ...
Definition: IReader.cpp:233
virtual FWIO_API void configuring() override
This method proposes to parse xml configuration to retrieve file/files/folder paths.
Definition: IReader.cpp:144
FWIO_API void setFiles(const ::fwIO::LocationsType &files)
Sets file paths.
Definition: IReader.cpp:89
virtual FWSERVICES_API void updating()=0
Perform some computations according to object (this service is attached to) attribute values and its ...
virtual FWIO_API void configureWithIHM()=0
Configure the image path (by default does nothing).
FWIO_APIconst::boost::filesystem::path & getFolder() const
Returns folder path set by the user or set during service configuration.
Definition: IReader.cpp:97
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IReader.cpp:71
virtual FWIO_API::fwIO::IOPathType getIOPathType() const
This method must be implemented by concrete service readers that use path file system to read data...
Definition: IReader.cpp:226
virtual FWIO_API std::string getSelectorDialogTitle()
returns the title of selector dialog box
Definition: IReader.cpp:48
#define SLM_ERROR_IF(message, cond)
Definition: spyLog.hpp:276
#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_APIconst::fwIO::LocationsType & getFiles() const
Returns file paths set by the user or set during service configuration.
Definition: IReader.cpp:80
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
FWIO_APIconst::fwIO::LocationsType & getLocations() const
Returns file/files/folder paths set by the user or set during service configuration.
Definition: IReader.cpp:129
The namespace fwIO contains the base interface for reader and writer services.
Definition: ioTypes.hpp:16
Defines the generic configuration element container class.
virtual FWIO_API std::vector< std::string > getSupportedExtensions()
returns (filename) extension
Definition: IReader.cpp:55
FWIO_API void setFolder(const ::boost::filesystem::path &folder)
Sets folder path.
Definition: IReader.cpp:106
std::vector< ::boost::filesystem::path > LocationsType
List of paths managed by io services.
Definition: ioTypes.hpp:28
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IReader.cpp:137
FWSERVICES_API ConfigType getConfigTree() const
Return the configuration, in an boost property tree.
Definition: IService.cpp:247