fw4spl
SAttachmentSeriesReader.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 "ioData/SAttachmentSeriesReader.hpp"
8 
9 #include <fwData/location/Folder.hpp>
10 #include <fwData/location/SingleFile.hpp>
11 #include <fwData/mt/ObjectWriteLock.hpp>
12 
13 #include <fwGui/dialog/LocationDialog.hpp>
14 
15 #include <fwIO/IReader.hpp>
16 
17 #include <fwMedData/AttachmentSeries.hpp>
18 #include <fwMedData/Equipment.hpp>
19 #include <fwMedData/Patient.hpp>
20 #include <fwMedData/SeriesDB.hpp>
21 #include <fwMedData/Study.hpp>
22 
23 #include <fwMedDataTools/helper/SeriesDB.hpp>
24 
25 #include <fwServices/macros.hpp>
26 
27 #include <fwTools/dateAndTime.hpp>
28 #include <fwTools/UUID.hpp>
29 
30 #include <boost/date_time/posix_time/posix_time.hpp>
31 #include <boost/filesystem/operations.hpp>
32 
33 fwServicesRegisterMacro( ::fwIO::IReader, ::ioData::SAttachmentSeriesReader, ::fwMedData::SeriesDB );
34 
35 namespace ioData
36 {
37 
38 //------------------------------------------------------------------------------
39 
40 std::string getMediaType(const ::boost::filesystem::path& media)
41 {
42  std::string mediaType;
43  std::map<std::string, std::string> mimeMap =
44  {
45  {".txt", "text/plain"},
46  {".rtf", "text/rtf"},
47  {".pdf", "application/pdf"},
48  {".zip", "application/zip"},
49  {".mp3", "audio/mpeg"},
50  {".wav", "audio/x-wav"},
51  {".bmp", "image/bmp"},
52  {".jpeg", "image/jpeg"},
53  {".mpeg", "video/mpeg"},
54  {".mp4", "video/mpeg"},
55  {".avi", "video/x-msvideo"}
56  };
57  const std::string extension = media.extension().string();
58  const auto iter = mimeMap.find(extension);
59  if(iter != mimeMap.end())
60  {
61  mediaType = iter->second;
62  }
63  return mediaType;
64 }
65 
66 //------------------------------------------------------------------------------
67 
68 void initSeries(::fwMedData::Series::sptr series, const std::string& instanceUID)
69 {
70  const std::string unknown = "unknown";
71  series->setModality("OT");
72  ::boost::posix_time::ptime now = ::boost::posix_time::second_clock::local_time();
73  const std::string date = ::fwTools::getDate(now);
74  const std::string time = ::fwTools::getTime(now);
75  series->setDate(date);
76  series->setTime(time);
77  //series->setDescription(??);
78  //series->setPerformingPhysiciansName(??);
79 
80  series->getEquipment()->setInstitutionName(unknown);
81 
82  series->getPatient()->setName(unknown);
83  series->getPatient()->setPatientId(unknown);
84  series->getPatient()->setBirthdate(unknown);
85  series->getPatient()->setSex(unknown);
86 
87  series->getStudy()->setInstanceUID(instanceUID);
88  series->getStudy()->setDate(date);
89  series->getStudy()->setTime(time);
90  series->getStudy()->setReferringPhysicianName(unknown);
91  series->getStudy()->setDescription(unknown);
92  series->getStudy()->setPatientAge(unknown);
93 }
94 
95 //-----------------------------------------------------------------------------
96 
98 {
99 }
100 
101 //-----------------------------------------------------------------------------
102 
103 void SAttachmentSeriesReader::info(std::ostream& _sstream )
104 {
105  this->SuperClass::info( _sstream );
106  _sstream << std::endl << " AttachmentSerie reader";
107 }
108 
109 //-----------------------------------------------------------------------------
110 
112 {
113  std::vector< std::string > extensions;
114  extensions.push_back(".*");
115  return extensions;
116 }
117 
118 //------------------------------------------------------------------------------
119 
121 {
122  return ::fwIO::FILE;
123 }
124 
125 //------------------------------------------------------------------------------
126 
128 {
130 }
131 
132 //------------------------------------------------------------------------------
133 
135 {
136  static ::boost::filesystem::path _sDefaultPath("");
137 
139  dialogFile.setTitle(m_windowTitle.empty() ? "Choose a attachment file" : m_windowTitle);
140  dialogFile.setDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
141  dialogFile.addFilter("Attachment file", "*.*");
142  dialogFile.setOption(::fwGui::dialog::ILocationDialog::READ);
143 
144  ::fwData::location::SingleFile::sptr result;
145  result = ::fwData::location::SingleFile::dynamicCast( dialogFile.show() );
146  if (result)
147  {
148  _sDefaultPath = result->getPath().parent_path();
149  dialogFile.saveDefaultLocation( ::fwData::location::Folder::New(_sDefaultPath) );
150  this->setFile(result->getPath());
151  }
152  else
153  {
154  this->clearLocations();
155  }
156 }
157 
158 //------------------------------------------------------------------------------
159 
161 {
162  if(this->hasLocationDefined())
163  {
164  // Retrieve object
165  ::fwMedData::SeriesDB::sptr seriesDB = this->getInOut< ::fwMedData::SeriesDB >(::fwIO::s_DATA_KEY);
166 
167  ::fwMedData::AttachmentSeries::sptr series = ::fwMedData::AttachmentSeries::New();
168  const std::string instanceUID = ::fwTools::UUID::generateUUID();
169  initSeries(series, instanceUID);
170  series->setAttachmentPath(this->getFile());
171  const std::string mediaType = getMediaType(this->getFile());
172  series->setMediaType(mediaType);
173 
174  ::fwMedDataTools::helper::SeriesDB seriesDBHelper(seriesDB);
175 
176  ::fwData::mt::ObjectWriteLock lock(seriesDB);
177  seriesDBHelper.add(series);
178  seriesDBHelper.notify();
179  }
180 }
181 
182 //------------------------------------------------------------------------------
183 
184 }
virtual IODATA_API void info(std::ostream &_sstream) override
Info method.
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
virtual IODATA_API std::vector< std::string > getSupportedExtensions() override
Returns (filename) extension.
virtual FWGUI_API void setDefaultLocation(::fwData::location::ILocation::sptr loc) override
Set the initial location for the dialog.
FWGUI_API::fwGui::dialog::ILocationDialog & setOption(::fwGui::dialog::ILocationDialog::Options option) override
allow to set option to the file dialog mode=READ/WRITE, check=FILE_MUST_EXIST
FWGUI_API::fwData::location::ILocation::sptr show() override
Display the dialog.
A helper to lock object on exclusive mode.
FWMEDDATATOOLS_API void add(::fwMedData::Series::sptr newSeries)
Add a Series in the SeriesDB.
FWGUI_API void addFilter(const std::string &filterName, const std::string &wildcardList) override
specify some filtering when browsing files:
Defines an helper to modify an fwMedData::SeriesDB and create in parallel the message to announce thi...
FWIO_APIconst::boost::filesystem::path & getFile() const
Returns the file path set by the user or set during service configuration.
Definition: IReader.cpp:62
IODATA_API void configureWithIHM() override
Configure the attachment path.
SAttachmentSeries reader service.
FWIO_API void setFile(const ::boost::filesystem::path &file)
Sets file path.
Definition: IReader.cpp:71
Reader service API. It manages extension points definition and extension configuration.
Definition: IReader.hpp:34
IODATA_API void updating() override
Updating method. This method is called by update() from base service ( fwServices::IService ) ...
virtual IODATA_API void configuring() override
Configuring method : calls implementation from io::IReader
IODATA_API SAttachmentSeriesReader()
Constructor : does nothing.
IOPathType
IOPathType defines different type of paths used by service readers/writers.
Definition: ioTypes.hpp:19
virtual IODATA_API::fwIO::IOPathType getIOPathType() const override
Returns path type managed by the service, here FILE.
FWGUI_API void setTitle(const std::string &title) override
Set the title for the dialog.
Defines the generic file/folder selector dialog for IHM.
FWMEDDATATOOLS_API void notify()
Send the signal of modification.
The namespace ioData contains reader and writer services for basic fwData::Object which doesn&#39;t need ...
FWIO_API void clearLocations()
Clear any location set by the setFile/setFiles/setFolder setter.
Definition: IReader.cpp:137
FWGUI_API void saveDefaultLocation(::fwData::location::ILocation::sptr loc) override
Save the specified default location for the dialog in preferences (if available)
virtual FWSERVICES_API void info(std::ostream &_sstream)
Write information in a stream.
Definition: IService.cpp:74
static FWTOOLS_API UUIDType generateUUID()
Return a new extended UUID;.
Definition: UUID.cpp:114