Medical Imaging Interaction Toolkit  2018.4.99-1640525a
Medical Imaging Interaction Toolkit
mitkCustomMimeType.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 "mitkCustomMimeType.h"
14 
15 #include "mitkMimeType.h"
16 
17 #include <algorithm>
18 
19 #include <itksys/SystemTools.hxx>
20 
21 namespace mitk
22 {
23  class FindCaseInsensitive
24  {
25  public:
26  FindCaseInsensitive(std::string value)
27  {
28  lcValue.resize(value.size());
29  std::transform(value.begin(), value.end(), lcValue.begin(), ::tolower);
30  }
31 
32  bool operator()(std::string elem)
33  {
34  std::transform(elem.begin(), elem.end(), elem.begin(), ::tolower);
35  return elem == lcValue;
36  }
37 
38  private:
39  std::string lcValue;
40  };
41 
42  struct CustomMimeType::Impl
43  {
44  std::string m_Name;
45  std::string m_Category;
46  std::vector<std::string> m_Extensions;
47  std::string m_Comment;
48  };
49 
51  CustomMimeType::CustomMimeType() : d(new Impl) {}
52  CustomMimeType::CustomMimeType(const std::string &name) : d(new Impl) { d->m_Name = name; }
53  CustomMimeType::CustomMimeType(const CustomMimeType &other) : d(new Impl(*other.d)) {}
54  CustomMimeType::CustomMimeType(const MimeType &other) : d(new Impl)
55  {
56  d->m_Name = other.GetName();
57  d->m_Category = other.GetCategory();
58  d->m_Extensions = other.GetExtensions();
59  d->m_Comment = other.GetComment();
60  }
61 
63  {
64  CustomMimeType tmp(other);
65  Swap(tmp);
66  return *this;
67  }
68 
70  {
71  CustomMimeType tmp(other);
72  Swap(tmp);
73  return *this;
74  }
75 
76  std::string CustomMimeType::GetName() const { return d->m_Name; }
77  std::string CustomMimeType::GetCategory() const { return d->m_Category; }
78  std::vector<std::string> CustomMimeType::GetExtensions() const { return d->m_Extensions; }
79  std::string CustomMimeType::GetComment() const
80  {
81  if (!d->m_Comment.empty())
82  return d->m_Comment;
83  if (!d->m_Extensions.empty())
84  {
85  return d->m_Extensions.front() + " File";
86  }
87  return "Unknown";
88  }
89 
90  bool CustomMimeType::AppliesTo(const std::string &path) const { return MatchesExtension(path); }
91  bool CustomMimeType::MatchesExtension(const std::string &path) const
92  {
93  std::string extension, filename;
94  return ParsePathForExtension(path, extension, filename);
95  }
96 
97  std::string CustomMimeType::GetExtension(const std::string &path) const
98  {
99  std::string extension, filename;
100  ParsePathForExtension(path, extension, filename);
101  return extension;
102  }
103 
104  std::string CustomMimeType::GetFilenameWithoutExtension(const std::string &path) const
105  {
106  std::string extension, filename;
107  ParsePathForExtension(path, extension, filename);
108  return filename;
109  }
110 
111  bool CustomMimeType::ParsePathForExtension(const std::string &path,
112  std::string &extension,
113  std::string &filename) const
114  {
115  for (std::vector<std::string>::const_iterator iter = d->m_Extensions.begin(), iterEnd = d->m_Extensions.end();
116  iter != iterEnd;
117  ++iter)
118  {
119  if (!iter->empty() && path.size() >= iter->size())
120  {
121  FindCaseInsensitive cmp(*iter);
122  if (cmp(path.substr(path.size() - iter->size())))
123  {
124  extension = "." + *iter;
125  std::string filenameWithExtension = itksys::SystemTools::GetFilenameName(path);
126  filename = filenameWithExtension.substr(0, filenameWithExtension.size() - extension.size());
127  return true;
128  }
129  }
130  }
131  return false;
132  }
133 
134  void CustomMimeType::SetName(const std::string &name) { d->m_Name = name; }
135  void CustomMimeType::SetCategory(const std::string &category) { d->m_Category = category; }
136  void CustomMimeType::SetExtension(const std::string &extension)
137  {
138  d->m_Extensions.clear();
139  d->m_Extensions.push_back(extension);
140  }
141 
142  void CustomMimeType::AddExtension(const std::string &extension)
143  {
144  if (std::find_if(d->m_Extensions.begin(), d->m_Extensions.end(), FindCaseInsensitive(extension)) ==
145  d->m_Extensions.end())
146  {
147  d->m_Extensions.push_back(extension);
148  }
149  }
150 
151  void CustomMimeType::SetComment(const std::string &comment) { d->m_Comment = comment; }
153  {
154  Impl *d1 = d;
155  d = r.d;
156  r.d = d1;
157  }
158 
159  CustomMimeType *CustomMimeType::Clone() const { return new CustomMimeType(*this); }
160  void swap(CustomMimeType &l, CustomMimeType &r) { l.Swap(r); }
161 }
std::string GetComment() const
Returns the Human readable comment of the MimeType, a string that describes its unique role...
std::string GetExtension(const std::string &path) const
Provides the first matching extension.
bool MatchesExtension(const std::string &path) const
Checks if the MimeType can handle the etension of the given path.
CustomMimeType & operator=(const CustomMimeType &other)
std::string GetName() const
Returns the unique name for the MimeType.
void SetExtension(const std::string &extension)
std::string GetCategory() const
Returns the human-readable Category of the mime-type. Allows grouping of similar mime-types (like Sur...
DataCollection - Class to facilitate loading/accessing structured data.
virtual bool AppliesTo(const std::string &path) const
Checks if the MimeType can handle file at the given location.
std::string GetName() const
std::string GetFilenameWithoutExtension(const std::string &path) const
Provides the filename minus the extension.
void SetComment(const std::string &comment)
std::vector< std::string > GetExtensions() const
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
std::vector< std::string > GetExtensions() const
Returns all extensions that this MimeType can handle.
std::string GetCategory() const
virtual CustomMimeType * Clone() const
void swap(CustomMimeType &l, CustomMimeType &r)
void AddExtension(const std::string &extension)
The MimeType class represens a registered mime-type. It is an immutable wrapper for mitk::CustomMimeT...
Definition: mitkMimeType.h:36
void SetCategory(const std::string &category)
void Swap(CustomMimeType &r)
std::string GetComment() const
void SetName(const std::string &name)