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: Test_fps.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"
35 //_________________________________________________________________________________________________________________________
37 //_________________________________________________________________________________________________________________________
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/registry/XSimpleRegistry.hpp>
40 #include <osl/file.hxx>
42 //_________________________________________________________________________________________________________________________
44 //_________________________________________________________________________________________________________________________
45 #include <cppuhelper/servicefactory.hxx>
48 #include <rtl/ustring>
50 #include <sal/types.h>
51 #include <osl/diagnose.h>
52 #include <com/sun/star/ui/dialogs/XFilePicker.hpp>
53 #include <com/sun/star/ui/dialogs/XFilterManager.hpp>
55 #ifndef _COM_SUN_STAR_UI_DIALOGS_FILEDIALOGRESULTS_HPP_
56 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
58 #include <cppuhelper/implbase1.hxx>
59 #include <com/sun/star/ui/dialogs/XFilePickerListener.hpp>
60 #include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
61 #include <com/sun/star/ui/dialogs/XFilePickerNotifier.hpp>
62 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
63 #include <com/sun/star/ui/dialogs/CommonFilePickerElementIds.hpp>
64 #include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
65 #include <com/sun/star/ui/dialogs/ListboxControlActions.hpp>
66 #include <com/sun/star/ui/dialogs/XFilePreview.hpp>
68 #include <osl/thread.h>
73 #include "..\FPServiceInfo.hxx"
79 using namespace ::rtl
;
80 using namespace ::cppu
;
81 using namespace ::com::sun::star::uno
;
82 using namespace ::com::sun::star::lang
;
83 using namespace ::com::sun::star::ui::dialogs
;
84 using namespace ::com::sun::star::ui::dialogs::TemplateDescription
;
86 using namespace ::com::sun::star::ui::dialogs::CommonFilePickerElementIds
;
87 using namespace ::com::sun::star::ui::dialogs::ExtendedFilePickerElementIds
;
88 using namespace ::com::sun::star::ui::dialogs::ListboxControlActions
;
94 void TestFilterManager( Reference
< XFilePicker
> xFilePicker
);
97 #define RDB_SYSPATH "D:\\Projects\\gsl\\sysui\\wntmsci7\\bin\\applicat.rdb"
99 //_________________________________________________________________________________________________________________________
101 //_________________________________________________________________________________________________________________________
103 Reference
< XMultiServiceFactory
> g_xFactory
;
105 const OUString BMP_EXTENSION
= OUString::createFromAscii( "bmp" );
107 //-------------------------------------------------------------------------------------------------------------------------
109 //-------------------------------------------------------------------------------------------------------------------------
111 class FilePickerListener
: public WeakImplHelper1
< XFilePickerListener
>
116 virtual void SAL_CALL
disposing( const ::com::sun::star::lang::EventObject
& Source
)
117 throw(::com::sun::star::uno::RuntimeException
);
119 // XFilePickerListener
120 virtual void SAL_CALL
fileSelectionChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
121 throw(::com::sun::star::uno::RuntimeException
);
123 virtual void SAL_CALL
directoryChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
124 throw(::com::sun::star::uno::RuntimeException
);
126 virtual OUString SAL_CALL
helpRequested( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
127 throw(::com::sun::star::uno::RuntimeException
);
129 virtual void SAL_CALL
controlStateChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
130 throw(::com::sun::star::uno::RuntimeException
);
132 virtual void SAL_CALL
dialogSizeChanged( )
133 throw (::com::sun::star::uno::RuntimeException
);
136 void SAL_CALL
FilePickerListener::disposing( const ::com::sun::star::lang::EventObject
& Source
)
137 throw(::com::sun::star::uno::RuntimeException
)
141 void SAL_CALL
FilePickerListener::fileSelectionChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
142 throw(::com::sun::star::uno::RuntimeException
)
146 Reference
< XFilePicker
> rXFilePicker( aEvent
.Source
, UNO_QUERY
);
147 Reference
< XFilePreview
> rXFilePreview( rXFilePicker
, UNO_QUERY
);
149 if ( !rXFilePreview
.is( ) )
152 Sequence
< OUString
> aFileList
= rXFilePicker
->getFiles( );
153 if ( 1 == aFileList
.getLength( ) )
155 OUString FilePath
= aFileList
[0];
157 // detect file extension
158 sal_Int32 nIndex
= FilePath
.lastIndexOf( BMP_EXTENSION
);
159 if ( (FilePath
.getLength( ) - 3) == nIndex
)
161 OUString FileSysPath
;
162 ::osl::FileBase::getSystemPathFromFileURL(
163 FilePath
, FileSysPath
);
165 HANDLE hFile
= CreateFileW(
166 FileSysPath
.getStr( ),
167 GENERIC_READ
, FILE_SHARE_READ
, NULL
,
168 OPEN_EXISTING
, FILE_FLAG_SEQUENTIAL_SCAN
, NULL
) ;
170 if (hFile
== INVALID_HANDLE_VALUE
)
174 DWORD dwFileSize
= GetFileSize (hFile
, &dwHighSize
) ;
178 CloseHandle (hFile
) ;
182 Sequence
< sal_Int8
> aDIB( dwFileSize
);
185 BOOL bSuccess
= ReadFile (hFile
, aDIB
.getArray( ), dwFileSize
, &dwBytesRead
, NULL
) ;
188 BITMAPFILEHEADER
* pbmfh
= (BITMAPFILEHEADER
*)aDIB
.getConstArray( );
189 if (!bSuccess
|| (dwBytesRead
!= dwFileSize
)
190 || (pbmfh
->bfType
!= * (WORD
*) "BM")
191 || (pbmfh
->bfSize
!= dwFileSize
))
199 rXFilePreview
->setImage( 1, aAny
);
203 catch( IllegalArgumentException
& ex
)
209 void SAL_CALL
FilePickerListener::directoryChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
210 throw(::com::sun::star::uno::RuntimeException
)
212 Reference
< XFilePickerControlAccess
> rFilePickerCtrlAccess( aEvent
.Source
, UNO_QUERY
);
215 OUString SAL_CALL
FilePickerListener::helpRequested( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
216 throw(::com::sun::star::uno::RuntimeException
)
221 void SAL_CALL
FilePickerListener::controlStateChanged( const ::com::sun::star::ui::dialogs::FilePickerEvent
& aEvent
)
222 throw(::com::sun::star::uno::RuntimeException
)
226 Reference
< XFilePickerControlAccess
> rFPCtrlAccess( aEvent
.Source
, UNO_QUERY
);
230 OUString
lbString( L
"Ein Eintrag 1" );
232 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
234 lbString
= OUString( L
"Ein Eintrag 2" );
236 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
238 lbString
= OUString( L
"Ein Eintrag 3" );
240 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aValue
);
244 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, SET_SELECT_ITEM
, aValue
);
248 rFPCtrlAccess
->setValue( LISTBOX_VERSION
, DELETE_ITEM
, aValue
);
255 void SAL_CALL
FilePickerListener::dialogSizeChanged( )
256 throw(::com::sun::star::uno::RuntimeException
)
260 //--------------------------------------------------------
262 //--------------------------------------------------------
265 int SAL_CALL
main(int nArgc
, char* Argv
[], char* Env
[] )
267 printf("Starting test of FPS-Service\n");
269 //-------------------------------------------------
270 // get the global service-manager
271 //-------------------------------------------------
273 // Get global factory for uno services.
274 OUString rdbName
= OUString( RTL_CONSTASCII_USTRINGPARAM( RDB_SYSPATH
) );
275 Reference
< XMultiServiceFactory
> g_xFactory( createRegistryServiceFactory( rdbName
) );
277 // Print a message if an error occured.
278 if ( g_xFactory
.is() == sal_False
)
280 OSL_ENSURE(sal_False
, "Can't create RegistryServiceFactory");
284 //-------------------------------------------------
285 // try to get an Interface to a XFilePicker Service
286 //-------------------------------------------------
288 Sequence
< Any
> arguments(1);
289 //arguments[0] = makeAny( FILEOPEN_SIMPLE );
290 //arguments[0] = makeAny( FILESAVE_SIMPLE );
291 //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_PASSWORD );
292 //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_PASSWORD_FILTEROPTIONS );
293 //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_SELECTION );
294 //arguments[0] = makeAny( FILESAVE_AUTOEXTENSION_TEMPLATE );
295 //arguments[0] = makeAny( FILEOPEN_LINK_PREVIEW_IMAGE_TEMPLATE );
296 //arguments[0] = makeAny( FILEOPEN_PLAY );
297 arguments
[0] = makeAny( FILEOPEN_READONLY_VERSION
);
299 Reference
< XFilePicker
> xFilePicker
= Reference
< XFilePicker
>(
300 g_xFactory
->createInstanceWithArguments(
301 OUString::createFromAscii( FILE_PICKER_SERVICE_NAME
), arguments
), UNO_QUERY
);
303 // install a FilePicker notifier
304 Reference
< XFilePickerListener
> xFPListener(
305 static_cast< XFilePickerListener
* >( new FilePickerListener()), UNO_QUERY
);
307 Reference
< XFilePickerNotifier
> xFPNotifier( xFilePicker
, UNO_QUERY
);
308 if ( xFPNotifier
.is( ) )
309 xFPNotifier
->addFilePickerListener( xFPListener
);
311 xFilePicker
->setTitle( OUString::createFromAscii("FileOpen Simple..."));
312 xFilePicker
->setMultiSelectionMode( sal_True
);
313 xFilePicker
->setDefaultName( OUString::createFromAscii("d:\\test2.sxw"));
316 OUString aSysPath
= OStringToOUString( "d:\\ueaeoe", osl_getThreadTextEncoding( ) );
317 ::osl::FileBase::getFileURLFromSystemPath( aSysPath
, aDirURL
);
318 xFilePicker
->setDisplayDirectory( aDirURL
);
320 Reference
< XFilterManager
> xFilterMgr( xFilePicker
, UNO_QUERY
);
321 if ( xFilterMgr
.is( ) )
323 xFilterMgr
->appendFilter( L
"Alle", L
"*.*" );
324 xFilterMgr
->appendFilter( L
"BMP", L
"*.bmp" );
325 xFilterMgr
->appendFilter( L
"SDW", L
"*.sdw;*.sdc;*.sdi" );
326 xFilterMgr
->appendFilter( L
"SXW", L
"*.sxw;*.sxi" );
329 Reference
< XFilePickerControlAccess
> xFPControlAccess( xFilePicker
, UNO_QUERY
);
332 sal_Bool bChkState
= sal_False
;
334 aAny
.setValue( &bChkState
, getCppuType( (sal_Bool
*)0 ) );
335 xFPControlAccess
->setValue( CHECKBOX_AUTOEXTENSION
, 0, aAny
);
337 OUString
aVersion( L
"Version 1" );
339 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
340 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
341 xFPControlAccess
->setValue( LISTBOX_VERSION
, ADD_ITEM
, aAny
);
343 xFilePicker
->execute( );
345 sal_Bool bCheckState
;
346 aAny
= xFPControlAccess
->getValue( CHECKBOX_AUTOEXTENSION
, 0 );
347 if ( aAny
.hasValue( ) )
348 bCheckState
= *reinterpret_cast< const sal_Bool
* >( aAny
.getValue( ) );
350 aAny
= xFPControlAccess
->getValue( CHECKBOX_READONLY
, 0 );
351 if ( aAny
.hasValue( ) )
352 bCheckState
= *reinterpret_cast< const sal_Bool
* >( aAny
.getValue( ) );
354 aAny
= xFPControlAccess
->getValue( LISTBOX_VERSION
, GET_SELECTED_ITEM
);
356 if ( aAny
.hasValue( ) )
359 aDirURL
= xFilePicker
->getDisplayDirectory( );
360 Sequence
< OUString
> aFileList
= xFilePicker
->getFiles( );
361 for ( int i
= 0; i
< aFileList
.getLength( ); i
++ )
363 OUString nextPath
= aFileList
[i
];
366 if ( xFPNotifier
.is( ) )
367 xFPNotifier
->removeFilePickerListener( xFPListener
);
369 //--------------------------------------------------
371 //--------------------------------------------------
373 // Cast factory to XComponent
374 Reference
< XComponent
> xComponent( g_xFactory
, UNO_QUERY
);
376 // Print a message if an error occured.
377 if ( xComponent
.is() == sal_False
)
379 OSL_ENSURE(sal_False
, "Error shuting down");
382 // Dispose and clear factory
383 xComponent
->dispose();
385 g_xFactory
= Reference
< XMultiServiceFactory
>();
387 printf("Test successful\n");