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 "minimizer.hrc"
30 #include "helpid.hrc"
31 
32 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
33 #include <com/sun/star/awt/XUnitConversion.hpp>
34 #include <com/sun/star/graphic/XGraphicProvider.hpp>
35 #include <com/sun/star/graphic/XGraphic.hpp>
36 #include <com/sun/star/graphic/GraphicType.hpp>
37 #include <rtl/ustrbuf.hxx>
38 #include "com/sun/star/util/URL.hpp"
39 #include "com/sun/star/util/XURLTransformer.hpp"
40 #include <com/sun/star/util/MeasureUnit.hpp>
41 #include <com/sun/star/awt/PosSize.hpp>
42 
43 #define DIALOG_WIDTH	240
44 #define DIALOG_HEIGHT	80
45 #define PAGE_POS_X		35
46 #define PAGE_WIDTH		( DIALOG_WIDTH - PAGE_POS_X ) - 6
47 
48 
49 // ---------------------
50 // - INFORMATIONDIALOG -
51 // ---------------------
52 
53 using namespace ::com::sun::star;
54 using namespace ::com::sun::star::io;
55 using namespace ::com::sun::star::ui;
56 using namespace ::com::sun::star::awt;
57 using namespace ::com::sun::star::uno;
58 using namespace ::com::sun::star::util;
59 using namespace ::com::sun::star::lang;
60 using namespace ::com::sun::star::frame;
61 using namespace ::com::sun::star::beans;
62 using namespace ::com::sun::star::script;
63 using namespace ::com::sun::star::container;
64 
65 using ::rtl::OUString;
66 
67 // -----------------------------------------------------------------------------
68 
InsertFixedText(const rtl::OUString & rControlName,const OUString & rLabel,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int32 nHeight,sal_Bool bMultiLine,sal_Int16 nTabIndex)69 Reference< XFixedText > InformationDialog::InsertFixedText(
70     const rtl::OUString& rControlName,
71     const OUString& rLabel,
72     sal_Int32 nXPos,
73     sal_Int32 nYPos,
74     sal_Int32 nWidth,
75     sal_Int32 nHeight,
76     sal_Bool bMultiLine,
77     sal_Int16 nTabIndex )
78 {
79 	OUString pNames[] = {
80 		TKGet( TK_Height ),
81 		TKGet( TK_Label ),
82 		TKGet( TK_MultiLine ),
83 		TKGet( TK_PositionX ),
84 		TKGet( TK_PositionY ),
85 		TKGet( TK_Step ),
86 		TKGet( TK_TabIndex ),
87 		TKGet( TK_Width ) };
88 
89 	Any	pValues[] = {
90 		Any( nHeight ),
91 		Any( rLabel ),
92 		Any( bMultiLine ),
93 		Any( nXPos ),
94 		Any( nYPos ),
95 		Any( (sal_Int16)0 ),
96 		Any( nTabIndex ),
97 		Any( nWidth ) };
98 
99 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
100 
101 	Sequence< rtl::OUString >	aNames( pNames, nCount );
102 	Sequence< Any >				aValues( pValues, nCount );
103 
104     return insertFixedText( rControlName, aNames, aValues );
105 }
106 
InsertImage(const OUString & rControlName,const OUString & rURL,sal_Int32 nPosX,sal_Int32 nPosY,sal_Int32 nWidth,sal_Int32 nHeight,sal_Bool bScale)107 Reference< XControl > InformationDialog::InsertImage(
108     const OUString& rControlName,
109     const OUString& rURL,
110     sal_Int32 nPosX,
111     sal_Int32 nPosY,
112     sal_Int32 nWidth,
113     sal_Int32 nHeight,
114     sal_Bool bScale )
115 {
116 	OUString pNames[] = {
117 		TKGet( TK_Border ),
118 		TKGet( TK_Height ),
119 		TKGet( TK_ImageURL ),
120 		TKGet( TK_PositionX ),
121 		TKGet( TK_PositionY ),
122 		TKGet( TK_ScaleImage ),
123 		TKGet( TK_Width ) };
124 
125 	Any	pValues[] = {
126 		Any( sal_Int16( 0 ) ),
127 		Any( nHeight ),
128 		Any( rURL ),
129 		Any( nPosX ),
130 		Any( nPosY ),
131 		Any( bScale ),
132 		Any( nWidth ) };
133 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
134 
135 	Sequence< rtl::OUString >	aNames( pNames, nCount );
136 	Sequence< Any >				aValues( pValues, nCount );
137 
138 	return insertImage( rControlName, aNames, aValues );
139 }
140 
InsertCheckBox(const OUString & rControlName,const OUString & rLabel,const OUString & rHelpURL,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int32 nHeight,sal_Int16 nTabIndex)141 Reference< XCheckBox > InformationDialog::InsertCheckBox(
142     const OUString& rControlName,
143     const OUString& rLabel,
144     const OUString& rHelpURL,
145     sal_Int32 nXPos,
146     sal_Int32 nYPos,
147     sal_Int32 nWidth,
148     sal_Int32 nHeight,
149     sal_Int16 nTabIndex )
150 {
151 	OUString pNames[] = {
152 		TKGet( TK_Enabled ),
153 		TKGet( TK_Height ),
154         TKGet( TK_HelpURL ),
155 		TKGet( TK_Label ),
156 		TKGet( TK_PositionX ),
157 		TKGet( TK_PositionY ),
158 		TKGet( TK_Step ),
159 		TKGet( TK_TabIndex ),
160 		TKGet( TK_Width ) };
161 
162 	Any	pValues[] = {
163 		Any( sal_True ),
164 		Any( nHeight ),
165         Any( rHelpURL ),
166 		Any( rLabel ),
167 		Any( nXPos ),
168 		Any( nYPos ),
169 		Any( (sal_Int16)0 ),
170 		Any( nTabIndex ),
171 		Any( nWidth ) };
172 
173 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
174 
175 	Sequence< rtl::OUString >	aNames( pNames, nCount );
176 	Sequence< Any >				aValues( pValues, nCount );
177 
178 	return insertCheckBox( rControlName, aNames, aValues );
179 }
180 
InsertButton(const OUString & rControlName,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int32 nHeight,sal_Int16 nTabIndex,sal_Int32 nResID)181 Reference< XButton > InformationDialog::InsertButton(
182     const OUString& rControlName,
183     sal_Int32 nXPos,
184     sal_Int32 nYPos,
185     sal_Int32 nWidth,
186     sal_Int32 nHeight,
187     sal_Int16 nTabIndex,
188     sal_Int32 nResID )
189 {
190 	OUString pNames[] = {
191 		TKGet( TK_Enabled ),
192 		TKGet( TK_Height ),
193 		TKGet( TK_Label ),
194 		TKGet( TK_PositionX ),
195 		TKGet( TK_PositionY ),
196 		TKGet( TK_PushButtonType ),
197 		TKGet( TK_Step ),
198 		TKGet( TK_TabIndex ),
199 		TKGet( TK_Width ) };
200 
201 	Any	pValues[] = {
202 		Any( sal_True ),
203 		Any( nHeight ),
204 		Any( getString( nResID ) ),
205 		Any( nXPos ),
206 		Any( nYPos ),
207 		Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
208 		Any( (sal_Int16)0 ),
209 		Any( nTabIndex ),
210 		Any( nWidth ) };
211 
212 
213 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
214 
215 	Sequence< rtl::OUString >	aNames( pNames, nCount );
216 	Sequence< Any >				aValues( pValues, nCount );
217 
218 	return insertButton( rControlName, Reference< XActionListener >(), aNames, aValues );
219 }
220 
221 
ImpValueOfInMB(const sal_Int64 & rVal)222 static OUString ImpValueOfInMB( const sal_Int64& rVal )
223 {
224 	double fVal( static_cast<double>( rVal ) );
225 	fVal /= ( 1 << 20 );
226 	fVal += 0.05;
227 	rtl::OUStringBuffer aVal( OUString::valueOf( fVal ) );
228 	sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.', 0 ) );
229 	if ( nX > 0 )
230 		aVal.setLength( nX + 2 );
231 	return aVal.makeStringAndClear();
232 }
233 
InitDialog()234 void InformationDialog::InitDialog()
235 {
236 	sal_Int32 nDialogHeight = DIALOG_HEIGHT;
237 	if ( !maSaveAsURL.getLength() )
238 		nDialogHeight -= 22;
239 
240    // setting the dialog properties
241 	OUString pNames[] = {
242 		TKGet( TK_Closeable ),
243 		TKGet( TK_Height ),
244         TKGet( TK_HelpURL ),
245 		TKGet( TK_Moveable ),
246 		TKGet( TK_PositionX ),
247 		TKGet( TK_PositionY ),
248 		TKGet( TK_Title ),
249 		TKGet( TK_Width ) };
250 
251 	Any	pValues[] = {
252 		Any( sal_True ),
253 		Any( nDialogHeight ),
254 		Any( HID( HID_SDEXT_MINIMIZER_INFOBOX_DLG ) ),
255 		Any( sal_True ),
256 		Any( sal_Int32( 245 ) ),
257 		Any( sal_Int32( 115 ) ),
258 		Any( getString( STR_INFO_DIALOG ) ),
259 		Any( sal_Int32( DIALOG_WIDTH ) ) };
260 
261 	sal_Int32 nCount = sizeof( pNames ) / sizeof( OUString );
262 
263 	Sequence< rtl::OUString >	aNames( pNames, nCount );
264 	Sequence< Any >				aValues( pValues, nCount );
265 
266 	mxDialogModelMultiPropertySet->setPropertyValues( aNames, aValues );
267 
268 	sal_Int64 nSource = mnSourceSize;
269 	sal_Int64 nDest   = mnDestSize;
270 
271 	sal_Int32 nInfoStrResId( STR_INFO_1 );
272 	if ( mnSourceSize )
273 	{
274 		if ( mnDestSize )
275 			nInfoStrResId = STR_INFO_1;
276 		else
277 		{
278 			nInfoStrResId = STR_INFO_2;
279 			nDest = mnApproxSize;
280 		}
281 	}
282 	else if ( mnDestSize )
283 		nInfoStrResId = STR_INFO_3;
284 	else
285 	{
286 		nInfoStrResId = STR_INFO_4;
287 		nDest = mnApproxSize;
288 	}
289 
290 	rtl::OUString aTitle;
291 	if ( maSaveAsURL.getLength() )
292 	{
293 		Reference< XURLTransformer > xURLTransformer( mxContext->getServiceManager()->createInstanceWithContext(
294 				OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.util.URLTransformer" ) ), mxContext ), UNO_QUERY );
295 		if ( xURLTransformer.is() )
296 		{
297 			util::URL aURL, aPresentationURL;
298 			aURL.Complete = maSaveAsURL;
299 			xURLTransformer->parseSmart( aURL, rtl::OUString() );
300 
301 			const OUString sFileProtocol( RTL_CONSTASCII_USTRINGPARAM( "file:///" ) );
302 			aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
303 			aTitle = xURLTransformer->getPresentation( aPresentationURL, sal_False );
304 
305 			if ( aTitle.match( sFileProtocol, 0 ) )
306 				aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), rtl::OUString() );
307 		}
308 	}
309 
310 	OUString aInfoString( getString( nInfoStrResId ) );
311 	const OUString aOldSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%OLDFILESIZE" ) );
312 	const OUString aNewSizePlaceholder( RTL_CONSTASCII_USTRINGPARAM( "%NEWFILESIZE" ) );
313 	const OUString aTitlePlaceholder( aTitle.getLength() ? OUString::createFromAscii( "%TITLE" ) : OUString::createFromAscii( "'%TITLE'" ) );
314 
315 	sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder, 0 );
316 	if ( i >= 0 )
317 		aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
318 
319 	sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder, 0 );
320 	if ( j >= 0 )
321 		aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
322 
323 	sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder, 0 );
324 	if ( k >= 0 )
325 		aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
326 
327 
328     Reference< XControl > xImageCtrl = InsertImage( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "aboutimage" ) ),
329                  rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "private:standardimage/query" ) ),
330                  3, 3, 25, 25, sal_False );
331 	Reference< XControl > xFixedText( InsertFixedText( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "fixedtext" ) ), aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, sal_True, 0 ), UNO_QUERY);
332 
333 	if ( maSaveAsURL.getLength() )
334         mxCheckBox = InsertCheckBox( TKGet( TK_OpenNewDocument ),
335                                      getString( STR_AUTOMATICALLY_OPEN ),
336                                      HID( HID_SDEXT_MINIMIZER_INFOBOX_CB_OPENNEW ),
337                                      PAGE_POS_X, 42,
338                                      PAGE_WIDTH, 8, 1 );
339     Reference< XControl> xButtonCtrl( InsertButton( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "button" ) ), DIALOG_WIDTH / 2 - 15, nDialogHeight - 20, 30, 14, 2, STR_OK ), UNO_QUERY );
340 
341 	sal_Bool bOpenNewDocument = mrbOpenNewDocument;
342 	setControlProperty( TKGet( TK_OpenNewDocument ), TKGet( TK_State ), Any( (sal_Int16)bOpenNewDocument ) );
343 
344     sal_Int32 nX, nY, nDlgWidth;
345     Size aSize3( 3, 3 );
346     Reference< XUnitConversion > xUnitConverter( mxDialogWindow, UNO_QUERY_THROW );
347     aSize3 = xUnitConverter->convertSizeToPixel( aSize3, util::MeasureUnit::APPFONT );
348     nX = nY = aSize3.Width;
349 
350     Reference< XPropertySet > xImageProps (xImageCtrl->getModel(), UNO_QUERY_THROW );
351     Reference< graphic::XGraphic > xGraphic(
352         xImageProps->getPropertyValue(
353             OUString( RTL_CONSTASCII_USTRINGPARAM( "Graphic") ) ), UNO_QUERY_THROW );
354     if ( xGraphic.is() && xGraphic->getType() != graphic::GraphicType::EMPTY )
355     {
356         Size aSizePixel;
357         Rectangle aPosSizePixel;
358         Reference< XPropertySet > xGraphicProps( xGraphic, UNO_QUERY_THROW );
359         xGraphicProps->getPropertyValue(OUString(RTL_CONSTASCII_USTRINGPARAM("SizePixel"))) >>= aSizePixel;
360         Reference< XWindow > xImage (xImageCtrl->getPeer(), UNO_QUERY_THROW );
361 
362         xImage->setPosSize( nX, nY,
363                             aSizePixel.Width,
364                             aSizePixel.Height,
365                             awt::PosSize::POSSIZE );
366 
367         nX += aSizePixel.Width + aSize3.Width;
368 
369         Reference< XWindow > xLabel ( xFixedText->getPeer(), UNO_QUERY_THROW );
370         aPosSizePixel = xLabel->getPosSize();
371         xLabel->setPosSize( nX, nY,
372                             aPosSizePixel.Width,
373                             aPosSizePixel.Height,
374                             awt::PosSize::POSSIZE );
375 
376         nDlgWidth = nX + aPosSizePixel.Width + aSize3.Width;
377         nY += aPosSizePixel.Height + aSize3.Height;
378 
379         if ( mxCheckBox.is() )
380         {
381             Reference< XControl > xCheckBoxCtrl( mxCheckBox, UNO_QUERY_THROW );
382             Reference< XWindow > xCheckBox( xCheckBoxCtrl->getPeer(), UNO_QUERY_THROW );
383             Rectangle aCBSize = xCheckBox->getPosSize();
384             xCheckBox->setPosSize( nX, nY,
385                                    aPosSizePixel.Width,
386                                    aCBSize.Height ,
387                                    awt::PosSize::POSSIZE);
388             nY += aCBSize.Height + aSize3.Height;
389         }
390 
391         nY += aSize3.Height;
392         Reference< XWindow > xButton ( xButtonCtrl->getPeer(), UNO_QUERY_THROW );
393         aPosSizePixel = xButton->getPosSize();
394         xButton->setPosSize( nDlgWidth / 2 - aPosSizePixel.Width / 2,
395                              nY ,
396                              aPosSizePixel.Width,
397                              aPosSizePixel.Height,
398                              awt::PosSize::POSSIZE );
399         nY += aPosSizePixel.Height + aSize3.Height;
400         mxDialogWindow->setPosSize( 0, 0, nDlgWidth, nY, awt::PosSize::SIZE );
401     }
402 
403     centerDialog();
404 }
405 
406 // -----------------------------------------------------------------------------
407 
InformationDialog(const Reference<XComponentContext> & rxContext,const Reference<XWindowPeer> & rxParent,const rtl::OUString & rSaveAsURL,sal_Bool & rbOpenNewDocument,const sal_Int64 & rSourceSize,const sal_Int64 & rDestSize,const sal_Int64 & rApproxSize)408 InformationDialog::InformationDialog(
409     const Reference< XComponentContext > &rxContext,
410     const Reference< XWindowPeer>& rxParent,
411     const rtl::OUString& rSaveAsURL,
412     sal_Bool& rbOpenNewDocument,
413     const sal_Int64& rSourceSize,
414     const sal_Int64& rDestSize,
415     const sal_Int64& rApproxSize )
416 :   UnoDialog( rxContext, rxParent ),
417 	ConfigurationAccess( rxContext, NULL ),
418 	mxContext( rxContext ),
419 	mnSourceSize( rSourceSize ),
420 	mnDestSize( rDestSize ),
421 	mnApproxSize( rApproxSize ),
422 	mrbOpenNewDocument( rbOpenNewDocument ),
423 	maSaveAsURL( rSaveAsURL )
424 {
425     OSL_TRACE( "InformationDialog::InformationDialog" );
426 	createWindowPeer();
427 
428 	InitDialog();
429 }
430 
431 // -----------------------------------------------------------------------------
432 
~InformationDialog()433 InformationDialog::~InformationDialog()
434 {
435     OSL_TRACE( "InformationDialog::~InformationDialog" );
436 }
437 
438 // -----------------------------------------------------------------------------
439 
execute()440 sal_Bool InformationDialog::execute()
441 {
442     OSL_TRACE( "InformationDialog::execute" );
443 	UnoDialog::execute();
444 
445 	if ( maSaveAsURL.getLength() && mxCheckBox.is() )
446 	{
447         sal_Bool bOpenNewDocument = static_cast< sal_Bool >( mxCheckBox->getState() );
448         mrbOpenNewDocument = bOpenNewDocument;
449 	}
450 
451 	endExecute( sal_True );
452 	return mbStatus;
453 }
454 
455