xref: /trunk/main/sdext/source/minimizer/fileopendialog.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_sdext.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "fileopendialog.hxx"
32*cdf0e10cSrcweir #include <sal/types.h>
33*cdf0e10cSrcweir #include "pppoptimizertoken.hxx"
34*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
35*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
37*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/FilePreviewImageFormats.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/ControlActions.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
45*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
46*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFilterGroupManager.hpp>
48*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UI_DIALOGS_XFOLDERPICKER_HDL_
49*cdf0e10cSrcweir #include <com/sun/star/ui/dialogs/XFolderPicker.hpp>
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
53*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
54*cdf0e10cSrcweir #include <com/sun/star/beans/NamedValue.hpp>
55*cdf0e10cSrcweir #include <com/sun/star/embed/ElementModes.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/container/XEnumeration.hpp>
57*cdf0e10cSrcweir #include <com/sun/star/container/XNameAccess.hpp>
58*cdf0e10cSrcweir #include <com/sun/star/container/XContainerQuery.hpp>
59*cdf0e10cSrcweir #include <com/sun/star/view/XControlAccess.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/ucb/InteractiveAugmentedIOException.hpp>
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir using namespace ::rtl;
64*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
65*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
66*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
67*cdf0e10cSrcweir using namespace ::com::sun::star::container;
68*cdf0e10cSrcweir using namespace ::com::sun::star::view;
69*cdf0e10cSrcweir using namespace ::com::sun::star::ui::dialogs;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir FileOpenDialog::FileOpenDialog( const Reference< XComponentContext >& rxMSF ) :
72*cdf0e10cSrcweir     mxMSF( rxMSF )
73*cdf0e10cSrcweir {
74*cdf0e10cSrcweir     Sequence< Any > aInitPropSeq( 1 );
75*cdf0e10cSrcweir     aInitPropSeq[ 0 ] <<= (sal_Int16)TemplateDescription::FILESAVE_AUTOEXTENSION;   // TemplateDescription.FILEOPEN_SIMPLE
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     mxFilePicker = Reference < XFilePicker >( mxMSF->getServiceManager()->createInstanceWithArgumentsAndContext(
78*cdf0e10cSrcweir         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) ), aInitPropSeq, rxMSF ),UNO_QUERY_THROW );
79*cdf0e10cSrcweir     mxFilePicker->setMultiSelectionMode( sal_False );
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir     Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
82*cdf0e10cSrcweir     if ( xAccess.is() )
83*cdf0e10cSrcweir     {
84*cdf0e10cSrcweir         Any aValue( static_cast< sal_Bool >( sal_True ) );
85*cdf0e10cSrcweir         try
86*cdf0e10cSrcweir         {
87*cdf0e10cSrcweir             xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, aValue );
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir         catch( com::sun::star::uno::Exception& )
90*cdf0e10cSrcweir         {}
91*cdf0e10cSrcweir     }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     // collecting a list of impress filters
94*cdf0e10cSrcweir     Reference< XNameAccess > xFilters( mxMSF->getServiceManager()->createInstanceWithContext(
95*cdf0e10cSrcweir         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ), rxMSF ), UNO_QUERY_THROW );
96*cdf0e10cSrcweir     Sequence< OUString > aFilterList( xFilters->getElementNames() );
97*cdf0e10cSrcweir     for ( int i = 0; i < aFilterList.getLength(); i++ )
98*cdf0e10cSrcweir     {
99*cdf0e10cSrcweir         try
100*cdf0e10cSrcweir         {
101*cdf0e10cSrcweir             Sequence< PropertyValue > aFilterProperties;
102*cdf0e10cSrcweir             if ( xFilters->getByName( aFilterList[ i ] ) >>= aFilterProperties )
103*cdf0e10cSrcweir             {
104*cdf0e10cSrcweir                 FilterEntry aFilterEntry;
105*cdf0e10cSrcweir                 sal_Bool bImpressFilter = sal_False;
106*cdf0e10cSrcweir                 for ( int j = 0; j < aFilterProperties.getLength(); j++ )
107*cdf0e10cSrcweir                 {
108*cdf0e10cSrcweir                     PropertyValue& rProperty( aFilterProperties[ j ] );
109*cdf0e10cSrcweir                     switch( TKGet( rProperty.Name ) )
110*cdf0e10cSrcweir                     {
111*cdf0e10cSrcweir                         case TK_DocumentService :
112*cdf0e10cSrcweir                         {
113*cdf0e10cSrcweir                             rtl::OUString sDocumentService;
114*cdf0e10cSrcweir                             rProperty.Value >>= sDocumentService;
115*cdf0e10cSrcweir                             if ( sDocumentService == OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument" ) ) )
116*cdf0e10cSrcweir                                 bImpressFilter = sal_True;
117*cdf0e10cSrcweir                             else
118*cdf0e10cSrcweir                                 j = aFilterProperties.getLength();
119*cdf0e10cSrcweir                         }
120*cdf0e10cSrcweir                         break;
121*cdf0e10cSrcweir                         case TK_Name :      rProperty.Value >>= aFilterEntry.maName; break;
122*cdf0e10cSrcweir                         case TK_UIName :    rProperty.Value >>= aFilterEntry.maUIName; break;
123*cdf0e10cSrcweir                         case TK_Type :      rProperty.Value >>= aFilterEntry.maType; break;
124*cdf0e10cSrcweir                         case TK_Flags :     rProperty.Value >>= aFilterEntry.maFlags; break;
125*cdf0e10cSrcweir                         default : break;
126*cdf0e10cSrcweir                     }
127*cdf0e10cSrcweir                 }
128*cdf0e10cSrcweir                 if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
129*cdf0e10cSrcweir                 {
130*cdf0e10cSrcweir                     aFilterEntryList.push_back( aFilterEntry );
131*cdf0e10cSrcweir                 }
132*cdf0e10cSrcweir             }
133*cdf0e10cSrcweir         }
134*cdf0e10cSrcweir         catch( Exception& )
135*cdf0e10cSrcweir         {
136*cdf0e10cSrcweir         }
137*cdf0e10cSrcweir     }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir     Reference< XNameAccess > xTypes( mxMSF->getServiceManager()->createInstanceWithContext(
140*cdf0e10cSrcweir         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ) ), rxMSF ), UNO_QUERY_THROW );
141*cdf0e10cSrcweir     Sequence< OUString > aTypeList( xFilters->getElementNames() );
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir //  mxFilePicker->setDefaultName( );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
146*cdf0e10cSrcweir     std::vector< FilterEntry >::iterator aIter( aFilterEntryList.begin() );
147*cdf0e10cSrcweir     while( aIter != aFilterEntryList.end() )
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         Sequence< PropertyValue > aTypeProperties;
150*cdf0e10cSrcweir         try
151*cdf0e10cSrcweir         {
152*cdf0e10cSrcweir             if ( xTypes->getByName( aIter->maType ) >>= aTypeProperties )
153*cdf0e10cSrcweir             {
154*cdf0e10cSrcweir                 Sequence< OUString > aExtensions;
155*cdf0e10cSrcweir                 for ( int i = 0; i < aTypeProperties.getLength(); i++ )
156*cdf0e10cSrcweir                 {
157*cdf0e10cSrcweir                     switch( TKGet( aTypeProperties[ i ].Name ) )
158*cdf0e10cSrcweir                     {
159*cdf0e10cSrcweir                         case TK_Extensions : aTypeProperties[ i ].Value >>= aExtensions; break;
160*cdf0e10cSrcweir                         default: break;
161*cdf0e10cSrcweir                     }
162*cdf0e10cSrcweir                 }
163*cdf0e10cSrcweir                 if ( aExtensions.getLength() )
164*cdf0e10cSrcweir                 {
165*cdf0e10cSrcweir                     xFilterManager->appendFilter( aIter->maUIName, aExtensions[ 0 ] );
166*cdf0e10cSrcweir                     if ( aIter->maFlags & 0x100 )
167*cdf0e10cSrcweir                         xFilterManager->setCurrentFilter( aIter->maUIName );
168*cdf0e10cSrcweir                 }
169*cdf0e10cSrcweir             }
170*cdf0e10cSrcweir         }
171*cdf0e10cSrcweir         catch ( Exception& )
172*cdf0e10cSrcweir         {
173*cdf0e10cSrcweir         }
174*cdf0e10cSrcweir         aIter++;
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir FileOpenDialog::~FileOpenDialog()
178*cdf0e10cSrcweir {
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir sal_Int16 FileOpenDialog::execute()
181*cdf0e10cSrcweir {
182*cdf0e10cSrcweir     return mxFilePicker->execute();
183*cdf0e10cSrcweir }
184*cdf0e10cSrcweir void FileOpenDialog::setDefaultName( const rtl::OUString& rDefaultName )
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir     mxFilePicker->setDefaultName( rDefaultName );
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir ::rtl::OUString FileOpenDialog::getURL() const
189*cdf0e10cSrcweir {
190*cdf0e10cSrcweir     Sequence< OUString > aFileSeq( mxFilePicker->getFiles() );
191*cdf0e10cSrcweir     return aFileSeq.getLength() ? aFileSeq[ 0 ] : OUString();
192*cdf0e10cSrcweir };
193*cdf0e10cSrcweir ::rtl::OUString FileOpenDialog::getFilterName() const
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir     rtl::OUString aFilterName;
196*cdf0e10cSrcweir     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
197*cdf0e10cSrcweir     rtl::OUString aUIName( xFilterManager->getCurrentFilter() );
198*cdf0e10cSrcweir     std::vector< FilterEntry >::const_iterator aIter( aFilterEntryList.begin() );
199*cdf0e10cSrcweir     while( aIter != aFilterEntryList.end() )
200*cdf0e10cSrcweir     {
201*cdf0e10cSrcweir         if ( aIter->maUIName == aUIName )
202*cdf0e10cSrcweir         {
203*cdf0e10cSrcweir             aFilterName = aIter->maName;
204*cdf0e10cSrcweir             break;
205*cdf0e10cSrcweir         }
206*cdf0e10cSrcweir         aIter++;
207*cdf0e10cSrcweir     }
208*cdf0e10cSrcweir     return aFilterName;
209*cdf0e10cSrcweir };
210