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 #include <rtl/ustrbuf.hxx>
51 using namespace ::rtl
;
52 using namespace ::com::sun::star::uno
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::com::sun::star::beans
;
55 using namespace ::com::sun::star::container
;
56 using namespace ::com::sun::star::view
;
57 using namespace ::com::sun::star::ui::dialogs
;
59 FileOpenDialog::FileOpenDialog( const Reference
< XComponentContext
>& rxContext
)
61 mxFilePicker
= FilePicker::createWithMode( rxContext
, TemplateDescription::FILESAVE_AUTOEXTENSION
);
62 mxFilePicker
->setMultiSelectionMode( sal_False
);
64 Reference
< XFilePickerControlAccess
> xAccess( mxFilePicker
, UNO_QUERY
);
67 Any
aValue( static_cast< sal_Bool
>( sal_True
) );
70 xAccess
->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION
, 0, aValue
);
72 catch( com::sun::star::uno::Exception
& )
76 // collecting a list of impress filters
77 Reference
< XNameAccess
> xFilters( rxContext
->getServiceManager()->createInstanceWithContext(
78 OUString( "com.sun.star.document.FilterFactory" ), rxContext
), UNO_QUERY_THROW
);
79 Sequence
< OUString
> aFilterList( xFilters
->getElementNames() );
80 for ( int i
= 0; i
< aFilterList
.getLength(); i
++ )
84 Sequence
< PropertyValue
> aFilterProperties
;
85 if ( xFilters
->getByName( aFilterList
[ i
] ) >>= aFilterProperties
)
87 FilterEntry aFilterEntry
;
88 sal_Bool bImpressFilter
= sal_False
;
89 for ( int j
= 0; j
< aFilterProperties
.getLength(); j
++ )
91 PropertyValue
& rProperty( aFilterProperties
[ j
] );
92 switch( TKGet( rProperty
.Name
) )
94 case TK_DocumentService
:
96 OUString sDocumentService
;
97 rProperty
.Value
>>= sDocumentService
;
98 if ( sDocumentService
== "com.sun.star.presentation.PresentationDocument" )
99 bImpressFilter
= sal_True
;
101 j
= aFilterProperties
.getLength();
104 case TK_Name
: rProperty
.Value
>>= aFilterEntry
.maName
; break;
105 case TK_UIName
: rProperty
.Value
>>= aFilterEntry
.maUIName
; break;
106 case TK_Type
: rProperty
.Value
>>= aFilterEntry
.maType
; break;
107 case TK_Flags
: rProperty
.Value
>>= aFilterEntry
.maFlags
; break;
111 if ( bImpressFilter
&& ( ( aFilterEntry
.maFlags
& 3 ) == 3 ) )
113 aFilterEntryList
.push_back( aFilterEntry
);
122 Reference
< XNameAccess
> xTypes( rxContext
->getServiceManager()->createInstanceWithContext(
123 OUString( "com.sun.star.document.TypeDetection" ), rxContext
), UNO_QUERY_THROW
);
124 Sequence
< OUString
> aTypeList( xFilters
->getElementNames() );
126 for( std::vector
< FilterEntry
>::const_iterator
aIter(aFilterEntryList
.begin()), aEnd(aFilterEntryList
.end()); aIter
!= aEnd
; ++aIter
)
128 Sequence
< PropertyValue
> aTypeProperties
;
131 if ( xTypes
->getByName( aIter
->maType
) >>= aTypeProperties
)
133 Sequence
< OUString
> aExtensions
;
134 for ( int i
= 0; i
< aTypeProperties
.getLength(); i
++ )
136 switch( TKGet( aTypeProperties
[ i
].Name
) )
138 case TK_Extensions
: aTypeProperties
[ i
].Value
>>= aExtensions
; break;
142 if ( aExtensions
.getLength() )
144 OUString aExtension
= aExtensions
[0];
146 const char filter
[] = "*.";
147 // the filter title must be formed in the same it is currently done
148 // in the internal implementation: "UIName (.<extension>)"
149 OUStringBuffer aUIName
;
150 // the filter must be in the form "*.<extension>"
151 OUStringBuffer aFilter
;
153 // form the title: "<UIName> (.<extension)"
154 aUIName
.append( aIter
->maUIName
);
155 aUIName
.appendAscii( RTL_CONSTASCII_STRINGPARAM( " (." ));
156 aUIName
.append( aExtension
);
157 aUIName
.append( sal_Unicode( ')' ) );
158 // form the filter: "(*.<extension>)"
159 aFilter
.appendAscii( RTL_CONSTASCII_STRINGPARAM( filter
) );
160 aFilter
.append( aExtensions
[0] );
162 mxFilePicker
->appendFilter( aUIName
.makeStringAndClear(),
163 aFilter
.makeStringAndClear() );
165 if ( aIter
->maFlags
& 0x100 )
166 mxFilePicker
->setCurrentFilter( aIter
->maUIName
);
170 catch ( const Exception
& )
175 FileOpenDialog::~FileOpenDialog()
178 sal_Int16
FileOpenDialog::execute()
180 return mxFilePicker
->execute();
182 void FileOpenDialog::setDefaultName( const OUString
& rDefaultName
)
184 mxFilePicker
->setDefaultName( rDefaultName
);
186 OUString
FileOpenDialog::getURL() const
188 Sequence
< OUString
> aFileSeq( mxFilePicker
->getFiles() );
189 return aFileSeq
.getLength() ? aFileSeq
[ 0 ] : OUString();
191 OUString
FileOpenDialog::getFilterName() const
193 OUString aFilterName
;
194 Reference
< XFilterManager
> xFilterManager( mxFilePicker
, UNO_QUERY_THROW
);
195 OUString
aUIName( xFilterManager
->getCurrentFilter() );
196 for( std::vector
< FilterEntry
>::const_iterator
aIter(aFilterEntryList
.begin()), aEnd(aFilterEntryList
.end()); aIter
!= aEnd
; ++aIter
)
198 if ( aIter
->maUIName
== aUIName
)
200 aFilterName
= aIter
->maName
;
207 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */