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(
96     InformationDialog& rInformationDialog,
97     const OUString& rControlName,
98     const OUString& rURL,
99     sal_Int32 nPosX,
100     sal_Int32 nPosY,
101     sal_Int32 nWidth,
102     sal_Int32 nHeight,
103     sal_Bool bScale )
104 {
105 	OUString pNames[] = {
106 		TKGet( TK_Border ),
107 		TKGet( TK_Height ),
108 		TKGet( TK_ImageURL ),
109 		TKGet( TK_PositionX ),
110 		TKGet( TK_PositionY ),
111 		TKGet( TK_ScaleImage ),
112 		TKGet( TK_Width ) };
113 
114 	Any	pValues[] = {
115 		Any( sal_Int16( 0 ) ),
116 		Any( nHeight ),
117 		Any( rURL ),
118 		Any( nPosX ),
119 		Any( nPosY ),
120 		Any( bScale ),
121 		Any( nWidth ) };
122 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
123 
124 	Sequence< rtl::OUString >	aNames( pNames, nCount );
125 	Sequence< Any >				aValues( pValues, nCount );
126 
127 	rInformationDialog.insertImage( rControlName, aNames, aValues );
128 	return rControlName;
129 }
130 
131 rtl::OUString InsertCheckBox( InformationDialog& rInformationDialog, const OUString& rControlName,
132 	const Reference< XItemListener > xItemListener, const OUString& rLabel,
133 		sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex )
134 {
135 	OUString pNames[] = {
136 		TKGet( TK_Enabled ),
137 		TKGet( TK_Height ),
138 		TKGet( TK_Label ),
139 		TKGet( TK_PositionX ),
140 		TKGet( TK_PositionY ),
141 		TKGet( TK_Step ),
142 		TKGet( TK_TabIndex ),
143 		TKGet( TK_Width ) };
144 
145 	Any	pValues[] = {
146 		Any( sal_True ),
147 		Any( nHeight ),
148 		Any( rLabel ),
149 		Any( nXPos ),
150 		Any( nYPos ),
151 		Any( (sal_Int16)0 ),
152 		Any( nTabIndex ),
153 		Any( nWidth ) };
154 
155 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
156 
157 	Sequence< rtl::OUString >	aNames( pNames, nCount );
158 	Sequence< Any >				aValues( pValues, nCount );
159 
160 	Reference< XCheckBox > xCheckBox( rInformationDialog.insertCheckBox( rControlName, aNames, aValues ) );
161 	if ( xItemListener.is() )
162 		xCheckBox->addItemListener( xItemListener );
163 	return rControlName;
164 }
165 
166 rtl::OUString InsertButton( InformationDialog& rInformationDialog, const OUString& rControlName, Reference< XActionListener >& xActionListener,
167 	sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, sal_Int16 nTabIndex, PPPOptimizerTokenEnum nResID )
168 {
169 	OUString pNames[] = {
170 		TKGet( TK_Enabled ),
171 		TKGet( TK_Height ),
172 		TKGet( TK_Label ),
173 		TKGet( TK_PositionX ),
174 		TKGet( TK_PositionY ),
175 		TKGet( TK_PushButtonType ),
176 		TKGet( TK_Step ),
177 		TKGet( TK_TabIndex ),
178 		TKGet( TK_Width ) };
179 
180 	Any	pValues[] = {
181 		Any( sal_True ),
182 		Any( nHeight ),
183 		Any( rInformationDialog.getString( nResID ) ),
184 		Any( nXPos ),
185 		Any( nYPos ),
186 		Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
187 		Any( (sal_Int16)0 ),
188 		Any( nTabIndex ),
189 		Any( nWidth ) };
190 
191 
192 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
193 
194 	Sequence< rtl::OUString >	aNames( pNames, nCount );
195 	Sequence< Any >				aValues( pValues, nCount );
196 
197 	rInformationDialog.insertButton( rControlName, xActionListener, aNames, aValues );
198 	return rControlName;
199 }
200 
201 
202 static OUString ImpValueOfInMB( const sal_Int64& rVal )
203 {
204 	double fVal( static_cast<double>( rVal ) );
205 	fVal /= ( 1 << 20 );
206 	fVal += 0.05;
207 	rtl::OUStringBuffer aVal( OUString::valueOf( fVal ) );
208 	sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.', 0 ) );
209 	if ( nX > 0 )
210 		aVal.setLength( nX + 2 );
211 	return aVal.makeStringAndClear();
212 }
213 
214 void InformationDialog::InitDialog()
215 {
216 	sal_Int32 nDialogHeight = DIALOG_HEIGHT;
217 	if ( !maSaveAsURL.getLength() )
218 		nDialogHeight -= 22;
219 
220    // setting the dialog properties
221 	OUString pNames[] = {
222 		TKGet( TK_Closeable ),
223 		TKGet( TK_Height ),
224 		TKGet( TK_Moveable ),
225 		TKGet( TK_PositionX ),
226 		TKGet( TK_PositionY ),
227 		TKGet( TK_Title ),
228 		TKGet( TK_Width ) };
229 
230 	Any	pValues[] = {
231 		Any( sal_True ),
232 		Any( nDialogHeight ),
233 		Any( sal_True ),
234 		Any( sal_Int32( 245 ) ),
235 		Any( sal_Int32( 115 ) ),
236 		Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
237 		Any( sal_Int32( DIALOG_WIDTH ) ) };
238 
239 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
240 
241 	Sequence< rtl::OUString >	aNames( pNames, nCount );
242 	Sequence< Any >				aValues( pValues, nCount );
243 
244 	mxDialogModelMultiPropertySet->setPropertyValues( aNames, aValues );
245 
246 	sal_Int64 nSource = mnSourceSize;
247 	sal_Int64 nDest   = mnDestSize;
248 
249 	PPPOptimizerTokenEnum eInfoString( STR_INFO_1 );
250 	if ( mnSourceSize )
251 	{
252 		if ( mnDestSize )
253 			eInfoString = STR_INFO_1;
254 		else
255 		{
256 			eInfoString = STR_INFO_2;
257 			nDest = mnApproxSize;
258 		}
259 	}
260 	else if ( mnDestSize )
261 		eInfoString = STR_INFO_3;
262 	else
263 	{
264 		eInfoString = STR_INFO_4;
265 		nDest = mnApproxSize;
266 	}
267 
268 	rtl::OUString aTitle;
269 	if ( maSaveAsURL.getLength() )
270 	{
271 		Reference< XURLTransformer > xURLTransformer( mxMSF->getServiceManager()->createInstanceWithContext(
272 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), mxMSF ), UNO_QUERY );
273 		if ( xURLTransformer.is() )
274 		{
275 			util::URL aURL, aPresentationURL;
276 			aURL.Complete = maSaveAsURL;
277 			xURLTransformer->parseSmart( aURL, rtl::OUString() );
278 
279 			const OUString sFileProtocol( RTL_CONSTASCII_USTRINGPARAM( "file:///" ) );
280 			aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
281 			aTitle = xURLTransformer->getPresentation( aPresentationURL, sal_False );
282 
283 			if ( aTitle.match( sFileProtocol, 0 ) )
284 				aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), rtl::OUString() );
285 		}
286 	}
287 
288 	OUString aInfoString( getString( eInfoString ) );
289 	const OUString aOldSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%OLDFILESIZE" ) );
290 	const OUString aNewSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%NEWFILESIZE" ) );
291 	const OUString aTitlePlaceholder( aTitle.getLength() ? OUString::createFromAscii( "%TITLE" ) : OUString::createFromAscii( "'%TITLE'" ) );
292 
293 	sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder, 0 );
294 	if ( i >= 0 )
295 		aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
296 
297 	sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder, 0 );
298 	if ( j >= 0 )
299 		aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
300 
301 	sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder, 0 );
302 	if ( k >= 0 )
303 		aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
304 
305     com::sun::star::uno::Reference< com::sun::star::awt::XItemListener > xItemListener;
306     InsertImage( *this,
307                  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "aboutimage" ) ),
308                  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:standardimage/query" ) ),
309                  5, 5, 25, 25, sal_False );
310 	InsertFixedText( *this, rtl::OUString( rtl::OUString::createFromAscii( "fixedtext" ) ), aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, sal_True, 0 );
311 	if ( maSaveAsURL.getLength() )
312 		InsertCheckBox(  *this, TKGet( TK_OpenNewDocument ), xItemListener, getString( STR_AUTOMATICALLY_OPEN ), PAGE_POS_X, 42, PAGE_WIDTH, 8, 1 );
313 	InsertButton( *this, rtl::OUString( rtl::OUString::createFromAscii( "button" ) ), mxActionListener, DIALOG_WIDTH / 2 - 25, nDialogHeight - 20, 50, 14, 2, STR_OK );
314 
315 	sal_Bool bOpenNewDocument = mrbOpenNewDocument;
316 	setControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ), Any( (sal_Int16)bOpenNewDocument ) );
317 }
318 
319 // -----------------------------------------------------------------------------
320 
321 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 ) :
322 	UnoDialog( rxMSF, rxFrame ),
323 	ConfigurationAccess( rxMSF, NULL ),
324 	mxMSF( rxMSF ),
325 	mxFrame( rxFrame ),
326 	mxActionListener( new OKActionListener( *this ) ),
327 	mnSourceSize( rSourceSize ),
328 	mnDestSize( rDestSize ),
329 	mnApproxSize( rApproxSize ),
330 	mrbOpenNewDocument( rbOpenNewDocument ),
331 	maSaveAsURL( rSaveAsURL )
332 {
333 	Reference< XFrame > xFrame( mxController->getFrame() );
334 	Reference< XWindow > xContainerWindow( xFrame->getContainerWindow() );
335 	Reference< XWindowPeer > xWindowPeer( xContainerWindow, UNO_QUERY_THROW );
336 	createWindowPeer( xWindowPeer );
337 
338 	InitDialog();
339 }
340 
341 // -----------------------------------------------------------------------------
342 
343 InformationDialog::~InformationDialog()
344 {
345 }
346 
347 // -----------------------------------------------------------------------------
348 
349 sal_Bool InformationDialog::execute()
350 {
351 	UnoDialog::execute();
352 
353 	if ( maSaveAsURL.getLength() )
354 	{
355 		sal_Int16 nInt16 = 0;
356 		Any aAny( getControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ) ) );
357 		if ( aAny >>= nInt16 )
358 		{
359 			sal_Bool bOpenNewDocument = static_cast< sal_Bool >( nInt16 );
360 			mrbOpenNewDocument = bOpenNewDocument;
361 		}
362 	}
363 	return mbStatus;
364 }
365 
366 // -----------------------------------------------------------------------------
367 
368 void OKActionListener::actionPerformed( const ActionEvent& rEvent )
369 	throw ( com::sun::star::uno::RuntimeException )
370 {
371 	if ( rEvent.ActionCommand == rtl::OUString( rtl::OUString::createFromAscii( "button" ) ) )
372 	{
373 		mrInformationDialog.endExecute( sal_True );
374 	}
375 }
376 void OKActionListener::disposing( const ::com::sun::star::lang::EventObject& /* Source */ )
377 	throw ( com::sun::star::uno::RuntimeException )
378 {
379 }
380