xref: /AOO41X/main/sdext/source/minimizer/fileopendialog.cxx (revision ca62e2c2083b5d0995f1245bad6c2edfb455fbec)
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 ::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 using ::com::sun::star::awt::XWindow;
68 using ::rtl::OUString;
69 
70 namespace
71 {
lcl_isSystemDialog(const Reference<XInterface> & rxIfce)72     inline bool lcl_isSystemDialog(
73         const Reference< XInterface > &rxIfce )
74     {
75         Reference< XServiceInfo > xInfo( rxIfce, UNO_QUERY );
76         if ( !xInfo.is() )
77             return false;
78 
79         return xInfo->supportsService(
80             OUString( RTL_CONSTASCII_USTRINGPARAM(
81                 "com.sun.star.ui.dialogs.SystemFilePicker" ) ) );
82     }
83 }
84 
FileOpenDialog(const Reference<XComponentContext> & rxContext,const Reference<XWindow> & rxParent)85 FileOpenDialog::FileOpenDialog(
86     const Reference< XComponentContext >& rxContext,
87     const Reference< XWindow > &rxParent )
88     : mxContext( rxContext )
89 {
90     mxFilePicker = Reference < XFilePicker >(
91         mxContext->getServiceManager()->createInstanceWithContext(
92             OUString( RTL_CONSTASCII_USTRINGPARAM(
93                 "com.sun.star.ui.dialogs.FilePicker" ) ),
94                     rxContext ), UNO_QUERY_THROW );
95     Reference< XInitialization > xInit( mxFilePicker, UNO_QUERY_THROW );
96     bool bIsSystemDlg = lcl_isSystemDialog( mxFilePicker );
97     Sequence< Any > aInitPropSeq( bIsSystemDlg ? 1 : 2 );
98     if ( bIsSystemDlg )
99     {
100         aInitPropSeq[0] <<= TemplateDescription::FILESAVE_AUTOEXTENSION;
101         xInit->initialize( aInitPropSeq );
102     }
103     else
104     {
105         aInitPropSeq[ 0 ] <<= NamedValue(
106             OUString(RTL_CONSTASCII_USTRINGPARAM("TemplateDescription")),
107             makeAny( (sal_Int16)TemplateDescription::FILESAVE_AUTOEXTENSION));
108         aInitPropSeq[ 1 ] <<= NamedValue(
109             OUString(RTL_CONSTASCII_USTRINGPARAM("ParentWindow")),
110             makeAny( rxParent ));
111         xInit->initialize( aInitPropSeq );
112     }
113 
114     mxFilePicker->setMultiSelectionMode( sal_False );
115 
116     Reference< XFilePickerControlAccess > xAccess( mxFilePicker, UNO_QUERY );
117     if ( xAccess.is() )
118     {
119         Any aValue( static_cast< sal_Bool >( sal_True ) );
120         try
121         {
122             xAccess->setValue( ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, aValue );
123         }
124         catch( com::sun::star::uno::Exception& )
125         {}
126     }
127 
128     // collecting a list of impress filters
129     Reference< XNameAccess > xFilters( mxContext->getServiceManager()->createInstanceWithContext(
130         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.FilterFactory" ) ), rxContext ), UNO_QUERY_THROW );
131     Sequence< OUString > aFilterList( xFilters->getElementNames() );
132     for ( int i = 0; i < aFilterList.getLength(); i++ )
133     {
134         try
135         {
136             Sequence< PropertyValue > aFilterProperties;
137             if ( xFilters->getByName( aFilterList[ i ] ) >>= aFilterProperties )
138             {
139                 FilterEntry aFilterEntry;
140                 sal_Bool bImpressFilter = sal_False;
141                 for ( int j = 0; j < aFilterProperties.getLength(); j++ )
142                 {
143                     PropertyValue& rProperty( aFilterProperties[ j ] );
144                     switch( TKGet( rProperty.Name ) )
145                     {
146                         case TK_DocumentService :
147                         {
148                             rtl::OUString sDocumentService;
149                             rProperty.Value >>= sDocumentService;
150                             if ( sDocumentService == OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.presentation.PresentationDocument" ) ) )
151                                 bImpressFilter = sal_True;
152                             else
153                                 j = aFilterProperties.getLength();
154                         }
155                         break;
156                         case TK_Name :      rProperty.Value >>= aFilterEntry.maName; break;
157                         case TK_UIName :    rProperty.Value >>= aFilterEntry.maUIName; break;
158                         case TK_Type :      rProperty.Value >>= aFilterEntry.maType; break;
159                         case TK_Flags :     rProperty.Value >>= aFilterEntry.maFlags; break;
160                         default : break;
161                     }
162                 }
163                 if ( bImpressFilter && ( ( aFilterEntry.maFlags & 3 ) == 3 ) )
164                 {
165                     aFilterEntryList.push_back( aFilterEntry );
166                 }
167             }
168         }
169         catch( Exception& )
170         {
171         }
172     }
173 
174     Reference< XNameAccess > xTypes( mxContext->getServiceManager()->createInstanceWithContext(
175         OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.document.TypeDetection" ) ), rxContext ), UNO_QUERY_THROW );
176     Sequence< OUString > aTypeList( xFilters->getElementNames() );
177 
178 //  mxFilePicker->setDefaultName( );
179 
180     const char filter[] = "*.";
181     // the filter title must be formed in the same way it is currently done
182     // in the internal implementation: "UIName (.<extension>)"
183     rtl::OUStringBuffer aUIName;
184     // the filter must be in the form "*.<extension>"
185     rtl::OUStringBuffer aFilter;
186     rtl::OUString aExtension;
187     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
188     std::vector< FilterEntry >::iterator aIter( aFilterEntryList.begin() );
189     while( aIter != aFilterEntryList.end() )
190     {
191         Sequence< PropertyValue > aTypeProperties;
192         try
193         {
194             if ( xTypes->getByName( aIter->maType ) >>= aTypeProperties )
195             {
196                 Sequence< OUString > aExtensions;
197                 for ( int i = 0; i < aTypeProperties.getLength(); i++ )
198                 {
199                     switch( TKGet( aTypeProperties[ i ].Name ) )
200                     {
201                         case TK_Extensions : aTypeProperties[ i ].Value >>= aExtensions; break;
202                         default: break;
203                     }
204                 }
205                 if ( aExtensions.getLength() )
206                 {
207                     aExtension = aExtensions[0];
208                     // form the title: "<UIName> (.<extension)"
209                     aUIName.append( aIter->maUIName );
210                     aUIName.appendAscii( RTL_CONSTASCII_STRINGPARAM( " (." ));
211                     aUIName.append( aExtension );
212                     aUIName.append( sal_Unicode( ')' ) );
213                     // form the filter: "(*.<extension>)"
214                     aFilter.appendAscii( RTL_CONSTASCII_STRINGPARAM( filter ) );
215                     aFilter.append( aExtensions[0] );
216 
217                     xFilterManager->appendFilter( aUIName.makeStringAndClear(),
218                                                   aFilter.makeStringAndClear() );
219                     if ( aIter->maFlags & 0x100 )
220                         xFilterManager->setCurrentFilter( aIter->maUIName );
221                 }
222             }
223         }
224         catch ( Exception& )
225         {
226         }
227         aIter++;
228     }
229 }
~FileOpenDialog()230 FileOpenDialog::~FileOpenDialog()
231 {
232 }
execute()233 sal_Int16 FileOpenDialog::execute()
234 {
235     return mxFilePicker->execute();
236 }
setDefaultName(const rtl::OUString & rDefaultName)237 void FileOpenDialog::setDefaultName( const rtl::OUString& rDefaultName )
238 {
239     mxFilePicker->setDefaultName( rDefaultName );
240 }
getURL() const241 ::rtl::OUString FileOpenDialog::getURL() const
242 {
243     Sequence< OUString > aFileSeq( mxFilePicker->getFiles() );
244     return aFileSeq.getLength() ? aFileSeq[ 0 ] : OUString();
245 };
getFilterName() const246 ::rtl::OUString FileOpenDialog::getFilterName() const
247 {
248     rtl::OUString aFilterName;
249     Reference< XFilterManager > xFilterManager( mxFilePicker, UNO_QUERY_THROW );
250     rtl::OUString aUIName( xFilterManager->getCurrentFilter() );
251     std::vector< FilterEntry >::const_iterator aIter( aFilterEntryList.begin() );
252     while( aIter != aFilterEntryList.end() )
253     {
254         if ( aIter->maUIName == aUIName )
255         {
256             aFilterName = aIter->maName;
257             break;
258         }
259         aIter++;
260     }
261     return aFilterName;
262 };
263