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_fpicker.hxx"
26 
27 //------------------------------------------------------------------------
28 // includes
29 //------------------------------------------------------------------------
30 
31 #ifndef _WINDIRBROWSEIMPL_HXX_
32 #include "WinFOPImpl.hxx"
33 #endif
34 #include <osl/diagnose.h>
35 #include <com/sun/star/lang/EventObject.hpp>
36 
37 #ifndef _COM_SUN_STAR_UI_FILEDIALOGRESULTS_HPP_
38 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
39 #endif
40 #include "FopEvtDisp.hxx"
41 #include <osl/file.hxx>
42 #include "FolderPicker.hxx"
43 
44 //------------------------------------------------------------------------
45 // namespace directives
46 //------------------------------------------------------------------------
47 
48 using com::sun::star::uno::RuntimeException;
49 using com::sun::star::lang::IllegalArgumentException;
50 using com::sun::star::lang::EventObject;
51 using rtl::OUString;
52 
53 using namespace com::sun::star::ui::dialogs;
54 using osl::FileBase;
55 
56 //------------------------------------------------------------------------
57 //
58 //------------------------------------------------------------------------
59 
60 const OUString BACKSLASH = OUString::createFromAscii( "\\" );
61 
62 //------------------------------------------------------------------------
63 // ctor
64 //------------------------------------------------------------------------
65 
CWinFolderPickerImpl(CFolderPicker * aFolderPicker)66 CWinFolderPickerImpl::CWinFolderPickerImpl( CFolderPicker* aFolderPicker ) :
67    CMtaFolderPicker( BIF_RETURNONLYFSDIRS | BIF_RETURNFSANCESTORS | BIF_EDITBOX | BIF_VALIDATE ),
68    m_pFolderPicker( aFolderPicker ),
69    m_nLastDlgResult( ::com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL )
70 {
71 }
72 
73 //------------------------------------------------------------------------
74 // get directory in URL format, convert it to system format and set the
75 // member variable
76 // If the given URL for the directory is invalid the function throws an
77 // IllegalArgumentException
78 // If the specified path is well formed but invalid for the underlying
79 // OS the FolderPicker starts in the root of the file system hierarchie
80 //------------------------------------------------------------------------
81 
setDisplayDirectory(const OUString & aDirectory)82 void SAL_CALL CWinFolderPickerImpl::setDisplayDirectory( const OUString& aDirectory )
83 	throw( IllegalArgumentException, RuntimeException )
84 {
85 	OUString sysDir;
86 
87 	if( aDirectory.getLength( ) )
88 	{
89         // assuming that this function succeeds after successful execution
90         // of getAbsolutePath
91         ::osl::FileBase::RC rc =
92             ::osl::FileBase::getSystemPathFromFileURL( aDirectory, sysDir );
93 
94         if ( ::osl::FileBase::E_None != rc )
95             throw IllegalArgumentException(
96                 OUString::createFromAscii( "directory is not a valid file url" ),
97                 static_cast< XFolderPicker* >( m_pFolderPicker ),
98                 1 );
99 
100         // we ensure that there is a trailing '/' at the end of
101         // he given file url, because the windows functions only
102         // works correctly when providing "c:\" or an environment
103         // variable like "=c:=c:\.." etc. is set, else the
104         // FolderPicker would stand in the root of the shell
105         // hierarchie which is the desktop folder
106         if ( sysDir.lastIndexOf( BACKSLASH ) != (sysDir.getLength( ) - 1) )
107             sysDir += BACKSLASH;
108 	}
109 
110 	// call base class method
111 	CMtaFolderPicker::setDisplayDirectory( sysDir );
112 }
113 
114 //------------------------------------------------------------------------
115 // we return the directory in URL format
116 //------------------------------------------------------------------------
117 
getDisplayDirectory()118 OUString CWinFolderPickerImpl::getDisplayDirectory( )
119 	throw( RuntimeException )
120 {
121 	// call base class method to get the directory in system format
122 	OUString displayDirectory = CMtaFolderPicker::getDisplayDirectory( );
123 
124 	OUString displayDirectoryURL;
125 	if ( displayDirectory.getLength( ) )
126         ::osl::FileBase::getFileURLFromSystemPath( displayDirectory, displayDirectoryURL );
127 
128 	return displayDirectoryURL;
129 }
130 
131 //------------------------------------------------------------------------
132 //
133 //------------------------------------------------------------------------
134 
getDirectory()135 OUString SAL_CALL CWinFolderPickerImpl::getDirectory( ) throw( RuntimeException )
136 {
137     OUString sysDir = CMtaFolderPicker::getDirectory( );
138     OUString dirURL;
139 
140     if ( sysDir.getLength( ) )
141         ::osl::FileBase::getFileURLFromSystemPath( sysDir, dirURL );
142 
143     return dirURL;
144 }
145 
146 //------------------------------------------------------------------------
147 //
148 //------------------------------------------------------------------------
149 
execute()150 sal_Int16 SAL_CALL CWinFolderPickerImpl::execute( ) throw( RuntimeException )
151 {
152 	return m_nLastDlgResult = CMtaFolderPicker::browseForFolder( ) ?
153         ::com::sun::star::ui::dialogs::ExecutableDialogResults::OK :
154         ::com::sun::star::ui::dialogs::ExecutableDialogResults::CANCEL;
155 }
156 
157 //---------------------------------------------------------------------
158 //
159 //---------------------------------------------------------------------
160 
onSelChanged(const OUString & aNewPath)161 void CWinFolderPickerImpl::onSelChanged( const OUString& aNewPath )
162 {
163 	setStatusText( aNewPath );
164 }
165