Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <com/sun/star/uno/Sequence.h>
21 : #include <com/sun/star/uno/Exception.hpp>
22 : #include <com/sun/star/ucb/UniversalContentBroker.hpp>
23 : #include <com/sun/star/ucb/XContentIdentifier.hpp>
24 : #include <com/sun/star/ucb/XCommandEnvironment.hpp>
25 : #include <com/sun/star/ucb/TransferInfo.hpp>
26 : #include <com/sun/star/ucb/NameClash.hpp>
27 : #include <com/sun/star/sdbc/XResultSet.hpp>
28 : #include <com/sun/star/sdbc/XRow.hpp>
29 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 : #include <comphelper/processfactory.hxx>
31 : #include <comphelper/types.hxx>
32 : #include <tools/urlobj.hxx>
33 : #include <tools/datetime.hxx>
34 : #include <tools/string.hxx>
35 : #include <ucbhelper/contentidentifier.hxx>
36 : #include <ucbhelper/content.hxx>
37 : #include <swunohelper.hxx>
38 :
39 : namespace SWUnoHelper {
40 :
41 656 : sal_Int32 GetEnumAsInt32( const ::com::sun::star::uno::Any& rVal )
42 : {
43 : sal_Int32 eVal;
44 : try
45 : {
46 656 : eVal = comphelper::getEnumAsINT32( rVal );
47 : }
48 0 : catch( ::com::sun::star::uno::Exception & )
49 : {
50 0 : eVal = 0;
51 : OSL_FAIL( "can't get EnumAsInt32" );
52 : }
53 656 : return eVal;
54 : }
55 :
56 :
57 : // methods for UCB actions
58 1 : sal_Bool UCB_DeleteFile( const String& rURL )
59 : {
60 : sal_Bool bRemoved;
61 : try
62 : {
63 : ucbhelper::Content aTempContent( rURL,
64 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
65 1 : comphelper::getProcessComponentContext() );
66 : aTempContent.executeCommand(OUString("delete"),
67 1 : ::com::sun::star::uno::makeAny( sal_True ) );
68 1 : bRemoved = sal_True;
69 : }
70 0 : catch( ::com::sun::star::uno::Exception& )
71 : {
72 0 : bRemoved = sal_False;
73 : OSL_FAIL( "Exeception from executeCommand( delete )" );
74 : }
75 1 : return bRemoved;
76 : }
77 :
78 2 : sal_Bool UCB_CopyFile( const String& rURL, const String& rNewURL, sal_Bool bCopyIsMove )
79 : {
80 2 : sal_Bool bCopyCompleted = sal_True;
81 : try
82 : {
83 2 : INetURLObject aURL( rNewURL );
84 4 : String sName( aURL.GetName() );
85 2 : aURL.removeSegment();
86 4 : String sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
87 :
88 : ucbhelper::Content aTempContent( sMainURL,
89 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
90 4 : comphelper::getProcessComponentContext() );
91 :
92 4 : ::com::sun::star::uno::Any aAny;
93 4 : ::com::sun::star::ucb::TransferInfo aInfo;
94 2 : aInfo.NameClash = ::com::sun::star::ucb::NameClash::ERROR;
95 2 : aInfo.NewTitle = sName;
96 2 : aInfo.SourceURL = rURL;
97 2 : aInfo.MoveData = bCopyIsMove;
98 2 : aAny <<= aInfo;
99 : aTempContent.executeCommand( OUString("transfer"),
100 4 : aAny );
101 : }
102 0 : catch( ::com::sun::star::uno::Exception& )
103 : {
104 : OSL_FAIL( "Exeception from executeCommand( transfer )" );
105 0 : bCopyCompleted = sal_False;
106 : }
107 2 : return bCopyCompleted;
108 : }
109 :
110 0 : sal_Bool UCB_IsCaseSensitiveFileName( const String& rURL )
111 : {
112 : sal_Bool bCaseSensitive;
113 : try
114 : {
115 0 : INetURLObject aTempObj( rURL );
116 0 : aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
117 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef1 = new
118 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
119 :
120 0 : aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
121 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef2 = new
122 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
123 :
124 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XUniversalContentBroker > xUcb =
125 0 : com::sun::star::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());
126 :
127 0 : sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
128 0 : bCaseSensitive = 0 != nCompare;
129 : }
130 0 : catch( ::com::sun::star::uno::Exception& )
131 : {
132 0 : bCaseSensitive = sal_False;
133 : OSL_FAIL( "Exeception from compareContentIds()" );
134 : }
135 0 : return bCaseSensitive;
136 : }
137 :
138 0 : sal_Bool UCB_IsReadOnlyFileName( const String& rURL )
139 : {
140 0 : sal_Bool bIsReadOnly = sal_False;
141 : try
142 : {
143 0 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
144 : ::com::sun::star::uno::Any aAny = aCnt.getPropertyValue(
145 0 : OUString("IsReadOnly"));
146 0 : if(aAny.hasValue())
147 0 : bIsReadOnly = *(sal_Bool*)aAny.getValue();
148 : }
149 0 : catch( ::com::sun::star::uno::Exception& )
150 : {
151 0 : bIsReadOnly = sal_False;
152 : }
153 0 : return bIsReadOnly;
154 : }
155 :
156 0 : sal_Bool UCB_IsFile( const String& rURL )
157 : {
158 0 : sal_Bool bExists = sal_False;
159 : try
160 : {
161 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
162 0 : bExists = aContent.isDocument();
163 : }
164 0 : catch (::com::sun::star::uno::Exception &)
165 : {
166 : }
167 0 : return bExists;
168 : }
169 :
170 0 : sal_Bool UCB_IsDirectory( const String& rURL )
171 : {
172 0 : sal_Bool bExists = sal_False;
173 : try
174 : {
175 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
176 0 : bExists = aContent.isFolder();
177 : }
178 0 : catch (::com::sun::star::uno::Exception &)
179 : {
180 : }
181 0 : return bExists;
182 : }
183 :
184 : // get a list of files from the folder of the URL
185 : // options: pExtension = 0 -> all, else this specific extension
186 : // pDateTime != 0 -> returns also the modified date/time of
187 : // the files in a std::vector<String*> -->
188 : // !! objects must be deleted from the caller!!
189 2 : bool UCB_GetFileListOfFolder( const String& rURL,
190 : std::vector<String*>& rList,
191 : const String* pExtension,
192 : std::vector< ::DateTime* >* pDateTimeList )
193 : {
194 2 : bool bOk = false;
195 : try
196 : {
197 2 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
198 4 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
199 :
200 2 : sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
201 4 : ::com::sun::star::uno::Sequence < OUString > aProps( nSeqSize );
202 2 : OUString* pProps = aProps.getArray();
203 2 : pProps[ 0 ] = OUString("Title");
204 2 : if( pDateTimeList )
205 0 : pProps[ 1 ] = OUString("DateModified");
206 :
207 : try
208 : {
209 2 : xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
210 : }
211 0 : catch( ::com::sun::star::uno::Exception& )
212 : {
213 : OSL_FAIL( "create cursor failed!" );
214 : }
215 :
216 2 : if( xResultSet.is() )
217 : {
218 2 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow > xRow( xResultSet, ::com::sun::star::uno::UNO_QUERY );
219 2 : xub_StrLen nExtLen = pExtension ? pExtension->Len() : 0;
220 : try
221 : {
222 2 : if( xResultSet->first() )
223 : {
224 4 : do {
225 4 : String sTitle( xRow->getString( 1 ) );
226 12 : if( !nExtLen ||
227 8 : ( sTitle.Len() > nExtLen &&
228 : sTitle.Equals( *pExtension,
229 4 : sTitle.Len() - nExtLen, nExtLen )) )
230 : {
231 4 : rList.push_back( new String(sTitle) );
232 :
233 4 : if( pDateTimeList )
234 : {
235 0 : ::com::sun::star::util::DateTime aStamp = xRow->getTimestamp(2);
236 : ::DateTime* pDateTime = new ::DateTime(
237 : ::Date( aStamp.Day,
238 : aStamp.Month,
239 : aStamp.Year ),
240 : ::Time( aStamp.Hours,
241 : aStamp.Minutes,
242 : aStamp.Seconds,
243 0 : aStamp.NanoSeconds ));
244 0 : pDateTimeList->push_back( pDateTime );
245 : }
246 4 : }
247 :
248 4 : } while( xResultSet->next() );
249 : }
250 2 : bOk = true;
251 : }
252 0 : catch( ::com::sun::star::uno::Exception& )
253 : {
254 : OSL_FAIL( "Exception caught!" );
255 2 : }
256 2 : }
257 : }
258 0 : catch( ::com::sun::star::uno::Exception& )
259 : {
260 : OSL_FAIL( "Exception caught!" );
261 0 : bOk = false;
262 : }
263 2 : return bOk;
264 : }
265 :
266 : }
267 :
268 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|