update dev300-m58
[ooovba.git] / fpicker / source / win32 / filepicker / getfilenamewrapper.cxx
bloba9b467316b32ed8955604e73ddc085c2a249acc0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: getfilenamewrapper.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_fpicker.hxx"
34 //------------------------------------------------------------------------
35 // includes
36 //------------------------------------------------------------------------
38 #include <stdio.h>
39 #include <osl/diagnose.h>
40 #include "getfilenamewrapper.hxx"
42 #if defined _MSC_VER
43 #pragma warning(push, 1)
44 #endif
45 #include <objbase.h>
46 #include <process.h>
47 #if defined _MSC_VER
48 #pragma warning(pop)
49 #endif
51 namespace /* private */
54 //-----------------------------------------------
55 // This class prevents changing of the working
56 // directory.
57 //-----------------------------------------------
58 class CurDirGuard
60 BOOL m_bValid;
61 wchar_t* m_pBuffer;
62 DWORD m_nBufLen;
64 public:
65 CurDirGuard()
66 : m_bValid( FALSE )
67 , m_pBuffer( NULL )
68 , m_nBufLen( 0 )
70 m_nBufLen = GetCurrentDirectoryW( 0, NULL );
71 if ( m_nBufLen )
73 m_pBuffer = new wchar_t[m_nBufLen];
74 m_bValid = ( GetCurrentDirectoryW( m_nBufLen, m_pBuffer ) == ( m_nBufLen - 1 ) );
78 ~CurDirGuard()
80 BOOL bDirSet = FALSE;
82 if ( m_pBuffer )
84 if ( m_bValid )
86 if ( m_nBufLen - 1 > MAX_PATH )
88 if ( (LONG32)GetVersion() < 0 )
90 // this is Win 98/ME branch, such a long path can not be set
91 // use the system path as fallback later
93 else
95 DWORD nNewLen = m_nBufLen + 8;
96 wchar_t* pNewBuffer = new wchar_t[nNewLen];
97 if ( m_nBufLen > 3 && m_pBuffer[0] == (wchar_t)'\\' && m_pBuffer[1] == (wchar_t)'\\' )
99 if ( m_pBuffer[2] == (wchar_t)'?' )
100 _snwprintf( pNewBuffer, nNewLen, L"%s", m_pBuffer );
101 else
102 _snwprintf( pNewBuffer, nNewLen, L"\\\\?\\UNC\\%s", m_pBuffer+2 );
104 else
105 _snwprintf( pNewBuffer, nNewLen, L"\\\\?\\%s", m_pBuffer );
106 bDirSet = SetCurrentDirectoryW( pNewBuffer );
108 delete [] pNewBuffer;
111 else
112 bDirSet = SetCurrentDirectoryW( m_pBuffer );
115 delete [] m_pBuffer;
116 m_pBuffer = NULL;
119 if ( !bDirSet )
121 // the fallback solution
122 wchar_t pPath[MAX_PATH+1];
123 if ( GetWindowsDirectoryW( pPath, MAX_PATH+1 ) <= MAX_PATH )
125 SetCurrentDirectoryW( pPath );
127 else
129 // the system path is also too long?!!
135 //-----------------------------------------------
137 //-----------------------------------------------
139 struct GetFileNameParam
141 GetFileNameParam(bool bOpen, LPOPENFILENAME lpofn) :
142 m_bOpen(bOpen),
143 m_lpofn(lpofn),
144 m_bRet(false),
145 m_ExtErr(0)
148 bool m_bOpen;
149 LPOPENFILENAME m_lpofn;
150 bool m_bRet;
151 int m_ExtErr;
154 //-----------------------------------------------
156 //-----------------------------------------------
158 unsigned __stdcall ThreadProc(void* pParam)
160 CurDirGuard aGuard;
162 GetFileNameParam* lpgfnp =
163 reinterpret_cast<GetFileNameParam*>(pParam);
165 HRESULT hr = OleInitialize( NULL );
167 if (lpgfnp->m_bOpen)
168 lpgfnp->m_bRet = GetOpenFileName(lpgfnp->m_lpofn);
169 else
170 lpgfnp->m_bRet = GetSaveFileName(lpgfnp->m_lpofn);
172 lpgfnp->m_ExtErr = CommDlgExtendedError();
174 if ( SUCCEEDED( hr ) )
175 OleUninitialize();
177 return 0;
180 //-----------------------------------------------
181 // exceutes GetOpenFileName/GetSaveFileName in
182 // a separat thread
183 //-----------------------------------------------
185 bool ThreadExecGetFileName(LPOPENFILENAME lpofn, bool bOpen, /*out*/ int& ExtErr)
187 GetFileNameParam gfnp(bOpen,lpofn);
188 unsigned id;
190 HANDLE hThread = reinterpret_cast<HANDLE>(
191 _beginthreadex(0, 0, ThreadProc, &gfnp, 0, &id));
193 OSL_POSTCOND(hThread, "could not create STA thread");
195 WaitForSingleObject(hThread, INFINITE);
196 CloseHandle(hThread);
198 ExtErr = gfnp.m_ExtErr;
200 return gfnp.m_bRet;
203 //-----------------------------------------------
204 // This function returns true if the calling
205 // thread belongs to a Multithreaded Appartment
206 // (MTA)
207 //-----------------------------------------------
209 bool IsMTA()
211 HRESULT hr = CoInitialize(NULL);
213 if (RPC_E_CHANGED_MODE == hr)
214 return true;
216 if(SUCCEEDED(hr))
217 CoUninitialize();
219 return false;
222 } // namespace private
225 //-----------------------------------------------
227 //-----------------------------------------------
229 CGetFileNameWrapper::CGetFileNameWrapper() :
230 m_ExtendedDialogError(0)
234 //-----------------------------------------------
236 //-----------------------------------------------
238 bool CGetFileNameWrapper::getOpenFileName(LPOPENFILENAME lpofn)
240 OSL_PRECOND(lpofn,"invalid parameter");
242 bool bRet = false;
244 if (IsMTA())
246 bRet = ThreadExecGetFileName(
247 lpofn, true, m_ExtendedDialogError);
249 else
251 CurDirGuard aGuard;
253 HRESULT hr = OleInitialize( NULL );
255 bRet = GetOpenFileName(lpofn);
256 m_ExtendedDialogError = CommDlgExtendedError();
258 if ( SUCCEEDED( hr ) )
259 OleUninitialize();
262 return bRet;
265 //-----------------------------------------------
267 //-----------------------------------------------
269 bool CGetFileNameWrapper::getSaveFileName(LPOPENFILENAME lpofn)
271 OSL_PRECOND(lpofn,"invalid parameter");
273 bool bRet = false;
275 if (IsMTA())
277 bRet = ThreadExecGetFileName(
278 lpofn, false, m_ExtendedDialogError);
280 else
282 CurDirGuard aGuard;
284 bRet = GetSaveFileName(lpofn);
285 m_ExtendedDialogError = CommDlgExtendedError();
288 return bRet;
291 //-----------------------------------------------
293 //-----------------------------------------------
295 int CGetFileNameWrapper::commDlgExtendedError( )
297 return m_ExtendedDialogError;