summaryrefslogtreecommitdiffstats
path: root/sc/source/ui/vba/vbafiledialog.cxx
blob: f7f31db2a579960172d590dd1871efdf03ee4b94 (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
/* -*- 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 "vbafiledialog.hxx"
#include "vbafiledialogitems.hxx"

#include <osl/file.hxx>

#include <ooo/vba/office/MsoFileDialogType.hpp>

#include <com/sun/star/ui/dialogs/FilePicker.hpp>
#include <com/sun/star/ui/dialogs/FolderPicker.hpp>
#include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
#include <com/sun/star/ui/dialogs/TemplateDescription.hpp>

using namespace ::com::sun::star;
using namespace ::ooo::vba;

ScVbaFileDialog::ScVbaFileDialog( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const sal_Int32 nType )
    : ScVbaFileDialog_BASE( xParent, xContext)
    , m_nType(nType)
    , m_sTitle("FileDialog")
    , m_bMultiSelectMode(false)
{}

uno::Any
ScVbaFileDialog::getInitialFileName() { return uno::Any( m_sInitialFileName ); }

void ScVbaFileDialog::setInitialFileName( const css::uno::Any& rName )
{
    OUString sDefaultPath;

    if( rName >>= sDefaultPath )
    {
        OUString sDefaultURL;
        sal_Int32 eSuccess = osl::FileBase::getFileURLFromSystemPath(
                sDefaultPath, sDefaultURL ) ;
        if( eSuccess == osl::FileBase::RC::E_INVAL )
            m_sInitialFileName = sDefaultPath; // the user may gave it in URL form
        else
            m_sInitialFileName = sDefaultURL;
    }
}

css::uno::Any ScVbaFileDialog::getTitle() { return uno::Any( m_sTitle ); }

void ScVbaFileDialog::setTitle( const css::uno::Any& rTitle )
{
    rTitle >>= m_sTitle;
}

uno::Any ScVbaFileDialog::getAllowMultiSelect()
{
    return uno::Any(m_bMultiSelectMode);
}

void ScVbaFileDialog::setAllowMultiSelect(const uno::Any& rAllowMultiSelect)
{
    rAllowMultiSelect >>= m_bMultiSelectMode;
}

uno::Reference< excel::XFileDialogSelectedItems > SAL_CALL ScVbaFileDialog::getSelectedItems()
{
    // TODO use InitialFileName when m_xItems is empty
    return m_xItems;
}

sal_Int32 ScVbaFileDialog::Show()
{
    std::vector<OUString> sSelectedPaths;
    sal_Int32 nRet = -1;

    switch (m_nType)
    {
        case office::MsoFileDialogType::msoFileDialogOpen:
            // TODO implement
            break;
        case office::MsoFileDialogType::msoFileDialogSaveAs:
            // TODO implement
            break;
        case office::MsoFileDialogType::msoFileDialogFilePicker:
            {
                uno::Reference<ui::dialogs::XFilePicker3> xFilePicker =
                        ui::dialogs::FilePicker::createWithMode(
                                mxContext, ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE );

                if( !m_sInitialFileName.isEmpty() )
                    xFilePicker->setDisplayDirectory( m_sInitialFileName );

                if( xFilePicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
                {
                    nRet = 0; // cancel pressed
                    break;
                }

                const uno::Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
                for( const auto& sURL : aSelectedFiles )
                {
                    OUString sPath;
                    osl::FileBase::getSystemPathFromFileURL(sURL, sPath);

                    sSelectedPaths.push_back(sPath);
                }
            }
            break;
        case office::MsoFileDialogType::msoFileDialogFolderPicker:
            {
                uno::Reference< ui::dialogs::XFolderPicker2 > xFolderPicker =
                        ui::dialogs::FolderPicker::create(mxContext);

                if( !m_sInitialFileName.isEmpty() )
                    xFolderPicker->setDisplayDirectory( m_sInitialFileName );

                if( xFolderPicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
                {
                    nRet = 0; // cancel pressed
                    break;
                }

                OUString sURL = xFolderPicker->getDirectory();

                if(!sURL.isEmpty())
                {
                    OUString sPath;
                    osl::FileBase::getSystemPathFromFileURL(sURL, sPath);

                    sSelectedPaths.push_back(sPath);
                }

            }
            break;
        default:
            throw uno::RuntimeException();
    }

    m_xItems = css::uno::Reference< ov::excel::XFileDialogSelectedItems >(
            new ScVbaFileDialogSelectedItems(this, mxContext, std::move(sSelectedPaths)) );
    return nRet;
}

// XHelperInterface
OUString
ScVbaFileDialog::getServiceImplName()
{
    return "ScVbaFileDialog";
}

uno::Sequence<OUString>
ScVbaFileDialog::getServiceNames()
{
    static uno::Sequence< OUString > const aServiceNames
    {
        "ooo.vba.FileDialog"
    };
    return aServiceNames;
}

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