LibreOffice Module sdext (master)  1
fileopendialog.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 
21 #include "fileopendialog.hxx"
22 #include <sal/types.h>
23 #include "pppoptimizertoken.hxx"
24 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
25 #include <com/sun/star/ui/dialogs/FilePicker.hpp>
26 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
27 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
28 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
29 #include <com/sun/star/beans/PropertyValue.hpp>
30 #include <com/sun/star/container/XEnumeration.hpp>
31 #include <com/sun/star/container/XNameAccess.hpp>
32 #include <com/sun/star/view/XControlAccess.hpp>
33 
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::lang;
36 using namespace ::com::sun::star::beans;
37 using namespace ::com::sun::star::container;
38 using namespace ::com::sun::star::view;
39 using namespace ::com::sun::star::ui::dialogs;
40 
42 {
43  mxFilePicker = FilePicker::createWithMode( rxContext, TemplateDescription::FILESAVE_AUTOEXTENSION);
44  mxFilePicker->setMultiSelectionMode( false );
45 
46  Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
47  if ( xAccess.is() )
48  {
49  try
50  {
51  xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, Any( true ) );
52  }
53  catch( css::uno::Exception& )
54  {}
55  }
56 
57  // collecting a list of impress filters
58  Reference< XNameAccess > xFilters( rxContext->getServiceManager()->createInstanceWithContext(
59  "com.sun.star.document.FilterFactory", rxContext ), UNO_QUERY_THROW );
60  const Sequence< OUString > aFilterList( xFilters->getElementNames() );
61  for ( const auto& rFilter : aFilterList )
62  {
63  try
64  {
65  Sequence< PropertyValue > aFilterProperties;
66  if ( xFilters->getByName( rFilter ) >>= aFilterProperties )
67  {
68  FilterEntry aFilterEntry;
69  bool bImpressFilter = false;
70  for ( const PropertyValue& rProperty : std::as_const(aFilterProperties) )
71  {
72  bool bStop = false;
73  switch( TKGet( rProperty.Name ) )
74  {
75  case TK_DocumentService :
76  {
77  OUString sDocumentService;
78  rProperty.Value >>= sDocumentService;
79  if ( sDocumentService == "com.sun.star.presentation.PresentationDocument" )
80  bImpressFilter = true;
81  else
82  bStop = true;
83  }
84  break;
85  case TK_Name : rProperty.Value >>= aFilterEntry.maFilterEntryName; break;
86  case TK_UIName : rProperty.Value >>= aFilterEntry.maUIName; break;
87  case TK_Type : rProperty.Value >>= aFilterEntry.maType; break;
88  case TK_Flags : rProperty.Value >>= aFilterEntry.maFlags; break;
89  default : break;
90  }
91 
92  if (bStop)
93  break;
94  }
95  if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
96  {
97  aFilterEntryList.push_back( aFilterEntry );
98  }
99  }
100  }
101  catch( Exception& )
102  {
103  }
104  }
105 
106  Reference< XNameAccess > xTypes( rxContext->getServiceManager()->createInstanceWithContext(
107  "com.sun.star.document.TypeDetection", rxContext ), UNO_QUERY_THROW );
108 
109  for( const auto& rFilterEntry : aFilterEntryList )
110  {
111  Sequence< PropertyValue > aTypeProperties;
112  try
113  {
114  if ( xTypes->getByName( rFilterEntry.maType ) >>= aTypeProperties )
115  {
116  Sequence< OUString > aExtensions;
117  auto pProp = std::find_if(aTypeProperties.begin(), aTypeProperties.end(),
118  [](const PropertyValue& rProp) { return rProp.Name == "Extensions"; });
119  if (pProp != aTypeProperties.end())
120  pProp->Value >>= aExtensions;
121  if ( aExtensions.hasElements() )
122  {
123  // The filter title must be formed in the same way it is
124  // currently done in the internal implementation:
125  OUString aTitle(
126  rFilterEntry.maUIName + " (." + aExtensions[0] + ")");
127  OUString aFilter("*." + aExtensions[0]);
128  mxFilePicker->appendFilter(aTitle, aFilter);
129  if ( rFilterEntry.maFlags & 0x100 )
130  mxFilePicker->setCurrentFilter(aTitle);
131  }
132  }
133  }
134  catch ( const Exception& )
135  {
136  }
137  }
138 }
140 {
141 }
143 {
144  return mxFilePicker->execute();
145 }
146 void FileOpenDialog::setDefaultName( const OUString& rDefaultName )
147 {
148  mxFilePicker->setDefaultName( rDefaultName );
149 }
150 OUString FileOpenDialog::getURL() const
151 {
152  Sequence< OUString > aFileSeq( mxFilePicker->getSelectedFiles() );
153  return aFileSeq.hasElements() ? aFileSeq[ 0 ] : OUString();
154 };
156 {
157  OUString aFilterName;
158  Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
159  OUString aUIName( xFilterManager->getCurrentFilter() );
160  auto aIter = std::find_if(aFilterEntryList.begin(), aFilterEntryList.end(),
161  [&aUIName](const FilterEntry& rFilterEntry) { return rFilterEntry.maUIName == aUIName; });
162  if (aIter != aFilterEntryList.end())
163  aFilterName = aIter->maFilterEntryName;
164  return aFilterName;
165 };
166 
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
FileOpenDialog(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
sal_Int16 execute()
void setDefaultName(const OUString &)
css::uno::Reference< css::ui::dialogs::XFilePicker3 > mxFilePicker
if(nullptr==pCandidateA||nullptr==pCandidateB)
OUString aUIName
std::vector< FilterEntry > aFilterEntryList
PPPOptimizerTokenEnum TKGet(const OUString &rToken)
OUString getURL() const
OUString getFilterName() const