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::ui::dialogs
;
40 FileOpenDialog::FileOpenDialog( const Reference
< XComponentContext
>& rxContext
)
42 mxFilePicker
= FilePicker::createWithMode( rxContext
, TemplateDescription::FILESAVE_AUTOEXTENSION
);
43 mxFilePicker
->setMultiSelectionMode( false );
45 Reference
< XFilePickerControlAccess
> xAccess( mxFilePicker
, UNO_QUERY
);
50 xAccess
->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION
, 0, Any( true ) );
52 catch( css::uno::Exception
& )
56 // collecting a list of impress filters
57 Reference
< XNameAccess
> xFilters( rxContext
->getServiceManager()->createInstanceWithContext(
58 u
"com.sun.star.document.FilterFactory"_ustr
, rxContext
), UNO_QUERY_THROW
);
59 const Sequence
< OUString
> aFilterList( xFilters
->getElementNames() );
60 for ( const auto& rFilter
: aFilterList
)
64 Sequence
< PropertyValue
> aFilterProperties
;
65 if ( xFilters
->getByName( rFilter
) >>= aFilterProperties
)
67 FilterEntry aFilterEntry
;
68 bool bImpressFilter
= false;
69 for (const PropertyValue
& rProperty
: aFilterProperties
)
72 switch( TKGet( rProperty
.Name
) )
74 case TK_DocumentService
:
76 OUString sDocumentService
;
77 rProperty
.Value
>>= sDocumentService
;
78 if ( sDocumentService
== "com.sun.star.presentation.PresentationDocument" )
79 bImpressFilter
= true;
84 case TK_Name
: rProperty
.Value
>>= aFilterEntry
.maFilterEntryName
; break;
85 case TK_UIName
: rProperty
.Value
>>= aFilterEntry
.maUIName
; break;
86 case TK_Type
: rProperty
.Value
>>= aFilterEntry
.maType
; break;
87 case TK_Flags
: rProperty
.Value
>>= aFilterEntry
.maFlags
; break;
94 if ( bImpressFilter
&& ( ( aFilterEntry
.maFlags
& 3 ) == 3 ) )
96 aFilterEntryList
.push_back( aFilterEntry
);
105 Reference
< XNameAccess
> xTypes( rxContext
->getServiceManager()->createInstanceWithContext(
106 u
"com.sun.star.document.TypeDetection"_ustr
, rxContext
), UNO_QUERY_THROW
);
108 for (auto& rFilterEntry
: aFilterEntryList
)
110 Sequence
< PropertyValue
> aTypeProperties
;
113 if ( xTypes
->getByName( rFilterEntry
.maType
) >>= aTypeProperties
)
115 Sequence
< OUString
> aExtensions
;
116 auto pProp
= std::find_if(std::cbegin(aTypeProperties
), std::cend(aTypeProperties
),
117 [](const PropertyValue
& rProp
) { return rProp
.Name
== "Extensions"; });
118 if (pProp
!= std::cend(aTypeProperties
))
119 pProp
->Value
>>= aExtensions
;
120 if ( aExtensions
.hasElements() )
122 // The filter title must be formed in the same way it is currently done in the
123 // internal implementation (see sfx2::appendFiltersForSave). And we will look
124 // for the same string returned from the dialog, so save it to maUIName:
126 rFilterEntry
.maUIName
+ " (." + aExtensions
[0] + ")");
127 rFilterEntry
.maUIName
= aTitle
;
128 OUString
aFilter("*." + aExtensions
[0]);
129 mxFilePicker
->appendFilter(aTitle
, aFilter
);
130 if ( rFilterEntry
.maFlags
& 0x100 )
131 mxFilePicker
->setCurrentFilter(aTitle
);
135 catch ( const Exception
& )
140 FileOpenDialog::~FileOpenDialog()
143 sal_Int16
FileOpenDialog::execute()
145 return mxFilePicker
->execute();
147 void FileOpenDialog::setDefaultName( const OUString
& rDefaultName
)
149 mxFilePicker
->setDefaultName( rDefaultName
);
151 OUString
FileOpenDialog::getURL() const
153 Sequence
< OUString
> aFileSeq( mxFilePicker
->getSelectedFiles() );
154 return aFileSeq
.hasElements() ? aFileSeq
[ 0 ] : OUString();
156 OUString
FileOpenDialog::getFilterName() const
158 OUString aFilterName
;
159 Reference
< XFilterManager
> xFilterManager( mxFilePicker
, UNO_QUERY_THROW
);
160 OUString
aUIName( xFilterManager
->getCurrentFilter() );
161 auto aIter
= std::find_if(aFilterEntryList
.begin(), aFilterEntryList
.end(),
162 [&aUIName
](const FilterEntry
& rFilterEntry
) { return rFilterEntry
.maUIName
== aUIName
; });
163 if (aIter
!= aFilterEntryList
.end())
164 aFilterName
= aIter
->maFilterEntryName
;
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */