LibreOffice Module comphelper (master)  1
graphicmimetype.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
21 
22 #include <com/sun/star/beans/PropertyValue.hpp>
23 #include <com/sun/star/beans/XPropertySet.hpp>
24 #include <com/sun/star/graphic/GraphicProvider.hpp>
25 #include <com/sun/star/graphic/XGraphicProvider.hpp>
26 #include <com/sun/star/io/XInputStream.hpp>
27 #include <com/sun/star/uno/Reference.hxx>
28 
30 
31 using namespace css;
32 using namespace css::beans;
33 using namespace css::graphic;
34 using namespace css::io;
35 using namespace css::uno;
36 
37 namespace comphelper
38 {
39 OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(const OString& rExt)
40 {
41  struct XMLGraphicMimeTypeMapper
42  {
43  const char* pExt;
44  const char* pMimeType;
45  };
46 
47  static const XMLGraphicMimeTypeMapper aMapper[]
48  = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
49  { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
50  { "wmf", "image/x-wmf" }, { "emf", "image/x-emf" }, { "eps", "image/x-eps" },
51  { "bmp", "image/bmp" }, { "pct", "image/x-pict" }, { "svm", "image/x-svm" } };
52 
53  OUString aMimeType;
54 
55  long const nCount = SAL_N_ELEMENTS(aMapper);
56  for (long i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
57  {
58  if (rExt == aMapper[i].pExt)
59  aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
60  RTL_TEXTENCODING_ASCII_US);
61  }
62 
63  return aMimeType;
64 }
65 
66 OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic)
67 {
68  OUString aSourceMimeType;
69  Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
70  if (xGraphicPropertySet.is() && // it's null if it's an external link
71  (xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
72  {
73  return aSourceMimeType;
74  }
75  return "";
76 }
77 
78 OUString
79 GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream)
80 {
81  // Create the graphic to retrieve the mimetype from it
82  Reference<XGraphicProvider> xProvider
83  = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
84  Sequence<PropertyValue> aMediaProperties(1);
85  aMediaProperties[0].Name = "InputStream";
86  aMediaProperties[0].Value <<= xInputStream;
87  Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
88 
89  return GetMimeTypeForXGraphic(xGraphic);
90 }
91 
92 OUString GraphicMimeTypeHelper::GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
93 {
94  switch (convertDataFormat)
95  {
96  case ConvertDataFormat::BMP:
97  return "image/bmp";
98  case ConvertDataFormat::GIF:
99  return "image/gif";
100  case ConvertDataFormat::JPG:
101  return "image/jpeg";
102  case ConvertDataFormat::PCT:
103  return "image/x-pict";
104  case ConvertDataFormat::PNG:
105  return "image/png";
106  case ConvertDataFormat::SVM:
107  return "image/x-svm";
108  case ConvertDataFormat::TIF:
109  return "image/tiff";
110  case ConvertDataFormat::WMF:
111  return "image/x-wmf";
112  case ConvertDataFormat::EMF:
113  return "image/x-emf";
114  case ConvertDataFormat::SVG:
115  return "image/svg+xml";
116  case ConvertDataFormat::MET: // What is this?
117  case ConvertDataFormat::Unknown:
118  default:
119  return "";
120  }
121 }
122 }
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
#define SAL_N_ELEMENTS(arr)
ConvertDataFormat
int i
const char *const aMimeType[]
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
const char * pExt