summaryrefslogtreecommitdiff
path: root/svl/source/fsstor/fsfactory.cxx
blob: 1efb63ecf8bbfaf40b9b6f3c8ba3e2e60a929676 (plain)
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
/* -*- 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 <fsfactory.hxx>
#include <cppuhelper/factory.hxx>
#include <com/sun/star/ucb/XSimpleFileAccess.hpp>
#include <com/sun/star/embed/ElementModes.hpp>
#include <com/sun/star/io/IOException.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <comphelper/processfactory.hxx>
#include <cppuhelper/supportsservice.hxx>

#include <ucbhelper/content.hxx>

#include <unotools/tempfile.hxx>
#include <unotools/ucbhelper.hxx>

#include "fsstorage.hxx"


using namespace ::com::sun::star;

uno::Sequence< OUString > SAL_CALL FSStorageFactory::impl_staticGetSupportedServiceNames()
{
    uno::Sequence< OUString > aRet(2);
    aRet[0] = "com.sun.star.embed.FileSystemStorageFactory";
    aRet[1] = "com.sun.star.comp.embed.FileSystemStorageFactory";
    return aRet;
}

OUString SAL_CALL FSStorageFactory::impl_staticGetImplementationName()
{
    return OUString("com.sun.star.comp.embed.FileSystemStorageFactory");
}

uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::impl_staticCreateSelfInstance(
            const uno::Reference< lang::XMultiServiceFactory >& xServiceManager )
{
    return uno::Reference< uno::XInterface >( *new FSStorageFactory( comphelper::getComponentContext(xServiceManager) ) );
}

uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstance()
{
    OUString aTempURL;

    aTempURL = ::utl::TempFile( nullptr, true ).GetURL();

    if ( aTempURL.isEmpty() )
        throw uno::RuntimeException(); // TODO: can not create tempfile

    ::ucbhelper::Content aResultContent(
        aTempURL, uno::Reference< ucb::XCommandEnvironment >(),
        comphelper::getProcessComponentContext() );

    return uno::Reference< uno::XInterface >(
        static_cast< OWeakObject* >(
            new FSStorage(  aResultContent,
                            embed::ElementModes::READWRITE,
                            m_xContext ) ),
        uno::UNO_QUERY );
}

/**
 * The request for storage can be done with up to three arguments.
 * The first argument specifies a source for the storage it must be URL.
 * The second value is a mode the storage should be open in.
 * The third value is a media descriptor.
 */
uno::Reference< uno::XInterface > SAL_CALL FSStorageFactory::createInstanceWithArguments(
            const uno::Sequence< uno::Any >& aArguments )
{
    sal_Int32 nArgNum = aArguments.getLength();
    OSL_ENSURE( nArgNum < 4, "Wrong parameter number" );

    if ( !nArgNum )
        return createInstance();

    // first try to retrieve storage open mode if any
    // by default the storage will be open in readonly mode
    sal_Int32 nStorageMode = embed::ElementModes::READ;
    if ( nArgNum >= 2 )
    {
        if( !( aArguments[1] >>= nStorageMode ) )
        {
            throw lang::IllegalArgumentException(
                ("second argument to css.embed.FileSystemStorageFactory."
                 "createInstanceWithArguments must be a"
                 " css.embed.ElementModes"),
                static_cast< OWeakObject * >(this), -1);
        }
        // it's always possible to read written storage in this implementation
        nStorageMode |= embed::ElementModes::READ;
    }

    // retrieve storage source URL
    OUString aURL;

    if ( !( aArguments[0] >>= aURL ) || aURL.isEmpty() )
    {
        throw lang::IllegalArgumentException(
            ("first argument to"
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
             " must be a (non-empty) URL"),
            static_cast< OWeakObject * >(this), -1);
    }

    // allow to use other ucp's
    // if ( !isLocalNotFile_Impl( aURL ) )
    if ( aURL.startsWithIgnoreAsciiCase("vnd.sun.star.pkg:")
      || aURL.startsWithIgnoreAsciiCase("vnd.sun.star.zip:")
      || ::utl::UCBContentHelper::IsDocument( aURL ) )
    {
        throw lang::IllegalArgumentException(
            ("URL \"" + aURL + "\" passed as first argument to"
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
             " must be a file URL denoting a directory"),
            static_cast< OWeakObject * >(this), -1);
    }

    if ( ( nStorageMode & embed::ElementModes::WRITE ) && !( nStorageMode & embed::ElementModes::NOCREATE ) )
        FSStorage::MakeFolderNoUI( aURL );
    else if ( !::utl::UCBContentHelper::IsFolder( aURL ) )
        throw io::IOException(
            ("URL \"" + aURL + "\" passed to"
             " css.embed.FileSystemStorageFactory.createInstanceWithArguments"
             " does not denote an existing directory"),
            static_cast< OWeakObject * >(this));

    ::ucbhelper::Content aResultContent(
        aURL, uno::Reference< ucb::XCommandEnvironment >(),
        comphelper::getProcessComponentContext() );

    // create storage based on source
    return uno::Reference< uno::XInterface >(
        static_cast< OWeakObject* >( new FSStorage( aResultContent,
                                                    nStorageMode,
                                                    m_xContext ) ),
        uno::UNO_QUERY );
}

OUString SAL_CALL FSStorageFactory::getImplementationName()
{
    return impl_staticGetImplementationName();
}

sal_Bool SAL_CALL FSStorageFactory::supportsService( const OUString& ServiceName )
{
    return cppu::supportsService(this, ServiceName);
}

uno::Sequence< OUString > SAL_CALL FSStorageFactory::getSupportedServiceNames()
{
    return impl_staticGetSupportedServiceNames();
}


extern "C"
{
SAL_DLLPUBLIC_EXPORT void * fsstorage_component_getFactory (
    const sal_Char * pImplementationName, void * pServiceManager,
    SAL_UNUSED_PARAMETER void * /* pRegistryKey */)
{
    void * pResult = nullptr;
    if (pServiceManager)
    {
        uno::Reference< lang::XSingleServiceFactory > xFactory;
        if (FSStorageFactory::impl_staticGetImplementationName().equalsAscii(pImplementationName))
        {
            xFactory = cppu::createOneInstanceFactory (
                static_cast< lang::XMultiServiceFactory* >(pServiceManager),
                FSStorageFactory::impl_staticGetImplementationName(),
                FSStorageFactory::impl_staticCreateSelfInstance,
                FSStorageFactory::impl_staticGetSupportedServiceNames() );
        }
        if (xFactory.is())
        {
            xFactory->acquire();
            pResult = xFactory.get();
        }
    }
    return pResult;
}

} // extern "C"

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