1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <osl/diagnose.h>
#include <osl/thread.h>
#include <o3tl/char16_t2wchar_t.hxx>
#include "MtaFop.hxx"
#include <wchar.h>
#include <process.h>
#include "../misc/resourceprovider.hxx"
#include <systools/win32/comtools.hxx>
const sal_uInt32 MSG_BROWSEFORFOLDER = WM_USER + 1;
const sal_uInt32 MSG_SHUTDOWN = WM_USER + 2;
const sal_uInt32 MAX_WAITTIME = 2000; // msec
const bool MANUAL_RESET = true;
const bool AUTO_RESET = false;
const bool INIT_NONSIGNALED = false;
#define FOLDERPICKER_TITLE 500
#define FOLDER_PICKER_DEF_DESCRIPTION 501
namespace
{
const wchar_t* const FOLDERPICKER_SRV_DLL_NAME = L"fps.dll";
const wchar_t g_szWndClsName[] = L"FopStaReqWnd###";
const wchar_t* const CURRENT_INSTANCE = L"CurrInst";
struct RequestContext
{
HANDLE hEvent;
bool bRet;
};
inline bool InitializeRequestContext( RequestContext* aRequestContext )
{
OSL_ASSERT( aRequestContext );
aRequestContext->hEvent = CreateEventW(
nullptr, AUTO_RESET, INIT_NONSIGNALED, nullptr );
aRequestContext->bRet = false;
return ( nullptr != aRequestContext->hEvent );
}
inline void DeinitializeRequestContext( RequestContext* aRequestContext )
{
OSL_ASSERT( aRequestContext && aRequestContext->hEvent );
CloseHandle( aRequestContext->hEvent );
}
// Determine if current thread is
// an MTA or STA thread
bool IsMTA()
{
HRESULT hr = CoInitialize(nullptr);
if (RPC_E_CHANGED_MODE == hr)
return true;
if(SUCCEEDED(hr))
CoUninitialize();
return false;
}
}
// static member initialization
ATOM CMtaFolderPicker::s_ClassAtom = 0;
osl::Mutex CMtaFolderPicker::s_Mutex;
sal_Int32 CMtaFolderPicker::s_StaRequestWndRegisterCount = 0;
// ctor
CMtaFolderPicker::CMtaFolderPicker( sal_uInt32 Flags ) :
m_hStaThread( nullptr ),
m_uStaThreadId( 0 ),
m_hEvtThrdReady( nullptr ),
m_hwndStaRequestWnd( nullptr )
{
m_hInstance = GetModuleHandleW( FOLDERPICKER_SRV_DLL_NAME );
OSL_ENSURE( m_hInstance, "The name of the FolderPicker service dll must have changed" );
ZeroMemory( &m_bi, sizeof( m_bi ) );
// !!!!!!!!!!!!!!!!! IMPORTANT !!!!!!!!!!!!!!!!!!!
// Remember: This HACK prevents you from stepping
// through your code in the debugger because if you
// set a break point in the ctor here the debugger
// may become the owner of the FolderBrowse dialog
// and so it seems that the Visual Studio and the
// office are hanging
m_bi.hwndOwner = GetForegroundWindow( );
/*
Flag Available
--------------------------------
BIF_EDITBOX Version 4.71
BIF_NEWDIALOGSTYLE Version 5.0
BIF_SHAREABLE Version 5.0
BIF_VALIDATE Version 4.71
Version 4.71 - Internet Explorer 4.0
Version 5.0 - Internet Explorer 5.0
Windows 2000
*/
m_bi.ulFlags = Flags;
m_bi.lpfn = CMtaFolderPicker::FolderPickerCallback;
m_bi.lParam = reinterpret_cast< LPARAM >( this );
m_dialogTitle = CResourceProvider::getResString(FOLDERPICKER_TITLE);
m_Description = CResourceProvider::getResString(FOLDER_PICKER_DEF_DESCRIPTION);
// signals that the thread was successfully set up
m_hEvtThrdReady = CreateEventW(
nullptr,
MANUAL_RESET,
INIT_NONSIGNALED,
nullptr );
if ( m_hEvtThrdReady )
{
// setup the sta thread
m_hStaThread = reinterpret_cast<HANDLE>(_beginthreadex(
nullptr,
0,
CMtaFolderPicker::StaThreadProc,
this,
0,
&m_uStaThreadId ));
OSL_ASSERT( m_hStaThread );
}
OSL_ASSERT( m_hEvtThrdReady );
}
// dtor
CMtaFolderPicker::~CMtaFolderPicker( )
{
// only if the is a valid event handle
// there may also be a thread a hidden
// target request window and so on
// see ctor
if ( m_hEvtThrdReady )
{
// block calling threads because we
// are about to shutdown
ResetEvent( m_hEvtThrdReady );
// force the destruction of the sta thread request window
// and the end of the thread
// remember: DestroyWindow may only be called from within
// the thread that created the window
if ( IsWindow( m_hwndStaRequestWnd ) )
{
SendMessageW( m_hwndStaRequestWnd, MSG_SHUTDOWN, 0, 0 );
// we place unregister class here because
// if we have a valid window we must have
// successfully registered a window class
// if the creation of the window itself
// failed after registering the window
// class we have unregistered it immediately
// in createStaRequestWindow below
UnregisterStaRequestWindowClass( );
}
if ( m_hStaThread )
{
// wait for thread shutdown
sal_uInt32 dwResult = WaitForSingleObject( m_hStaThread, MAX_WAITTIME );
OSL_ENSURE( dwResult == WAIT_OBJECT_0, "sta thread could not terminate" );
// terminate the thread if it
// doesn't shutdown itself
if ( WAIT_OBJECT_0 != dwResult )
TerminateThread(
m_hStaThread, sal::static_int_cast< DWORD >(-1) );
CloseHandle( m_hStaThread );
}
CloseHandle( m_hEvtThrdReady );
}
}
bool CMtaFolderPicker::browseForFolder( )
{
bool bRet = false;
if (IsMTA())
{
OSL_ASSERT( m_hEvtThrdReady );
if ( WaitForSingleObject( m_hEvtThrdReady, MAX_WAITTIME ) != WAIT_OBJECT_0 )
{
OSL_FAIL( "sta thread not ready" );
return false;
}
RequestContext aReqCtx;
if ( !InitializeRequestContext( &aReqCtx ) )
{
OSL_ASSERT( false );
return false;
}
// marshall request into the sta thread
BOOL const ret = PostMessageW(
m_hwndStaRequestWnd,
MSG_BROWSEFORFOLDER,
0,
reinterpret_cast< LPARAM >( &aReqCtx ) );
SAL_WARN_IF(0 == ret, "fpicker", "ERROR: PostMessage() failed!");
// waiting for the event to be signaled or
// window messages so that we don't block
// our parent window
bool bContinue = true;
while ( bContinue )
{
DWORD dwResult = MsgWaitForMultipleObjects(
1, &aReqCtx.hEvent, false, INFINITE, QS_ALLEVENTS );
switch ( dwResult )
{
// the request context event is signaled
case WAIT_OBJECT_0:
bContinue = false;
break;
// a window message has arrived
case WAIT_OBJECT_0 + 1:
{
// dispatching all messages but we expect to
// receive only paint or timer messages that's
// why we don't need to call TranslateMessage or
// TranslateAccelerator, because keyboard or
// mouse messages are for the FolderPicker which
// is in the foreground and should not arrive here
MSG msg;
while ( PeekMessageW( &msg, nullptr, 0, 0, PM_REMOVE ) )
DispatchMessageW(&msg);
}
break;
// should not happen
default:
OSL_ASSERT( false );
}
}
/*sal_Bool*/ bRet = aReqCtx.bRet;
DeinitializeRequestContext( &aReqCtx );
}
else
{
bRet = onBrowseForFolder();
}
return bRet;
}
void SAL_CALL CMtaFolderPicker::setDisplayDirectory( const OUString& aDirectory )
{
m_displayDir = aDirectory;
}
OUString SAL_CALL CMtaFolderPicker::getDisplayDirectory( )
{
return m_displayDir;
}
OUString SAL_CALL CMtaFolderPicker::getDirectory( )
{
return m_SelectedDir;
}
void SAL_CALL CMtaFolderPicker::setDescription( const OUString& aDescription )
{
m_Description = aDescription;
}
void SAL_CALL CMtaFolderPicker::setTitle( const OUString& aTitle )
{
m_dialogTitle = aTitle;
}
OUString SAL_CALL CMtaFolderPicker::getTitle( )
{
return m_dialogTitle;
}
// XCancellable
void SAL_CALL CMtaFolderPicker::cancel( )
{
if ( IsWindow( m_hwnd ) )
{
// simulate a mouse click to the
// cancel button
BOOL const ret = PostMessageW(
m_hwnd,
WM_COMMAND,
MAKEWPARAM( IDCANCEL, BN_CLICKED ),
reinterpret_cast<LPARAM>(GetDlgItem( m_hwnd, IDCANCEL )) );
SAL_WARN_IF(0 == ret, "fpicker", "ERROR: PostMessage() failed!");
}
}
bool SAL_CALL CMtaFolderPicker::onBrowseForFolder( )
{
bool bRet;
LPITEMIDLIST lpiid;
// pre SHBrowseFroFolder
m_bi.pidlRoot = nullptr;
m_bi.pszDisplayName = o3tl::toW(m_pathBuff);
if ( m_Description.getLength( ) )
m_bi.lpszTitle = o3tl::toW(m_Description.getStr( ));
lpiid = SHBrowseForFolderW( &m_bi );
bRet = ( nullptr != lpiid );
// post SHBrowseForFolder
m_SelectedDir = getPathFromItemIdList( lpiid );
releaseItemIdList( lpiid );
return bRet;
}
void SAL_CALL CMtaFolderPicker::releaseItemIdList( LPITEMIDLIST lpItemIdList )
{
sal::systools::COMReference<IMalloc> pIMalloc;
SHGetMalloc(&pIMalloc);
if (pIMalloc.is())
{
pIMalloc->Free(lpItemIdList);
lpItemIdList = nullptr;
}
}
LPITEMIDLIST SAL_CALL CMtaFolderPicker::getItemIdListFromPath( const OUString& aDirectory )
{
// parameter checking
if ( !aDirectory.getLength( ) )
return nullptr;
LPITEMIDLIST lpItemIdList(nullptr);
sal::systools::COMReference<IShellFolder> pIShellFolder;
SHGetDesktopFolder(&pIShellFolder);
if (pIShellFolder.is())
{
pIShellFolder->ParseDisplayName(
nullptr,
nullptr,
const_cast<LPWSTR>(o3tl::toW( aDirectory.getStr( ) )),
nullptr,
&lpItemIdList,
nullptr );
}
return lpItemIdList;
}
OUString SAL_CALL CMtaFolderPicker::getPathFromItemIdList( LPCITEMIDLIST lpItemIdList )
{
OUString path;
if ( lpItemIdList )
{
bool bRet = SHGetPathFromIDListW( lpItemIdList, o3tl::toW(m_pathBuff) );
if ( bRet )
path = m_pathBuff;
}
return path;
}
void SAL_CALL CMtaFolderPicker::enableOk( bool bEnable )
{
OSL_ASSERT( IsWindow( m_hwnd ) );
SendMessageW(
m_hwnd,
BFFM_ENABLEOK,
static_cast< WPARAM >( 0 ),
static_cast< LPARAM >( bEnable ) );
}
void SAL_CALL CMtaFolderPicker::setSelection( const OUString& aDirectory )
{
OSL_ASSERT( IsWindow( m_hwnd ) );
SendMessageW(
m_hwnd,
BFFM_SETSELECTIONW,
static_cast< WPARAM >( true ),
reinterpret_cast< LPARAM >( aDirectory.getStr( ) ) );
}
void SAL_CALL CMtaFolderPicker::setStatusText( const OUString& aStatusText )
{
OSL_ASSERT( IsWindow( m_hwnd ) );
SendMessageW(
m_hwnd,
BFFM_SETSTATUSTEXTW,
static_cast< WPARAM >( 0 ),
reinterpret_cast< LPARAM >( aStatusText.getStr( ) ) );
}
void SAL_CALL CMtaFolderPicker::onInitialized( )
{
LPITEMIDLIST lpiidDisplayDir = getItemIdListFromPath( m_displayDir );
if ( lpiidDisplayDir )
{
SendMessageW(
m_hwnd,
BFFM_SETSELECTION,
(WPARAM)false,
reinterpret_cast<LPARAM>(lpiidDisplayDir) );
releaseItemIdList( lpiidDisplayDir );
}
}
sal_uInt32 CMtaFolderPicker::onValidateFailed()
{
// to be overwritten by subclasses
return 1;
}
int CALLBACK CMtaFolderPicker::FolderPickerCallback( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData )
{
CMtaFolderPicker* pImpl = reinterpret_cast< CMtaFolderPicker* >( lpData );
OSL_ASSERT( pImpl );
int nRC = 0;
switch( uMsg )
{
case BFFM_INITIALIZED:
pImpl->m_hwnd = hwnd;
pImpl->onInitialized( );
SetWindowTextW( hwnd, o3tl::toW(pImpl->m_dialogTitle.getStr()) );
break;
case BFFM_SELCHANGED:
pImpl->m_hwnd = hwnd;
pImpl->onSelChanged(
pImpl->getPathFromItemIdList(
reinterpret_cast< LPITEMIDLIST >( lParam ) ) );
break;
case BFFM_VALIDATEFAILEDW:
nRC = CMtaFolderPicker::onValidateFailed();
break;
default:
OSL_ASSERT( false );
}
return nRC;
}
// the window proc
LRESULT CALLBACK CMtaFolderPicker::StaWndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
LRESULT lResult = 0;
CMtaFolderPicker* pImpl = nullptr;
/*
we connect to the belonging class instance of this
window using SetProp, GetProp etc.
this may fail if somehow the class instance destroyed
before the window
*/
switch( uMsg )
{
case WM_CREATE:
{
LPCREATESTRUCTW lpcs =
reinterpret_cast< LPCREATESTRUCTW >( lParam );
OSL_ASSERT( lpcs->lpCreateParams );
// connect the instance handle to the window
SetPropW( hWnd, CURRENT_INSTANCE, lpcs->lpCreateParams );
}
break;
case WM_NCDESTROY:
// RemoveProp returns the saved value on success
pImpl = static_cast< CMtaFolderPicker* >(
RemovePropW( hWnd, CURRENT_INSTANCE ) );
OSL_ASSERT( pImpl && !IsBadReadPtr( pImpl, sizeof( CMtaFolderPicker ) ) );
break;
case MSG_BROWSEFORFOLDER:
{
RequestContext* aReqCtx = reinterpret_cast< RequestContext* >( lParam );
OSL_ASSERT( aReqCtx );
pImpl = static_cast< CMtaFolderPicker* >(
GetPropW( hWnd, CURRENT_INSTANCE ) );
OSL_ASSERT( pImpl && !IsBadReadPtr( pImpl, sizeof( CMtaFolderPicker ) ) );
aReqCtx->bRet = pImpl->onBrowseForFolder( );
SetEvent( aReqCtx->hEvent );
}
break;
case MSG_SHUTDOWN:
pImpl = static_cast< CMtaFolderPicker* >(
GetPropW( hWnd, CURRENT_INSTANCE ) );
OSL_ASSERT( pImpl && !IsBadReadPtr( pImpl, sizeof( CMtaFolderPicker ) ) );
DestroyWindow( pImpl->m_hwndStaRequestWnd );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
default:
lResult = DefWindowProcW( hWnd, uMsg, wParam, lParam );
break;
}
return lResult;
}
bool SAL_CALL CMtaFolderPicker::createStaRequestWindow( )
{
bool bIsWnd = false;
if ( RegisterStaRequestWindowClass( ) )
{
m_hwndStaRequestWnd = CreateWindowW(
g_szWndClsName, nullptr,
0, 0, 0, 0, 0,
nullptr, nullptr, m_hInstance,
this // provide the instance of the class
);
bIsWnd = IsWindow( m_hwndStaRequestWnd );
// we do immediately unregister the window class
// if the creation of the window fails because we
// don't want to spoil the register class counter
if ( !bIsWnd )
UnregisterStaRequestWindowClass( );
OSL_ENSURE( bIsWnd, "sta request window creation failed" );
}
return bIsWnd;
}
unsigned int CMtaFolderPicker::run( )
{
OSL_ASSERT( m_hEvtThrdReady );
// setup an sta environment
HRESULT hr = CoInitialize( nullptr );
// if we can't setup an sta environment
// we stop here and return
if ( FAILED( hr ) )
{
OSL_FAIL( "CoInitialize failed" );
return sal::static_int_cast< unsigned int >(-1);
}
unsigned int nRet;
if ( createStaRequestWindow( ) )
{
SetEvent( m_hEvtThrdReady );
// pumping messages
MSG msg;
while( GetMessageW( &msg, nullptr, 0, 0 ) )
DispatchMessageW( &msg );
nRet = 0;
}
else
{
OSL_FAIL( "failed to create sta thread" );
nRet = sal::static_int_cast< unsigned int >(-1);
}
// shutdown sta environment
CoUninitialize( );
return nRet;
}
unsigned int WINAPI CMtaFolderPicker::StaThreadProc( LPVOID pParam )
{
osl_setThreadName("fpicker CMtaFolderPicker::run()");
CMtaFolderPicker* pInst =
static_cast<CMtaFolderPicker*>( pParam );
OSL_ASSERT( pInst );
HRESULT hr = OleInitialize( nullptr );
unsigned int result = pInst->run( );
if ( SUCCEEDED( hr ) )
OleUninitialize();
return result;
}
ATOM SAL_CALL CMtaFolderPicker::RegisterStaRequestWindowClass( )
{
osl::MutexGuard aGuard( s_Mutex );
if ( 0 == s_ClassAtom )
{
WNDCLASSEXW wcex;
ZeroMemory( &wcex, sizeof( wcex ) );
wcex.cbSize = sizeof(wcex);
wcex.style = 0;
wcex.lpfnWndProc = static_cast< WNDPROC >( CMtaFolderPicker::StaWndProc );
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = m_hInstance;
wcex.hIcon = nullptr;
wcex.hCursor = nullptr;
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = g_szWndClsName;
wcex.hIconSm = nullptr;
s_ClassAtom = RegisterClassExW( &wcex );
OSL_ASSERT( s_ClassAtom );
}
// increment the register class counter
// so that we keep track of the number
// of class registrations
if ( 0 != s_ClassAtom )
s_StaRequestWndRegisterCount++;
return s_ClassAtom;
}
void SAL_CALL CMtaFolderPicker::UnregisterStaRequestWindowClass( )
{
osl::MutexGuard aGuard( s_Mutex );
OSL_ASSERT( 0 != s_ClassAtom );
// update the register class counter
// and unregister the window class if
// counter drops to zero
if ( 0 != s_ClassAtom )
{
s_StaRequestWndRegisterCount--;
OSL_ASSERT( s_StaRequestWndRegisterCount >= 0 );
}
if ( 0 == s_StaRequestWndRegisterCount )
{
UnregisterClass( MAKEINTATOM(s_ClassAtom), m_hInstance );
s_ClassAtom = 0;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|