Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / doc / docfilt.cxx
blob18f38d422ffe16ae8fe78cbb1998a570c39c870c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifdef __sun
21 #include <ctime>
22 #endif
24 #include <sot/exchange.hxx>
25 #include <sot/storage.hxx>
26 #include <comphelper/fileformat.h>
27 #include <com/sun/star/beans/XPropertySet.hpp>
28 #include <com/sun/star/embed/XStorage.hpp>
29 #include <sfx2/docfilt.hxx>
30 #include <sfx2/fcontnr.hxx>
31 #include <sfx2/objsh.hxx>
33 using namespace ::com::sun::star;
35 SfxFilter::SfxFilter( const OUString& rProvider, const OUString &rFilterName ) :
36 maFilterName(rFilterName),
37 maProvider(rProvider),
38 nFormatType(SfxFilterFlags::NONE),
39 nVersion(0),
40 lFormat(SotClipboardFormatId::NONE),
41 mbEnabled(true)
45 SfxFilter::SfxFilter( const OUString &rName,
46 const OUString &rWildCard,
47 SfxFilterFlags nType,
48 SotClipboardFormatId lFmt,
49 const OUString &rTypNm,
50 const OUString &rMimeType,
51 const OUString &rUsrDat,
52 const OUString &rServiceName,
53 bool bEnabled ):
54 aWildCard(rWildCard, ';'),
55 aTypeName(rTypNm),
56 aUserData(rUsrDat),
57 aServiceName(rServiceName),
58 aMimeType(rMimeType),
59 maFilterName(rName),
60 aUIName(maFilterName),
61 nFormatType(nType),
62 nVersion(SOFFICE_FILEFORMAT_50),
63 lFormat(lFmt),
64 mbEnabled(bEnabled)
66 const OUString aExts = GetWildcard().getGlob();
67 sal_Int32 nLen{ aExts.getLength() };
68 if (nLen<=0)
69 return;
71 // truncate to first empty extension
72 if (aExts[0]==';')
74 aWildCard.setGlob("");
75 return;
77 const sal_Int32 nIdx{ aExts.indexOf(";;") };
78 if (nIdx>0)
79 nLen = nIdx;
80 else if (aExts[nLen-1]==';')
81 --nLen;
82 if (nLen<aExts.getLength())
83 aWildCard.setGlob(aExts.copy(0, nLen));
86 SfxFilter::~SfxFilter()
90 OUString SfxFilter::GetDefaultExtension() const
92 return GetWildcard().getGlob().getToken(0, ';');
96 OUString SfxFilter::GetSuffixes() const
98 OUString aRet = GetWildcard().getGlob();
99 aRet = aRet.replaceAll( "*.", "" );
100 aRet = aRet.replaceAll( ";", "," );
101 return aRet;
104 std::shared_ptr<const SfxFilter> SfxFilter::GetDefaultFilter( const OUString& rName )
106 return SfxFilterContainer::GetDefaultFilter_Impl( rName );
109 std::shared_ptr<const SfxFilter> SfxFilter::GetDefaultFilterFromFactory( const OUString& rFact )
111 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
114 std::shared_ptr<const SfxFilter> SfxFilter::GetFilterByName( const OUString& rName )
116 SfxFilterMatcher aMatch;
117 return aMatch.GetFilter4FilterName( rName, SfxFilterFlags::NONE, SfxFilterFlags::NONE );
120 OUString SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
122 const char* pType=nullptr;
123 if ( rStg.IsStream( "WordDocument" ) )
125 if ( rStg.IsStream( "0Table" ) || rStg.IsStream( "1Table" ) )
126 pType = "writer_MS_Word_97";
127 else
128 pType = "writer_MS_Word_95";
130 else if ( rStg.IsStream( "Book" ) )
132 pType = "calc_MS_Excel_95";
134 else if ( rStg.IsStream( "Workbook" ) )
136 pType = "calc_MS_Excel_97";
138 else if ( rStg.IsStream( "PowerPoint Document" ) )
140 pType = "impress_MS_PowerPoint_97";
142 else if ( rStg.IsStream( "Equation Native" ) )
144 pType = "math_MathType_3x";
146 else
148 SotClipboardFormatId nClipId = const_cast<SotStorage&>(rStg).GetFormat();
149 if ( nClipId != SotClipboardFormatId::NONE )
151 std::shared_ptr<const SfxFilter> pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
152 if ( pFilter )
153 return pFilter->GetTypeName();
157 return pType ? OUString::createFromAscii(pType) : OUString();
160 OUString SfxFilter::GetTypeFromStorage(
161 const uno::Reference<embed::XStorage>& xStorage )
163 SfxFilterMatcher aMatcher;
165 css::uno::Reference< css::beans::XPropertySet > xProps( xStorage, css::uno::UNO_QUERY );
166 if ( xProps.is() )
168 OUString aMediaType;
169 xProps->getPropertyValue("MediaType") >>= aMediaType;
170 if ( !aMediaType.isEmpty() )
172 css::datatransfer::DataFlavor aDataFlavor;
173 aDataFlavor.MimeType = aMediaType;
174 SotClipboardFormatId nClipId = SotExchange::GetFormat( aDataFlavor );
175 if ( nClipId != SotClipboardFormatId::NONE )
177 SfxFilterFlags const nMust = SfxFilterFlags::IMPORT;
178 // template filters shouldn't be detected if not explicitly asked for
179 SfxFilterFlags const nDont = SFX_FILTER_NOTINSTALLED | SfxFilterFlags::TEMPLATEPATH;
181 // get filter from storage MediaType
182 std::shared_ptr<const SfxFilter> pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
183 if ( !pFilter )
184 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
185 // or storage *is* a template, but bTemplate is not set
186 pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
188 if ( pFilter )
190 return pFilter->GetTypeName();
196 return OUString();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */