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 347 : sal_Int32 GetEnumAsInt32( const ::com::sun::star::uno::Any& rVal )
42 : {
43 : sal_Int32 eVal;
44 : try
45 : {
46 347 : 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 347 : return eVal;
54 : }
55 :
56 :
57 : // methods for UCB actions
58 0 : 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 0 : comphelper::getProcessComponentContext() );
66 : aTempContent.executeCommand(
67 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("delete")),
68 0 : ::com::sun::star::uno::makeAny( sal_True ) );
69 0 : bRemoved = sal_True;
70 : }
71 0 : catch( ::com::sun::star::uno::Exception& )
72 : {
73 0 : bRemoved = sal_False;
74 : OSL_FAIL( "Exeception from executeCommand( delete )" );
75 : }
76 0 : return bRemoved;
77 : }
78 :
79 0 : sal_Bool UCB_CopyFile( const String& rURL, const String& rNewURL, sal_Bool bCopyIsMove )
80 : {
81 0 : sal_Bool bCopyCompleted = sal_True;
82 : try
83 : {
84 0 : INetURLObject aURL( rNewURL );
85 0 : String sName( aURL.GetName() );
86 0 : aURL.removeSegment();
87 0 : String sMainURL( aURL.GetMainURL(INetURLObject::NO_DECODE) );
88 :
89 : ucbhelper::Content aTempContent( sMainURL,
90 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(),
91 0 : comphelper::getProcessComponentContext() );
92 :
93 0 : ::com::sun::star::uno::Any aAny;
94 0 : ::com::sun::star::ucb::TransferInfo aInfo;
95 0 : aInfo.NameClash = ::com::sun::star::ucb::NameClash::ERROR;
96 0 : aInfo.NewTitle = sName;
97 0 : aInfo.SourceURL = rURL;
98 0 : aInfo.MoveData = bCopyIsMove;
99 0 : aAny <<= aInfo;
100 : aTempContent.executeCommand(
101 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("transfer")),
102 0 : aAny );
103 : }
104 0 : catch( ::com::sun::star::uno::Exception& )
105 : {
106 : OSL_FAIL( "Exeception from executeCommand( transfer )" );
107 0 : bCopyCompleted = sal_False;
108 : }
109 0 : return bCopyCompleted;
110 : }
111 :
112 0 : sal_Bool UCB_IsCaseSensitiveFileName( const String& rURL )
113 : {
114 : sal_Bool bCaseSensitive;
115 : try
116 : {
117 0 : INetURLObject aTempObj( rURL );
118 0 : aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
119 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef1 = new
120 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
121 :
122 0 : aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
123 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XContentIdentifier > xRef2 = new
124 0 : ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::NO_DECODE ));
125 :
126 : ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XUniversalContentBroker > xUcb =
127 0 : com::sun::star::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());
128 :
129 0 : sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
130 0 : bCaseSensitive = 0 != nCompare;
131 : }
132 0 : catch( ::com::sun::star::uno::Exception& )
133 : {
134 0 : bCaseSensitive = sal_False;
135 : OSL_FAIL( "Exeception from compareContentIds()" );
136 : }
137 0 : return bCaseSensitive;
138 : }
139 :
140 0 : sal_Bool UCB_IsReadOnlyFileName( const String& rURL )
141 : {
142 0 : sal_Bool bIsReadOnly = sal_False;
143 : try
144 : {
145 0 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
146 : ::com::sun::star::uno::Any aAny = aCnt.getPropertyValue(
147 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("IsReadOnly")));
148 0 : if(aAny.hasValue())
149 0 : bIsReadOnly = *(sal_Bool*)aAny.getValue();
150 : }
151 0 : catch( ::com::sun::star::uno::Exception& )
152 : {
153 0 : bIsReadOnly = sal_False;
154 : }
155 0 : return bIsReadOnly;
156 : }
157 :
158 0 : sal_Bool UCB_IsFile( const String& rURL )
159 : {
160 0 : sal_Bool bExists = sal_False;
161 : try
162 : {
163 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
164 0 : bExists = aContent.isDocument();
165 : }
166 0 : catch (::com::sun::star::uno::Exception &)
167 : {
168 : }
169 0 : return bExists;
170 : }
171 :
172 0 : sal_Bool UCB_IsDirectory( const String& rURL )
173 : {
174 0 : sal_Bool bExists = sal_False;
175 : try
176 : {
177 0 : ::ucbhelper::Content aContent( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
178 0 : bExists = aContent.isFolder();
179 : }
180 0 : catch (::com::sun::star::uno::Exception &)
181 : {
182 : }
183 0 : return bExists;
184 : }
185 :
186 : // get a list of files from the folder of the URL
187 : // options: pExtension = 0 -> all, else this specific extension
188 : // pDateTime != 0 -> returns also the modified date/time of
189 : // the files in a std::vector<String*> -->
190 : // !! objects must be deleted from the caller!!
191 0 : bool UCB_GetFileListOfFolder( const String& rURL,
192 : std::vector<String*>& rList,
193 : const String* pExtension,
194 : std::vector< ::DateTime* >* pDateTimeList )
195 : {
196 0 : bool bOk = false;
197 : try
198 : {
199 0 : ucbhelper::Content aCnt( rURL, ::com::sun::star::uno::Reference< ::com::sun::star::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
200 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XResultSet > xResultSet;
201 :
202 0 : sal_uInt16 nSeqSize = pDateTimeList ? 2 : 1;
203 0 : ::com::sun::star::uno::Sequence < rtl::OUString > aProps( nSeqSize );
204 0 : rtl::OUString* pProps = aProps.getArray();
205 0 : pProps[ 0 ] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("Title"));
206 0 : if( pDateTimeList )
207 0 : pProps[ 1 ] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("DateModified"));
208 :
209 : try
210 : {
211 0 : xResultSet = aCnt.createCursor( aProps, ::ucbhelper::INCLUDE_DOCUMENTS_ONLY );
212 : }
213 0 : catch( ::com::sun::star::uno::Exception& )
214 : {
215 : OSL_FAIL( "create cursor failed!" );
216 : }
217 :
218 0 : if( xResultSet.is() )
219 : {
220 0 : ::com::sun::star::uno::Reference< ::com::sun::star::sdbc::XRow > xRow( xResultSet, ::com::sun::star::uno::UNO_QUERY );
221 0 : xub_StrLen nExtLen = pExtension ? pExtension->Len() : 0;
222 : try
223 : {
224 0 : if( xResultSet->first() )
225 : {
226 0 : do {
227 0 : String sTitle( xRow->getString( 1 ) );
228 0 : if( !nExtLen ||
229 0 : ( sTitle.Len() > nExtLen &&
230 : sTitle.Equals( *pExtension,
231 0 : sTitle.Len() - nExtLen, nExtLen )) )
232 : {
233 0 : rList.push_back( new String(sTitle) );
234 :
235 0 : if( pDateTimeList )
236 : {
237 0 : ::com::sun::star::util::DateTime aStamp = xRow->getTimestamp(2);
238 : ::DateTime* pDateTime = new ::DateTime(
239 : ::Date( aStamp.Day,
240 : aStamp.Month,
241 : aStamp.Year ),
242 : ::Time( aStamp.Hours,
243 : aStamp.Minutes,
244 : aStamp.Seconds,
245 0 : aStamp.HundredthSeconds ));
246 0 : pDateTimeList->push_back( pDateTime );
247 : }
248 0 : }
249 :
250 0 : } while( xResultSet->next() );
251 : }
252 0 : bOk = true;
253 : }
254 0 : catch( ::com::sun::star::uno::Exception& )
255 : {
256 : OSL_FAIL( "Exception caught!" );
257 0 : }
258 0 : }
259 : }
260 0 : catch( ::com::sun::star::uno::Exception& )
261 : {
262 : OSL_FAIL( "Exception caught!" );
263 0 : bOk = false;
264 : }
265 0 : return bOk;
266 : }
267 :
268 : }
269 :
270 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|