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 
59 using namespace ::rtl;
60 using namespace ::com::sun::star::uno;
61 using namespace ::com::sun::star::lang;
62 using namespace ::com::sun::star::beans;
63 using namespace ::com::sun::star::container;
64 using namespace ::com::sun::star::view;
65 using namespace ::com::sun::star::ui::dialogs;
66 
67 FileOpenDialog::FileOpenDialog( const Reference< XComponentContext >& rxMSF ) :
68     mxMSF( rxMSF )
69 {
70 	Sequence< Any > aInitPropSeq( 1 );
71 	aInitPropSeq[ 0 ] <<= (sal_Int16)TemplateDescription::FILESAVE_AUTOEXTENSION;	// TemplateDescription.FILEOPEN_SIMPLE
72 
73 	mxFilePicker = Reference < XFilePicker >( mxMSF->getServiceManager()->createInstanceWithArgumentsAndContext(
74 		OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.ui.dialogs.FilePicker" ) ), aInitPropSeq, rxMSF ),UNO_QUERY_THROW );
75 	mxFilePicker->setMultiSelectionMode( sal_False );
76 
77 	Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
78 	if ( xAccess.is() )
79 	{
80 		Any aValue( static_cast< sal_Bool >( sal_True ) );
81 		try
82 		{
83 			xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, aValue );
84 		}
85 		catch( com::sun::star::uno::Exception& )
86 		{}
87 	}
88 
89 	// collecting a list of impress filters
90 	Reference< XNameAccess > xFilters( mxMSF->getServiceManager()->createInstanceWithContext(
91 		OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ), rxMSF ), UNO_QUERY_THROW );
92 	Sequence< OUString > aFilterList( xFilters->getElementNames() );
93 	for ( int i = 0; i < aFilterList.getLength(); i++ )
94 	{
95 		try
96 		{
97 			Sequence< PropertyValue > aFilterProperties;
98 			if ( xFilters->getByName( aFilterList[ i ] ) >>= aFilterProperties )
99 			{
100 				FilterEntry aFilterEntry;
101 				sal_Bool bImpressFilter = sal_False;
102 				for ( int j = 0; j < aFilterProperties.getLength(); j++ )
103 				{
104 					PropertyValue& rProperty( aFilterProperties[ j ] );
105 					switch( TKGet( rProperty.Name ) )
106 					{
107 						case TK_DocumentService :
108 						{
109 							rtl::OUString sDocumentService;
110 							rProperty.Value >>= sDocumentService;
111 							if ( sDocumentService == OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument" ) ) )
112 								bImpressFilter = sal_True;
113 							else
114 								j = aFilterProperties.getLength();
115 						}
116 						break;
117 						case TK_Name :		rProperty.Value >>= aFilterEntry.maName; break;
118 						case TK_UIName :	rProperty.Value >>= aFilterEntry.maUIName; break;
119 						case TK_Type :		rProperty.Value >>= aFilterEntry.maType; break;
120 						case TK_Flags :		rProperty.Value >>= aFilterEntry.maFlags; break;
121 						default : break;
122 					}
123 				}
124 				if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
125 				{
126 					aFilterEntryList.push_back( aFilterEntry );
127 				}
128 			}
129 		}
130 		catch( Exception& )
131 		{
132 		}
133 	}
134 
135 	Reference< XNameAccess > xTypes( mxMSF->getServiceManager()->createInstanceWithContext(
136 		OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ) ), rxMSF ), UNO_QUERY_THROW );
137 	Sequence< OUString > aTypeList( xFilters->getElementNames() );
138 
139 //	mxFilePicker->setDefaultName( );
140 
141 	Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
142 	std::vector< FilterEntry >::iterator aIter( aFilterEntryList.begin() );
143 	while( aIter != aFilterEntryList.end() )
144 	{
145 		Sequence< PropertyValue > aTypeProperties;
146 		try
147 		{
148 			if ( xTypes->getByName( aIter->maType ) >>= aTypeProperties )
149 			{
150 				Sequence< OUString > aExtensions;
151 				for ( int i = 0; i < aTypeProperties.getLength(); i++ )
152 				{
153 					switch( TKGet( aTypeProperties[ i ].Name ) )
154 					{
155 						case TK_Extensions : aTypeProperties[ i ].Value >>= aExtensions; break;
156 						default: break;
157 					}
158 				}
159 				if ( aExtensions.getLength() )
160 				{
161 					xFilterManager->appendFilter( aIter->maUIName, aExtensions[ 0 ] );
162 					if ( aIter->maFlags & 0x100 )
163 						xFilterManager->setCurrentFilter( aIter->maUIName );
164 				}
165 			}
166 		}
167 		catch ( Exception& )
168 		{
169 		}
170 		aIter++;
171 	}
172 }
173 FileOpenDialog::~FileOpenDialog()
174 {
175 }
176 sal_Int16 FileOpenDialog::execute()
177 {
178 	return mxFilePicker->execute();
179 }
180 void FileOpenDialog::setDefaultName( const rtl::OUString& rDefaultName )
181 {
182 	mxFilePicker->setDefaultName( rDefaultName );
183 }
184 ::rtl::OUString	FileOpenDialog::getURL() const
185 {
186 	Sequence< OUString > aFileSeq( mxFilePicker->getFiles() );
187 	return aFileSeq.getLength() ? aFileSeq[ 0 ] : OUString();
188 };
189 ::rtl::OUString	FileOpenDialog::getFilterName() const
190 {
191 	rtl::OUString aFilterName;
192 	Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
193 	rtl::OUString aUIName( xFilterManager->getCurrentFilter() );
194 	std::vector< FilterEntry >::const_iterator aIter( aFilterEntryList.begin() );
195 	while( aIter != aFilterEntryList.end() )
196 	{
197 		if ( aIter->maUIName == aUIName )
198 		{
199 			aFilterName = aIter->maName;
200 			break;
201 		}
202 		aIter++;
203 	}
204 	return aFilterName;
205 };
206