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