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/lang/XInitialization.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
27 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
28 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
29 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
30 #include <com/sun/star/ui/dialogs/FilePicker.hpp>
31 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
32 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
33 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
34 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
35 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
36 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
37 #include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
38 #include <com/sun/star/lang/XServiceInfo.hpp>
39 #include <com/sun/star/beans/XPropertySet.hpp>
40 #include <com/sun/star/beans/PropertyValue.hpp>
41 #include <com/sun/star/beans/NamedValue.hpp>
42 #include <com/sun/star/embed/ElementModes.hpp>
43 #include <com/sun/star/container/XEnumeration.hpp>
44 #include <com/sun/star/container/XNameAccess.hpp>
45 #include <com/sun/star/container/XContainerQuery.hpp>
46 #include <com/sun/star/view/XControlAccess.hpp>
47 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::lang
;
51 using namespace ::com::sun::star::beans
;
52 using namespace ::com::sun::star::container
;
53 using namespace ::com::sun::star::view
;
54 using namespace ::com::sun::star::ui::dialogs
;
56 FileOpenDialog::FileOpenDialog( const Reference
< XComponentContext
>& rxContext
)
58 mxFilePicker
= FilePicker::createWithMode( rxContext
, TemplateDescription::FILESAVE_AUTOEXTENSION
);
59 mxFilePicker
->setMultiSelectionMode( sal_False
);
61 Reference
< XFilePickerControlAccess
> xAccess( mxFilePicker
, UNO_QUERY
);
66 xAccess
->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION
, 0, Any( true ) );
68 catch( com::sun::star::uno::Exception
& )
72 // collecting a list of impress filters
73 Reference
< XNameAccess
> xFilters( rxContext
->getServiceManager()->createInstanceWithContext(
74 OUString( "com.sun.star.document.FilterFactory" ), rxContext
), UNO_QUERY_THROW
);
75 Sequence
< OUString
> aFilterList( xFilters
->getElementNames() );
76 for ( int i
= 0; i
< aFilterList
.getLength(); i
++ )
80 Sequence
< PropertyValue
> aFilterProperties
;
81 if ( xFilters
->getByName( aFilterList
[ i
] ) >>= aFilterProperties
)
83 FilterEntry aFilterEntry
;
84 bool bImpressFilter
= false;
85 for ( int j
= 0; j
< aFilterProperties
.getLength(); j
++ )
87 PropertyValue
& rProperty( aFilterProperties
[ j
] );
88 switch( TKGet( rProperty
.Name
) )
90 case TK_DocumentService
:
92 OUString sDocumentService
;
93 rProperty
.Value
>>= sDocumentService
;
94 if ( sDocumentService
== "com.sun.star.presentation.PresentationDocument" )
95 bImpressFilter
= true;
97 j
= aFilterProperties
.getLength();
100 case TK_Name
: rProperty
.Value
>>= aFilterEntry
.maName
; break;
101 case TK_UIName
: rProperty
.Value
>>= aFilterEntry
.maUIName
; break;
102 case TK_Type
: rProperty
.Value
>>= aFilterEntry
.maType
; break;
103 case TK_Flags
: rProperty
.Value
>>= aFilterEntry
.maFlags
; break;
107 if ( bImpressFilter
&& ( ( aFilterEntry
.maFlags
& 3 ) == 3 ) )
109 aFilterEntryList
.push_back( aFilterEntry
);
118 Reference
< XNameAccess
> xTypes( rxContext
->getServiceManager()->createInstanceWithContext(
119 OUString( "com.sun.star.document.TypeDetection" ), rxContext
), UNO_QUERY_THROW
);
121 for( std::vector
< FilterEntry
>::const_iterator
aIter(aFilterEntryList
.begin()), aEnd(aFilterEntryList
.end()); aIter
!= aEnd
; ++aIter
)
123 Sequence
< PropertyValue
> aTypeProperties
;
126 if ( xTypes
->getByName( aIter
->maType
) >>= aTypeProperties
)
128 Sequence
< OUString
> aExtensions
;
129 for ( int i
= 0; i
< aTypeProperties
.getLength(); i
++ )
131 if( aTypeProperties
[ i
].Name
== "Extensions" )
133 aTypeProperties
[ i
].Value
>>= aExtensions
;
137 if ( aExtensions
.getLength() )
139 // The filter title must be formed in the same way it is
140 // currently done in the internal implementation:
142 aIter
->maUIName
+ " (." + aExtensions
[0] + ")");
143 OUString
aFilter("*." + aExtensions
[0]);
144 mxFilePicker
->appendFilter(aTitle
, aFilter
);
145 if ( aIter
->maFlags
& 0x100 )
146 mxFilePicker
->setCurrentFilter(aTitle
);
150 catch ( const Exception
& )
155 FileOpenDialog::~FileOpenDialog()
158 sal_Int16
FileOpenDialog::execute()
160 return mxFilePicker
->execute();
162 void FileOpenDialog::setDefaultName( const OUString
& rDefaultName
)
164 mxFilePicker
->setDefaultName( rDefaultName
);
166 OUString
FileOpenDialog::getURL() const
168 Sequence
< OUString
> aFileSeq( mxFilePicker
->getFiles() );
169 return aFileSeq
.getLength() ? aFileSeq
[ 0 ] : OUString();
171 OUString
FileOpenDialog::getFilterName() const
173 OUString aFilterName
;
174 Reference
< XFilterManager
> xFilterManager( mxFilePicker
, UNO_QUERY_THROW
);
175 OUString
aUIName( xFilterManager
->getCurrentFilter() );
176 for( std::vector
< FilterEntry
>::const_iterator
aIter(aFilterEntryList
.begin()), aEnd(aFilterEntryList
.end()); aIter
!= aEnd
; ++aIter
)
178 if ( aIter
->maUIName
== aUIName
)
180 aFilterName
= aIter
->maName
;
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */