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