1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5*efeef26fSAndrew Rist * distributed with this work for additional information
6*efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17*efeef26fSAndrew Rist * specific language governing permissions and limitations
18*efeef26fSAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*efeef26fSAndrew Rist *************************************************************/
21*efeef26fSAndrew Rist
22*efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #define _SVSTDARR_STRINGS
28cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.h>
29cdf0e10cSrcweir #include <com/sun/star/uno/Exception.hpp>
30cdf0e10cSrcweir #include <com/sun/star/ucb/XContentIdentifier.hpp>
31cdf0e10cSrcweir #include <com/sun/star/ucb/XContentProvider.hpp>
32cdf0e10cSrcweir #include <com/sun/star/ucb/XCommandEnvironment.hpp>
33cdf0e10cSrcweir #include <com/sun/star/ucb/TransferInfo.hpp>
34cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UCB_NAMECLASH_HDL_
35cdf0e10cSrcweir #include <com/sun/star/ucb/NameClash.hdl>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #include <com/sun/star/sdbc/XResultSet.hpp>
38cdf0e10cSrcweir #include <com/sun/star/sdbc/XRow.hpp>
39cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
40cdf0e10cSrcweir #include <comphelper/processfactory.hxx>
41cdf0e10cSrcweir #include <comphelper/types.hxx>
42cdf0e10cSrcweir #include <tools/urlobj.hxx>
43cdf0e10cSrcweir #include <tools/datetime.hxx>
44cdf0e10cSrcweir #include <tools/debug.hxx>
45cdf0e10cSrcweir #include <ucbhelper/contentidentifier.hxx>
46cdf0e10cSrcweir #include <ucbhelper/contentbroker.hxx>
47cdf0e10cSrcweir #include <ucbhelper/content.hxx>
48cdf0e10cSrcweir #include <svl/svstdarr.hxx>
49cdf0e10cSrcweir #include <swunohelper.hxx>
50cdf0e10cSrcweir #include <swunodef.hxx>
51cdf0e10cSrcweir #include <errhdl.hxx>
52cdf0e10cSrcweir
53cdf0e10cSrcweir namespace SWUnoHelper {
54cdf0e10cSrcweir
GetEnumAsInt32(const UNO_NMSPC::Any & rVal)55cdf0e10cSrcweir sal_Int32 GetEnumAsInt32( const UNO_NMSPC::Any& rVal )
56cdf0e10cSrcweir {
57cdf0e10cSrcweir sal_Int32 eVal;
58cdf0e10cSrcweir try
59cdf0e10cSrcweir {
60cdf0e10cSrcweir eVal = comphelper::getEnumAsINT32( rVal );
61cdf0e10cSrcweir }
62cdf0e10cSrcweir catch( UNO_NMSPC::Exception & )
63cdf0e10cSrcweir {
64cdf0e10cSrcweir eVal = 0;
65cdf0e10cSrcweir ASSERT( sal_False, "can't get EnumAsInt32" );
66cdf0e10cSrcweir }
67cdf0e10cSrcweir return eVal;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir
70cdf0e10cSrcweir
71cdf0e10cSrcweir // methods for UCB actions
UCB_DeleteFile(const String & rURL)72cdf0e10cSrcweir sal_Bool UCB_DeleteFile( const String& rURL )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir sal_Bool bRemoved;
75cdf0e10cSrcweir try
76cdf0e10cSrcweir {
77cdf0e10cSrcweir ucbhelper::Content aTempContent( rURL,
78cdf0e10cSrcweir STAR_REFERENCE( ucb::XCommandEnvironment )());
79cdf0e10cSrcweir aTempContent.executeCommand(
80cdf0e10cSrcweir rtl::OUString::createFromAscii( "delete" ),
81cdf0e10cSrcweir UNO_NMSPC::makeAny( sal_Bool( sal_True ) ) );
82cdf0e10cSrcweir bRemoved = sal_True;
83cdf0e10cSrcweir }
84cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
85cdf0e10cSrcweir {
86cdf0e10cSrcweir bRemoved = sal_False;
87cdf0e10cSrcweir ASSERT( sal_False, "Exeception from executeCommand( delete )" );
88cdf0e10cSrcweir }
89cdf0e10cSrcweir return bRemoved;
90cdf0e10cSrcweir }
91cdf0e10cSrcweir
UCB_CopyFile(const String & rURL,const String & rNewURL,sal_Bool bCopyIsMove)92cdf0e10cSrcweir sal_Bool UCB_CopyFile( const String& rURL, const String& rNewURL, sal_Bool bCopyIsMove )
93cdf0e10cSrcweir {
94cdf0e10cSrcweir sal_Bool bCopyCompleted = sal_True;
95cdf0e10cSrcweir try
96cdf0e10cSrcweir {
97cdf0e10cSrcweir INetURLObject aURL( rNewURL );
98cdf0e10cSrcweir String sName( aURL.GetName() );
99cdf0e10cSrcweir aURL.removeSegment();
100cdf0e10cSrcweir String sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
101cdf0e10cSrcweir
102cdf0e10cSrcweir ucbhelper::Content aTempContent( sMainURL,
103cdf0e10cSrcweir STAR_REFERENCE( ucb::XCommandEnvironment )());
104cdf0e10cSrcweir
105cdf0e10cSrcweir UNO_NMSPC::Any aAny;
106cdf0e10cSrcweir STAR_NMSPC::ucb::TransferInfo aInfo;
107cdf0e10cSrcweir aInfo.NameClash = STAR_NMSPC::ucb::NameClash::ERROR;
108cdf0e10cSrcweir aInfo.NewTitle = sName;
109cdf0e10cSrcweir aInfo.SourceURL = rURL;
110cdf0e10cSrcweir aInfo.MoveData = bCopyIsMove;
111cdf0e10cSrcweir aAny <<= aInfo;
112cdf0e10cSrcweir aTempContent.executeCommand(
113cdf0e10cSrcweir rtl::OUString::createFromAscii( "transfer" ),
114cdf0e10cSrcweir aAny );
115cdf0e10cSrcweir }
116cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
117cdf0e10cSrcweir {
118cdf0e10cSrcweir ASSERT( sal_False, "Exeception from executeCommand( transfer )" );
119cdf0e10cSrcweir bCopyCompleted = sal_False;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir return bCopyCompleted;
122cdf0e10cSrcweir }
123cdf0e10cSrcweir
UCB_IsCaseSensitiveFileName(const String & rURL)124cdf0e10cSrcweir sal_Bool UCB_IsCaseSensitiveFileName( const String& rURL )
125cdf0e10cSrcweir {
126cdf0e10cSrcweir sal_Bool bCaseSensitive;
127cdf0e10cSrcweir try
128cdf0e10cSrcweir {
129cdf0e10cSrcweir STAR_REFERENCE( lang::XMultiServiceFactory ) xMSF =
130cdf0e10cSrcweir comphelper::getProcessServiceFactory();
131cdf0e10cSrcweir
132cdf0e10cSrcweir INetURLObject aTempObj( rURL );
133cdf0e10cSrcweir aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
134cdf0e10cSrcweir STAR_REFERENCE( ucb::XContentIdentifier ) xRef1 = new
135cdf0e10cSrcweir ucbhelper::ContentIdentifier( xMSF,
136cdf0e10cSrcweir aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
137cdf0e10cSrcweir
138cdf0e10cSrcweir aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
139cdf0e10cSrcweir STAR_REFERENCE( ucb::XContentIdentifier ) xRef2 = new
140cdf0e10cSrcweir ucbhelper::ContentIdentifier( xMSF,
141cdf0e10cSrcweir aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
142cdf0e10cSrcweir
143cdf0e10cSrcweir STAR_REFERENCE( ucb::XContentProvider ) xProv =
144cdf0e10cSrcweir ucbhelper::ContentBroker::get()->getContentProviderInterface();
145cdf0e10cSrcweir
146cdf0e10cSrcweir sal_Int32 nCompare = xProv->compareContentIds( xRef1, xRef2 );
147cdf0e10cSrcweir bCaseSensitive = 0 != nCompare;
148cdf0e10cSrcweir }
149cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
150cdf0e10cSrcweir {
151cdf0e10cSrcweir bCaseSensitive = sal_False;
152cdf0e10cSrcweir ASSERT( sal_False, "Exeception from compareContentIds()" );
153cdf0e10cSrcweir }
154cdf0e10cSrcweir return bCaseSensitive;
155cdf0e10cSrcweir }
156cdf0e10cSrcweir
UCB_IsReadOnlyFileName(const String & rURL)157cdf0e10cSrcweir sal_Bool UCB_IsReadOnlyFileName( const String& rURL )
158cdf0e10cSrcweir {
159cdf0e10cSrcweir sal_Bool bIsReadOnly = sal_False;
160cdf0e10cSrcweir try
161cdf0e10cSrcweir {
162cdf0e10cSrcweir ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
163cdf0e10cSrcweir UNO_NMSPC::Any aAny = aCnt.getPropertyValue(
164cdf0e10cSrcweir rtl::OUString::createFromAscii( "IsReadOnly" ));
165cdf0e10cSrcweir if(aAny.hasValue())
166cdf0e10cSrcweir bIsReadOnly = *(sal_Bool*)aAny.getValue();
167cdf0e10cSrcweir }
168cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
169cdf0e10cSrcweir {
170cdf0e10cSrcweir bIsReadOnly = sal_False;
171cdf0e10cSrcweir }
172cdf0e10cSrcweir return bIsReadOnly;
173cdf0e10cSrcweir }
174cdf0e10cSrcweir
UCB_IsFile(const String & rURL)175cdf0e10cSrcweir sal_Bool UCB_IsFile( const String& rURL )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir sal_Bool bExists = sal_False;
178cdf0e10cSrcweir try
179cdf0e10cSrcweir {
180cdf0e10cSrcweir ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
181cdf0e10cSrcweir bExists = aContent.isDocument();
182cdf0e10cSrcweir }
183cdf0e10cSrcweir catch (UNO_NMSPC::Exception &)
184cdf0e10cSrcweir {
185cdf0e10cSrcweir }
186cdf0e10cSrcweir return bExists;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir
UCB_IsDirectory(const String & rURL)189cdf0e10cSrcweir sal_Bool UCB_IsDirectory( const String& rURL )
190cdf0e10cSrcweir {
191cdf0e10cSrcweir sal_Bool bExists = sal_False;
192cdf0e10cSrcweir try
193cdf0e10cSrcweir {
194cdf0e10cSrcweir ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
195cdf0e10cSrcweir bExists = aContent.isFolder();
196cdf0e10cSrcweir }
197cdf0e10cSrcweir catch (UNO_NMSPC::Exception &)
198cdf0e10cSrcweir {
199cdf0e10cSrcweir }
200cdf0e10cSrcweir return bExists;
201cdf0e10cSrcweir }
202cdf0e10cSrcweir
203cdf0e10cSrcweir // get a list of files from the folder of the URL
204cdf0e10cSrcweir // options: pExtension = 0 -> all, else this specific extension
205cdf0e10cSrcweir // pDateTime != 0 -> returns also the modified date/time of
206cdf0e10cSrcweir // the files in a SvPtrarr -->
207cdf0e10cSrcweir // !! objects must be deleted from the caller!!
UCB_GetFileListOfFolder(const String & rURL,SvStrings & rList,const String * pExtension,SvPtrarr * pDateTimeList)208cdf0e10cSrcweir sal_Bool UCB_GetFileListOfFolder( const String& rURL, SvStrings& rList,
209cdf0e10cSrcweir const String* pExtension,
210cdf0e10cSrcweir SvPtrarr* pDateTimeList )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir sal_Bool bOk = sal_False;
213cdf0e10cSrcweir try
214cdf0e10cSrcweir {
215cdf0e10cSrcweir ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
216cdf0e10cSrcweir STAR_REFERENCE( sdbc::XResultSet ) xResultSet;
217cdf0e10cSrcweir
218cdf0e10cSrcweir sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
219cdf0e10cSrcweir UNO_NMSPC::Sequence < rtl::OUString > aProps( nSeqSize );
220cdf0e10cSrcweir rtl::OUString* pProps = aProps.getArray();
221cdf0e10cSrcweir pProps[ 0 ] = rtl::OUString::createFromAscii( "Title" );
222cdf0e10cSrcweir if( pDateTimeList )
223cdf0e10cSrcweir pProps[ 1 ] = rtl::OUString::createFromAscii( "DateModified" );
224cdf0e10cSrcweir
225cdf0e10cSrcweir try
226cdf0e10cSrcweir {
227cdf0e10cSrcweir xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
228cdf0e10cSrcweir }
229cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
230cdf0e10cSrcweir {
231cdf0e10cSrcweir DBG_ERRORFILE( "create cursor failed!" );
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
234cdf0e10cSrcweir if( xResultSet.is() )
235cdf0e10cSrcweir {
236cdf0e10cSrcweir STAR_REFERENCE( sdbc::XRow ) xRow( xResultSet, UNO_NMSPC::UNO_QUERY );
237cdf0e10cSrcweir xub_StrLen nExtLen = pExtension ? pExtension->Len() : 0;
238cdf0e10cSrcweir try
239cdf0e10cSrcweir {
240cdf0e10cSrcweir if( xResultSet->first() )
241cdf0e10cSrcweir {
242cdf0e10cSrcweir do {
243cdf0e10cSrcweir String sTitle( xRow->getString( 1 ) );
244cdf0e10cSrcweir if( !nExtLen ||
245cdf0e10cSrcweir ( sTitle.Len() > nExtLen &&
246cdf0e10cSrcweir sTitle.Equals( *pExtension,
247cdf0e10cSrcweir sTitle.Len() - nExtLen, nExtLen )) )
248cdf0e10cSrcweir {
249cdf0e10cSrcweir String* pStr = new String( sTitle );
250cdf0e10cSrcweir rList.Insert( pStr, rList.Count() );
251cdf0e10cSrcweir
252cdf0e10cSrcweir if( pDateTimeList )
253cdf0e10cSrcweir {
254cdf0e10cSrcweir STAR_NMSPC::util::DateTime aStamp = xRow->getTimestamp(2);
255cdf0e10cSrcweir ::DateTime* pDateTime = new ::DateTime(
256cdf0e10cSrcweir ::Date( aStamp.Day,
257cdf0e10cSrcweir aStamp.Month,
258cdf0e10cSrcweir aStamp.Year ),
259cdf0e10cSrcweir ::Time( aStamp.Hours,
260cdf0e10cSrcweir aStamp.Minutes,
261cdf0e10cSrcweir aStamp.Seconds,
262cdf0e10cSrcweir aStamp.HundredthSeconds ));
263cdf0e10cSrcweir void* p = pDateTime;
264cdf0e10cSrcweir pDateTimeList->Insert( p,
265cdf0e10cSrcweir pDateTimeList->Count() );
266cdf0e10cSrcweir }
267cdf0e10cSrcweir }
268cdf0e10cSrcweir
269cdf0e10cSrcweir } while( xResultSet->next() );
270cdf0e10cSrcweir }
271cdf0e10cSrcweir bOk = sal_True;
272cdf0e10cSrcweir }
273cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
274cdf0e10cSrcweir {
275cdf0e10cSrcweir DBG_ERRORFILE( "Exception caught!" );
276cdf0e10cSrcweir }
277cdf0e10cSrcweir }
278cdf0e10cSrcweir }
279cdf0e10cSrcweir catch( UNO_NMSPC::Exception& )
280cdf0e10cSrcweir {
281cdf0e10cSrcweir DBG_ERRORFILE( "Exception caught!" );
282cdf0e10cSrcweir bOk = sal_False;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir return bOk;
285cdf0e10cSrcweir }
286cdf0e10cSrcweir
287cdf0e10cSrcweir }
288