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