Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/svl/source/fsstor/fsfactory.cxx
Line
Count
Source
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
21
#include <fsfactory.hxx>
22
#include <com/sun/star/embed/ElementModes.hpp>
23
#include <com/sun/star/io/IOException.hpp>
24
#include <comphelper/processfactory.hxx>
25
#include <cppuhelper/supportsservice.hxx>
26
#include <cppuhelper/weak.hxx>
27
#include <ucbhelper/content.hxx>
28
29
#include <unotools/tempfile.hxx>
30
#include <unotools/ucbhelper.hxx>
31
32
#include "fsstorage.hxx"
33
34
35
using namespace ::com::sun::star;
36
37
38
uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
39
0
{
40
0
    OUString aTempURL = ::utl::CreateTempURL( nullptr, true );
41
0
    if ( aTempURL.isEmpty() )
42
0
        throw uno::RuntimeException(u"Cannot create tempfile."_ustr);
43
44
0
    ::ucbhelper::Content aResultContent(
45
0
        aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
46
0
        comphelper::getProcessComponentContext() );
47
48
0
    return cppu::getXWeak(
49
0
            new FSStorage(  aResultContent,
50
0
                            embed::ElementModes::READWRITE,
51
0
                            m_xContext ) );
52
0
}
53
54
/**
55
 * The request for storage can be done with up to three arguments.
56
 * The first argument specifies a source for the storage it must be URL.
57
 * The second value is a mode the storage should be open in.
58
 * The third value is a media descriptor.
59
 */
60
uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
61
            const uno::Sequence< uno::Any >& aArguments )
62
0
{
63
0
    sal_Int32 nArgNum = aArguments.getLength();
64
0
    OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );
65
66
0
    if ( !nArgNum )
67
0
        return createInstance();
68
69
    // first try to retrieve storage open mode if any
70
    // by default the storage will be open in readonly mode
71
0
    sal_Int32 nStorageMode = embed::ElementModes::READ;
72
0
    if ( nArgNum >= 2 )
73
0
    {
74
0
        if( !( aArguments[1] >>= nStorageMode ) )
75
0
        {
76
0
            throw lang::IllegalArgumentException(
77
0
                (u"second argument to css.embed.FileSystemStorageFactory."
78
0
                 "createInstanceWithArguments must be a"
79
0
                 " css.embed.ElementModes"_ustr),
80
0
                getXWeak(), -1);
81
0
        }
82
        // it's always possible to read written storage in this implementation
83
0
        nStorageMode |= embed::ElementModes::READ;
84
0
    }
85
86
    // retrieve storage source URL
87
0
    OUString aURL;
88
89
0
    if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
90
0
    {
91
0
        throw lang::IllegalArgumentException(
92
0
            (u"first argument to"
93
0
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
94
0
             " must be a (non-empty) URL"_ustr),
95
0
            getXWeak(), -1);
96
0
    }
97
98
    // allow to use other ucp's
99
    // if ( !isLocalNotFile_Impl( aURL ) )
100
0
    if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
101
0
      || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
102
0
      || ::utl::UCBContentHelper::IsDocument( aURL ) )
103
0
    {
104
0
        throw lang::IllegalArgumentException(
105
0
            ("URL \"" + aURL + "\" passed as first argument to"
106
0
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
107
0
             " must be a file URL denoting a directory"),
108
0
            getXWeak(), -1);
109
0
    }
110
111
0
    if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
112
0
        FSStorage::MakeFolderNoUI( aURL );
113
0
    else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
114
0
        throw io::IOException(
115
0
            ("URL \"" + aURL + "\" passed to"
116
0
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
117
0
             " does not denote an existing directory"),
118
0
            getXWeak());
119
120
0
    ::ucbhelper::Content aResultContent(
121
0
        aURL, uno::Reference< ucb::XCommandEnvironment >(),
122
0
        comphelper::getProcessComponentContext() );
123
124
    // create storage based on source
125
0
    return cppu::getXWeak( new FSStorage( aResultContent,
126
0
                                                    nStorageMode,
127
0
                                                    m_xContext ) );
128
0
}
129
130
OUString SAL_CALL FSStorageFactory::getImplementationName()
131
0
{
132
0
    return u"com.sun.star.comp.embed.FileSystemStorageFactory"_ustr;
133
0
}
134
135
sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
136
0
{
137
0
    return cppu::supportsService(this, ServiceName);
138
0
}
139
140
uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
141
0
{
142
0
    return { u"com.sun.star.embed.FileSystemStorageFactory"_ustr,
143
0
                u"com.sun.star.comp.embed.FileSystemStorageFactory"_ustr };
144
0
}
145
146
147
148
extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
149
svl_FSStorageFactory_get_implementation(
150
    css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
151
0
{
152
0
    return cppu::acquire(new FSStorageFactory(context));
153
0
}
154
155
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */