update dev300-m58
[ooovba.git] / sw / source / core / unocore / swunohelper.cxx
blob0a269e7aa7e56a7657bb09d2b7d1b4ab83af7e49
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: swunohelper.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
34 #define _SVSTDARR_STRINGS
35 #include <com/sun/star/uno/Sequence.h>
36 #include <com/sun/star/uno/Exception.hpp>
37 #include <com/sun/star/ucb/XContentIdentifier.hpp>
38 #include <com/sun/star/ucb/XContentProvider.hpp>
39 #include <com/sun/star/ucb/XCommandEnvironment.hpp>
40 #include <com/sun/star/ucb/TransferInfo.hpp>
41 #ifndef _COM_SUN_STAR_UCB_NAMECLASH_HDL_
42 #include <com/sun/star/ucb/NameClash.hdl>
43 #endif
44 #include <com/sun/star/sdbc/XResultSet.hpp>
45 #include <com/sun/star/sdbc/XRow.hpp>
46 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47 #include <comphelper/processfactory.hxx>
48 #include <comphelper/types.hxx>
49 #include <tools/urlobj.hxx>
50 #include <tools/datetime.hxx>
51 #include <tools/debug.hxx>
52 #include <ucbhelper/contentidentifier.hxx>
53 #include <ucbhelper/contentbroker.hxx>
54 #include <ucbhelper/content.hxx>
55 #include <svtools/svstdarr.hxx>
56 #include <swunohelper.hxx>
57 #include <swunodef.hxx>
58 #include <errhdl.hxx>
60 namespace SWUnoHelper {
62 sal_Int32 GetEnumAsInt32( const UNO_NMSPC::Any& rVal )
64 sal_Int32 eVal;
65 try
67 eVal = comphelper::getEnumAsINT32( rVal );
69 catch( UNO_NMSPC::Exception & )
71 eVal = 0;
72 ASSERT( FALSE, "can't get EnumAsInt32" );
74 return eVal;
78 // methods for UCB actions
79 BOOL UCB_DeleteFile( const String& rURL )
81 BOOL bRemoved;
82 try
84 ucbhelper::Content aTempContent( rURL,
85 STAR_REFERENCE( ucb::XCommandEnvironment )());
86 aTempContent.executeCommand(
87 rtl::OUString::createFromAscii( "delete" ),
88 UNO_NMSPC::makeAny( sal_Bool( sal_True ) ) );
89 bRemoved = TRUE;
91 catch( UNO_NMSPC::Exception& )
93 bRemoved = FALSE;
94 ASSERT( FALSE, "Exeception from executeCommand( delete )" );
96 return bRemoved;
99 BOOL UCB_CopyFile( const String& rURL, const String& rNewURL, BOOL bCopyIsMove )
101 BOOL bCopyCompleted = TRUE;
104 INetURLObject aURL( rNewURL );
105 String sName( aURL.GetName() );
106 aURL.removeSegment();
107 String sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
109 ucbhelper::Content aTempContent( sMainURL,
110 STAR_REFERENCE( ucb::XCommandEnvironment )());
112 UNO_NMSPC::Any aAny;
113 STAR_NMSPC::ucb::TransferInfo aInfo;
114 aInfo.NameClash = STAR_NMSPC::ucb::NameClash::ERROR;
115 aInfo.NewTitle = sName;
116 aInfo.SourceURL = rURL;
117 aInfo.MoveData = bCopyIsMove;
118 aAny <<= aInfo;
119 aTempContent.executeCommand(
120 rtl::OUString::createFromAscii( "transfer" ),
121 aAny );
123 catch( UNO_NMSPC::Exception& )
125 ASSERT( FALSE, "Exeception from executeCommand( transfer )" );
126 bCopyCompleted = FALSE;
128 return bCopyCompleted;
131 BOOL UCB_IsCaseSensitiveFileName( const String& rURL )
133 BOOL bCaseSensitive;
136 STAR_REFERENCE( lang::XMultiServiceFactory ) xMSF =
137 comphelper::getProcessServiceFactory();
139 INetURLObject aTempObj( rURL );
140 aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
141 STAR_REFERENCE( ucb::XContentIdentifier ) xRef1 = new
142 ucbhelper::ContentIdentifier( xMSF,
143 aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
145 aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
146 STAR_REFERENCE( ucb::XContentIdentifier ) xRef2 = new
147 ucbhelper::ContentIdentifier( xMSF,
148 aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
150 STAR_REFERENCE( ucb::XContentProvider ) xProv =
151 ucbhelper::ContentBroker::get()->getContentProviderInterface();
153 sal_Int32 nCompare = xProv->compareContentIds( xRef1, xRef2 );
154 bCaseSensitive = 0 != nCompare;
156 catch( UNO_NMSPC::Exception& )
158 bCaseSensitive = FALSE;
159 ASSERT( FALSE, "Exeception from compareContentIds()" );
161 return bCaseSensitive;
164 BOOL UCB_IsReadOnlyFileName( const String& rURL )
166 BOOL bIsReadOnly = FALSE;
169 ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
170 UNO_NMSPC::Any aAny = aCnt.getPropertyValue(
171 rtl::OUString::createFromAscii( "IsReadOnly" ));
172 if(aAny.hasValue())
173 bIsReadOnly = *(sal_Bool*)aAny.getValue();
175 catch( UNO_NMSPC::Exception& )
177 bIsReadOnly = FALSE;
179 return bIsReadOnly;
182 BOOL UCB_IsFile( const String& rURL )
184 BOOL bExists = FALSE;
187 ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
188 bExists = aContent.isDocument();
190 catch (UNO_NMSPC::Exception &)
193 return bExists;
196 BOOL UCB_IsDirectory( const String& rURL )
198 BOOL bExists = FALSE;
201 ::ucbhelper::Content aContent( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )() );
202 bExists = aContent.isFolder();
204 catch (UNO_NMSPC::Exception &)
207 return bExists;
210 // get a list of files from the folder of the URL
211 // options: pExtension = 0 -> all, else this specific extension
212 // pDateTime != 0 -> returns also the modified date/time of
213 // the files in a SvPtrarr -->
214 // !! objects must be deleted from the caller!!
215 BOOL UCB_GetFileListOfFolder( const String& rURL, SvStrings& rList,
216 const String* pExtension,
217 SvPtrarr* pDateTimeList )
219 BOOL bOk = FALSE;
222 ucbhelper::Content aCnt( rURL, STAR_REFERENCE( ucb::XCommandEnvironment )());
223 STAR_REFERENCE( sdbc::XResultSet ) xResultSet;
225 USHORT nSeqSize = pDateTimeList ? 2 : 1;
226 UNO_NMSPC::Sequence < rtl::OUString > aProps( nSeqSize );
227 rtl::OUString* pProps = aProps.getArray();
228 pProps[ 0 ] = rtl::OUString::createFromAscii( "Title" );
229 if( pDateTimeList )
230 pProps[ 1 ] = rtl::OUString::createFromAscii( "DateModified" );
234 xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
236 catch( UNO_NMSPC::Exception& )
238 DBG_ERRORFILE( "create cursor failed!" );
241 if( xResultSet.is() )
243 STAR_REFERENCE( sdbc::XRow ) xRow( xResultSet, UNO_NMSPC::UNO_QUERY );
244 xub_StrLen nExtLen = pExtension ? pExtension->Len() : 0;
247 if( xResultSet->first() )
249 do {
250 String sTitle( xRow->getString( 1 ) );
251 if( !nExtLen ||
252 ( sTitle.Len() > nExtLen &&
253 sTitle.Equals( *pExtension,
254 sTitle.Len() - nExtLen, nExtLen )) )
256 String* pStr = new String( sTitle );
257 rList.Insert( pStr, rList.Count() );
259 if( pDateTimeList )
261 STAR_NMSPC::util::DateTime aStamp = xRow->getTimestamp(2);
262 ::DateTime* pDateTime = new ::DateTime(
263 ::Date( aStamp.Day,
264 aStamp.Month,
265 aStamp.Year ),
266 ::Time( aStamp.Hours,
267 aStamp.Minutes,
268 aStamp.Seconds,
269 aStamp.HundredthSeconds ));
270 void* p = pDateTime;
271 pDateTimeList->Insert( p,
272 pDateTimeList->Count() );
276 } while( xResultSet->next() );
278 bOk = TRUE;
280 catch( UNO_NMSPC::Exception& )
282 DBG_ERRORFILE( "Exception caught!" );
286 catch( UNO_NMSPC::Exception& )
288 DBG_ERRORFILE( "Exception caught!" );
289 bOk = FALSE;
291 return bOk;