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