1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sdext.hxx"
26 
27 #include "informationdialog.hxx"
28 #include "optimizationstats.hxx"
29 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
30 #include <com/sun/star/graphic/XGraphicProvider.hpp>
31 #include <com/sun/star/graphic/XGraphic.hpp>
32 #include <rtl/ustrbuf.hxx>
33 #include "com/sun/star/util/URL.hpp"
34 #include "com/sun/star/util/XURLTransformer.hpp"
35 
36 #define DIALOG_WIDTH	240
37 #define DIALOG_HEIGHT	80
38 #define PAGE_POS_X		35
39 #define PAGE_WIDTH		( DIALOG_WIDTH - PAGE_POS_X ) - 6
40 
41 
42 // ---------------------
43 // - INFORMATIONDIALOG -
44 // ---------------------
45 
46 using namespace ::rtl;
47 using namespace ::com::sun::star;
48 using namespace ::com::sun::star::io;
49 using namespace ::com::sun::star::ui;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::util;
53 using namespace ::com::sun::star::lang;
54 using namespace ::com::sun::star::frame;
55 using namespace ::com::sun::star::beans;
56 using namespace ::com::sun::star::script;
57 using namespace ::com::sun::star::container;
58 
59 
60 
61 // -----------------------------------------------------------------------------
62 
63 rtl::OUString InsertFixedText( InformationDialog& rInformationDialog, const rtl::OUString& rControlName, const OUString& rLabel,
64 								sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Bool bMultiLine, sal_Int16 nTabIndex )
65 {
66 	OUString pNames[] = {
67 		TKGet( TK_Height ),
68 		TKGet( TK_Label ),
69 		TKGet( TK_MultiLine ),
70 		TKGet( TK_PositionX ),
71 		TKGet( TK_PositionY ),
72 		TKGet( TK_Step ),
73 		TKGet( TK_TabIndex ),
74 		TKGet( TK_Width ) };
75 
76 	Any	pValues[] = {
77 		Any( nHeight ),
78 		Any( rLabel ),
79 		Any( bMultiLine ),
80 		Any( nXPos ),
81 		Any( nYPos ),
82 		Any( (sal_Int16)0 ),
83 		Any( nTabIndex ),
84 		Any( nWidth ) };
85 
86 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
87 
88 	Sequence< rtl::OUString >	aNames( pNames, nCount );
89 	Sequence< Any >				aValues( pValues, nCount );
90 
91 	rInformationDialog.insertFixedText( rControlName, aNames, aValues );
92 	return rControlName;
93 }
94 
95 rtl::OUString InsertImage( InformationDialog& rInformationDialog, const OUString& rControlName, const OUString& rURL,
96 						sal_Int32 nPosX, sal_Int32 nPosY, sal_Int32 nWidth, sal_Int32 nHeight )
97 {
98 	OUString pNames[] = {
99 		TKGet( TK_Border ),
100 		TKGet( TK_Height ),
101 		TKGet( TK_ImageURL ),
102 		TKGet( TK_PositionX ),
103 		TKGet( TK_PositionY ),
104 		TKGet( TK_ScaleImage ),
105 		TKGet( TK_Width ) };
106 
107 	Any	pValues[] = {
108 		Any( sal_Int16( 0 ) ),
109 		Any( nHeight ),
110 		Any( rURL ),
111 		Any( nPosX ),
112 		Any( nPosY ),
113 		Any( sal_True ),
114 		Any( nWidth ) };
115 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
116 
117 	Sequence< rtl::OUString >	aNames( pNames, nCount );
118 	Sequence< Any >				aValues( pValues, nCount );
119 
120 	rInformationDialog.insertImage( rControlName, aNames, aValues );
121 	return rControlName;
122 }
123 
124 rtl::OUString InsertCheckBox( InformationDialog& rInformationDialog, const OUString& rControlName,
125 	const Reference< XItemListener > xItemListener, const OUString& rLabel,
126 		sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex )
127 {
128 	OUString pNames[] = {
129 		TKGet( TK_Enabled ),
130 		TKGet( TK_Height ),
131 		TKGet( TK_Label ),
132 		TKGet( TK_PositionX ),
133 		TKGet( TK_PositionY ),
134 		TKGet( TK_Step ),
135 		TKGet( TK_TabIndex ),
136 		TKGet( TK_Width ) };
137 
138 	Any	pValues[] = {
139 		Any( sal_True ),
140 		Any( nHeight ),
141 		Any( rLabel ),
142 		Any( nXPos ),
143 		Any( nYPos ),
144 		Any( (sal_Int16)0 ),
145 		Any( nTabIndex ),
146 		Any( nWidth ) };
147 
148 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
149 
150 	Sequence< rtl::OUString >	aNames( pNames, nCount );
151 	Sequence< Any >				aValues( pValues, nCount );
152 
153 	Reference< XCheckBox > xCheckBox( rInformationDialog.insertCheckBox( rControlName, aNames, aValues ) );
154 	if ( xItemListener.is() )
155 		xCheckBox->addItemListener( xItemListener );
156 	return rControlName;
157 }
158 
159 rtl::OUString InsertButton( InformationDialog& rInformationDialog, const OUString& rControlName, Reference< XActionListener >& xActionListener,
160 	sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex, PPPOptimizerTokenEnum nResID )
161 {
162 	OUString pNames[] = {
163 		TKGet( TK_Enabled ),
164 		TKGet( TK_Height ),
165 		TKGet( TK_Label ),
166 		TKGet( TK_PositionX ),
167 		TKGet( TK_PositionY ),
168 		TKGet( TK_PushButtonType ),
169 		TKGet( TK_Step ),
170 		TKGet( TK_TabIndex ),
171 		TKGet( TK_Width ) };
172 
173 	Any	pValues[] = {
174 		Any( sal_True ),
175 		Any( nHeight ),
176 		Any( rInformationDialog.getString( nResID ) ),
177 		Any( nXPos ),
178 		Any( nYPos ),
179 		Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
180 		Any( (sal_Int16)0 ),
181 		Any( nTabIndex ),
182 		Any( nWidth ) };
183 
184 
185 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
186 
187 	Sequence< rtl::OUString >	aNames( pNames, nCount );
188 	Sequence< Any >				aValues( pValues, nCount );
189 
190 	rInformationDialog.insertButton( rControlName, xActionListener, aNames, aValues );
191 	return rControlName;
192 }
193 
194 
195 static OUString ImpValueOfInMB( const sal_Int64& rVal )
196 {
197 	double fVal( static_cast<double>( rVal ) );
198 	fVal /= ( 1 << 20 );
199 	fVal += 0.05;
200 	rtl::OUStringBuffer aVal( OUString::valueOf( fVal ) );
201 	sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.', 0 ) );
202 	if ( nX > 0 )
203 		aVal.setLength( nX + 2 );
204 	return aVal.makeStringAndClear();
205 }
206 
207 OUString InformationDialog::ImpGetStandardImage( const OUString& sPrivateURL )
208 {
209 	rtl::OUString sURL;
210 	try
211 	{
212 		mxTempFile = Reference< XStream >( mxMSF->getServiceManager()->createInstanceWithContext( OUString::createFromAscii( "com.sun.star.io.TempFile" ), mxMSF ), UNO_QUERY_THROW );
213 		Reference< XPropertySet > xPropSet( mxTempFile, UNO_QUERY );
214 		Reference< XOutputStream > xOutputStream( mxTempFile->getOutputStream() );
215 		if ( xOutputStream.is() && xPropSet.is() )
216 		{
217 			Reference< graphic::XGraphicProvider > xGraphicProvider( mxMSF->getServiceManager()->createInstanceWithContext(
218 						OUString::createFromAscii( "com.sun.star.graphic.GraphicProvider" ), mxMSF ), UNO_QUERY_THROW );
219 			Sequence< PropertyValue > aArgs( 1 );
220 			aArgs[ 0 ].Name = OUString::createFromAscii( "URL" );
221 			aArgs[ 0 ].Value <<= sPrivateURL;
222 			Reference< graphic::XGraphic > xGraphic( xGraphicProvider->queryGraphic( aArgs ) );
223 			if ( xGraphic.is() )
224 			{
225 				OUString aDestMimeType( RTL_CONSTASCII_USTRINGPARAM( "image/png" ) );
226 				Sequence< PropertyValue > aArgs2( 2 );
227 				aArgs2[ 0 ].Name = TKGet( TK_MimeType );				// the GraphicProvider is using "MimeType", the GraphicExporter "MediaType"...
228 				aArgs2[ 0 ].Value <<= aDestMimeType;
229 				aArgs2[ 1 ].Name = TKGet( TK_OutputStream );
230 				aArgs2[ 1 ].Value <<= xOutputStream;
231 				xGraphicProvider->storeGraphic( xGraphic, aArgs2 );
232 			}
233 			xPropSet->getPropertyValue( OUString::createFromAscii( "Uri" ) ) >>= sURL;
234 		}
235 	}
236 	catch( Exception& )
237 	{
238 	}
239 	return sURL;
240 }
241 
242 void InformationDialog::InitDialog()
243 {
244 	sal_Int32 nDialogHeight = DIALOG_HEIGHT;
245 	if ( !maSaveAsURL.getLength() )
246 		nDialogHeight -= 22;
247 
248    // setting the dialog properties
249 	OUString pNames[] = {
250 		TKGet( TK_Closeable ),
251 		TKGet( TK_Height ),
252 		TKGet( TK_Moveable ),
253 		TKGet( TK_PositionX ),
254 		TKGet( TK_PositionY ),
255 		TKGet( TK_Title ),
256 		TKGet( TK_Width ) };
257 
258 	Any	pValues[] = {
259 		Any( sal_True ),
260 		Any( nDialogHeight ),
261 		Any( sal_True ),
262 		Any( sal_Int32( 245 ) ),
263 		Any( sal_Int32( 115 ) ),
264 		Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
265 		Any( sal_Int32( DIALOG_WIDTH ) ) };
266 
267 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
268 
269 	Sequence< rtl::OUString >	aNames( pNames, nCount );
270 	Sequence< Any >				aValues( pValues, nCount );
271 
272 	mxDialogModelMultiPropertySet->setPropertyValues( aNames, aValues );
273 
274 	sal_Int64 nSource = mnSourceSize;
275 	sal_Int64 nDest   = mnDestSize;
276 
277 	PPPOptimizerTokenEnum eInfoString( STR_INFO_1 );
278 	if ( mnSourceSize )
279 	{
280 		if ( mnDestSize )
281 			eInfoString = STR_INFO_1;
282 		else
283 		{
284 			eInfoString = STR_INFO_2;
285 			nDest = mnApproxSize;
286 		}
287 	}
288 	else if ( mnDestSize )
289 		eInfoString = STR_INFO_3;
290 	else
291 	{
292 		eInfoString = STR_INFO_4;
293 		nDest = mnApproxSize;
294 	}
295 
296 	rtl::OUString aTitle;
297 	if ( maSaveAsURL.getLength() )
298 	{
299 		Reference< XURLTransformer > xURLTransformer( mxMSF->getServiceManager()->createInstanceWithContext(
300 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), mxMSF ), UNO_QUERY );
301 		if ( xURLTransformer.is() )
302 		{
303 			util::URL aURL, aPresentationURL;
304 			aURL.Complete = maSaveAsURL;
305 			xURLTransformer->parseSmart( aURL, rtl::OUString() );
306 
307 			const OUString sFileProtocol( RTL_CONSTASCII_USTRINGPARAM( "file:///" ) );
308 			aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
309 			aTitle = xURLTransformer->getPresentation( aPresentationURL, sal_False );
310 
311 			if ( aTitle.match( sFileProtocol, 0 ) )
312 				aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), rtl::OUString() );
313 		}
314 	}
315 
316 	OUString aInfoString( getString( eInfoString ) );
317 	const OUString aOldSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%OLDFILESIZE" ) );
318 	const OUString aNewSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%NEWFILESIZE" ) );
319 	const OUString aTitlePlaceholder( aTitle.getLength() ? OUString::createFromAscii( "%TITLE" ) : OUString::createFromAscii( "'%TITLE'" ) );
320 
321 	sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder, 0 );
322 	if ( i >= 0 )
323 		aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
324 
325 	sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder, 0 );
326 	if ( j >= 0 )
327 		aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
328 
329 	sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder, 0 );
330 	if ( k >= 0 )
331 		aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
332 
333 	com::sun::star::uno::Reference< com::sun::star::awt::XItemListener > xItemListener;
334 	InsertImage( *this, rtl::OUString( rtl::OUString::createFromAscii( "aboutimage" ) ), ImpGetStandardImage( rtl::OUString::createFromAscii( "private:standardimage/query" ) ), 5, 5, 25, 25 );
335 	InsertFixedText( *this, rtl::OUString( rtl::OUString::createFromAscii( "fixedtext" ) ), aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, sal_True, 0 );
336 	if ( maSaveAsURL.getLength() )
337 		InsertCheckBox(  *this, TKGet( TK_OpenNewDocument ), xItemListener, getString( STR_AUTOMATICALLY_OPEN ), PAGE_POS_X, 42, PAGE_WIDTH, 8, 1 );
338 	InsertButton( *this, rtl::OUString( rtl::OUString::createFromAscii( "button" ) ), mxActionListener, DIALOG_WIDTH / 2 - 25, nDialogHeight - 20, 50, 14, 2, STR_OK );
339 
340 	sal_Bool bOpenNewDocument = mrbOpenNewDocument;
341 	setControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ), Any( (sal_Int16)bOpenNewDocument ) );
342 }
343 
344 // -----------------------------------------------------------------------------
345 
346 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 ) :
347 	UnoDialog( rxMSF, rxFrame ),
348 	ConfigurationAccess( rxMSF, NULL ),
349 	mxMSF( rxMSF ),
350 	mxFrame( rxFrame ),
351 	mxActionListener( new OKActionListener( *this ) ),
352 	mnSourceSize( rSourceSize ),
353 	mnDestSize( rDestSize ),
354 	mnApproxSize( rApproxSize ),
355 	mrbOpenNewDocument( rbOpenNewDocument ),
356 	maSaveAsURL( rSaveAsURL )
357 {
358 	Reference< XFrame > xFrame( mxController->getFrame() );
359 	Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
360 	Reference< XWindowPeer > xWindowPeer( xContainerWindow, UNO_QUERY_THROW );
361 	createWindowPeer( xWindowPeer );
362 
363 	InitDialog();
364 }
365 
366 // -----------------------------------------------------------------------------
367 
368 InformationDialog::~InformationDialog()
369 {
370 }
371 
372 // -----------------------------------------------------------------------------
373 
374 sal_Bool InformationDialog::execute()
375 {
376 	UnoDialog::execute();
377 
378 	if ( maSaveAsURL.getLength() )
379 	{
380 		sal_Int16 nInt16 = 0;
381 		Any aAny( getControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ) ) );
382 		if ( aAny >>= nInt16 )
383 		{
384 			sal_Bool bOpenNewDocument = static_cast< sal_Bool >( nInt16 );
385 			mrbOpenNewDocument = bOpenNewDocument;
386 		}
387 	}
388 	return mbStatus;
389 }
390 
391 // -----------------------------------------------------------------------------
392 
393 void OKActionListener::actionPerformed( const ActionEvent& rEvent )
394 	throw ( com::sun::star::uno::RuntimeException )
395 {
396 	if ( rEvent.ActionCommand == rtl::OUString( rtl::OUString::createFromAscii( "button" ) ) )
397 	{
398 		mrInformationDialog.endExecute( sal_True );
399 	}
400 }
401 void OKActionListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
402 	throw ( com::sun::star::uno::RuntimeException )
403 {
404 }
405