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_svtools.hxx"
26 
27 // this file contains code from filectrl.cxx which needs to be compiled with enabled exception hanling
28 #include <svtools/filectrl.hxx>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
31 #include <vcl/unohelp.hxx>
32 #include <tools/urlobj.hxx>
33 #include <osl/file.h>
34 #include <vcl/stdtext.hxx>
35 #include <tools/debug.hxx>
36 
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::ui;
40 
ImplBrowseFile()41 void FileControl::ImplBrowseFile( )
42 {
43 	try
44 	{
45 		XubString aNewText;
46 
47 		const ::rtl::OUString sServiceName = ::rtl::OUString::createFromAscii( "com.sun.star.ui.dialogs.FilePicker" );
48 
49 		Reference< XMultiServiceFactory > xMSF = vcl::unohelper::GetMultiServiceFactory();
50 		Reference < dialogs::XFilePicker > xFilePicker( xMSF->createInstance( sServiceName ), UNO_QUERY );
51 		if ( xFilePicker.is() )
52 		{
53 			// transform the system notation text into a file URL
54 			::rtl::OUString sSystemNotation = GetText(), sFileURL;
55 			oslFileError nError = osl_getFileURLFromSystemPath( sSystemNotation.pData, &sFileURL.pData );
56             if ( nError == osl_File_E_INVAL )
57                 sFileURL = GetText();   // #97709# Maybe URL is already a file URL...
58 
59             //#90430# Check if URL is really a file URL
60             ::rtl::OUString aTmp;
61             if ( osl_getSystemPathFromFileURL( sFileURL.pData, &aTmp.pData ) == osl_File_E_None )
62             {
63 			    // initially set this directory
64 			    xFilePicker->setDisplayDirectory( sFileURL );
65             }
66 
67 			if ( xFilePicker.is() && xFilePicker->execute() )
68 			{
69 				Sequence < rtl::OUString > aPathSeq = xFilePicker->getFiles();
70 
71 				if ( aPathSeq.getLength() )
72 				{
73 					aNewText = aPathSeq[0];
74 					INetURLObject aObj( aNewText );
75 					if ( aObj.GetProtocol() == INET_PROT_FILE )
76 						aNewText = aObj.PathToFileName();
77 					SetText( aNewText );
78 					maEdit.GetModifyHdl().Call( &maEdit );
79 				}
80 			}
81 		}
82 		else
83 			ShowServiceNotAvailableError( this, sServiceName, sal_True );
84 	}
85 	catch( const Exception& )
86 	{
87 		DBG_ERROR( "FileControl::ImplBrowseFile: caught an exception while executing the file picker!" );
88 	}
89 }
90 
91