bump product version to 4.1.6.2
[LibreOffice.git] / sdext / source / minimizer / informationdialog.cxx
blobbabba10cfaf144f9206421fd9f11f4398fa5fe27
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include "informationdialog.hxx"
22 #include "optimizationstats.hxx"
23 #include <com/sun/star/graphic/GraphicProvider.hpp>
24 #include <com/sun/star/graphic/XGraphicProvider.hpp>
25 #include <com/sun/star/graphic/XGraphic.hpp>
26 #include <com/sun/star/io/TempFile.hpp>
27 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
28 #include "com/sun/star/util/URL.hpp"
29 #include "com/sun/star/util/URLTransformer.hpp"
30 #include "com/sun/star/util/XURLTransformer.hpp"
31 #include <rtl/ustrbuf.hxx>
32 #include <sal/macros.h>
34 #define DIALOG_WIDTH 240
35 #define DIALOG_HEIGHT 80
36 #define PAGE_POS_X 35
37 #define PAGE_WIDTH ( DIALOG_WIDTH - PAGE_POS_X ) - 6
40 // ---------------------
41 // - INFORMATIONDIALOG -
42 // ---------------------
44 using namespace ::rtl;
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::io;
47 using namespace ::com::sun::star::ui;
48 using namespace ::com::sun::star::awt;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::util;
51 using namespace ::com::sun::star::lang;
52 using namespace ::com::sun::star::frame;
53 using namespace ::com::sun::star::beans;
54 using namespace ::com::sun::star::script;
55 using namespace ::com::sun::star::container;
59 // -----------------------------------------------------------------------------
61 OUString InsertFixedText( InformationDialog& rInformationDialog, const OUString& rControlName, const OUString& rLabel,
62 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Bool bMultiLine, sal_Int16 nTabIndex )
64 OUString pNames[] = {
65 TKGet( TK_Height ),
66 TKGet( TK_Label ),
67 TKGet( TK_MultiLine ),
68 TKGet( TK_PositionX ),
69 TKGet( TK_PositionY ),
70 TKGet( TK_Step ),
71 TKGet( TK_TabIndex ),
72 TKGet( TK_Width ) };
74 Any pValues[] = {
75 Any( nHeight ),
76 Any( rLabel ),
77 Any( bMultiLine ),
78 Any( nXPos ),
79 Any( nYPos ),
80 Any( (sal_Int16)0 ),
81 Any( nTabIndex ),
82 Any( nWidth ) };
84 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
86 Sequence< OUString > aNames( pNames, nCount );
87 Sequence< Any > aValues( pValues, nCount );
89 rInformationDialog.insertFixedText( rControlName, aNames, aValues );
90 return rControlName;
93 OUString InsertImage(
94 InformationDialog& rInformationDialog,
95 const OUString& rControlName,
96 const OUString& rURL,
97 sal_Int32 nPosX,
98 sal_Int32 nPosY,
99 sal_Int32 nWidth,
100 sal_Int32 nHeight,
101 sal_Bool bScale )
103 OUString pNames[] = {
104 TKGet( TK_Border ),
105 TKGet( TK_Height ),
106 TKGet( TK_ImageURL ),
107 TKGet( TK_PositionX ),
108 TKGet( TK_PositionY ),
109 TKGet( TK_ScaleImage ),
110 TKGet( TK_Width ) };
112 Any pValues[] = {
113 Any( sal_Int16( 0 ) ),
114 Any( nHeight ),
115 Any( rURL ),
116 Any( nPosX ),
117 Any( nPosY ),
118 Any( bScale ),
119 Any( nWidth ) };
120 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
122 Sequence< OUString > aNames( pNames, nCount );
123 Sequence< Any > aValues( pValues, nCount );
125 rInformationDialog.insertImage( rControlName, aNames, aValues );
126 return rControlName;
129 OUString InsertCheckBox( InformationDialog& rInformationDialog, const OUString& rControlName,
130 const Reference< XItemListener > xItemListener, const OUString& rLabel,
131 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex )
133 OUString pNames[] = {
134 TKGet( TK_Enabled ),
135 TKGet( TK_Height ),
136 TKGet( TK_Label ),
137 TKGet( TK_PositionX ),
138 TKGet( TK_PositionY ),
139 TKGet( TK_Step ),
140 TKGet( TK_TabIndex ),
141 TKGet( TK_Width ) };
143 Any pValues[] = {
144 Any( sal_True ),
145 Any( nHeight ),
146 Any( rLabel ),
147 Any( nXPos ),
148 Any( nYPos ),
149 Any( (sal_Int16)0 ),
150 Any( nTabIndex ),
151 Any( nWidth ) };
153 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
155 Sequence< OUString > aNames( pNames, nCount );
156 Sequence< Any > aValues( pValues, nCount );
158 Reference< XCheckBox > xCheckBox( rInformationDialog.insertCheckBox( rControlName, aNames, aValues ) );
159 if ( xItemListener.is() )
160 xCheckBox->addItemListener( xItemListener );
161 return rControlName;
164 OUString InsertButton( InformationDialog& rInformationDialog, const OUString& rControlName, Reference< XActionListener >& xActionListener,
165 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex, PPPOptimizerTokenEnum nResID )
167 OUString pNames[] = {
168 TKGet( TK_Enabled ),
169 TKGet( TK_Height ),
170 TKGet( TK_Label ),
171 TKGet( TK_PositionX ),
172 TKGet( TK_PositionY ),
173 TKGet( TK_PushButtonType ),
174 TKGet( TK_Step ),
175 TKGet( TK_TabIndex ),
176 TKGet( TK_Width ) };
178 Any pValues[] = {
179 Any( sal_True ),
180 Any( nHeight ),
181 Any( rInformationDialog.getString( nResID ) ),
182 Any( nXPos ),
183 Any( nYPos ),
184 Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
185 Any( (sal_Int16)0 ),
186 Any( nTabIndex ),
187 Any( nWidth ) };
190 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
192 Sequence< OUString > aNames( pNames, nCount );
193 Sequence< Any > aValues( pValues, nCount );
195 rInformationDialog.insertButton( rControlName, xActionListener, aNames, aValues );
196 return rControlName;
200 static OUString ImpValueOfInMB( const sal_Int64& rVal )
202 double fVal( static_cast<double>( rVal ) );
203 fVal /= ( 1 << 20 );
204 fVal += 0.05;
205 OUStringBuffer aVal( OUString::valueOf( fVal ) );
206 sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.', 0 ) );
207 if ( nX > 0 )
208 aVal.setLength( nX + 2 );
209 return aVal.makeStringAndClear();
212 void InformationDialog::InitDialog()
214 sal_Int32 nDialogHeight = DIALOG_HEIGHT;
215 if ( maSaveAsURL.isEmpty() )
216 nDialogHeight -= 22;
218 // setting the dialog properties
219 OUString pNames[] = {
220 TKGet( TK_Closeable ),
221 TKGet( TK_Height ),
222 TKGet( TK_Moveable ),
223 TKGet( TK_PositionX ),
224 TKGet( TK_PositionY ),
225 TKGet( TK_Title ),
226 TKGet( TK_Width ) };
228 Any pValues[] = {
229 Any( sal_True ),
230 Any( nDialogHeight ),
231 Any( sal_True ),
232 Any( sal_Int32( 245 ) ),
233 Any( sal_Int32( 115 ) ),
234 Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
235 Any( sal_Int32( DIALOG_WIDTH ) ) };
237 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
239 Sequence< OUString > aNames( pNames, nCount );
240 Sequence< Any > aValues( pValues, nCount );
242 mxDialogModelMultiPropertySet->setPropertyValues( aNames, aValues );
244 sal_Int64 nSource = mnSourceSize;
245 sal_Int64 nDest = mnDestSize;
247 PPPOptimizerTokenEnum eInfoString( STR_INFO_1 );
248 if ( mnSourceSize )
250 if ( mnDestSize )
251 eInfoString = STR_INFO_1;
252 else
254 eInfoString = STR_INFO_2;
255 nDest = mnApproxSize;
258 else if ( mnDestSize )
259 eInfoString = STR_INFO_3;
260 else
262 eInfoString = STR_INFO_4;
263 nDest = mnApproxSize;
266 OUString aTitle;
267 if ( !maSaveAsURL.isEmpty() )
269 Reference< XURLTransformer > xURLTransformer( URLTransformer::create(mxMSF) );
270 util::URL aURL, aPresentationURL;
271 aURL.Complete = maSaveAsURL;
272 xURLTransformer->parseSmart( aURL, OUString() );
274 const OUString sFileProtocol( "file:///" );
275 aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
276 aTitle = xURLTransformer->getPresentation( aPresentationURL, sal_False );
278 if ( aTitle.match( sFileProtocol, 0 ) )
279 aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), OUString() );
282 OUString aInfoString( getString( eInfoString ) );
283 const OUString aOldSizePlaceholder( "%OLDFILESIZE" );
284 const OUString aNewSizePlaceholder( "%NEWFILESIZE" );
285 const OUString aTitlePlaceholder( !aTitle.isEmpty() ? OUString("%TITLE" )
286 : OUString("'%TITLE'") );
288 sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder, 0 );
289 if ( i >= 0 )
290 aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
292 sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder, 0 );
293 if ( j >= 0 )
294 aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
296 sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder, 0 );
297 if ( k >= 0 )
298 aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
300 com::sun::star::uno::Reference< com::sun::star::awt::XItemListener > xItemListener;
301 InsertImage( *this,
302 OUString( "aboutimage" ),
303 OUString( "private:standardimage/query" ),
304 5, 5, 25, 25, sal_False );
305 InsertFixedText( *this, OUString("fixedtext"), aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, sal_True, 0 );
306 if ( !maSaveAsURL.isEmpty() )
307 InsertCheckBox( *this, TKGet( TK_OpenNewDocument ), xItemListener, getString( STR_AUTOMATICALLY_OPEN ), PAGE_POS_X, 42, PAGE_WIDTH, 8, 1 );
308 InsertButton( *this, OUString("button"), mxActionListener, DIALOG_WIDTH / 2 - 25, nDialogHeight - 20, 50, 14, 2, STR_OK );
310 sal_Bool bOpenNewDocument = mrbOpenNewDocument;
311 setControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ), Any( (sal_Int16)bOpenNewDocument ) );
314 // -----------------------------------------------------------------------------
316 InformationDialog::InformationDialog( const Reference< XComponentContext > &rxMSF, Reference< XFrame >& rxFrame, const OUString& rSaveAsURL, sal_Bool& rbOpenNewDocument, const sal_Int64& rSourceSize, const sal_Int64& rDestSize, const sal_Int64& rApproxSize ) :
317 UnoDialog( rxMSF, rxFrame ),
318 ConfigurationAccess( rxMSF, NULL ),
319 mxMSF( rxMSF ),
320 mxFrame( rxFrame ),
321 mxActionListener( new OKActionListener( *this ) ),
322 mnSourceSize( rSourceSize ),
323 mnDestSize( rDestSize ),
324 mnApproxSize( rApproxSize ),
325 mrbOpenNewDocument( rbOpenNewDocument ),
326 maSaveAsURL( rSaveAsURL )
328 Reference< XFrame > xFrame( mxController->getFrame() );
329 Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
330 Reference< XWindowPeer > xWindowPeer( xContainerWindow, UNO_QUERY_THROW );
331 createWindowPeer( xWindowPeer );
333 InitDialog();
336 // -----------------------------------------------------------------------------
338 InformationDialog::~InformationDialog()
342 // -----------------------------------------------------------------------------
344 sal_Bool InformationDialog::execute()
346 UnoDialog::execute();
348 if ( !maSaveAsURL.isEmpty() )
350 sal_Int16 nInt16 = 0;
351 Any aAny( getControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ) ) );
352 if ( aAny >>= nInt16 )
354 sal_Bool bOpenNewDocument = static_cast< sal_Bool >( nInt16 );
355 mrbOpenNewDocument = bOpenNewDocument;
358 return mbStatus;
361 // -----------------------------------------------------------------------------
363 void OKActionListener::actionPerformed( const ActionEvent& rEvent )
364 throw ( com::sun::star::uno::RuntimeException )
366 if ( rEvent.ActionCommand == "button" )
368 mrInformationDialog.endExecute( sal_True );
371 void OKActionListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
372 throw ( com::sun::star::uno::RuntimeException )
376 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */