1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
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
;
41 FileOpenDialog::FileOpenDialog( const Reference
< XComponentContext
>& rxContext
)
43 mxFilePicker
= FilePicker::createWithMode( rxContext
, TemplateDescription::FILESAVE_AUTOEXTENSION
);
44 mxFilePicker
->setMultiSelectionMode( false );
46 Reference
< XFilePickerControlAccess
> xAccess( mxFilePicker
, UNO_QUERY
);
51 xAccess
->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION
, 0, Any( true ) );
53 catch( css::uno::Exception
& )
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
)
65 Sequence
< PropertyValue
> aFilterProperties
;
66 if ( xFilters
->getByName( rFilter
) >>= aFilterProperties
)
68 FilterEntry aFilterEntry
;
69 bool bImpressFilter
= false;
70 for ( const PropertyValue
& rProperty
: std::as_const(aFilterProperties
) )
73 switch( TKGet( rProperty
.Name
) )
75 case TK_DocumentService
:
77 OUString sDocumentService
;
78 rProperty
.Value
>>= sDocumentService
;
79 if ( sDocumentService
== "com.sun.star.presentation.PresentationDocument" )
80 bImpressFilter
= true;
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;
95 if ( bImpressFilter
&& ( ( aFilterEntry
.maFlags
& 3 ) == 3 ) )
97 aFilterEntryList
.push_back( aFilterEntry
);
106 Reference
< XNameAccess
> xTypes( rxContext
->getServiceManager()->createInstanceWithContext(
107 "com.sun.star.document.TypeDetection", rxContext
), UNO_QUERY_THROW
);
109 for (auto& rFilterEntry
: aFilterEntryList
)
111 Sequence
< PropertyValue
> aTypeProperties
;
114 if ( xTypes
->getByName( rFilterEntry
.maType
) >>= aTypeProperties
)
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() )
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:
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
);
136 catch ( const Exception
& )
141 FileOpenDialog::~FileOpenDialog()
144 sal_Int16
FileOpenDialog::execute()
146 return mxFilePicker
->execute();
148 void FileOpenDialog::setDefaultName( const OUString
& rDefaultName
)
150 mxFilePicker
->setDefaultName( rDefaultName
);
152 OUString
FileOpenDialog::getURL() const
154 Sequence
< OUString
> aFileSeq( mxFilePicker
->getSelectedFiles() );
155 return aFileSeq
.hasElements() ? aFileSeq
[ 0 ] : OUString();
157 OUString
FileOpenDialog::getFilterName() const
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
;
169 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */