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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/* -*- 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 <comphelper/processfactory.hxx>
#include <comphelper/extract.hxx>
#include <o3tl/any.hxx>
#include <tools/urlobj.hxx>
#include <tools/datetime.hxx>
#include <rtl/ustring.hxx>
#include <osl/diagnose.h>
#include <ucbhelper/contentidentifier.hxx>
#include <ucbhelper/content.hxx>
#include <swunohelper.hxx>
#include <svx/xdef.hxx>
#include <svx/xfillit0.hxx>
#include <editeng/memberids.h>
#include <svl/itemset.hxx>

using namespace com::sun::star;

namespace SWUnoHelper
{

sal_Int32 GetEnumAsInt32( const css::uno::Any& rVal )
{
    sal_Int32 nReturn = 0;
    if (! ::cppu::enum2int(nReturn,rVal) )
         OSL_FAIL( "can't get EnumAsInt32" );
    return nReturn;
}

// methods for UCB actions
bool UCB_DeleteFile( const OUString& rURL )
{
    bool bRemoved;
    try
    {
        ucbhelper::Content aTempContent( rURL,
                                css::uno::Reference< css::ucb::XCommandEnvironment >(),
                                comphelper::getProcessComponentContext() );
        aTempContent.executeCommand("delete", css::uno::makeAny( true ) );
        bRemoved = true;
    }
    catch( css::uno::Exception& )
    {
        bRemoved = false;
        OSL_FAIL( "Exception from executeCommand( delete )" );
    }
    return bRemoved;
}

bool UCB_MoveFile( const OUString& rURL, const OUString& rNewURL )
{
    bool bCopyCompleted = true;
    try
    {
        INetURLObject aURL( rNewURL );
        const OUString sName(aURL.GetLastName());
        aURL.removeSegment();
        const OUString sMainURL( aURL.GetMainURL(INetURLObject::DecodeMechanism::NONE) );

        ucbhelper::Content aTempContent( sMainURL,
                                css::uno::Reference< css::ucb::XCommandEnvironment >(),
                                comphelper::getProcessComponentContext() );

        css::ucb::TransferInfo aInfo;
        aInfo.NameClash = css::ucb::NameClash::ERROR;
        aInfo.NewTitle = sName;
        aInfo.SourceURL = rURL;
        aInfo.MoveData = true;
        aTempContent.executeCommand( "transfer", uno::Any(aInfo) );
    }
    catch( css::uno::Exception& )
    {
        OSL_FAIL( "Exception from executeCommand( transfer )" );
        bCopyCompleted = false;
    }
    return bCopyCompleted;
}

bool UCB_IsCaseSensitiveFileName( const OUString& rURL )
{
    bool bCaseSensitive;
    try
    {
        INetURLObject aTempObj( rURL );
        aTempObj.SetBase( aTempObj.GetBase().toAsciiLowerCase() );
        css::uno::Reference< css::ucb::XContentIdentifier > xRef1 = new
                ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ));

        aTempObj.SetBase(aTempObj.GetBase().toAsciiUpperCase());
        css::uno::Reference< css::ucb::XContentIdentifier > xRef2 = new
                ucbhelper::ContentIdentifier( aTempObj.GetMainURL( INetURLObject::DecodeMechanism::NONE ));

        css::uno::Reference< css::ucb::XUniversalContentBroker > xUcb =
              css::ucb::UniversalContentBroker::create(comphelper::getProcessComponentContext());

        sal_Int32 nCompare = xUcb->compareContentIds( xRef1, xRef2 );
        bCaseSensitive = 0 != nCompare;
    }
    catch( css::uno::Exception& )
    {
        bCaseSensitive = false;
        OSL_FAIL( "Exception from compareContentIds()" );
    }
    return bCaseSensitive;
}

bool UCB_IsReadOnlyFileName( const OUString& rURL )
{
    bool bIsReadOnly = false;
    try
    {
        ucbhelper::Content aCnt( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
        css::uno::Any aAny = aCnt.getPropertyValue("IsReadOnly");
        if(aAny.hasValue())
            bIsReadOnly = *o3tl::doAccess<bool>(aAny);
    }
    catch( css::uno::Exception& )
    {
        bIsReadOnly = false;
    }
    return bIsReadOnly;
}

bool UCB_IsFile( const OUString& rURL )
{
    bool bExists = false;
    try
    {
        ::ucbhelper::Content aContent( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
        bExists = aContent.isDocument();
    }
    catch (css::uno::Exception &)
    {
    }
    return bExists;
}

bool UCB_IsDirectory( const OUString& rURL )<--- The function 'UCB_IsDirectory' is never used.
{
    bool bExists = false;
    try
    {
        ::ucbhelper::Content aContent( rURL, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
        bExists = aContent.isFolder();
    }
    catch (css::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, css::uno::Reference< css::ucb::XCommandEnvironment >(), comphelper::getProcessComponentContext() );
        css::uno::Reference< css::sdbc::XResultSet > xResultSet;

        const sal_Int32 nSeqSize = pDateTimeList ? 2 : 1;
        css::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( css::uno::Exception& )
        {
            OSL_FAIL( "create cursor failed!" );
        }

        if( xResultSet.is() )
        {
            css::uno::Reference< css::sdbc::XRow > xRow( xResultSet, css::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 )
                            {
                                css::util::DateTime aStamp = xRow->getTimestamp(2);
                                ::DateTime aDateTime(
                                        ::Date( aStamp.Day,
                                                aStamp.Month,
                                                aStamp.Year ),
                                        ::tools::Time( aStamp.Hours,
                                                aStamp.Minutes,
                                                aStamp.Seconds,
                                                aStamp.NanoSeconds ));
                                pDateTimeList->push_back( aDateTime );
                            }
                        }

                    } while( xResultSet->next() );
                }
                bOk = true;
            }
            catch( css::uno::Exception& )
            {
                OSL_FAIL( "Exception caught!" );
            }
        }
    }
    catch( css::uno::Exception& )
    {
        OSL_FAIL( "Exception caught!" );
        bOk = false;
    }
    return bOk;
}

bool needToMapFillItemsToSvxBrushItemTypes(const SfxItemSet& rSet,
        sal_uInt16 const nMID)
{
    const XFillStyleItem* pXFillStyleItem(rSet.GetItem<XFillStyleItem>(XATTR_FILLSTYLE, false));

    if(!pXFillStyleItem)
    {
        return false;
    }

    // here different FillStyles can be excluded for export; it will depend on the
    // quality these fallbacks can reach. That again is done in getSvxBrushItemFromSourceSet,
    // take a look there how the superset of DrawObject FillStyles is mapped to SvxBrushItem.
    const drawing::FillStyle eFill = pXFillStyleItem->GetValue();
    switch (eFill)
    {
        case drawing::FillStyle_NONE:
            // claim that BackColor and BackTransparent are available so that
            // fo:background="transparent" attribute is exported to override
            // the parent style in case it is != NONE
            switch (nMID)
            {
                case MID_BACK_COLOR:
                case MID_BACK_COLOR_R_G_B:
                case MID_GRAPHIC_TRANSPARENT: // this is *BackTransparent
                    return true;
                default:
                    return false;
            }
            break;
        case drawing::FillStyle_SOLID:
        case drawing::FillStyle_GRADIENT: // gradient and hatch don't exist in
        case drawing::FillStyle_HATCH: // SvxBrushItem so average color is emulated
            switch (nMID)
            {
                case MID_BACK_COLOR:
                case MID_GRAPHIC_TRANSPARENT: // this is *BackTransparent
                    // Gradient/Hatch always have emulated color
                    return (drawing::FillStyle_SOLID != eFill)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLCOLOR)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
                case MID_BACK_COLOR_R_G_B:
                    // Gradient/Hatch always have emulated color
                    return (drawing::FillStyle_SOLID != eFill)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLCOLOR);
                case MID_BACK_COLOR_TRANSPARENCY:
                    return SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
            }
            break;
        case drawing::FillStyle_BITMAP:
            switch (nMID)
            {
                case MID_GRAPHIC:
                    return SfxItemState::SET == rSet.GetItemState(XATTR_FILLBITMAP);
                case MID_GRAPHIC_POSITION:
                    return SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_STRETCH)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_TILE)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLBMP_POS);
                case MID_GRAPHIC_TRANSPARENT:
                case MID_GRAPHIC_TRANSPARENCY:
                    return SfxItemState::SET == rSet.GetItemState(XATTR_FILLTRANSPARENCE)
                        || SfxItemState::SET == rSet.GetItemState(XATTR_FILLFLOATTRANSPARENCE);
            }
            break;
        default:
            assert(false);
    }


    return false;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */