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