1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <osl/diagnose.h>
22 #include "getfilenamewrapper.hxx"
25 #pragma warning(push, 1)
33 namespace /* private */
36 //-----------------------------------------------
37 // This class prevents changing of the working
39 //-----------------------------------------------
48 : m_bValid( sal_False
)
52 m_nBufLen
= GetCurrentDirectoryW( 0, NULL
);
55 m_pBuffer
= new wchar_t[m_nBufLen
];
56 m_bValid
= ( GetCurrentDirectoryW( m_nBufLen
, m_pBuffer
) == ( m_nBufLen
- 1 ) );
68 if ( m_nBufLen
- 1 > MAX_PATH
)
70 DWORD nNewLen
= m_nBufLen
+ 8;
71 wchar_t* pNewBuffer
= new wchar_t[nNewLen
];
72 if ( m_nBufLen
> 3 && m_pBuffer
[0] == (wchar_t)'\\' && m_pBuffer
[1] == (wchar_t)'\\' )
74 if ( m_pBuffer
[2] == (wchar_t)'?' )
75 _snwprintf( pNewBuffer
, nNewLen
, L
"%s", m_pBuffer
);
77 _snwprintf( pNewBuffer
, nNewLen
, L
"\\\\?\\UNC\\%s", m_pBuffer
+2 );
80 _snwprintf( pNewBuffer
, nNewLen
, L
"\\\\?\\%s", m_pBuffer
);
81 bDirSet
= SetCurrentDirectoryW( pNewBuffer
);
86 bDirSet
= SetCurrentDirectoryW( m_pBuffer
);
95 // the fallback solution
96 wchar_t pPath
[MAX_PATH
+1];
97 if ( GetWindowsDirectoryW( pPath
, MAX_PATH
+1 ) <= MAX_PATH
)
99 SetCurrentDirectoryW( pPath
);
103 // the system path is also too long?!!
109 //-----------------------------------------------
111 //-----------------------------------------------
113 struct GetFileNameParam
115 GetFileNameParam(bool bOpen
, LPOPENFILENAME lpofn
) :
123 LPOPENFILENAME m_lpofn
;
128 //-----------------------------------------------
130 //-----------------------------------------------
132 unsigned __stdcall
ThreadProc(void* pParam
)
136 GetFileNameParam
* lpgfnp
=
137 reinterpret_cast<GetFileNameParam
*>(pParam
);
139 HRESULT hr
= OleInitialize( NULL
);
142 lpgfnp
->m_bRet
= GetOpenFileName(lpgfnp
->m_lpofn
);
144 lpgfnp
->m_bRet
= GetSaveFileName(lpgfnp
->m_lpofn
);
146 lpgfnp
->m_ExtErr
= CommDlgExtendedError();
148 if ( SUCCEEDED( hr
) )
154 //-----------------------------------------------
155 // exceutes GetOpenFileName/GetSaveFileName in
157 //-----------------------------------------------
159 bool ThreadExecGetFileName(LPOPENFILENAME lpofn
, bool bOpen
, /*out*/ int& ExtErr
)
161 GetFileNameParam
gfnp(bOpen
,lpofn
);
164 HANDLE hThread
= reinterpret_cast<HANDLE
>(
165 _beginthreadex(0, 0, ThreadProc
, &gfnp
, 0, &id
));
167 OSL_POSTCOND(hThread
, "could not create STA thread");
169 WaitForSingleObject(hThread
, INFINITE
);
170 CloseHandle(hThread
);
172 ExtErr
= gfnp
.m_ExtErr
;
177 //-----------------------------------------------
178 // This function returns true if the calling
179 // thread belongs to a Multithreaded Appartment
181 //-----------------------------------------------
185 HRESULT hr
= CoInitialize(NULL
);
187 if (RPC_E_CHANGED_MODE
== hr
)
196 } // namespace private
199 //-----------------------------------------------
201 //-----------------------------------------------
203 CGetFileNameWrapper::CGetFileNameWrapper() :
204 m_ExtendedDialogError(0)
208 //-----------------------------------------------
210 //-----------------------------------------------
212 bool CGetFileNameWrapper::getOpenFileName(LPOPENFILENAME lpofn
)
214 OSL_PRECOND(lpofn
,"invalid parameter");
220 bRet
= ThreadExecGetFileName(
221 lpofn
, true, m_ExtendedDialogError
);
227 HRESULT hr
= OleInitialize( NULL
);
229 bRet
= GetOpenFileName(lpofn
);
230 m_ExtendedDialogError
= CommDlgExtendedError();
232 if ( SUCCEEDED( hr
) )
239 //-----------------------------------------------
241 //-----------------------------------------------
243 bool CGetFileNameWrapper::getSaveFileName(LPOPENFILENAME lpofn
)
245 OSL_PRECOND(lpofn
,"invalid parameter");
251 bRet
= ThreadExecGetFileName(
252 lpofn
, false, m_ExtendedDialogError
);
258 bRet
= GetSaveFileName(lpofn
);
259 m_ExtendedDialogError
= CommDlgExtendedError();
265 //-----------------------------------------------
267 //-----------------------------------------------
269 int CGetFileNameWrapper::commDlgExtendedError( )
271 return m_ExtendedDialogError
;
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */