bump product version to 5.0.4.1
[LibreOffice.git] / sdext / source / minimizer / informationdialog.cxx
blob8a125de75c92e3ddacbe2f9e93450cb8c883cbde
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
41 // - INFORMATIONDIALOG -
44 using namespace ::com::sun::star;
45 using namespace ::com::sun::star::io;
46 using namespace ::com::sun::star::ui;
47 using namespace ::com::sun::star::awt;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::util;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::frame;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::script;
54 using namespace ::com::sun::star::container;
60 OUString InsertFixedText( InformationDialog& rInformationDialog, const OUString& rControlName, const OUString& rLabel,
61 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, bool bMultiLine, sal_Int16 nTabIndex )
63 OUString pNames[] = {
64 OUString("Height"),
65 OUString("Label"),
66 OUString("MultiLine"),
67 OUString("PositionX"),
68 OUString("PositionY"),
69 OUString("Step"),
70 OUString("TabIndex"),
71 OUString("Width") };
73 Any pValues[] = {
74 Any( nHeight ),
75 Any( rLabel ),
76 Any( bMultiLine ),
77 Any( nXPos ),
78 Any( nYPos ),
79 Any( (sal_Int16)0 ),
80 Any( nTabIndex ),
81 Any( nWidth ) };
83 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
85 Sequence< OUString > aNames( pNames, nCount );
86 Sequence< Any > aValues( pValues, nCount );
88 rInformationDialog.insertFixedText( rControlName, aNames, aValues );
89 return rControlName;
92 OUString InsertImage(
93 InformationDialog& rInformationDialog,
94 const OUString& rControlName,
95 const OUString& rURL,
96 sal_Int32 nPosX,
97 sal_Int32 nPosY,
98 sal_Int32 nWidth,
99 sal_Int32 nHeight,
100 bool bScale )
102 OUString pNames[] = {
103 OUString("Border"),
104 OUString("Height"),
105 OUString("ImageURL"),
106 OUString("PositionX"),
107 OUString("PositionY"),
108 OUString("ScaleImage"),
109 OUString("Width") };
111 Any pValues[] = {
112 Any( sal_Int16( 0 ) ),
113 Any( nHeight ),
114 Any( rURL ),
115 Any( nPosX ),
116 Any( nPosY ),
117 Any( bScale ),
118 Any( nWidth ) };
119 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
121 Sequence< OUString > aNames( pNames, nCount );
122 Sequence< Any > aValues( pValues, nCount );
124 rInformationDialog.insertImage( rControlName, aNames, aValues );
125 return rControlName;
128 OUString InsertCheckBox( InformationDialog& rInformationDialog, const OUString& rControlName,
129 const Reference< XItemListener >& rItemListener, const OUString& rLabel,
130 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex )
132 OUString pNames[] = {
133 OUString("Enabled"),
134 OUString("Height"),
135 OUString("Label"),
136 OUString("PositionX"),
137 OUString("PositionY"),
138 OUString("Step"),
139 OUString("TabIndex"),
140 OUString("Width") };
142 Any pValues[] = {
143 Any( sal_True ),
144 Any( nHeight ),
145 Any( rLabel ),
146 Any( nXPos ),
147 Any( nYPos ),
148 Any( (sal_Int16)0 ),
149 Any( nTabIndex ),
150 Any( nWidth ) };
152 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
154 Sequence< OUString > aNames( pNames, nCount );
155 Sequence< Any > aValues( pValues, nCount );
157 Reference< XCheckBox > xCheckBox( rInformationDialog.insertCheckBox( rControlName, aNames, aValues ) );
158 if ( rItemListener.is() )
159 xCheckBox->addItemListener( rItemListener );
160 return rControlName;
163 OUString InsertButton( InformationDialog& rInformationDialog, const OUString& rControlName, Reference< XActionListener >& xActionListener,
164 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex, PPPOptimizerTokenEnum nResID )
166 OUString pNames[] = {
167 OUString("Enabled"),
168 OUString("Height"),
169 OUString("Label"),
170 OUString("PositionX"),
171 OUString("PositionY"),
172 OUString("PushButtonType"),
173 OUString("Step"),
174 OUString("TabIndex"),
175 OUString("Width") };
177 Any pValues[] = {
178 Any( sal_True ),
179 Any( nHeight ),
180 Any( rInformationDialog.getString( nResID ) ),
181 Any( nXPos ),
182 Any( nYPos ),
183 Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
184 Any( (sal_Int16)0 ),
185 Any( nTabIndex ),
186 Any( nWidth ) };
189 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
191 Sequence< OUString > aNames( pNames, nCount );
192 Sequence< Any > aValues( pValues, nCount );
194 rInformationDialog.insertButton( rControlName, xActionListener, aNames, aValues );
195 return rControlName;
199 static OUString ImpValueOfInMB( const sal_Int64& rVal )
201 double fVal( static_cast<double>( rVal ) );
202 fVal /= ( 1 << 20 );
203 fVal += 0.05;
204 OUStringBuffer aVal( OUString::number( fVal ) );
205 sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.', 0 ) );
206 if ( nX > 0 )
207 aVal.setLength( nX + 2 );
208 return aVal.makeStringAndClear();
211 void InformationDialog::InitDialog()
213 sal_Int32 nDialogHeight = DIALOG_HEIGHT;
214 if ( maSaveAsURL.isEmpty() )
215 nDialogHeight -= 22;
217 // setting the dialog properties
218 OUString pNames[] = {
219 OUString("Closeable"),
220 OUString("Height"),
221 OUString("Moveable"),
222 OUString("PositionX"),
223 OUString("PositionY"),
224 OUString("Title"),
225 OUString("Width") };
227 Any pValues[] = {
228 Any( sal_True ),
229 Any( nDialogHeight ),
230 Any( sal_True ),
231 Any( sal_Int32( 245 ) ),
232 Any( sal_Int32( 115 ) ),
233 Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
234 Any( sal_Int32( DIALOG_WIDTH ) ) };
236 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
238 Sequence< OUString > aNames( pNames, nCount );
239 Sequence< Any > aValues( pValues, nCount );
241 mxDialogModelMultiPropertySet->setPropertyValues( aNames, aValues );
243 sal_Int64 nSource = mnSourceSize;
244 sal_Int64 nDest = mnDestSize;
246 PPPOptimizerTokenEnum eInfoString( STR_INFO_1 );
247 if ( mnSourceSize )
249 if ( mnDestSize )
250 eInfoString = STR_INFO_1;
251 else
253 eInfoString = STR_INFO_2;
254 nDest = mnApproxSize;
257 else if ( mnDestSize )
258 eInfoString = STR_INFO_3;
259 else
261 eInfoString = STR_INFO_4;
262 nDest = mnApproxSize;
265 OUString aTitle;
266 if ( !maSaveAsURL.isEmpty() )
268 Reference< XURLTransformer > xURLTransformer( URLTransformer::create(UnoDialog::mxContext) );
269 util::URL aURL, aPresentationURL;
270 aURL.Complete = maSaveAsURL;
271 xURLTransformer->parseSmart( aURL, OUString() );
273 const OUString sFileProtocol( "file:///" );
274 aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
275 aTitle = xURLTransformer->getPresentation( aPresentationURL, sal_False );
277 if ( aTitle.match( sFileProtocol, 0 ) )
278 aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), OUString() );
281 OUString aInfoString( getString( eInfoString ) );
282 const OUString aOldSizePlaceholder( "%OLDFILESIZE" );
283 const OUString aNewSizePlaceholder( "%NEWFILESIZE" );
284 const OUString aTitlePlaceholder( !aTitle.isEmpty() ? OUString("%TITLE" )
285 : OUString("'%TITLE'") );
287 sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder, 0 );
288 if ( i >= 0 )
289 aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
291 sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder, 0 );
292 if ( j >= 0 )
293 aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
295 sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder, 0 );
296 if ( k >= 0 )
297 aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
299 com::sun::star::uno::Reference< com::sun::star::awt::XItemListener > xItemListener;
300 InsertImage( *this,
301 OUString( "aboutimage" ),
302 OUString( "private:standardimage/query" ),
303 5, 5, 25, 25, false );
304 InsertFixedText( *this, OUString("fixedtext"), aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, true, 0 );
305 if ( !maSaveAsURL.isEmpty() )
306 InsertCheckBox( *this, "OpenNewDocument", xItemListener, getString( STR_AUTOMATICALLY_OPEN ), PAGE_POS_X, 42, PAGE_WIDTH, 8, 1 );
307 InsertButton( *this, OUString("button"), mxActionListener, DIALOG_WIDTH / 2 - 25, nDialogHeight - 20, 50, 14, 2, STR_OK );
309 bool bOpenNewDocument = mrbOpenNewDocument;
310 setControlProperty( "OpenNewDocument", "State", Any( (sal_Int16)bOpenNewDocument ) );
315 InformationDialog::InformationDialog( const Reference< XComponentContext > &rxContext, Reference< XFrame >& rxFrame, const OUString& rSaveAsURL, bool& rbOpenNewDocument, const sal_Int64& rSourceSize, const sal_Int64& rDestSize, const sal_Int64& rApproxSize ) :
316 UnoDialog( rxContext, rxFrame ),
317 ConfigurationAccess( rxContext, NULL ),
318 mxFrame( rxFrame ),
319 mxActionListener( new OKActionListener( *this ) ),
320 mnSourceSize( rSourceSize ),
321 mnDestSize( rDestSize ),
322 mnApproxSize( rApproxSize ),
323 mrbOpenNewDocument( rbOpenNewDocument ),
324 maSaveAsURL( rSaveAsURL )
326 Reference< XFrame > xFrame( mxController->getFrame() );
327 Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
328 Reference< XWindowPeer > xWindowPeer( xContainerWindow, UNO_QUERY_THROW );
329 createWindowPeer( xWindowPeer );
331 InitDialog();
336 InformationDialog::~InformationDialog()
342 bool InformationDialog::execute()
344 UnoDialog::execute();
346 if ( !maSaveAsURL.isEmpty() )
348 sal_Int16 nInt16 = 0;
349 Any aAny( getControlProperty( "OpenNewDocument", "State" ) );
350 if ( aAny >>= nInt16 )
352 bool bOpenNewDocument = static_cast< bool >( nInt16 );
353 mrbOpenNewDocument = bOpenNewDocument;
356 return mbStatus;
361 void OKActionListener::actionPerformed( const ActionEvent& rEvent )
362 throw ( com::sun::star::uno::RuntimeException, std::exception )
364 if ( rEvent.ActionCommand == "button" )
366 mrInformationDialog.endExecute( true );
369 void OKActionListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
370 throw ( com::sun::star::uno::RuntimeException, std::exception )
374 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */