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 //_________________________________________________________________________________________________________________________
29 //	interface includes
30 //_________________________________________________________________________________________________________________________
31 #include <com/sun/star/lang/XComponent.hpp>
32 #include <com/sun/star/registry/XSimpleRegistry.hpp>
33 #include <osl/file.hxx>
34 
35 //_________________________________________________________________________________________________________________________
36 //	other includes
37 //_________________________________________________________________________________________________________________________
38 #include <cppuhelper/servicefactory.hxx>
39 
40 #ifndef _RTL_USTRING_
41 #include <rtl/ustring>
42 #endif
43 #include <sal/types.h>
44 #include <osl/diagnose.h>
45 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
46 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
47 
48 #ifndef _COM_SUN_STAR_UI_DIALOGS_FILEDIALOGRESULTS_HPP_
49 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
50 #endif
51 #include <cppuhelper/implbase1.hxx>
52 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
53 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
54 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
55 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
56 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
57 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
58 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
59 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
60 
61 #include <osl/thread.h>
62 
63 #include <stdio.h>
64 #include <windows.h>
65 
66 #include "..\FPServiceInfo.hxx"
67 
68 //
69 //	namesapces
70 //
71 
72 using namespace	::rtl					;
73 using namespace	::cppu					;
74 using namespace	::com::sun::star::uno	;
75 using namespace	::com::sun::star::lang	;
76 using namespace ::com::sun::star::ui::dialogs	;
77 using namespace ::com::sun::star::ui::dialogs::TemplateDescription;
78 
79 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds;
80 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds;
81 using namespace ::com::sun::star::ui::dialogs::ListboxControlActions;
82 
83 using namespace std						;
84 
85 // forward
86 
87 void TestFilterManager( Reference< XFilePicker > xFilePicker );
88 
89 
90 #define RDB_SYSPATH "D:\\Projects\\gsl\\sysui\\wntmsci7\\bin\\applicat.rdb"
91 
92 //_________________________________________________________________________________________________________________________
93 //	global variables
94 //_________________________________________________________________________________________________________________________
95 
96 Reference< XMultiServiceFactory >	g_xFactory;
97 
98 const OUString BMP_EXTENSION = OUString::createFromAscii( "bmp" );
99 
100 //-------------------------------------------------------------------------------------------------------------------------
101 // a test client
102 //-------------------------------------------------------------------------------------------------------------------------
103 
104 class FilePickerListener : public WeakImplHelper1< XFilePickerListener >
105 {
106 public:
107 
108 	// XEventListener
109 	virtual void SAL_CALL disposing( const ::com::sun::star::lang::EventObject& Source )
110         throw(::com::sun::star::uno::RuntimeException);
111 
112 	// XFilePickerListener
113 	virtual void SAL_CALL fileSelectionChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
114         throw(::com::sun::star::uno::RuntimeException);
115 
116     virtual void SAL_CALL directoryChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
117         throw(::com::sun::star::uno::RuntimeException);
118 
119     virtual OUString SAL_CALL helpRequested( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
120         throw(::com::sun::star::uno::RuntimeException);
121 
122     virtual void SAL_CALL controlStateChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
123         throw(::com::sun::star::uno::RuntimeException);
124 
125     virtual void SAL_CALL dialogSizeChanged(  )
126         throw (::com::sun::star::uno::RuntimeException);
127 };
128 
129 void SAL_CALL FilePickerListener::disposing( const ::com::sun::star::lang::EventObject& Source )
130     throw(::com::sun::star::uno::RuntimeException)
131 {
132 }
133 
134 void SAL_CALL FilePickerListener::fileSelectionChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
135     throw(::com::sun::star::uno::RuntimeException)
136 {
137     try
138     {
139         Reference< XFilePicker > rXFilePicker( aEvent.Source, UNO_QUERY );
140         Reference< XFilePreview > rXFilePreview( rXFilePicker, UNO_QUERY );
141 
142         if ( !rXFilePreview.is( ) )
143             return;
144 
145         Sequence< OUString > aFileList = rXFilePicker->getFiles( );
146         if ( 1 == aFileList.getLength( ) )
147         {
148             OUString FilePath = aFileList[0];
149 
150             // detect file extension
151             sal_Int32 nIndex = FilePath.lastIndexOf( BMP_EXTENSION );
152             if ( (FilePath.getLength( ) - 3) == nIndex )
153             {
154                  OUString FileSysPath;
155                 ::osl::FileBase::getSystemPathFromFileURL(
156                     FilePath, FileSysPath );
157 
158                  HANDLE hFile = CreateFileW(
159                      FileSysPath.getStr( ),
160                      GENERIC_READ, FILE_SHARE_READ, NULL,
161                      OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL) ;
162 
163                  if (hFile == INVALID_HANDLE_VALUE)
164                       return;
165 
166                  DWORD dwHighSize;
167                  DWORD dwFileSize = GetFileSize (hFile, &dwHighSize) ;
168 
169                  if (dwHighSize)
170                  {
171                       CloseHandle (hFile) ;
172                       return;
173                  }
174 
175                  Sequence< sal_Int8 > aDIB( dwFileSize );
176 
177                  DWORD dwBytesRead;
178                  sal_Bool bSuccess = ReadFile (hFile, aDIB.getArray( ), dwFileSize, &dwBytesRead, NULL) ;
179                  CloseHandle (hFile);
180 
181                  BITMAPFILEHEADER* pbmfh = (BITMAPFILEHEADER*)aDIB.getConstArray( );
182                  if (!bSuccess || (dwBytesRead != dwFileSize)
183                                || (pbmfh->bfType != * (WORD *) "BM")
184                                || (pbmfh->bfSize != dwFileSize))
185                  {
186                     return;
187                  }
188 
189                 Any aAny;
190 
191                 aAny <<= aDIB;
192                 rXFilePreview->setImage( 1, aAny );
193             }
194         }
195     }
196     catch( IllegalArgumentException& ex )
197     {
198         ex = ex;
199     }
200 }
201 
202 void SAL_CALL FilePickerListener::directoryChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
203     throw(::com::sun::star::uno::RuntimeException)
204 {
205 	Reference< XFilePickerControlAccess > rFilePickerCtrlAccess( aEvent.Source, UNO_QUERY );
206 }
207 
208 OUString SAL_CALL FilePickerListener::helpRequested( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
209     throw(::com::sun::star::uno::RuntimeException)
210 {
211 	return OUString( );
212 }
213 
214 void SAL_CALL FilePickerListener::controlStateChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent& aEvent )
215     throw(::com::sun::star::uno::RuntimeException)
216 {
217     try
218     {
219 	    Reference< XFilePickerControlAccess > rFPCtrlAccess( aEvent.Source, UNO_QUERY );
220 
221         Any aValue;
222 
223         OUString lbString( L"Ein Eintrag 1" );
224         aValue <<= lbString;
225         rFPCtrlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aValue );
226 
227         lbString = OUString( L"Ein Eintrag 2" );
228         aValue <<= lbString;
229         rFPCtrlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aValue );
230 
231         lbString = OUString( L"Ein Eintrag 3" );
232         aValue <<= lbString;
233         rFPCtrlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aValue );
234 
235         sal_Int16 nSel = 1;
236         aValue <<= nSel;
237         rFPCtrlAccess->setValue( LISTBOX_VERSION, SET_SELECT_ITEM, aValue );
238 
239         sal_Int32 nDel = 0;
240         aValue <<= nDel;
241         rFPCtrlAccess->setValue( LISTBOX_VERSION, DELETE_ITEM, aValue );
242     }
243     catch( ... )
244     {
245     }
246 }
247 
248 void SAL_CALL FilePickerListener::dialogSizeChanged( )
249     throw(::com::sun::star::uno::RuntimeException)
250 {
251 }
252 
253 //--------------------------------------------------------
254 //	main
255 //--------------------------------------------------------
256 
257 
258 int SAL_CALL main(int nArgc, char* Argv[], char* Env[]	)
259 {
260 	printf("Starting test of FPS-Service\n");
261 
262 	//-------------------------------------------------
263 	// get the global service-manager
264 	//-------------------------------------------------
265 
266 	// Get global factory for uno services.
267 	OUString rdbName = OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH ) );
268 	Reference< XMultiServiceFactory > g_xFactory( createRegistryServiceFactory( rdbName ) );
269 
270 	// Print a message if an error occured.
271 	if ( g_xFactory.is() == sal_False )
272 	{
273 		OSL_ENSURE(sal_False, "Can't create RegistryServiceFactory");
274 		return(-1);
275 	}
276 
277 	//-------------------------------------------------
278 	// try to get an Interface to a XFilePicker Service
279 	//-------------------------------------------------
280 
281 	Sequence< Any > arguments(1);
282         //arguments[0] = makeAny( FILEOPEN_SIMPLE );
283         //arguments[0] = makeAny( FILESAVE_SIMPLE );
284         //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_PASSWORD );
285         //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS );
286         //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_SELECTION );
287         //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_TEMPLATE );
288         //arguments[0] = makeAny( FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE );
289         //arguments[0] = makeAny( FILEOPEN_PLAY );
290         arguments[0] = makeAny( FILEOPEN_READONLY_VERSION );
291 
292     Reference< XFilePicker > xFilePicker = Reference< XFilePicker >(
293 		g_xFactory->createInstanceWithArguments(
294 			OUString::createFromAscii( FILE_PICKER_SERVICE_NAME ), arguments ), UNO_QUERY );
295 
296         // install a FilePicker notifier
297         Reference< XFilePickerListener > xFPListener(
298             static_cast< XFilePickerListener* >( new FilePickerListener()), UNO_QUERY );
299 
300         Reference< XFilePickerNotifier > xFPNotifier( xFilePicker, UNO_QUERY );
301         if ( xFPNotifier.is( ) )
302             xFPNotifier->addFilePickerListener( xFPListener );
303 
304         xFilePicker->setTitle( OUString::createFromAscii("FileOpen Simple..."));
305         xFilePicker->setMultiSelectionMode( sal_True );
306         xFilePicker->setDefaultName( OUString::createFromAscii("d:\\test2.sxw"));
307 
308         OUString aDirURL;
309         OUString aSysPath = OStringToOUString( "d:\\ueaeoe", osl_getThreadTextEncoding( ) );
310         ::osl::FileBase::getFileURLFromSystemPath( aSysPath, aDirURL );
311         xFilePicker->setDisplayDirectory( aDirURL );
312 
313         Reference< XFilterManager > xFilterMgr( xFilePicker, UNO_QUERY );
314         if ( xFilterMgr.is( ) )
315         {
316             xFilterMgr->appendFilter( L"Alle", L"*.*" );
317             xFilterMgr->appendFilter( L"BMP", L"*.bmp" );
318             xFilterMgr->appendFilter( L"SDW", L"*.sdw;*.sdc;*.sdi" );
319             xFilterMgr->appendFilter( L"SXW", L"*.sxw;*.sxi" );
320         }
321 
322         Reference< XFilePickerControlAccess > xFPControlAccess( xFilePicker, UNO_QUERY );
323 
324         Any aAny;
325         sal_Bool bChkState = sal_False;
326 
327         aAny.setValue( &bChkState, getCppuType( (sal_Bool*)0 ) );
328         xFPControlAccess->setValue( CHECKBOX_AUTOEXTENSION, 0, aAny );
329 
330         OUString aVersion( L"Version 1" );
331         aAny <<= aVersion;
332         xFPControlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aAny );
333         xFPControlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aAny );
334         xFPControlAccess->setValue( LISTBOX_VERSION, ADD_ITEM, aAny );
335 
336         xFilePicker->execute( );
337 
338         sal_Bool bCheckState;
339         aAny = xFPControlAccess->getValue( CHECKBOX_AUTOEXTENSION, 0 );
340         if ( aAny.hasValue( ) )
341             bCheckState = *reinterpret_cast< const sal_Bool* >( aAny.getValue( ) );
342 
343         aAny = xFPControlAccess->getValue( CHECKBOX_READONLY, 0 );
344         if ( aAny.hasValue( ) )
345             bCheckState = *reinterpret_cast< const sal_Bool* >( aAny.getValue( ) );
346 
347         aAny = xFPControlAccess->getValue( LISTBOX_VERSION, GET_SELECTED_ITEM );
348         sal_Int32 nSel;
349         if ( aAny.hasValue( ) )
350             aAny >>= nSel;
351 
352         aDirURL = xFilePicker->getDisplayDirectory( );
353         Sequence< OUString > aFileList = xFilePicker->getFiles( );
354         for ( int i = 0; i < aFileList.getLength( ); i++ )
355         {
356             OUString nextPath = aFileList[i];
357         }
358 
359         if ( xFPNotifier.is( ) )
360             xFPNotifier->removeFilePickerListener( xFPListener );
361 
362 	//--------------------------------------------------
363 	// shutdown
364 	//--------------------------------------------------
365 
366 	// Cast factory to XComponent
367 	Reference< XComponent > xComponent( g_xFactory, UNO_QUERY );
368 
369 	// Print a message if an error occured.
370 	if ( xComponent.is() == sal_False )
371 	{
372 		OSL_ENSURE(sal_False, "Error shuting down");
373 	}
374 
375 	// Dispose and clear factory
376 	xComponent->dispose();
377 	g_xFactory.clear();
378 	g_xFactory = Reference< XMultiServiceFactory >();
379 
380 	printf("Test successful\n");
381 
382 	return 0;
383 }
384