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
34using namespace ::com::sun::star::uno;
35using namespace ::com::sun::star::lang;
36using namespace ::com::sun::star::beans;
37using namespace ::com::sun::star::container;
38using namespace ::com::sun::star::view;
39using namespace ::com::sun::star::ui::dialogs;
40
41FileOpenDialog::FileOpenDialog( const Reference< XComponentContext >& rxContext )
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 {
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 (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(std::cbegin(aTypeProperties), std::cend(aTypeProperties),
118 [](const PropertyValue& rProp) { return rProp.Name == "Extensions"; });
119 if (pProp != std::cend(aTypeProperties))
120 pProp->Value >>= aExtensions;
121 if ( aExtensions.hasElements() )
122 {
123 // The filter title must be formed in the same way it is currently done in the
124 // internal implementation (see sfx2::appendFiltersForSave). And we will look
125 // for the same string returned from the dialog, so save it to maUIName:
126 OUString aTitle(
127 rFilterEntry.maUIName + " (." + aExtensions[0] + ")");
128 rFilterEntry.maUIName = aTitle;
129 OUString aFilter("*." + aExtensions[0]);
130 mxFilePicker->appendFilter(aTitle, aFilter);
131 if ( rFilterEntry.maFlags & 0x100 )
132 mxFilePicker->setCurrentFilter(aTitle);
133 }
134 }
135 }
136 catch ( const Exception& )
137 {
138 }
139 }
140}
142{
143}
145{
146 return mxFilePicker->execute();
147}
148void FileOpenDialog::setDefaultName( const OUString& rDefaultName )
149{
150 mxFilePicker->setDefaultName( rDefaultName );
151}
153{
154 Sequence< OUString > aFileSeq( mxFilePicker->getSelectedFiles() );
155 return aFileSeq.hasElements() ? aFileSeq[ 0 ] : OUString();
156};
158{
159 OUString aFilterName;
160 Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
161 OUString aUIName( xFilterManager->getCurrentFilter() );
162 auto aIter = std::find_if(aFilterEntryList.begin(), aFilterEntryList.end(),
163 [&aUIName](const FilterEntry& rFilterEntry) { return rFilterEntry.maUIName == aUIName; });
164 if (aIter != aFilterEntryList.end())
165 aFilterName = aIter->maFilterEntryName;
166 return aFilterName;
167};
168
169/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sDocumentService
FileOpenDialog(const css::uno::Reference< css::uno::XComponentContext > &rxContext)
void setDefaultName(const OUString &)
OUString getFilterName() const
css::uno::Reference< css::ui::dialogs::XFilePicker3 > mxFilePicker
sal_Int16 execute()
std::vector< FilterEntry > aFilterEntryList
OUString getURL() const
OUString aUIName
@ Exception
PPPOptimizerTokenEnum TKGet(std::u16string_view rToken)
@ TK_Type
@ TK_Flags
@ TK_DocumentService
@ TK_UIName
@ TK_Name