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