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