Medical Imaging Interaction Toolkit  2018.4.99-389bf124
Medical Imaging Interaction Toolkit
mitkFileReaderWriterBase.cpp
Go to the documentation of this file.
1 /*============================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center (DKFZ)
6 All rights reserved.
7 
8 Use of this source code is governed by a 3-clause BSD license that can be
9 found in the LICENSE file.
10 
11 ============================================================================*/
12 
14 
15 #include "mitkCoreServices.h"
16 #include "mitkIMimeTypeProvider.h"
17 #include "mitkIOMimeTypes.h"
18 #include "mitkLogMacros.h"
19 
20 #include <usGetModuleContext.h>
21 #include <usLDAPProp.h>
22 
23 namespace mitk
24 {
25  FileReaderWriterBase::FileReaderWriterBase() : m_Ranking(0), m_MimeTypePrefix(IOMimeTypes::DEFAULT_BASE_NAME() + ".")
26  {
27  }
28 
32  m_Ranking(other.m_Ranking),
34  m_Options(other.m_Options),
37  {
38  }
39 
41  {
42  Options options = m_Options;
43  options.insert(m_DefaultOptions.begin(), m_DefaultOptions.end());
44  return options;
45  }
46 
47  us::Any FileReaderWriterBase::GetOption(const std::string &name) const
48  {
49  auto iter = m_Options.find(name);
50  if (iter != m_Options.end())
51  {
52  return iter->second;
53  }
54  iter = m_DefaultOptions.find(name);
55  if (iter != m_DefaultOptions.end())
56  {
57  return iter->second;
58  }
59  return us::Any();
60  }
61 
63  {
64  for (const auto &option : options)
65  {
66  this->SetOption(option.first, option.second);
67  }
68  }
69 
70  void FileReaderWriterBase::SetOption(const std::string &name, const us::Any &value)
71  {
72  if (m_DefaultOptions.find(name) == m_DefaultOptions.end())
73  {
74  MITK_WARN << "Ignoring unknown IFileReader option '" << name << "'";
75  }
76  else
77  {
78  if (value.Empty())
79  {
80  // an empty Any signals 'reset to default value'
81  m_Options.erase(name);
82  }
83  else
84  {
85  m_Options[name] = value;
86  }
87  }
88  }
89 
91  {
92  m_DefaultOptions = defaultOptions;
93  }
94 
96  void FileReaderWriterBase::SetRanking(int ranking) { m_Ranking = ranking; }
98  void FileReaderWriterBase::SetMimeType(const CustomMimeType &mimeType) { m_CustomMimeType.reset(mimeType.Clone()); }
102  {
103  MimeType result;
104  if (!m_MimeTypeReg)
105  {
106  if (!m_CustomMimeType->GetName().empty())
107  {
108  CoreServicePointer<IMimeTypeProvider> mimeTypeProvider(
110  return mimeTypeProvider->GetMimeTypeForName(m_CustomMimeType->GetName());
111  }
112  return result;
113  }
114 
115  us::ServiceReferenceU reference = m_MimeTypeReg.GetReference();
116  try
117  {
118  int rank = 0;
119  us::Any rankProp = reference.GetProperty(us::ServiceConstants::SERVICE_RANKING());
120  if (!rankProp.Empty())
121  {
122  rank = us::any_cast<int>(rankProp);
123  }
124  auto id = us::any_cast<long>(reference.GetProperty(us::ServiceConstants::SERVICE_ID()));
125  result = MimeType(*m_CustomMimeType, rank, id);
126  }
127  catch (const us::BadAnyCastException &e)
128  {
129  MITK_WARN << "Unexpected exception: " << e.what();
130  }
131  return result;
132  }
133 
134  void FileReaderWriterBase::SetMimeTypePrefix(const std::string &prefix) { m_MimeTypePrefix = prefix; }
136  void FileReaderWriterBase::SetDescription(const std::string &description) { m_Description = description; }
137  std::string FileReaderWriterBase::GetDescription() const { return m_Description; }
139  {
140  m_ProgressMessage += callback;
141  }
142 
144  {
145  m_ProgressMessage -= callback;
146  }
147 
149  {
150  if (context == nullptr)
151  throw std::invalid_argument("The context argument must not be nullptr.");
152 
154 
155  const std::vector<std::string> extensions = m_CustomMimeType->GetExtensions();
156 
157  // If the mime type name is set and the list of extensions is empty,
158  // look up the mime type in the registry and print a warning if
159  // there is none
160  if (!m_CustomMimeType->GetName().empty() && extensions.empty())
161  {
162  if (!mimeTypeProvider->GetMimeTypeForName(m_CustomMimeType->GetName()).IsValid())
163  {
164  MITK_WARN << "Registering a MITK reader or writer with an unknown MIME type " << m_CustomMimeType->GetName();
165  }
166  return m_MimeTypeReg;
167  }
168 
169  // If the mime type name and extensions list is empty, print a warning
170  if (m_CustomMimeType->GetName().empty() && extensions.empty())
171  {
172  MITK_WARN << "Trying to register a MITK reader or writer with an empty mime type name and empty extension list.";
173  return m_MimeTypeReg;
174  }
175 
176  // extensions is not empty
177 
178  if (m_CustomMimeType->GetName().empty())
179  {
180  // Create a synthetic mime type name from the
181  // first extension in the list
182  m_CustomMimeType->SetName(m_MimeTypePrefix + extensions.front());
183  }
184 
185  // Register a new mime type
186  // us::ServiceProperties props;
187  // props["name"] = m_CustomMimeType.GetName();
188  // props["extensions"] = m_CustomMimeType.GetExtensions();
189  m_MimeTypeReg = context->RegisterService<CustomMimeType>(m_CustomMimeType.get());
190 
191  return m_MimeTypeReg;
192  }
193 
195  {
196  if (m_MimeTypeReg)
197  {
198  try
199  {
200  m_MimeTypeReg.Unregister();
201  }
202  catch (const std::logic_error &)
203  {
204  // service already unregistered
205  }
206  }
207  }
208 }
us::ServiceRegistration< CustomMimeType > RegisterMimeType(us::ModuleContext *context)
void SetDefaultOptions(const Options &defaultOptions)
The IOMimeTypes class.
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
US_Core_EXPORT const std::string & SERVICE_RANKING()
void RemoveProgressCallback(const ProgressCallback &callback)
void SetRanking(int ranking)
Set the service ranking for this file reader.
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
bool Empty() const
Definition: usAny.h:246
us::Any GetOption(const std::string &name) const
us::ServiceRegistration< CustomMimeType > m_MimeTypeReg
void SetMimeTypePrefix(const std::string &prefix)
const char * what() const override
Definition: usAny.h:352
void SetDescription(const std::string &description)
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
void SetOptions(const Options &options)
#define MITK_WARN
Definition: mitkLogMacros.h:19
const CustomMimeType * GetMimeType() const
Definition: usAny.h:163
void SetOption(const std::string &name, const us::Any &value)
virtual CustomMimeType * Clone() const
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
Definition: mitkMimeType.h:36
std::map< std::string, us::Any > Options
Options m_Options
Options supported by this reader. Set sensible default values!
A RAII helper class for core service objects.
US_Core_EXPORT const std::string & SERVICE_ID()
static mitk::PlanarFigure::Pointer Clone(mitk::PlanarFigure::Pointer original)
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void SetMimeType(const CustomMimeType &mimeType)
std::unique_ptr< CustomMimeType > m_CustomMimeType
void AddProgressCallback(const ProgressCallback &callback)