1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
34 using namespace ::com::sun::star
;
36 SfxFilter::SfxFilter( OUString aProvider
, OUString aFilterName
) :
37 maFilterName(std::move(aFilterName
)),
38 maProvider(std::move(aProvider
)),
39 nFormatType(SfxFilterFlags::NONE
),
41 lFormat(SotClipboardFormatId::NONE
),
46 SfxFilter::SfxFilter( OUString aName
,
47 std::u16string_view rWildCard
,
49 SotClipboardFormatId lFmt
,
53 OUString _aServiceName
,
55 aWildCard(rWildCard
, ';'),
56 aTypeName(std::move(aTypNm
)),
57 aUserData(std::move(aUsrDat
)),
58 aServiceName(std::move(_aServiceName
)),
59 aMimeType(std::move(_aMimeType
)),
60 maFilterName(std::move(aName
)),
61 aUIName(maFilterName
),
63 nVersion(SOFFICE_FILEFORMAT_50
),
67 const OUString aExts
= GetWildcard().getGlob();
68 sal_Int32 nLen
{ aExts
.getLength() };
72 // truncate to first empty extension
75 aWildCard
.setGlob(u
"");
78 const sal_Int32 nIdx
{ aExts
.indexOf(";;") };
81 else if (aExts
[nLen
-1]==';')
83 if (nLen
<aExts
.getLength())
84 aWildCard
.setGlob(aExts
.subView(0, nLen
));
87 SfxFilter::~SfxFilter()
91 OUString
SfxFilter::GetDefaultExtension() const
93 return GetWildcard().getGlob().getToken(0, ';');
97 OUString
SfxFilter::GetSuffixes() const
99 OUString aRet
= GetWildcard().getGlob();
100 aRet
= aRet
.replaceAll( "*.", "" );
101 aRet
= aRet
.replaceAll( ";", "," );
105 std::shared_ptr
<const SfxFilter
> SfxFilter::GetDefaultFilter( std::u16string_view rName
)
107 return SfxFilterContainer::GetDefaultFilter_Impl( rName
);
110 std::shared_ptr
<const SfxFilter
> SfxFilter::GetDefaultFilterFromFactory( const OUString
& rFact
)
112 return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact
) );
115 std::shared_ptr
<const SfxFilter
> SfxFilter::GetFilterByName( const OUString
& rName
)
117 SfxFilterMatcher aMatch
;
118 return aMatch
.GetFilter4FilterName( rName
, SfxFilterFlags::NONE
, SfxFilterFlags::NONE
);
121 OUString
SfxFilter::GetTypeFromStorage( const SotStorage
& rStg
)
123 const char* pType
=nullptr;
124 if ( rStg
.IsStream( u
"WordDocument"_ustr
) )
126 if ( rStg
.IsStream( u
"0Table"_ustr
) || rStg
.IsStream( u
"1Table"_ustr
) )
127 pType
= "writer_MS_Word_97";
129 pType
= "writer_MS_Word_95";
131 else if ( rStg
.IsStream( u
"Book"_ustr
) )
133 pType
= "calc_MS_Excel_95";
135 else if ( rStg
.IsStream( u
"Workbook"_ustr
) )
137 pType
= "calc_MS_Excel_97";
139 else if ( rStg
.IsStream( u
"PowerPoint Document"_ustr
) )
141 pType
= "impress_MS_PowerPoint_97";
143 else if ( rStg
.IsStream( u
"Equation Native"_ustr
) )
145 pType
= "math_MathType_3x";
149 SotClipboardFormatId nClipId
= const_cast<SotStorage
&>(rStg
).GetFormat();
150 if ( nClipId
!= SotClipboardFormatId::NONE
)
152 std::shared_ptr
<const SfxFilter
> pFilter
= SfxFilterMatcher().GetFilter4ClipBoardId( nClipId
);
154 return pFilter
->GetTypeName();
158 return pType
? OUString::createFromAscii(pType
) : OUString();
161 OUString
SfxFilter::GetTypeFromStorage(
162 const uno::Reference
<embed::XStorage
>& xStorage
)
164 SfxFilterMatcher aMatcher
;
166 css::uno::Reference
< css::beans::XPropertySet
> xProps( xStorage
, css::uno::UNO_QUERY
);
170 xProps
->getPropertyValue(u
"MediaType"_ustr
) >>= aMediaType
;
171 if ( !aMediaType
.isEmpty() )
173 css::datatransfer::DataFlavor aDataFlavor
;
174 aDataFlavor
.MimeType
= aMediaType
;
175 SotClipboardFormatId nClipId
= SotExchange::GetFormat( aDataFlavor
);
176 if ( nClipId
!= SotClipboardFormatId::NONE
)
178 SfxFilterFlags
const nMust
= SfxFilterFlags::IMPORT
;
179 // template filters shouldn't be detected if not explicitly asked for
180 SfxFilterFlags
const nDont
= SFX_FILTER_NOTINSTALLED
| SfxFilterFlags::TEMPLATEPATH
;
182 // get filter from storage MediaType
183 std::shared_ptr
<const SfxFilter
> pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
, nMust
, nDont
);
185 // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
186 // or storage *is* a template, but bTemplate is not set
187 pFilter
= aMatcher
.GetFilter4ClipBoardId( nClipId
);
191 return pFilter
->GetTypeName();
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */