Line data Source code
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/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>
48 :
49 : using namespace ::rtl;
50 : using namespace ::com::sun::star::uno;
51 : using namespace ::com::sun::star::lang;
52 : using namespace ::com::sun::star::beans;
53 : using namespace ::com::sun::star::container;
54 : using namespace ::com::sun::star::view;
55 : using namespace ::com::sun::star::ui::dialogs;
56 :
57 0 : FileOpenDialog::FileOpenDialog( const Reference< XComponentContext >& rxContext )
58 : {
59 0 : mxFilePicker = FilePicker::createWithMode( rxContext, TemplateDescription::FILESAVE_AUTOEXTENSION);
60 0 : mxFilePicker->setMultiSelectionMode( sal_False );
61 :
62 0 : Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
63 0 : if ( xAccess.is() )
64 : {
65 0 : Any aValue( static_cast< sal_Bool >( sal_True ) );
66 : try
67 : {
68 0 : xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, aValue );
69 : }
70 0 : catch( com::sun::star::uno::Exception& )
71 0 : {}
72 : }
73 :
74 : // collecting a list of impress filters
75 0 : Reference< XNameAccess > xFilters( rxContext->getServiceManager()->createInstanceWithContext(
76 0 : OUString( "com.sun.star.document.FilterFactory" ), rxContext ), UNO_QUERY_THROW );
77 0 : Sequence< OUString > aFilterList( xFilters->getElementNames() );
78 0 : for ( int i = 0; i < aFilterList.getLength(); i++ )
79 : {
80 : try
81 : {
82 0 : Sequence< PropertyValue > aFilterProperties;
83 0 : if ( xFilters->getByName( aFilterList[ i ] ) >>= aFilterProperties )
84 : {
85 0 : FilterEntry aFilterEntry;
86 0 : sal_Bool bImpressFilter = sal_False;
87 0 : for ( int j = 0; j < aFilterProperties.getLength(); j++ )
88 : {
89 0 : PropertyValue& rProperty( aFilterProperties[ j ] );
90 0 : switch( TKGet( rProperty.Name ) )
91 : {
92 : case TK_DocumentService :
93 : {
94 0 : OUString sDocumentService;
95 0 : rProperty.Value >>= sDocumentService;
96 0 : if ( sDocumentService == "com.sun.star.presentation.PresentationDocument" )
97 0 : bImpressFilter = sal_True;
98 : else
99 0 : j = aFilterProperties.getLength();
100 : }
101 0 : break;
102 0 : case TK_Name : rProperty.Value >>= aFilterEntry.maName; break;
103 0 : case TK_UIName : rProperty.Value >>= aFilterEntry.maUIName; break;
104 0 : case TK_Type : rProperty.Value >>= aFilterEntry.maType; break;
105 0 : case TK_Flags : rProperty.Value >>= aFilterEntry.maFlags; break;
106 0 : default : break;
107 : }
108 : }
109 0 : if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
110 : {
111 0 : aFilterEntryList.push_back( aFilterEntry );
112 0 : }
113 0 : }
114 : }
115 0 : catch( Exception& )
116 : {
117 : }
118 : }
119 :
120 0 : Reference< XNameAccess > xTypes( rxContext->getServiceManager()->createInstanceWithContext(
121 0 : OUString( "com.sun.star.document.TypeDetection" ), rxContext ), UNO_QUERY_THROW );
122 :
123 0 : for( std::vector< FilterEntry >::const_iterator aIter(aFilterEntryList.begin()), aEnd(aFilterEntryList.end()); aIter != aEnd; ++aIter )
124 : {
125 0 : Sequence< PropertyValue > aTypeProperties;
126 : try
127 : {
128 0 : if ( xTypes->getByName( aIter->maType ) >>= aTypeProperties )
129 : {
130 0 : Sequence< OUString > aExtensions;
131 0 : for ( int i = 0; i < aTypeProperties.getLength(); i++ )
132 : {
133 0 : if( aTypeProperties[ i ].Name == "Extensions" )
134 : {
135 0 : aTypeProperties[ i ].Value >>= aExtensions;
136 0 : break;
137 : }
138 : }
139 0 : if ( aExtensions.getLength() )
140 : {
141 : // The filter title must be formed in the same way it is
142 : // currently done in the internal implementation:
143 : OUString aTitle(
144 0 : aIter->maUIName + " (." + aExtensions[0] + ")");
145 0 : OUString aFilter("*." + aExtensions[0]);
146 0 : mxFilePicker->appendFilter(aTitle, aFilter);
147 0 : if ( aIter->maFlags & 0x100 )
148 0 : mxFilePicker->setCurrentFilter(aTitle);
149 0 : }
150 : }
151 : }
152 0 : catch ( const Exception& )
153 : {
154 : }
155 0 : }
156 0 : }
157 0 : FileOpenDialog::~FileOpenDialog()
158 : {
159 0 : }
160 0 : sal_Int16 FileOpenDialog::execute()
161 : {
162 0 : return mxFilePicker->execute();
163 : }
164 0 : void FileOpenDialog::setDefaultName( const OUString& rDefaultName )
165 : {
166 0 : mxFilePicker->setDefaultName( rDefaultName );
167 0 : }
168 0 : OUString FileOpenDialog::getURL() const
169 : {
170 0 : Sequence< OUString > aFileSeq( mxFilePicker->getFiles() );
171 0 : return aFileSeq.getLength() ? aFileSeq[ 0 ] : OUString();
172 : };
173 0 : OUString FileOpenDialog::getFilterName() const
174 : {
175 0 : OUString aFilterName;
176 0 : Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
177 0 : OUString aUIName( xFilterManager->getCurrentFilter() );
178 0 : for( std::vector< FilterEntry >::const_iterator aIter(aFilterEntryList.begin()), aEnd(aFilterEntryList.end()); aIter != aEnd; ++aIter )
179 : {
180 0 : if ( aIter->maUIName == aUIName )
181 : {
182 0 : aFilterName = aIter->maName;
183 0 : break;
184 : }
185 : }
186 0 : return aFilterName;
187 : };
188 :
189 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|