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