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