LibreOffice Module sc (master)  1
vbafiledialog.cxx
Go to the documentation of this file.
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 #include "vbafiledialog.hxx"
21 #include "vbafiledialogitems.hxx"
22 
23 #include <osl/file.hxx>
24 
25 #include <ooo/vba/office/MsoFileDialogType.hpp>
26 
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>
31 
32 using namespace ::com::sun::star;
33 using namespace ::ooo::vba;
34 
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("FileDialog")
39  , m_bMultiSelectMode(false)
40 {}
41 
44 
45 void ScVbaFileDialog::setInitialFileName( const css::uno::Any& rName )
46 {
47  OUString sDefaultPath;
48 
49  if( rName >>= sDefaultPath )
50  {
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;
58  }
59 }
60 
61 css::uno::Any ScVbaFileDialog::getTitle() { return uno::Any( m_sTitle ); }
62 
63 void ScVbaFileDialog::setTitle( const css::uno::Any& rTitle )
64 {
65  rTitle >>= m_sTitle;
66 }
67 
69 {
71 }
72 
73 void ScVbaFileDialog::setAllowMultiSelect(const uno::Any& rAllowMultiSelect)
74 {
75  rAllowMultiSelect >>= m_bMultiSelectMode;
76 }
77 
78 uno::Reference< excel::XFileDialogSelectedItems > SAL_CALL ScVbaFileDialog::getSelectedItems()
79 {
80  // TODO use InitialFileName when m_xItems is empty
81  return m_xItems;
82 }
83 
85 {
86  std::vector<OUString> sSelectedPaths;
87  sal_Int32 nRet = -1;
88 
89  switch (m_nType)
90  {
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:
98  {
99  uno::Reference<ui::dialogs::XFilePicker3> xFilePicker =
100  ui::dialogs::FilePicker::createWithMode(
101  mxContext, ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE );
102 
103  if( !m_sInitialFileName.isEmpty() )
104  xFilePicker->setDisplayDirectory( m_sInitialFileName );
105 
106  if( xFilePicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
107  {
108  nRet = 0; // cancel pressed
109  break;
110  }
111 
112  const uno::Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
113  for( const auto& sURL : aSelectedFiles )
114  {
115  OUString sPath;
116  osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
117 
118  sSelectedPaths.push_back(sPath);
119  }
120  }
121  break;
122  case office::MsoFileDialogType::msoFileDialogFolderPicker:
123  {
124  uno::Reference< ui::dialogs::XFolderPicker2 > xFolderPicker =
125  ui::dialogs::FolderPicker::create(mxContext);
126 
127  if( !m_sInitialFileName.isEmpty() )
128  xFolderPicker->setDisplayDirectory( m_sInitialFileName );
129 
130  if( xFolderPicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
131  {
132  nRet = 0; // cancel pressed
133  break;
134  }
135 
136  OUString sURL = xFolderPicker->getDirectory();
137 
138  if(!sURL.isEmpty())
139  {
140  OUString sPath;
141  osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
142 
143  sSelectedPaths.push_back(sPath);
144  }
145 
146  }
147  break;
148  default:
149  throw uno::RuntimeException();
150  }
151 
152  m_xItems = css::uno::Reference< ov::excel::XFileDialogSelectedItems >(
153  new ScVbaFileDialogSelectedItems(this, mxContext, std::move(sSelectedPaths)) );
154  return nRet;
155 }
156 
157 // XHelperInterface
158 OUString
160 {
161  return "ScVbaFileDialog";
162 }
163 
164 uno::Sequence<OUString>
166 {
167  static uno::Sequence< OUString > const aServiceNames
168  {
169  "ooo.vba.FileDialog"
170  };
171  return aServiceNames;
172 }
173 
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > getServiceNames() override
OUString m_sTitle
eSuccess
virtual css::uno::Any SAL_CALL getAllowMultiSelect() override
Sequence< OUString > aServiceNames
virtual void SAL_CALL setAllowMultiSelect(const css::uno::Any &rAllowMultiSelect) override
ScVbaFileDialog(const css::uno::Reference< ov::XHelperInterface > &xParent, const css::uno::Reference< css::uno::XComponentContext > &xContext, const sal_Int32 nType)
OUString m_sInitialFileName
virtual void SAL_CALL setTitle(const css::uno::Any &rTitle) override
virtual sal_Int32 SAL_CALL Show() override
virtual OUString getServiceImplName() override
css::uno::Reference< css::uno::XComponentContext > mxContext
virtual void SAL_CALL setInitialFileName(const css::uno::Any &rName) override
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
virtual css::uno::Any SAL_CALL getInitialFileName() override
virtual css::uno::Reference< ov::excel::XFileDialogSelectedItems > SAL_CALL getSelectedItems() override
virtual css::uno::Any SAL_CALL getTitle() override
css::uno::Reference< ov::excel::XFileDialogSelectedItems > m_xItems
bool m_bDetectedRangeSegmentation false