xref: /trunk/main/sdext/source/minimizer/fileopendialog.cxx (revision 7134ea5b3325a6cb3a72ce0741cd8b4b8a50b364)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "fileopendialog.hxx"
28 #include <sal/types.h>
29 #include "pppoptimizertoken.hxx"
30 #include <com/sun/star/lang/XInitialization.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
33 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
34 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
35 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
36 #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
37 #include <com/sun/star/ui/dialogs/ControlActions.hpp>
38 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
39 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
40 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
41 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
42 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
43 #include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
44 #ifndef _COM_SUN_STAR_UI_DIALOGS_XFOLDERPICKER_HDL_
45 #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
46 #endif
47 #include <com/sun/star/lang/XServiceInfo.hpp>
48 #include <com/sun/star/beans/XPropertySet.hpp>
49 #include <com/sun/star/beans/PropertyValue.hpp>
50 #include <com/sun/star/beans/NamedValue.hpp>
51 #include <com/sun/star/embed/ElementModes.hpp>
52 #include <com/sun/star/container/XEnumeration.hpp>
53 #include <com/sun/star/container/XNameAccess.hpp>
54 #include <com/sun/star/container/XContainerQuery.hpp>
55 #include <com/sun/star/view/XControlAccess.hpp>
56 #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
57 
58 #include <rtl/ustrbuf.hxx>
59 
60 using namespace ::rtl;
61 using namespace ::com::sun::star::uno;
62 using namespace ::com::sun::star::lang;
63 using namespace ::com::sun::star::beans;
64 using namespace ::com::sun::star::container;
65 using namespace ::com::sun::star::view;
66 using namespace ::com::sun::star::ui::dialogs;
67 
68 FileOpenDialog::FileOpenDialog( const Reference< XComponentContext >& rxMSF ) :
69     mxMSF( rxMSF )
70 {
71     Sequence< Any > aInitPropSeq( 1 );
72     aInitPropSeq[ 0 ] <<= (sal_Int16)TemplateDescription::FILESAVE_AUTOEXTENSION;   // TemplateDescription.FILEOPEN_SIMPLE
73 
74     mxFilePicker = Reference < XFilePicker >( mxMSF->getServiceManager()->createInstanceWithArgumentsAndContext(
75         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) ), aInitPropSeq, rxMSF ),UNO_QUERY_THROW );
76     mxFilePicker->setMultiSelectionMode( sal_False );
77 
78     Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
79     if ( xAccess.is() )
80     {
81         Any aValue( static_cast< sal_Bool >( sal_True ) );
82         try
83         {
84             xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, aValue );
85         }
86         catch( com::sun::star::uno::Exception& )
87         {}
88     }
89 
90     // collecting a list of impress filters
91     Reference< XNameAccess > xFilters( mxMSF->getServiceManager()->createInstanceWithContext(
92         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ), rxMSF ), UNO_QUERY_THROW );
93     Sequence< OUString > aFilterList( xFilters->getElementNames() );
94     for ( int i = 0; i < aFilterList.getLength(); i++ )
95     {
96         try
97         {
98             Sequence< PropertyValue > aFilterProperties;
99             if ( xFilters->getByName( aFilterList[ i ] ) >>= aFilterProperties )
100             {
101                 FilterEntry aFilterEntry;
102                 sal_Bool bImpressFilter = sal_False;
103                 for ( int j = 0; j < aFilterProperties.getLength(); j++ )
104                 {
105                     PropertyValue& rProperty( aFilterProperties[ j ] );
106                     switch( TKGet( rProperty.Name ) )
107                     {
108                         case TK_DocumentService :
109                         {
110                             rtl::OUString sDocumentService;
111                             rProperty.Value >>= sDocumentService;
112                             if ( sDocumentService == OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument" ) ) )
113                                 bImpressFilter = sal_True;
114                             else
115                                 j = aFilterProperties.getLength();
116                         }
117                         break;
118                         case TK_Name :      rProperty.Value >>= aFilterEntry.maName; break;
119                         case TK_UIName :    rProperty.Value >>= aFilterEntry.maUIName; break;
120                         case TK_Type :      rProperty.Value >>= aFilterEntry.maType; break;
121                         case TK_Flags :     rProperty.Value >>= aFilterEntry.maFlags; break;
122                         default : break;
123                     }
124                 }
125                 if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
126                 {
127                     aFilterEntryList.push_back( aFilterEntry );
128                 }
129             }
130         }
131         catch( Exception& )
132         {
133         }
134     }
135 
136     Reference< XNameAccess > xTypes( mxMSF->getServiceManager()->createInstanceWithContext(
137         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ) ), rxMSF ), UNO_QUERY_THROW );
138     Sequence< OUString > aTypeList( xFilters->getElementNames() );
139 
140 //  mxFilePicker->setDefaultName( );
141 
142     const char filter[] = "*.";
143     // the filter title must be formed in the same it is currently done
144     // in the internal implementation: "UIName (.<extension>)"
145     rtl::OUStringBuffer aUIName;
146     // the filter must be in the form "*.<extension>"
147     rtl::OUStringBuffer aFilter;
148     rtl::OUString aExtension;
149     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
150     std::vector< FilterEntry >::iterator aIter( aFilterEntryList.begin() );
151     while( aIter != aFilterEntryList.end() )
152     {
153         Sequence< PropertyValue > aTypeProperties;
154         try
155         {
156             if ( xTypes->getByName( aIter->maType ) >>= aTypeProperties )
157             {
158                 Sequence< OUString > aExtensions;
159                 for ( int i = 0; i < aTypeProperties.getLength(); i++ )
160                 {
161                     switch( TKGet( aTypeProperties[ i ].Name ) )
162                     {
163                         case TK_Extensions : aTypeProperties[ i ].Value >>= aExtensions; break;
164                         default: break;
165                     }
166                 }
167                 if ( aExtensions.getLength() )
168                 {
169                     aExtension = aExtensions[0];
170                     // form the title: "<UIName> (.<extension)"
171                     aUIName.append( aIter->maUIName );
172                     aUIName.appendAscii( RTL_CONSTASCII_STRINGPARAM( " (." ));
173                     aUIName.append( aExtension );
174                     aUIName.append( sal_Unicode( ')' ) );
175                     // form the filter: "(*.<extension>)"
176                     aFilter.appendAscii( RTL_CONSTASCII_STRINGPARAM( filter ) );
177                     aFilter.append( aExtensions[0] );
178 
179                     xFilterManager->appendFilter( aUIName.makeStringAndClear(),
180                                                   aFilter.makeStringAndClear() );
181                     if ( aIter->maFlags & 0x100 )
182                         xFilterManager->setCurrentFilter( aIter->maUIName );
183                 }
184             }
185         }
186         catch ( Exception& )
187         {
188         }
189         aIter++;
190     }
191 }
192 FileOpenDialog::~FileOpenDialog()
193 {
194 }
195 sal_Int16 FileOpenDialog::execute()
196 {
197     return mxFilePicker->execute();
198 }
199 void FileOpenDialog::setDefaultName( const rtl::OUString& rDefaultName )
200 {
201     mxFilePicker->setDefaultName( rDefaultName );
202 }
203 ::rtl::OUString FileOpenDialog::getURL() const
204 {
205     Sequence< OUString > aFileSeq( mxFilePicker->getFiles() );
206     return aFileSeq.getLength() ? aFileSeq[ 0 ] : OUString();
207 };
208 ::rtl::OUString FileOpenDialog::getFilterName() const
209 {
210     rtl::OUString aFilterName;
211     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
212     rtl::OUString aUIName( xFilterManager->getCurrentFilter() );
213     std::vector< FilterEntry >::const_iterator aIter( aFilterEntryList.begin() );
214     while( aIter != aFilterEntryList.end() )
215     {
216         if ( aIter->maUIName == aUIName )
217         {
218             aFilterName = aIter->maName;
219             break;
220         }
221         aIter++;
222     }
223     return aFilterName;
224 };
225