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