Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
18 
19 #include "mitkCoreServices.h"
20 #include "mitkIMimeTypeProvider.h"
21 #include "mitkIOMimeTypes.h"
22 #include "mitkLogMacros.h"
23 
24 #include <usGetModuleContext.h>
25 #include <usLDAPProp.h>
26 
27 namespace mitk
28 {
29  FileReaderWriterBase::FileReaderWriterBase() : m_Ranking(0), m_MimeTypePrefix(IOMimeTypes::DEFAULT_BASE_NAME() + ".")
30  {
31  }
32 
35  : m_Description(other.m_Description),
36  m_Ranking(other.m_Ranking),
37  m_MimeTypePrefix(other.m_MimeTypePrefix),
38  m_Options(other.m_Options),
39  m_DefaultOptions(other.m_DefaultOptions),
40  m_CustomMimeType(other.m_CustomMimeType->Clone())
41  {
42  }
43 
45  {
46  Options options = m_Options;
47  options.insert(m_DefaultOptions.begin(), m_DefaultOptions.end());
48  return options;
49  }
50 
51  us::Any FileReaderWriterBase::GetOption(const std::string &name) const
52  {
53  Options::const_iterator iter = m_Options.find(name);
54  if (iter != m_Options.end())
55  {
56  return iter->second;
57  }
58  iter = m_DefaultOptions.find(name);
59  if (iter != m_DefaultOptions.end())
60  {
61  return iter->second;
62  }
63  return us::Any();
64  }
65 
67  {
68  for (const auto &option : options)
69  {
70  this->SetOption(option.first, option.second);
71  }
72  }
73 
74  void FileReaderWriterBase::SetOption(const std::string &name, const us::Any &value)
75  {
76  if (m_DefaultOptions.find(name) == m_DefaultOptions.end())
77  {
78  MITK_WARN << "Ignoring unknown IFileReader option '" << name << "'";
79  }
80  else
81  {
82  if (value.Empty())
83  {
84  // an empty Any signals 'reset to default value'
85  m_Options.erase(name);
86  }
87  else
88  {
89  m_Options[name] = value;
90  }
91  }
92  }
93 
95  {
96  m_DefaultOptions = defaultOptions;
97  }
98 
100  void FileReaderWriterBase::SetRanking(int ranking) { m_Ranking = ranking; }
102  void FileReaderWriterBase::SetMimeType(const CustomMimeType &mimeType) { m_CustomMimeType.reset(mimeType.Clone()); }
106  {
107  MimeType result;
108  if (!m_MimeTypeReg)
109  {
110  if (!m_CustomMimeType->GetName().empty())
111  {
112  CoreServicePointer<IMimeTypeProvider> mimeTypeProvider(
114  return mimeTypeProvider->GetMimeTypeForName(m_CustomMimeType->GetName());
115  }
116  return result;
117  }
118 
119  us::ServiceReferenceU reference = m_MimeTypeReg.GetReference();
120  try
121  {
122  int rank = 0;
123  us::Any rankProp = reference.GetProperty(us::ServiceConstants::SERVICE_RANKING());
124  if (!rankProp.Empty())
125  {
126  rank = us::any_cast<int>(rankProp);
127  }
128  long id = us::any_cast<long>(reference.GetProperty(us::ServiceConstants::SERVICE_ID()));
129  result = MimeType(*m_CustomMimeType, rank, id);
130  }
131  catch (const us::BadAnyCastException &e)
132  {
133  MITK_WARN << "Unexpected exception: " << e.what();
134  }
135  return result;
136  }
137 
138  void FileReaderWriterBase::SetMimeTypePrefix(const std::string &prefix) { m_MimeTypePrefix = prefix; }
140  void FileReaderWriterBase::SetDescription(const std::string &description) { m_Description = description; }
141  std::string FileReaderWriterBase::GetDescription() const { return m_Description; }
143  {
144  m_ProgressMessage += callback;
145  }
146 
148  {
149  m_ProgressMessage -= callback;
150  }
151 
153  {
154  if (context == NULL)
155  throw std::invalid_argument("The context argument must not be NULL.");
156 
158 
159  const std::vector<std::string> extensions = m_CustomMimeType->GetExtensions();
160 
161  // If the mime type name is set and the list of extensions is empty,
162  // look up the mime type in the registry and print a warning if
163  // there is none
164  if (!m_CustomMimeType->GetName().empty() && extensions.empty())
165  {
166  if (!mimeTypeProvider->GetMimeTypeForName(m_CustomMimeType->GetName()).IsValid())
167  {
168  MITK_WARN << "Registering a MITK reader or writer with an unknown MIME type " << m_CustomMimeType->GetName();
169  }
170  return m_MimeTypeReg;
171  }
172 
173  // If the mime type name and extensions list is empty, print a warning
174  if (m_CustomMimeType->GetName().empty() && extensions.empty())
175  {
176  MITK_WARN << "Trying to register a MITK reader or writer with an empty mime type name and empty extension list.";
177  return m_MimeTypeReg;
178  }
179 
180  // extensions is not empty
181 
182  if (m_CustomMimeType->GetName().empty())
183  {
184  // Create a synthetic mime type name from the
185  // first extension in the list
186  m_CustomMimeType->SetName(m_MimeTypePrefix + extensions.front());
187  }
188 
189  // Register a new mime type
190  // us::ServiceProperties props;
191  // props["name"] = m_CustomMimeType.GetName();
192  // props["extensions"] = m_CustomMimeType.GetExtensions();
193  m_MimeTypeReg = context->RegisterService<CustomMimeType>(m_CustomMimeType.get());
194 
195  return m_MimeTypeReg;
196  }
197 
199  {
200  if (m_MimeTypeReg)
201  {
202  try
203  {
204  m_MimeTypeReg.Unregister();
205  }
206  catch (const std::logic_error &)
207  {
208  // service already unregistered
209  }
210  }
211  }
212 }
us::ServiceRegistration< CustomMimeType > RegisterMimeType(us::ModuleContext *context)
void SetDefaultOptions(const Options &defaultOptions)
The IOMimeTypes class.
Pointer Clone() const
static IMimeTypeProvider * GetMimeTypeProvider(us::ModuleContext *context=us::GetModuleContext())
Get an IMimeTypeProvider instance.
US_Core_EXPORT const std::string & SERVICE_RANKING()
virtual const char * what() const override
Definition: usAny.h:352
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
us::ServiceRegistration< CustomMimeType > m_MimeTypeReg
void SetMimeTypePrefix(const std::string &prefix)
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:23
virtual CustomMimeType * Clone() const
Definition: usAny.h:163
void SetOption(const std::string &name, const us::Any &value)
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
Definition: mitkMimeType.h:45
std::map< std::string, us::Any > Options
us::Any GetOption(const std::string &name) const
const CustomMimeType * GetMimeType() const
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 ModuleContext * GetModuleContext()
Returns the module context of the calling module.
void SetMimeType(const CustomMimeType &mimeType)
std::unique_ptr< CustomMimeType > m_CustomMimeType
bool Empty() const
Definition: usAny.h:246
void AddProgressCallback(const ProgressCallback &callback)