1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22 #include "vbafilesearch.hxx"
23 #include "helperdecl.hxx"
24 #include "vbaapplication.hxx"
25 #include "vbafoundfiles.hxx"
26 #include "helperdecl.hxx"
27 #include <comphelper/processfactory.hxx>
28 #include <tools/urlobj.hxx>
29 #include <tools/wldcrd.hxx>
30 #include <com/sun/star/ucb/XSimpleFileAccess3.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/uno/Sequence.hxx>
33 #include <vector>
34 #include <unotools/viewoptions.hxx>
35 #include <osl/file.hxx>
36
37 using namespace ::ooo::vba;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::ucb;
41 using namespace ::com::sun::star::lang;
42 using namespace comphelper;
43
getFileAccess(void)44 static Reference< XSimpleFileAccess3 > getFileAccess( void )
45 {
46 static Reference< XSimpleFileAccess3 > xSFI;
47 if( !xSFI.is() )
48 {
49 Reference< XMultiServiceFactory > xSMgr = getProcessServiceFactory();
50 if( xSMgr.is() )
51 {
52 xSFI = Reference< XSimpleFileAccess3 >( xSMgr->createInstance
53 ( ::rtl::OUString::createFromAscii( "com.sun.star.ucb.SimpleFileAccess" ) ), UNO_QUERY );
54 }
55 }
56 return xSFI;
57 }
58
ScVbaFileSearch(ScVbaApplication * pApp,const css::uno::Reference<ov::XHelperInterface> & xParent,const css::uno::Reference<css::uno::XComponentContext> & xContext)59 ScVbaFileSearch::ScVbaFileSearch( ScVbaApplication* pApp, const css::uno::Reference< ov::XHelperInterface >& xParent, const css::uno::Reference< css::uno::XComponentContext >& xContext )
60 : ScVbaFileSearchImpl_BASE( xParent, xContext ), m_pApplication( pApp )
61 {
62 NewSearch();
63 }
64
ScVbaFileSearch(const css::uno::Sequence<css::uno::Any> &,const css::uno::Reference<css::uno::XComponentContext> & xContext)65 ScVbaFileSearch::ScVbaFileSearch( const css::uno::Sequence< css::uno::Any >& /*args*/, const css::uno::Reference< css::uno::XComponentContext >& xContext )
66 : ScVbaFileSearchImpl_BASE( xContext ), m_pApplication( NULL )
67 {
68 NewSearch();
69 }
70
~ScVbaFileSearch()71 ScVbaFileSearch::~ScVbaFileSearch()
72 {
73 }
74
getFileName()75 ::rtl::OUString SAL_CALL ScVbaFileSearch::getFileName() throw (css::uno::RuntimeException)
76 {
77 return m_sFileName;
78 }
79
setFileName(const::rtl::OUString & _fileName)80 void SAL_CALL ScVbaFileSearch::setFileName( const ::rtl::OUString& _fileName ) throw (css::uno::RuntimeException)
81 {
82 m_sFileName = _fileName;
83 }
84
getLookIn()85 ::rtl::OUString SAL_CALL ScVbaFileSearch::getLookIn() throw (css::uno::RuntimeException)
86 {
87 return m_sLookIn;
88 }
89
setLookIn(const::rtl::OUString & _lookIn)90 void SAL_CALL ScVbaFileSearch::setLookIn( const ::rtl::OUString& _lookIn ) throw (css::uno::RuntimeException)
91 {
92 m_sLookIn = _lookIn;
93 }
94
getSearchSubFolders()95 sal_Bool SAL_CALL ScVbaFileSearch::getSearchSubFolders() throw (css::uno::RuntimeException)
96 {
97 return m_bSearchSubFolders;
98 }
99
setSearchSubFolders(sal_Bool _searchSubFolders)100 void SAL_CALL ScVbaFileSearch::setSearchSubFolders( sal_Bool _searchSubFolders ) throw (css::uno::RuntimeException)
101 {
102 m_bSearchSubFolders = _searchSubFolders;
103 }
104
getMatchTextExactly()105 sal_Bool SAL_CALL ScVbaFileSearch::getMatchTextExactly() throw (css::uno::RuntimeException)
106 {
107 return m_bMatchTextExactly;
108 }
109
setMatchTextExactly(sal_Bool _matchTextExactly)110 void SAL_CALL ScVbaFileSearch::setMatchTextExactly( sal_Bool _matchTextExactly ) throw (css::uno::RuntimeException)
111 {
112 m_bMatchTextExactly = _matchTextExactly;
113 }
114
IsWildCard(const::rtl::OUString & fileName)115 static bool IsWildCard( const ::rtl::OUString& fileName )
116 {
117 static sal_Char cWild1 = '*';
118 static sal_Char cWild2 = '?';
119
120 return ( ( fileName.indexOf( cWild1 ) >= 0 )
121 || ( fileName.indexOf( cWild2 ) >= 0 ) );
122 }
123
SearchWildCard(const WildCard & wildCard,const::rtl::OUString & aDir,bool bSearchSubFolders,css::uno::Sequence<rtl::OUString> & aSearchedFiles)124 static void SearchWildCard( const WildCard& wildCard, const ::rtl::OUString& aDir, bool bSearchSubFolders, css::uno::Sequence< rtl::OUString >& aSearchedFiles )
125 {
126 Reference< XSimpleFileAccess3 > xSFI = getFileAccess();
127 Sequence< rtl::OUString > aDirSeq;
128 try
129 {
130 if ( xSFI.is() )
131 {
132 aDirSeq = xSFI->getFolderContents( aDir, bSearchSubFolders );
133 }
134 }
135 catch( css::uno::Exception& )
136 {
137 }
138 sal_Int32 nLength = aDirSeq.getLength();
139 for ( sal_Int32 i = 0; i < nLength; i++ )
140 {
141 rtl::OUString aURLStr = aDirSeq[i];
142 if ( xSFI->isFolder( aURLStr ) )
143 {
144 if ( bSearchSubFolders )
145 {
146 SearchWildCard( wildCard, aURLStr, true, aSearchedFiles );
147 }
148 }
149 else
150 {
151 INetURLObject aFileURL( aURLStr );
152 rtl::OUString aFileName = aFileURL.GetLastName( INetURLObject::DECODE_UNAMBIGUOUS );
153 if ( wildCard.Matches( aFileName.toAsciiLowerCase() ) )
154 {
155 sal_Int32 nLength = aSearchedFiles.getLength();
156 aSearchedFiles.realloc( nLength + 1 );
157 rtl::OUString sSystemPath;
158 ::osl::File::getSystemPathFromFileURL( aURLStr, sSystemPath );
159 aSearchedFiles[nLength] = sSystemPath;
160 }
161 }
162 }
163 }
164
SearchExact(const::rtl::OUString & aFileName,const::rtl::OUString & aDir,bool bSearchSubFolders,css::uno::Sequence<rtl::OUString> & aSearchedFiles)165 static void SearchExact( const ::rtl::OUString& aFileName, const ::rtl::OUString& aDir, bool bSearchSubFolders, css::uno::Sequence< rtl::OUString >& aSearchedFiles )
166 {
167 Reference< XSimpleFileAccess3 > xSFI = getFileAccess();
168 Sequence< rtl::OUString > aDirSeq;
169 try
170 {
171 if ( xSFI.is() )
172 {
173 aDirSeq = xSFI->getFolderContents( aDir, bSearchSubFolders );
174 }
175 }
176 catch( css::uno::Exception& )
177 {
178 }
179 sal_Int32 nLength = aDirSeq.getLength();
180 for ( sal_Int32 i = 0; i < nLength; i++ )
181 {
182 rtl::OUString aURLStr = aDirSeq[i];
183 if ( xSFI->isFolder( aURLStr ) )
184 {
185 if ( bSearchSubFolders )
186 {
187 SearchExact( aFileName, aURLStr, true, aSearchedFiles );
188 }
189 }
190 else
191 {
192 INetURLObject aFileURL( aURLStr );
193 rtl::OUString aName = aFileURL.GetLastName( INetURLObject::DECODE_UNAMBIGUOUS );
194 if ( aName.toAsciiLowerCase() == aFileName )
195 {
196 sal_Int32 nLength = aSearchedFiles.getLength();
197 aSearchedFiles.realloc( nLength + 1 );
198 rtl::OUString sSystemPath;
199 ::osl::File::getSystemPathFromFileURL( aURLStr, sSystemPath );
200 aSearchedFiles[nLength] = sSystemPath;
201 }
202 }
203 }
204 }
205
Execute()206 sal_Int32 SAL_CALL ScVbaFileSearch::Execute() throw (css::uno::RuntimeException)
207 {
208 m_aSearchedFiles.realloc(0);
209 Reference< XSimpleFileAccess3 > xSFI = getFileAccess();
210 if ( !xSFI.is() || !xSFI->isFolder( m_sLookIn ) )
211 {
212 return 0;
213 }
214
215 if ( m_sFileName == ::rtl::OUString::createFromAscii( "" ) )
216 {
217 return 0;
218 }
219
220 ::rtl::OUString aTempFileName = m_sFileName.toAsciiLowerCase();
221
222 if ( m_bMatchTextExactly )
223 {
224 // When MatchTextExactly is True, only match files whose name exactly
225 // matches the FileName property (case-insensitive). Wildcards are NOT
226 // honored in this mode — they are treated as literal characters.
227 SearchExact( aTempFileName, m_sLookIn, m_bSearchSubFolders, m_aSearchedFiles );
228 }
229 else
230 {
231 if ( IsWildCard( aTempFileName ) )
232 {
233 bool bEndWithAsterisk = aTempFileName.endsWithAsciiL("*", 1);
234 bool bStartWithAsterisk = (aTempFileName.indexOf(::rtl::OUString::createFromAscii("*")) == 0);
235 if ( !bEndWithAsterisk && !bStartWithAsterisk )
236 {
237 aTempFileName = ::rtl::OUString::createFromAscii("*") + aTempFileName + ::rtl::OUString::createFromAscii("*");
238 }
239 }
240 else
241 {
242 aTempFileName = ::rtl::OUString::createFromAscii("*") + aTempFileName + ::rtl::OUString::createFromAscii("*");
243 }
244 WildCard wildCard( aTempFileName );
245 SearchWildCard( wildCard, m_sLookIn, m_bSearchSubFolders, m_aSearchedFiles );
246 }
247
248 return m_aSearchedFiles.getLength();
249 }
250
251 // 2009-11-5 set ScVbaApplication::getDefaultFilePath() as the InitPath for FileSearch
getInitPath()252 ::rtl::OUString ScVbaFileSearch::getInitPath() throw ( com::sun::star::uno::RuntimeException )
253 {
254 String aPath;
255
256 if (m_pApplication != NULL)
257 {
258 aPath = m_pApplication->getDefaultFilePath();
259 }
260
261 return aPath;
262 }
263
NewSearch()264 void SAL_CALL ScVbaFileSearch::NewSearch() throw (css::uno::RuntimeException)
265 {
266 m_sFileName = ::rtl::OUString::createFromAscii( "" );
267 m_sLookIn = getInitPath();
268 m_bSearchSubFolders = false;
269 m_bMatchTextExactly = false;
270 m_aSearchedFiles.realloc(0);
271 }
272
getFoundFiles()273 Reference< XFoundFiles > SAL_CALL ScVbaFileSearch::getFoundFiles() throw (css::uno::RuntimeException)
274 {
275 VbaFoundFilesEnum* pEnum = new VbaFoundFilesEnum(m_aSearchedFiles);
276 css::uno::Reference< css::container::XIndexAccess > xIndexAccess(
277 static_cast<css::container::XIndexAccess*>(pEnum) );
278 css::uno::Reference< ov::XFoundFiles > xFoundFiles = new VbaFoundFiles(
279 mxParent, mxContext, xIndexAccess );
280 return xFoundFiles;
281 }
282
getServiceImplName()283 rtl::OUString& ScVbaFileSearch::getServiceImplName()
284 {
285 static rtl::OUString sImplName( RTL_CONSTASCII_USTRINGPARAM("VbaFileSearch") );
286 return sImplName;
287 }
288
getServiceNames()289 css::uno::Sequence< rtl::OUString > ScVbaFileSearch::getServiceNames()
290 {
291 static css::uno::Sequence< rtl::OUString > aServiceNames;
292 if ( aServiceNames.getLength() == 0 )
293 {
294 aServiceNames.realloc( 1 );
295 aServiceNames[ 0 ] = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM("ooo.vba.FileSearch") );
296 }
297 return aServiceNames;
298 }
299