tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sc / source / ui / vba / vbafiledialog.cxx
blob9bef15a2609e5b2fde45caaf3fe91664d02d384c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "vbafiledialog.hxx"
21 #include "vbafiledialogitems.hxx"
23 #include <osl/file.hxx>
25 #include <ooo/vba/office/MsoFileDialogType.hpp>
27 #include <com/sun/star/ui/dialogs/FilePicker.hpp>
28 #include <com/sun/star/ui/dialogs/FolderPicker.hpp>
29 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
30 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
32 using namespace ::com::sun::star;
33 using namespace ::ooo::vba;
35 ScVbaFileDialog::ScVbaFileDialog( const uno::Reference< XHelperInterface >& xParent, const uno::Reference< uno::XComponentContext >& xContext, const sal_Int32 nType )
36 : ScVbaFileDialog_BASE( xParent, xContext)
37 , m_nType(nType)
38 , m_sTitle(u"FileDialog"_ustr)
39 , m_bMultiSelectMode(false)
42 uno::Any
43 ScVbaFileDialog::getInitialFileName() { return uno::Any( m_sInitialFileName ); }
45 void ScVbaFileDialog::setInitialFileName( const css::uno::Any& rName )
47 OUString sDefaultPath;
49 if( rName >>= sDefaultPath )
51 OUString sDefaultURL;
52 sal_Int32 eSuccess = osl::FileBase::getFileURLFromSystemPath(
53 sDefaultPath, sDefaultURL ) ;
54 if( eSuccess == osl::FileBase::RC::E_INVAL )
55 m_sInitialFileName = sDefaultPath; // the user may gave it in URL form
56 else
57 m_sInitialFileName = sDefaultURL;
61 css::uno::Any ScVbaFileDialog::getTitle() { return uno::Any( m_sTitle ); }
63 void ScVbaFileDialog::setTitle( const css::uno::Any& rTitle )
65 rTitle >>= m_sTitle;
68 uno::Any ScVbaFileDialog::getAllowMultiSelect()
70 return uno::Any(m_bMultiSelectMode);
73 void ScVbaFileDialog::setAllowMultiSelect(const uno::Any& rAllowMultiSelect)
75 rAllowMultiSelect >>= m_bMultiSelectMode;
78 uno::Reference< excel::XFileDialogSelectedItems > SAL_CALL ScVbaFileDialog::getSelectedItems()
80 // TODO use InitialFileName when m_xItems is empty
81 return m_xItems;
84 sal_Int32 ScVbaFileDialog::Show()
86 std::vector<OUString> sSelectedPaths;
87 sal_Int32 nRet = -1;
89 switch (m_nType)
91 case office::MsoFileDialogType::msoFileDialogOpen:
92 // TODO implement
93 break;
94 case office::MsoFileDialogType::msoFileDialogSaveAs:
95 // TODO implement
96 break;
97 case office::MsoFileDialogType::msoFileDialogFilePicker:
99 uno::Reference<ui::dialogs::XFilePicker3> xFilePicker =
100 ui::dialogs::FilePicker::createWithMode(
101 mxContext, ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE );
103 if( !m_sInitialFileName.isEmpty() )
104 xFilePicker->setDisplayDirectory( m_sInitialFileName );
106 if( xFilePicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
108 nRet = 0; // cancel pressed
109 break;
112 const uno::Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
113 for( const auto& sURL : aSelectedFiles )
115 OUString sPath;
116 osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
118 sSelectedPaths.push_back(sPath);
121 break;
122 case office::MsoFileDialogType::msoFileDialogFolderPicker:
124 uno::Reference< ui::dialogs::XFolderPicker2 > xFolderPicker =
125 ui::dialogs::FolderPicker::create(mxContext);
127 if( !m_sInitialFileName.isEmpty() )
128 xFolderPicker->setDisplayDirectory( m_sInitialFileName );
130 if( xFolderPicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
132 nRet = 0; // cancel pressed
133 break;
136 OUString sURL = xFolderPicker->getDirectory();
138 if(!sURL.isEmpty())
140 OUString sPath;
141 osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
143 sSelectedPaths.push_back(sPath);
147 break;
148 default:
149 throw uno::RuntimeException();
152 m_xItems = css::uno::Reference< ov::excel::XFileDialogSelectedItems >(
153 new ScVbaFileDialogSelectedItems(this, mxContext, std::move(sSelectedPaths)) );
154 return nRet;
157 // XHelperInterface
158 OUString
159 ScVbaFileDialog::getServiceImplName()
161 return u"ScVbaFileDialog"_ustr;
164 uno::Sequence<OUString>
165 ScVbaFileDialog::getServiceNames()
167 static uno::Sequence< OUString > const aServiceNames
169 u"ooo.vba.FileDialog"_ustr
171 return aServiceNames;
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */