Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
mitkMimeTypeProvider.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 
17 #include "mitkMimeTypeProvider.h"
18 
19 #include "mitkLogMacros.h"
20 
21 #include <usGetModuleContext.h>
22 #include <usModuleContext.h>
23 
24 #include <itksys/SystemTools.hxx>
25 
26 #ifdef _MSC_VER
27 #pragma warning(disable : 4503) // decorated name length exceeded, name was truncated
28 #pragma warning(disable : 4355)
29 #endif
30 
31 namespace mitk
32 {
33  MimeTypeProvider::MimeTypeProvider() : m_Tracker(NULL) {}
34  MimeTypeProvider::~MimeTypeProvider() { delete m_Tracker; }
36  {
37  if (m_Tracker == NULL)
38  {
40  }
41  m_Tracker->Open();
42  }
43 
44  void MimeTypeProvider::Stop() { m_Tracker->Close(); }
45  std::vector<MimeType> MimeTypeProvider::GetMimeTypes() const
46  {
47  std::vector<MimeType> result;
48  for (const auto &elem : m_NameToMimeType)
49  {
50  result.push_back(elem.second);
51  }
52  return result;
53  }
54 
55  std::vector<MimeType> MimeTypeProvider::GetMimeTypesForFile(const std::string &filePath) const
56  {
57  std::vector<MimeType> result;
58  for (const auto &elem : m_NameToMimeType)
59  {
60  if (elem.second.AppliesTo(filePath))
61  {
62  result.push_back(elem.second);
63  }
64  }
65  std::sort(result.begin(), result.end());
66  std::reverse(result.begin(), result.end());
67  return result;
68  }
69 
70  std::vector<MimeType> MimeTypeProvider::GetMimeTypesForCategory(const std::string &category) const
71  {
72  std::vector<MimeType> result;
73  for (const auto &elem : m_NameToMimeType)
74  {
75  if (elem.second.GetCategory() == category)
76  {
77  result.push_back(elem.second);
78  }
79  }
80  return result;
81  }
82 
83  MimeType MimeTypeProvider::GetMimeTypeForName(const std::string &name) const
84  {
85  std::map<std::string, MimeType>::const_iterator iter = m_NameToMimeType.find(name);
86  if (iter != m_NameToMimeType.end())
87  return iter->second;
88  return MimeType();
89  }
90 
91  std::vector<std::string> MimeTypeProvider::GetCategories() const
92  {
93  std::vector<std::string> result;
94  for (const auto &elem : m_NameToMimeType)
95  {
96  std::string category = elem.second.GetCategory();
97  if (!category.empty())
98  {
99  result.push_back(category);
100  }
101  }
102  std::sort(result.begin(), result.end());
103  result.erase(std::unique(result.begin(), result.end()), result.end());
104  return result;
105  }
106 
107  MimeTypeProvider::TrackedType MimeTypeProvider::AddingService(const ServiceReferenceType &reference)
108  {
109  MimeType result = this->GetMimeType(reference);
110  if (result.IsValid())
111  {
112  std::string name = result.GetName();
113  m_NameToMimeTypes[name].insert(result);
114 
115  // get the highest ranked mime-type
116  m_NameToMimeType[name] = *(m_NameToMimeTypes[name].rbegin());
117  }
118  return result;
119  }
120 
121  void MimeTypeProvider::ModifiedService(const ServiceReferenceType & /*reference*/, TrackedType /*mimetype*/)
122  {
123  // should we track changes in the ranking property?
124  }
125 
126  void MimeTypeProvider::RemovedService(const ServiceReferenceType & /*reference*/, TrackedType mimeType)
127  {
128  std::string name = mimeType.GetName();
129  std::set<MimeType> &mimeTypes = m_NameToMimeTypes[name];
130  mimeTypes.erase(mimeType);
131  if (mimeTypes.empty())
132  {
133  m_NameToMimeTypes.erase(name);
134  m_NameToMimeType.erase(name);
135  }
136  else
137  {
138  // get the highest ranked mime-type
139  m_NameToMimeType[name] = *(mimeTypes.rbegin());
140  }
141  }
142 
143  MimeType MimeTypeProvider::GetMimeType(const ServiceReferenceType &reference) const
144  {
145  MimeType result;
146  if (!reference)
147  return result;
148 
149  CustomMimeType *mimeType = us::GetModuleContext()->GetService(reference);
150  if (mimeType != NULL)
151  {
152  try
153  {
154  int rank = 0;
155  us::Any rankProp = reference.GetProperty(us::ServiceConstants::SERVICE_RANKING());
156  if (!rankProp.Empty())
157  {
158  rank = us::any_cast<int>(rankProp);
159  }
160  long id = us::any_cast<long>(reference.GetProperty(us::ServiceConstants::SERVICE_ID()));
161  result = MimeType(*mimeType, rank, id);
162  }
163  catch (const us::BadAnyCastException &e)
164  {
165  MITK_WARN << "Unexpected exception: " << e.what();
166  }
167  us::GetModuleContext()->UngetService(reference);
168  }
169  return result;
170  }
171 }
US_Core_EXPORT const std::string & SERVICE_RANKING()
virtual const char * what() const override
Definition: usAny.h:352
bool UngetService(const ServiceReferenceBase &reference)
bool IsValid() const
DataCollection - Class to facilitate loading/accessing structured data.
ValueType * any_cast(Any *operand)
Definition: usAny.h:377
void * GetService(const ServiceReferenceBase &reference)
virtual MimeType GetMimeTypeForName(const std::string &name) const override
std::string GetName() const
virtual std::vector< MimeType > GetMimeTypesForFile(const std::string &filePath) const override
#define MITK_WARN
Definition: mitkLogMacros.h:23
Definition: usAny.h:163
virtual std::vector< MimeType > GetMimeTypes() const override
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
Definition: mitkMimeType.h:45
US_Core_EXPORT const std::string & SERVICE_ID()
virtual std::vector< std::string > GetCategories() const override
Get a sorted and unique list of mime-type categories.
static ModuleContext * GetModuleContext()
Returns the module context of the calling module.
virtual std::vector< MimeType > GetMimeTypesForCategory(const std::string &category) const override
bool Empty() const
Definition: usAny.h:246