Line data Source code
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 SOLARIS
21 : #include <ctime>
22 : #endif
23 :
24 : #include <string>
25 : #include <sot/exchange.hxx>
26 : #include <comphelper/processfactory.hxx>
27 : #include <comphelper/string.hxx>
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 "fltfnc.hxx"
34 : #include <sfx2/sfxuno.hxx>
35 : #include <sfx2/objsh.hxx>
36 :
37 : using namespace ::com::sun::star;
38 :
39 : // STATIC DATA -----------------------------------------------------------
40 :
41 : DBG_NAME(SfxFilter)
42 :
43 870 : SfxFilter::SfxFilter( const String &rName,
44 : const String &rWildCard,
45 : SfxFilterFlags nType,
46 : sal_uInt32 lFmt,
47 : const String &rTypNm,
48 : sal_uInt16 nIcon,
49 : const String &rMimeType,
50 : const String &rUsrDat,
51 : const String &rServiceName ):
52 : aWildCard(rWildCard, ';'),
53 : lFormat(lFmt),
54 : aTypeName(rTypNm),
55 : aUserData(rUsrDat),
56 : nFormatType(nType),
57 : nDocIcon(nIcon),
58 : aServiceName( rServiceName ),
59 : aMimeType( rMimeType ),
60 870 : aFilterName( rName )
61 : {
62 870 : String aExts = GetWildcard().getGlob();
63 870 : String aShort, aLong;
64 870 : String aRet;
65 870 : sal_uInt16 nMaxLength = USHRT_MAX;
66 870 : String aTest;
67 870 : sal_uInt16 nPos = 0;
68 2798 : while( ( aRet = aExts.GetToken( nPos++, ';' ) ).Len() )
69 : {
70 1058 : aTest = aRet;
71 1058 : aTest.SearchAndReplace( DEFINE_CONST_UNICODE( "*." ), String() );
72 1058 : if( aTest.Len() <= nMaxLength )
73 : {
74 1058 : if( aShort.Len() ) aShort += ';';
75 1058 : aShort += aRet;
76 : }
77 : else
78 : {
79 0 : if( aLong.Len() ) aLong += ';';
80 0 : aLong += aRet;
81 : }
82 : }
83 870 : if( aShort.Len() && aLong.Len() )
84 : {
85 0 : aShort += ';';
86 0 : aShort += aLong;
87 : }
88 870 : aWildCard.setGlob(aShort);
89 :
90 870 : nVersion = SOFFICE_FILEFORMAT_50;
91 870 : aUIName = aFilterName;
92 870 : }
93 :
94 8 : SfxFilter::~SfxFilter()
95 : {
96 8 : }
97 :
98 0 : String SfxFilter::GetDefaultExtension() const
99 : {
100 0 : return comphelper::string::getToken(GetWildcard().getGlob(), 0, ';');
101 : }
102 :
103 0 : String SfxFilter::GetSuffixes() const
104 : {
105 0 : String aRet = GetWildcard().getGlob();
106 0 : while( aRet.SearchAndReplaceAscii( "*.", String() ) != STRING_NOTFOUND ) ;
107 0 : while( aRet.SearchAndReplace( ';', ',' ) != STRING_NOTFOUND ) ;
108 0 : return aRet;
109 : }
110 :
111 0 : const SfxFilter* SfxFilter::GetDefaultFilter( const String& rName )
112 : {
113 0 : return SfxFilterContainer::GetDefaultFilter_Impl( rName );
114 : }
115 :
116 0 : const SfxFilter* SfxFilter::GetDefaultFilterFromFactory( const String& rFact )
117 : {
118 0 : return GetDefaultFilter( SfxObjectShell::GetServiceNameFromFactory( rFact ) );
119 : }
120 :
121 2009 : const SfxFilter* SfxFilter::GetFilterByName( const String& rName )
122 : {
123 2009 : SfxFilterMatcher aMatch;
124 2009 : return aMatch.GetFilter4FilterName( rName, 0, 0 );
125 : }
126 :
127 0 : String SfxFilter::GetTypeFromStorage( const SotStorage& rStg )
128 : {
129 0 : const char* pType=0;
130 0 : if ( rStg.IsStream( rtl::OUString("WordDocument") ) )
131 : {
132 0 : if ( rStg.IsStream( rtl::OUString("0Table") ) || rStg.IsStream( rtl::OUString("1Table") ) )
133 0 : pType = "writer_MS_Word_97";
134 : else
135 0 : pType = "writer_MS_Word_95";
136 : }
137 0 : else if ( rStg.IsStream( rtl::OUString("Book") ) )
138 : {
139 0 : pType = "calc_MS_Excel_95";
140 : }
141 0 : else if ( rStg.IsStream( rtl::OUString("Workbook" ) ) )
142 : {
143 0 : pType = "calc_MS_Excel_97";
144 : }
145 0 : else if ( rStg.IsStream( rtl::OUString("PowerPoint Document") ) )
146 : {
147 0 : pType = "impress_MS_PowerPoint_97";
148 : }
149 0 : else if ( rStg.IsStream( rtl::OUString("Equation Native") ) )
150 : {
151 0 : pType = "math_MathType_3x";
152 : }
153 : else
154 : {
155 0 : sal_Int32 nClipId = ((SotStorage&)rStg).GetFormat();
156 0 : if ( nClipId )
157 : {
158 0 : const SfxFilter* pFilter = SfxFilterMatcher().GetFilter4ClipBoardId( nClipId );
159 0 : if ( pFilter )
160 0 : return pFilter->GetTypeName();
161 : }
162 : }
163 :
164 0 : return pType ? rtl::OUString::createFromAscii(pType) : rtl::OUString();
165 : }
166 :
167 589 : String SfxFilter::GetTypeFromStorage( const com::sun::star::uno::Reference< com::sun::star::embed::XStorage >& xStorage, sal_Bool bTemplate,
168 : String* pFilterName )
169 : throw ( beans::UnknownPropertyException,
170 : lang::WrappedTargetException,
171 : uno::RuntimeException )
172 : {
173 589 : SfxFilterMatcher aMatcher;
174 589 : const char* pType=0;
175 589 : String aName;
176 589 : if ( pFilterName )
177 : {
178 17 : aName = *pFilterName;
179 17 : pFilterName->Erase();
180 : }
181 :
182 589 : com::sun::star::uno::Reference< com::sun::star::beans::XPropertySet > xProps( xStorage, com::sun::star::uno::UNO_QUERY );
183 589 : if ( xProps.is() )
184 : {
185 589 : ::rtl::OUString aMediaType;
186 589 : xProps->getPropertyValue( ::rtl::OUString("MediaType") ) >>= aMediaType;
187 589 : if ( !aMediaType.isEmpty() )
188 : {
189 589 : ::com::sun::star::datatransfer::DataFlavor aDataFlavor;
190 589 : aDataFlavor.MimeType = aMediaType;
191 589 : sal_uInt32 nClipId = SotExchange::GetFormat( aDataFlavor );
192 589 : if ( nClipId )
193 : {
194 589 : SfxFilterFlags nMust = SFX_FILTER_IMPORT, nDont = SFX_FILTER_NOTINSTALLED;
195 589 : if ( bTemplate )
196 : // template filter was preselected, try to verify
197 0 : nMust |= SFX_FILTER_TEMPLATEPATH;
198 : else
199 : // template filters shouldn't be detected if not explicitly asked for
200 589 : nDont |= SFX_FILTER_TEMPLATEPATH;
201 :
202 589 : const SfxFilter* pFilter = 0;
203 589 : if ( aName.Len() )
204 : // get preselected Filter if it matches the desired filter flags
205 15 : pFilter = aMatcher.GetFilter4FilterName( aName, nMust, nDont );
206 :
207 589 : if ( !pFilter || pFilter->GetFormat() != nClipId )
208 : {
209 : // get filter from storage MediaType
210 575 : pFilter = aMatcher.GetFilter4ClipBoardId( nClipId, nMust, nDont );
211 575 : if ( !pFilter )
212 : // template filter is asked for , but there isn't one; so at least the "normal" format should be detected
213 : // or storage *is* a template, but bTemplate is not set
214 1 : pFilter = aMatcher.GetFilter4ClipBoardId( nClipId );
215 : }
216 :
217 589 : if ( pFilter )
218 : {
219 589 : if ( pFilterName )
220 17 : *pFilterName = pFilter->GetName();
221 589 : return pFilter->GetTypeName();
222 : }
223 589 : }
224 589 : }
225 : }
226 :
227 : //TODO: do it without SfxFilter
228 : //TODO/LATER: don't yield FilterName, should be done in FWK!
229 0 : String aRet;
230 0 : if ( pType )
231 : {
232 0 : aRet = rtl::OUString::createFromAscii(pType);
233 0 : if ( pFilterName )
234 0 : *pFilterName = aMatcher.GetFilter4EA( aRet )->GetName();
235 : }
236 :
237 0 : return aRet;
238 : }
239 :
240 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|