1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: getfilenamewrapper.cxx,v $
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 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
39 #include <osl/diagnose.h>
40 #include "getfilenamewrapper.hxx"
43 #pragma warning(push, 1)
51 namespace /* private */
54 //-----------------------------------------------
55 // This class prevents changing of the working
57 //-----------------------------------------------
70 m_nBufLen
= GetCurrentDirectoryW( 0, NULL
);
73 m_pBuffer
= new wchar_t[m_nBufLen
];
74 m_bValid
= ( GetCurrentDirectoryW( m_nBufLen
, m_pBuffer
) == ( m_nBufLen
- 1 ) );
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
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
);
102 _snwprintf( pNewBuffer
, nNewLen
, L
"\\\\?\\UNC\\%s", m_pBuffer
+2 );
105 _snwprintf( pNewBuffer
, nNewLen
, L
"\\\\?\\%s", m_pBuffer
);
106 bDirSet
= SetCurrentDirectoryW( pNewBuffer
);
108 delete [] pNewBuffer
;
112 bDirSet
= SetCurrentDirectoryW( m_pBuffer
);
121 // the fallback solution
122 wchar_t pPath
[MAX_PATH
+1];
123 if ( GetWindowsDirectoryW( pPath
, MAX_PATH
+1 ) <= MAX_PATH
)
125 SetCurrentDirectoryW( pPath
);
129 // the system path is also too long?!!
135 //-----------------------------------------------
137 //-----------------------------------------------
139 struct GetFileNameParam
141 GetFileNameParam(bool bOpen
, LPOPENFILENAME lpofn
) :
149 LPOPENFILENAME m_lpofn
;
154 //-----------------------------------------------
156 //-----------------------------------------------
158 unsigned __stdcall
ThreadProc(void* pParam
)
162 GetFileNameParam
* lpgfnp
=
163 reinterpret_cast<GetFileNameParam
*>(pParam
);
165 HRESULT hr
= OleInitialize( NULL
);
168 lpgfnp
->m_bRet
= GetOpenFileName(lpgfnp
->m_lpofn
);
170 lpgfnp
->m_bRet
= GetSaveFileName(lpgfnp
->m_lpofn
);
172 lpgfnp
->m_ExtErr
= CommDlgExtendedError();
174 if ( SUCCEEDED( hr
) )
180 //-----------------------------------------------
181 // exceutes GetOpenFileName/GetSaveFileName in
183 //-----------------------------------------------
185 bool ThreadExecGetFileName(LPOPENFILENAME lpofn
, bool bOpen
, /*out*/ int& ExtErr
)
187 GetFileNameParam
gfnp(bOpen
,lpofn
);
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
;
203 //-----------------------------------------------
204 // This function returns true if the calling
205 // thread belongs to a Multithreaded Appartment
207 //-----------------------------------------------
211 HRESULT hr
= CoInitialize(NULL
);
213 if (RPC_E_CHANGED_MODE
== hr
)
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");
246 bRet
= ThreadExecGetFileName(
247 lpofn
, true, m_ExtendedDialogError
);
253 HRESULT hr
= OleInitialize( NULL
);
255 bRet
= GetOpenFileName(lpofn
);
256 m_ExtendedDialogError
= CommDlgExtendedError();
258 if ( SUCCEEDED( hr
) )
265 //-----------------------------------------------
267 //-----------------------------------------------
269 bool CGetFileNameWrapper::getSaveFileName(LPOPENFILENAME lpofn
)
271 OSL_PRECOND(lpofn
,"invalid parameter");
277 bRet
= ThreadExecGetFileName(
278 lpofn
, false, m_ExtendedDialogError
);
284 bRet
= GetSaveFileName(lpofn
);
285 m_ExtendedDialogError
= CommDlgExtendedError();
291 //-----------------------------------------------
293 //-----------------------------------------------
295 int CGetFileNameWrapper::commDlgExtendedError( )
297 return m_ExtendedDialogError
;