1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 
20 #ifdef __sun
21 #include <ctime>
22 #endif
23 
24 #include <string>
25 #include <sot/exchange.hxx>
26 #include <sot/storage.hxx>
27 #include <comphelper/fileformat.h>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/container/XNameAccess.hpp>
30 
31 #include <sfx2/docfac.hxx>
32 #include <sfx2/docfilt.hxx>
33 #include <sfx2/fcontnr.hxx>
34 #include <sfx2/sfxuno.hxx>
35 #include <sfx2/objsh.hxx>
36 
37 using namespace ::com::sun::star;
38 
SfxFilter(const OUString & rProvider,const OUString & rFilterName)39 SfxFilter::SfxFilter( const OUString& rProvider, const OUString &rFilterName ) :
40     maFilterName(rFilterName),
41     maProvider(rProvider),
42     nFormatType(SfxFilterFlags::NONE),
43     nVersion(0),
44     lFormat(SotClipboardFormatId::NONE),
45     mbEnabled(true)
46 {
47 }
48 
SfxFilter(const OUString & rName,const OUString & rWildCard,SfxFilterFlags nType,SotClipboardFormatId lFmt,const OUString & rTypNm,const OUString & rMimeType,const OUString & rUsrDat,const OUString & rServiceName,bool bEnabled)49 SfxFilter::SfxFilter( const OUString &rName,
50                       const OUString &rWildCard,
51                       SfxFilterFlags nType,
52                       SotClipboardFormatId lFmt,
53                       const OUString &rTypNm,
54                       const OUString &rMimeType,
55                       const OUString &rUsrDat,
56                       const OUString &rServiceName,
57                       bool bEnabled ):
58     aWildCard(rWildCard, ';'),
59     aTypeName(rTypNm),
60     aUserData(rUsrDat),
61     aServiceName(rServiceName),
62     aMimeType(rMimeType),
63     maFilterName(rName),
64     aUIName(maFilterName),
65     nFormatType(nType),
66     nVersion(SOFFICE_FILEFORMAT_50),
67     lFormat(lFmt),
68     mbEnabled(bEnabled)
69 {
70     const OUString aExts = GetWildcard().getGlob();
71     sal_Int32 nLen{ aExts.getLength() };
72     if (nLen>0)
73     {
74         // truncate to first empty extension
75         if (aExts[0]==';')
76         {
77             aWildCard.setGlob("");
78             return;
79         }
80         const sal_Int32 nIdx{ aExts.indexOf(";;") };
81         if (nIdx>0)
82             nLen = nIdx;
83         else if (aExts[nLen-1]==';')
84             --nLen;
85         if (nLen<aExts.getLength())
86             aWildCard.setGlob(aExts.copy(0, nLen));
87     }
88 }
89 
~SfxFilter()90 SfxFilter::~SfxFilter()
91 {
92 }
93 
GetDefaultExtension() const94 OUString SfxFilter::GetDefaultExtension() const
95 {
96     return GetWildcard().getGlob().getToken(0, ';');
97 }
98 
99 
GetSuffixes() const100 OUString SfxFilter::GetSuffixes() const
101 {
102     OUString aRet = GetWildcard().getGlob();
103     aRet = aRet.replaceAll( "*.", "" );
104     aRet = aRet.replaceAll( ";", "," );
105     return aRet;
106 }
107 
GetDefaultFilter(const OUString & rName)108 std::shared_ptr<const SfxFilter> SfxFilter::GetDefaultFilter( const OUString& rName )
109 {
110     return SfxFilterContainer::GetDefaultFilter_Impl( rName );
111 }
112 
GetDefaultFilterFromFactory(const OUString & rFact)113 std::shared_ptr<const SfxFilter> SfxFilter::GetDefaultFilterFromFactory( const OUString& rFact )
114 {
115     return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
116 }
117 
GetFilterByName(const OUString & rName)118 std::shared_ptr<const SfxFilter> SfxFilter::GetFilterByName( const OUString& rName )
119 {
120     SfxFilterMatcher aMatch;
121     return aMatch.GetFilter4FilterName( rName, SfxFilterFlags::NONE, SfxFilterFlags::NONE );
122 }
123 
GetTypeFromStorage(const SotStorage & rStg)124 OUString SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
125 {
126     const char* pType=nullptr;
127     if ( rStg.IsStream( "WordDocument" ) )
128     {
129         if ( rStg.IsStream( "0Table" ) || rStg.IsStream( "1Table" ) )
130             pType = "writer_MS_Word_97";
131         else
132             pType = "writer_MS_Word_95";
133     }
134     else if ( rStg.IsStream( "Book" ) )
135     {
136         pType = "calc_MS_Excel_95";
137     }
138     else if ( rStg.IsStream( "Workbook" ) )
139     {
140         pType = "calc_MS_Excel_97";
141     }
142     else if ( rStg.IsStream( "PowerPoint Document" ) )
143     {
144         pType = "impress_MS_PowerPoint_97";
145     }
146     else if ( rStg.IsStream( "Equation Native" ) )
147     {
148         pType = "math_MathType_3x";
149     }
150     else
151     {
152         SotClipboardFormatId nClipId = const_cast<SotStorage&>(rStg).GetFormat();
153         if ( nClipId != SotClipboardFormatId::NONE )
154         {
155             std::shared_ptr<const SfxFilter> pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
156             if ( pFilter )
157                 return pFilter->GetTypeName();
158         }
159     }
160 
161     return pType ? OUString::createFromAscii(pType) : OUString();
162 }
163 
GetTypeFromStorage(const uno::Reference<embed::XStorage> & xStorage)164 OUString SfxFilter::GetTypeFromStorage(
165     const uno::Reference<embed::XStorage>& xStorage )
166 {
167     SfxFilterMatcher aMatcher;
168 
169     css::uno::Reference< css::beans::XPropertySet > xProps( xStorage, css::uno::UNO_QUERY );
170     if ( xProps.is() )
171     {
172         OUString aMediaType;
173         xProps->getPropertyValue("MediaType") >>= aMediaType;
174         if ( !aMediaType.isEmpty() )
175         {
176             css::datatransfer::DataFlavor aDataFlavor;
177             aDataFlavor.MimeType = aMediaType;
178             SotClipboardFormatId nClipId = SotExchange::GetFormat( aDataFlavor );
179             if ( nClipId != SotClipboardFormatId::NONE )
180             {
181                 SfxFilterFlags const nMust = SfxFilterFlags::IMPORT;
182                 // template filters shouldn't be detected if not explicitly asked for
183                 SfxFilterFlags const nDont = SFX_FILTER_NOTINSTALLED | SfxFilterFlags::TEMPLATEPATH;
184 
185                 // get filter from storage MediaType
186                 std::shared_ptr<const SfxFilter> pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
187                 if ( !pFilter )
188                     // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
189                     // or storage *is* a template, but bTemplate is not set
190                     pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
191 
192                 if ( pFilter )
193                 {
194                     return pFilter->GetTypeName();
195                 }
196             }
197         }
198     }
199 
200     return OUString();
201 }
202 
203 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
204