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 {}
40 
43 
44 void ScVbaFileDialog::setInitialFileName( const css::uno::Any& rName )
45 {
46  OUString sDefaultPath;
47  OUString sDefaultURL;
48 
49  if( rName >>= sDefaultPath )
50  {
51  sal_Int32 eSuccess = osl::FileBase::getFileURLFromSystemPath(
52  sDefaultPath, sDefaultURL ) ;
53  if( eSuccess == osl::FileBase::RC::E_INVAL )
54  m_sInitialFileName = sDefaultPath; // the user may gave it in URL form
55  else
56  m_sInitialFileName = sDefaultURL;
57  }
58 }
59 
60 css::uno::Any ScVbaFileDialog::getTitle() { return uno::makeAny( m_sTitle ); }
61 
62 void ScVbaFileDialog::setTitle( const css::uno::Any& rTitle )
63 {
64  rTitle >>= m_sTitle;
65 }
66 
67 uno::Reference< excel::XFileDialogSelectedItems > SAL_CALL ScVbaFileDialog::getSelectedItems()
68 {
69  // TODO use InitialFileName when m_xItems is empty
70  return m_xItems;
71 }
72 
74 {
75  std::vector<OUString> sSelectedPaths;
76  sal_Int32 nRet = -1;
77 
78  switch (m_nType)
79  {
80  case office::MsoFileDialogType::msoFileDialogOpen:
81  // TODO implement
82  break;
83  case office::MsoFileDialogType::msoFileDialogSaveAs:
84  // TODO implement
85  break;
86  case office::MsoFileDialogType::msoFileDialogFilePicker:
87  {
88  uno::Reference<ui::dialogs::XFilePicker3> xFilePicker =
89  ui::dialogs::FilePicker::createWithMode(
90  mxContext, ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE );
91 
92  if( !m_sInitialFileName.isEmpty() )
93  xFilePicker->setDisplayDirectory( m_sInitialFileName );
94 
95  if( xFilePicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
96  {
97  nRet = 0; // cancel pressed
98  break;
99  }
100 
101  const uno::Sequence<OUString> aSelectedFiles = xFilePicker->getSelectedFiles();
102  for( const auto& sURL : aSelectedFiles )
103  {
104  OUString sPath;
105  osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
106 
107  sSelectedPaths.push_back(sPath);
108  }
109  }
110  break;
111  case office::MsoFileDialogType::msoFileDialogFolderPicker:
112  {
113  uno::Reference< ui::dialogs::XFolderPicker2 > xFolderPicker =
114  ui::dialogs::FolderPicker::create(mxContext);
115 
116  if( !m_sInitialFileName.isEmpty() )
117  xFolderPicker->setDisplayDirectory( m_sInitialFileName );
118 
119  if( xFolderPicker->execute() != ui::dialogs::ExecutableDialogResults::OK )
120  {
121  nRet = 0; // cancel pressed
122  break;
123  }
124 
125  OUString sURL = xFolderPicker->getDirectory();
126 
127  if(!sURL.isEmpty())
128  {
129  OUString sPath;
130  osl::FileBase::getSystemPathFromFileURL(sURL, sPath);
131 
132  sSelectedPaths.push_back(sPath);
133  }
134 
135  }
136  break;
137  default:
138  throw uno::RuntimeException();
139  }
140 
141  m_xItems = css::uno::Reference< ov::excel::XFileDialogSelectedItems >(
142  new ScVbaFileDialogSelectedItems(this, mxContext, sSelectedPaths) );
143  return nRet;
144 }
145 
146 // XHelperInterface
147 OUString
149 {
150  return "ScVbaFileDialog";
151 }
152 
153 uno::Sequence<OUString>
155 {
156  static uno::Sequence< OUString > const aServiceNames
157  {
158  "ooo.vba.FileDialog"
159  };
160  return aServiceNames;
161 }
162 
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual css::uno::Sequence< OUString > getServiceNames() override
OUString m_sTitle
eSuccess
Sequence< OUString > aServiceNames
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:401
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