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: informationdialog.cxx,v $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sdext.hxx"
35 #include "informationdialog.hxx"
36 #include "optimizationstats.hxx"
37 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
38 #include <com/sun/star/graphic/XGraphicProvider.hpp>
39 #include <com/sun/star/graphic/XGraphic.hpp>
40 #include <rtl/ustrbuf.hxx>
41 #include "com/sun/star/util/URL.hpp"
42 #include "com/sun/star/util/XURLTransformer.hpp"
44 #define DIALOG_WIDTH 240
45 #define DIALOG_HEIGHT 80
47 #define PAGE_WIDTH ( DIALOG_WIDTH - PAGE_POS_X ) - 6
50 // ---------------------
51 // - INFORMATIONDIALOG -
52 // ---------------------
54 using namespace ::rtl
;
55 using namespace ::com::sun::star
;
56 using namespace ::com::sun::star::io
;
57 using namespace ::com::sun::star::ui
;
58 using namespace ::com::sun::star::awt
;
59 using namespace ::com::sun::star::uno
;
60 using namespace ::com::sun::star::util
;
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::frame
;
63 using namespace ::com::sun::star::beans
;
64 using namespace ::com::sun::star::script
;
65 using namespace ::com::sun::star::container
;
69 // -----------------------------------------------------------------------------
71 rtl::OUString
InsertFixedText( InformationDialog
& rInformationDialog
, const rtl::OUString
& rControlName
, const OUString
& rLabel
,
72 sal_Int32 nXPos
, sal_Int32 nYPos
, sal_Int32 nWidth
, sal_Int32 nHeight
, sal_Bool bMultiLine
, sal_Int16 nTabIndex
)
77 TKGet( TK_MultiLine
),
78 TKGet( TK_PositionX
),
79 TKGet( TK_PositionY
),
94 sal_Int32 nCount
= sizeof( pNames
) / sizeof( OUString
);
96 Sequence
< rtl::OUString
> aNames( pNames
, nCount
);
97 Sequence
< Any
> aValues( pValues
, nCount
);
99 rInformationDialog
.insertFixedText( rControlName
, aNames
, aValues
);
103 rtl::OUString
InsertImage( InformationDialog
& rInformationDialog
, const OUString
& rControlName
, const OUString
& rURL
,
104 sal_Int32 nPosX
, sal_Int32 nPosY
, sal_Int32 nWidth
, sal_Int32 nHeight
)
106 OUString pNames
[] = {
109 TKGet( TK_ImageURL
),
110 TKGet( TK_PositionX
),
111 TKGet( TK_PositionY
),
112 TKGet( TK_ScaleImage
),
116 Any( sal_Int16( 0 ) ),
123 sal_Int32 nCount
= sizeof( pNames
) / sizeof( OUString
);
125 Sequence
< rtl::OUString
> aNames( pNames
, nCount
);
126 Sequence
< Any
> aValues( pValues
, nCount
);
128 rInformationDialog
.insertImage( rControlName
, aNames
, aValues
);
132 rtl::OUString
InsertCheckBox( InformationDialog
& rInformationDialog
, const OUString
& rControlName
,
133 const Reference
< XItemListener
> xItemListener
, const OUString
& rLabel
,
134 sal_Int32 nXPos
, sal_Int32 nYPos
, sal_Int32 nWidth
, sal_Int32 nHeight
, sal_Int16 nTabIndex
)
136 OUString pNames
[] = {
140 TKGet( TK_PositionX
),
141 TKGet( TK_PositionY
),
143 TKGet( TK_TabIndex
),
156 sal_Int32 nCount
= sizeof( pNames
) / sizeof( OUString
);
158 Sequence
< rtl::OUString
> aNames( pNames
, nCount
);
159 Sequence
< Any
> aValues( pValues
, nCount
);
161 Reference
< XCheckBox
> xCheckBox( rInformationDialog
.insertCheckBox( rControlName
, aNames
, aValues
) );
162 if ( xItemListener
.is() )
163 xCheckBox
->addItemListener( xItemListener
);
167 rtl::OUString
InsertButton( InformationDialog
& rInformationDialog
, const OUString
& rControlName
, Reference
< XActionListener
>& xActionListener
,
168 sal_Int32 nXPos
, sal_Int32 nYPos
, sal_Int32 nWidth
, sal_Int32 nHeight
, sal_Int16 nTabIndex
, PPPOptimizerTokenEnum nResID
)
170 OUString pNames
[] = {
174 TKGet( TK_PositionX
),
175 TKGet( TK_PositionY
),
176 TKGet( TK_PushButtonType
),
178 TKGet( TK_TabIndex
),
184 Any( rInformationDialog
.getString( nResID
) ),
187 Any( static_cast< sal_Int16
>( PushButtonType_OK
) ),
193 sal_Int32 nCount
= sizeof( pNames
) / sizeof( OUString
);
195 Sequence
< rtl::OUString
> aNames( pNames
, nCount
);
196 Sequence
< Any
> aValues( pValues
, nCount
);
198 rInformationDialog
.insertButton( rControlName
, xActionListener
, aNames
, aValues
);
203 static OUString
ImpValueOfInMB( const sal_Int64
& rVal
)
205 double fVal( static_cast<double>( rVal
) );
208 rtl::OUStringBuffer
aVal( OUString::valueOf( fVal
) );
209 sal_Int32
nX( OUString( aVal
.getStr() ).indexOf( '.', 0 ) );
211 aVal
.setLength( nX
+ 2 );
212 return aVal
.makeStringAndClear();
215 OUString
InformationDialog::ImpGetStandardImage( const OUString
& sPrivateURL
)
220 mxTempFile
= Reference
< XStream
>( mxMSF
->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.io.TempFile" ), mxMSF
), UNO_QUERY_THROW
);
221 Reference
< XPropertySet
> xPropSet( mxTempFile
, UNO_QUERY
);
222 Reference
< XOutputStream
> xOutputStream( mxTempFile
->getOutputStream() );
223 if ( xOutputStream
.is() && xPropSet
.is() )
225 Reference
< graphic::XGraphicProvider
> xGraphicProvider( mxMSF
->getServiceManager()->createInstanceWithContext(
226 OUString::createFromAscii( "com.sun.star.graphic.GraphicProvider" ), mxMSF
), UNO_QUERY_THROW
);
227 Sequence
< PropertyValue
> aArgs( 1 );
228 aArgs
[ 0 ].Name
= OUString::createFromAscii( "URL" );
229 aArgs
[ 0 ].Value
<<= sPrivateURL
;
230 Reference
< graphic::XGraphic
> xGraphic( xGraphicProvider
->queryGraphic( aArgs
) );
233 OUString
aDestMimeType( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
234 Sequence
< PropertyValue
> aArgs2( 2 );
235 aArgs2
[ 0 ].Name
= TKGet( TK_MimeType
); // the GraphicProvider is using "MimeType", the GraphicExporter "MediaType"...
236 aArgs2
[ 0 ].Value
<<= aDestMimeType
;
237 aArgs2
[ 1 ].Name
= TKGet( TK_OutputStream
);
238 aArgs2
[ 1 ].Value
<<= xOutputStream
;
239 xGraphicProvider
->storeGraphic( xGraphic
, aArgs2
);
241 xPropSet
->getPropertyValue( OUString::createFromAscii( "Uri" ) ) >>= sURL
;
250 void InformationDialog::InitDialog()
252 sal_Int32 nDialogHeight
= DIALOG_HEIGHT
;
253 if ( !maSaveAsURL
.getLength() )
256 // setting the dialog properties
257 OUString pNames
[] = {
258 TKGet( TK_Closeable
),
260 TKGet( TK_Moveable
),
261 TKGet( TK_PositionX
),
262 TKGet( TK_PositionY
),
268 Any( nDialogHeight
),
270 Any( sal_Int32( 245 ) ),
271 Any( sal_Int32( 115 ) ),
272 Any( getString( STR_ABOUT2
) ),
273 Any( sal_Int32( DIALOG_WIDTH
) ) };
275 sal_Int32 nCount
= sizeof( pNames
) / sizeof( OUString
);
277 Sequence
< rtl::OUString
> aNames( pNames
, nCount
);
278 Sequence
< Any
> aValues( pValues
, nCount
);
280 mxDialogModelMultiPropertySet
->setPropertyValues( aNames
, aValues
);
282 sal_Int64 nSource
= mnSourceSize
;
283 sal_Int64 nDest
= mnDestSize
;
285 PPPOptimizerTokenEnum
eInfoString( STR_INFO_1
);
289 eInfoString
= STR_INFO_1
;
292 eInfoString
= STR_INFO_2
;
293 nDest
= mnApproxSize
;
296 else if ( mnDestSize
)
297 eInfoString
= STR_INFO_3
;
300 eInfoString
= STR_INFO_4
;
301 nDest
= mnApproxSize
;
304 rtl::OUString aTitle
;
305 if ( maSaveAsURL
.getLength() )
307 Reference
< XURLTransformer
> xURLTransformer( mxMSF
->getServiceManager()->createInstanceWithContext(
308 OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), mxMSF
), UNO_QUERY
);
309 if ( xURLTransformer
.is() )
311 util::URL aURL
, aPresentationURL
;
312 aURL
.Complete
= maSaveAsURL
;
313 xURLTransformer
->parseSmart( aURL
, rtl::OUString() );
315 const OUString
sFileProtocol( RTL_CONSTASCII_USTRINGPARAM( "file:///" ) );
316 aPresentationURL
.Complete
= sFileProtocol
.concat( aURL
.Name
);
317 aTitle
= xURLTransformer
->getPresentation( aPresentationURL
, sal_False
);
319 if ( aTitle
.match( sFileProtocol
, 0 ) )
320 aTitle
= aTitle
.replaceAt( 0, sFileProtocol
.getLength(), rtl::OUString() );
324 OUString
aInfoString( getString( eInfoString
) );
325 const OUString
aOldSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%OLDFILESIZE" ) );
326 const OUString
aNewSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%NEWFILESIZE" ) );
327 const OUString
aTitlePlaceholder( aTitle
.getLength() ? OUString::createFromAscii( "%TITLE" ) : OUString::createFromAscii( "'%TITLE'" ) );
328 const OUString
aExtensionPlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%EXTENSIONNAME" ) );
330 sal_Int32 i
= aInfoString
.indexOf( aOldSizePlaceholder
, 0 );
332 aInfoString
= aInfoString
.replaceAt( i
, aOldSizePlaceholder
.getLength(), ImpValueOfInMB( nSource
) );
334 sal_Int32 j
= aInfoString
.indexOf( aNewSizePlaceholder
, 0 );
336 aInfoString
= aInfoString
.replaceAt( j
, aNewSizePlaceholder
.getLength(), ImpValueOfInMB( nDest
) );
338 sal_Int32 k
= aInfoString
.indexOf( aTitlePlaceholder
, 0 );
340 aInfoString
= aInfoString
.replaceAt( k
, aTitlePlaceholder
.getLength(), aTitle
);
342 sal_Int32 l
= aInfoString
.indexOf( aExtensionPlaceholder
, 0 );
344 aInfoString
= aInfoString
.replaceAt( l
, aExtensionPlaceholder
.getLength(), getString( STR_SUN_OPTIMIZATION_WIZARD2
) );
346 com::sun::star::uno::Reference
< com::sun::star::awt::XItemListener
> xItemListener
;
347 InsertImage( *this, rtl::OUString( rtl::OUString::createFromAscii( "aboutimage" ) ), ImpGetStandardImage( rtl::OUString::createFromAscii( "private:standardimage/query" ) ), 5, 5, 25, 25 );
348 InsertFixedText( *this, rtl::OUString( rtl::OUString::createFromAscii( "fixedtext" ) ), aInfoString
, PAGE_POS_X
, 6, PAGE_WIDTH
, 24, sal_True
, 0 );
349 if ( maSaveAsURL
.getLength() )
350 InsertCheckBox( *this, TKGet( TK_OpenNewDocument
), xItemListener
, getString( STR_AUTOMATICALLY_OPEN
), PAGE_POS_X
, 42, PAGE_WIDTH
, 8, 1 );
351 InsertButton( *this, rtl::OUString( rtl::OUString::createFromAscii( "button" ) ), mxActionListener
, DIALOG_WIDTH
/ 2 - 25, nDialogHeight
- 20, 50, 14, 2, STR_OK
);
353 sal_Bool bOpenNewDocument
= mrbOpenNewDocument
;
354 setControlProperty( TKGet( TK_OpenNewDocument
), TKGet( TK_State
), Any( (sal_Int16
)bOpenNewDocument
) );
357 // -----------------------------------------------------------------------------
359 InformationDialog::InformationDialog( const Reference
< XComponentContext
> &rxMSF
, Reference
< XFrame
>& rxFrame
, const rtl::OUString
& rSaveAsURL
, sal_Bool
& rbOpenNewDocument
, const sal_Int64
& rSourceSize
, const sal_Int64
& rDestSize
, const sal_Int64
& rApproxSize
) :
360 UnoDialog( rxMSF
, rxFrame
),
361 ConfigurationAccess( rxMSF
, NULL
),
364 mxActionListener( new OKActionListener( *this ) ),
365 mnSourceSize( rSourceSize
),
366 mnDestSize( rDestSize
),
367 mnApproxSize( rApproxSize
),
368 mrbOpenNewDocument( rbOpenNewDocument
),
369 maSaveAsURL( rSaveAsURL
)
371 Reference
< XFrame
> xFrame( mxController
->getFrame() );
372 Reference
< XWindow
> xContainerWindow( xFrame
->getContainerWindow() );
373 Reference
< XWindowPeer
> xWindowPeer( xContainerWindow
, UNO_QUERY_THROW
);
374 createWindowPeer( xWindowPeer
);
379 // -----------------------------------------------------------------------------
381 InformationDialog::~InformationDialog()
385 // -----------------------------------------------------------------------------
387 sal_Bool
InformationDialog::execute()
389 UnoDialog::execute();
391 if ( maSaveAsURL
.getLength() )
393 sal_Int16 nInt16
= 0;
394 Any
aAny( getControlProperty( TKGet( TK_OpenNewDocument
), TKGet( TK_State
) ) );
395 if ( aAny
>>= nInt16
)
397 sal_Bool bOpenNewDocument
= static_cast< sal_Bool
>( nInt16
);
398 mrbOpenNewDocument
= bOpenNewDocument
;
404 // -----------------------------------------------------------------------------
406 void OKActionListener::actionPerformed( const ActionEvent
& rEvent
)
407 throw ( com::sun::star::uno::RuntimeException
)
409 if ( rEvent
.ActionCommand
== rtl::OUString( rtl::OUString::createFromAscii( "button" ) ) )
411 mrInformationDialog
.endExecute( sal_True
);
414 void OKActionListener::disposing( const ::com::sun::star::lang::EventObject
& /* Source */ )
415 throw ( com::sun::star::uno::RuntimeException
)