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>
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
),
40 lFormat(SotClipboardFormatId::NONE
),
45 SfxFilter::SfxFilter( const OUString
&rName
,
46 std::u16string_view rWildCard
,
48 SotClipboardFormatId lFmt
,
49 const OUString
&rTypNm
,
50 const OUString
&rMimeType
,
51 const OUString
&rUsrDat
,
52 const OUString
&rServiceName
,
54 aWildCard(rWildCard
, ';'),
57 aServiceName(rServiceName
),
60 aUIName(maFilterName
),
62 nVersion(SOFFICE_FILEFORMAT_50
),
66 const OUString aExts
= GetWildcard().getGlob();
67 sal_Int32 nLen
{ aExts
.getLength() };
71 // truncate to first empty extension
74 aWildCard
.setGlob(u
"");
77 const sal_Int32 nIdx
{ aExts
.indexOf(";;") };
80 else if (aExts
[nLen
-1]==';')
82 if (nLen
<aExts
.getLength())
83 aWildCard
.setGlob(aExts
.subView(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( ";", "," );
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";
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";
148 SotClipboardFormatId nClipId
= const_cast<SotStorage
&>(rStg
).GetFormat();
149 if ( nClipId
!= SotClipboardFormatId::NONE
)
151 std::shared_ptr
<const SfxFilter
> pFilter
= SfxFilterMatcher().GetFilter4ClipBoardId( nClipId
);
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
);
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
);
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
);
190 return pFilter
->GetTypeName();
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */