1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21 #include "informationdialog.hxx"
22 #include <com/sun/star/awt/PushButtonType.hpp>
23 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
24 #include <com/sun/star/util/URL.hpp>
25 #include <com/sun/star/util/URLTransformer.hpp>
26 #include <com/sun/star/util/XURLTransformer.hpp>
27 #include <rtl/ustrbuf.hxx>
28 #include <sal/macros.h>
29
30 #define DIALOG_WIDTH 240
31 #define DIALOG_HEIGHT 80
32 #define PAGE_POS_X 35
33 #define PAGE_WIDTH ( DIALOG_WIDTH - PAGE_POS_X ) - 6
34
35
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::ui;
38 using namespace ::com::sun::star::awt;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::util;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::beans;
44 using namespace ::com::sun::star::container;
45
46
InsertFixedText(UnoDialog & rInformationDialog,const OUString & rControlName,const OUString & rLabel,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int32 nHeight,bool bMultiLine,sal_Int16 nTabIndex)47 OUString InsertFixedText( UnoDialog& rInformationDialog, const OUString& rControlName, const OUString& rLabel,
48 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int32 nHeight, bool bMultiLine, sal_Int16 nTabIndex )
49 {
50 OUString pNames[] = {
51 OUString("Height"),
52 OUString("Label"),
53 OUString("MultiLine"),
54 OUString("PositionX"),
55 OUString("PositionY"),
56 OUString("Step"),
57 OUString("TabIndex"),
58 OUString("Width") };
59
60 Any pValues[] = {
61 Any( nHeight ),
62 Any( rLabel ),
63 Any( bMultiLine ),
64 Any( nXPos ),
65 Any( nYPos ),
66 Any( sal_Int16(0) ),
67 Any( nTabIndex ),
68 Any( nWidth ) };
69
70 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
71
72 Sequence< OUString > aNames( pNames, nCount );
73 Sequence< Any > aValues( pValues, nCount );
74
75 rInformationDialog.insertFixedText( rControlName, aNames, aValues );
76 return rControlName;
77 }
78
InsertImage(UnoDialog & rInformationDialog,const OUString & rControlName,const OUString & rURL,sal_Int32 nPosX,sal_Int32 nPosY,sal_Int32 nWidth,sal_Int32 nHeight,bool bScale)79 OUString InsertImage(
80 UnoDialog& rInformationDialog,
81 const OUString& rControlName,
82 const OUString& rURL,
83 sal_Int32 nPosX,
84 sal_Int32 nPosY,
85 sal_Int32 nWidth,
86 sal_Int32 nHeight,
87 bool bScale )
88 {
89 OUString pNames[] = {
90 OUString("Border"),
91 OUString("Height"),
92 OUString("ImageURL"),
93 OUString("PositionX"),
94 OUString("PositionY"),
95 OUString("ScaleImage"),
96 OUString("Width") };
97
98 Any pValues[] = {
99 Any( sal_Int16( 0 ) ),
100 Any( nHeight ),
101 Any( rURL ),
102 Any( nPosX ),
103 Any( nPosY ),
104 Any( bScale ),
105 Any( nWidth ) };
106 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
107
108 Sequence< OUString > aNames( pNames, nCount );
109 Sequence< Any > aValues( pValues, nCount );
110
111 rInformationDialog.insertImage( rControlName, aNames, aValues );
112 return rControlName;
113 }
114
InsertCheckBox(UnoDialog & rInformationDialog,const OUString & rControlName,const Reference<XItemListener> & rItemListener,const OUString & rLabel,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int16 nTabIndex)115 OUString InsertCheckBox( UnoDialog& rInformationDialog, const OUString& rControlName,
116 const Reference< XItemListener >& rItemListener, const OUString& rLabel,
117 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int16 nTabIndex )
118 {
119 sal_Int32 nHeight = 8;
120 OUString pNames[] = {
121 OUString("Enabled"),
122 OUString("Height"),
123 OUString("Label"),
124 OUString("PositionX"),
125 OUString("PositionY"),
126 OUString("Step"),
127 OUString("TabIndex"),
128 OUString("Width") };
129
130 Any pValues[] = {
131 Any( true ),
132 Any( nHeight ),
133 Any( rLabel ),
134 Any( nXPos ),
135 Any( nYPos ),
136 Any( sal_Int16(0) ),
137 Any( nTabIndex ),
138 Any( nWidth ) };
139
140 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
141
142 Sequence< OUString > aNames( pNames, nCount );
143 Sequence< Any > aValues( pValues, nCount );
144
145 Reference< XCheckBox > xCheckBox( rInformationDialog.insertCheckBox( rControlName, aNames, aValues ) );
146 if ( rItemListener.is() )
147 xCheckBox->addItemListener( rItemListener );
148 return rControlName;
149 }
150
InsertButton(UnoDialog & rInformationDialog,const OUString & rControlName,Reference<XActionListener> const & xActionListener,sal_Int32 nXPos,sal_Int32 nYPos,sal_Int32 nWidth,sal_Int16 nTabIndex,const OUString & rText)151 OUString InsertButton( UnoDialog& rInformationDialog, const OUString& rControlName, Reference< XActionListener > const & xActionListener,
152 sal_Int32 nXPos, sal_Int32 nYPos, sal_Int32 nWidth, sal_Int16 nTabIndex, const OUString& rText )
153 {
154 sal_Int32 nHeight = 14;
155 OUString pNames[] = {
156 OUString("Enabled"),
157 OUString("Height"),
158 OUString("Label"),
159 OUString("PositionX"),
160 OUString("PositionY"),
161 OUString("PushButtonType"),
162 OUString("Step"),
163 OUString("TabIndex"),
164 OUString("Width") };
165
166 Any pValues[] = {
167 Any( true ),
168 Any( nHeight ),
169 Any( rText ),
170 Any( nXPos ),
171 Any( nYPos ),
172 Any( static_cast< sal_Int16 >( PushButtonType_OK ) ),
173 Any( sal_Int16(0) ),
174 Any( nTabIndex ),
175 Any( nWidth ) };
176
177
178 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
179
180 Sequence< OUString > aNames( pNames, nCount );
181 Sequence< Any > aValues( pValues, nCount );
182
183 rInformationDialog.insertButton( rControlName, xActionListener, aNames, aValues );
184 return rControlName;
185 }
186
187
ImpValueOfInMB(sal_Int64 rVal)188 static OUString ImpValueOfInMB( sal_Int64 rVal )
189 {
190 double fVal( static_cast<double>( rVal ) );
191 fVal /= ( 1 << 20 );
192 fVal += 0.05;
193 OUStringBuffer aVal( OUString::number( fVal ) );
194 sal_Int32 nX( OUString( aVal.getStr() ).indexOf( '.' ) );
195 if ( nX > 0 )
196 aVal.setLength( nX + 2 );
197 return aVal.makeStringAndClear();
198 }
199
InitDialog()200 void InformationDialog::InitDialog()
201 {
202 sal_Int32 nDialogHeight = DIALOG_HEIGHT;
203 if ( maSaveAsURL.isEmpty() )
204 nDialogHeight -= 22;
205
206 // setting the dialog properties
207 OUString pNames[] = {
208 OUString("Closeable"),
209 OUString("Height"),
210 OUString("Moveable"),
211 OUString("PositionX"),
212 OUString("PositionY"),
213 OUString("Title"),
214 OUString("Width") };
215
216 Any pValues[] = {
217 Any( true ),
218 Any( nDialogHeight ),
219 Any( true ),
220 Any( sal_Int32( 245 ) ),
221 Any( sal_Int32( 115 ) ),
222 Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
223 Any( sal_Int32( DIALOG_WIDTH ) ) };
224
225 sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
226
227 Sequence< OUString > aNames( pNames, nCount );
228 Sequence< Any > aValues( pValues, nCount );
229
230 setPropertyValues(aNames, aValues);
231
232 sal_Int64 nSource = mnSourceSize;
233 sal_Int64 nDest = mnDestSize;
234
235 PPPOptimizerTokenEnum eInfoString( STR_INFO_1 );
236 if ( mnSourceSize )
237 {
238 if ( mnDestSize )
239 eInfoString = STR_INFO_1;
240 else
241 {
242 eInfoString = STR_INFO_2;
243 nDest = mnApproxSize;
244 }
245 }
246 else if ( mnDestSize )
247 eInfoString = STR_INFO_3;
248 else
249 {
250 eInfoString = STR_INFO_4;
251 nDest = mnApproxSize;
252 }
253
254 OUString aTitle;
255 if ( !maSaveAsURL.isEmpty() )
256 {
257 Reference< XURLTransformer > xURLTransformer( URLTransformer::create(UnoDialog::mxContext) );
258 util::URL aURL, aPresentationURL;
259 aURL.Complete = maSaveAsURL;
260 xURLTransformer->parseSmart( aURL, OUString() );
261
262 const OUString sFileProtocol( "file:///" );
263 aPresentationURL.Complete = sFileProtocol.concat( aURL.Name );
264 aTitle = xURLTransformer->getPresentation( aPresentationURL, false );
265
266 if ( aTitle.match( sFileProtocol ) )
267 aTitle = aTitle.replaceAt( 0, sFileProtocol.getLength(), OUString() );
268 }
269
270 OUString aInfoString( getString( eInfoString ) );
271 const OUString aOldSizePlaceholder( "%OLDFILESIZE" );
272 const OUString aNewSizePlaceholder( "%NEWFILESIZE" );
273 const OUString aTitlePlaceholder( !aTitle.isEmpty() ? OUString("%TITLE" )
274 : OUString("'%TITLE'") );
275
276 sal_Int32 i = aInfoString.indexOf( aOldSizePlaceholder );
277 if ( i >= 0 )
278 aInfoString = aInfoString.replaceAt( i, aOldSizePlaceholder.getLength(), ImpValueOfInMB( nSource ) );
279
280 sal_Int32 j = aInfoString.indexOf( aNewSizePlaceholder );
281 if ( j >= 0 )
282 aInfoString = aInfoString.replaceAt( j, aNewSizePlaceholder.getLength(), ImpValueOfInMB( nDest ) );
283
284 sal_Int32 k = aInfoString.indexOf( aTitlePlaceholder );
285 if ( k >= 0 )
286 aInfoString = aInfoString.replaceAt( k, aTitlePlaceholder.getLength(), aTitle );
287
288 css::uno::Reference< css::awt::XItemListener > xItemListener;
289 InsertImage( *this,
290 "aboutimage",
291 "private:standardimage/query",
292 5, 5, 25, 25, false );
293 InsertFixedText( *this, "fixedtext", aInfoString, PAGE_POS_X, 6, PAGE_WIDTH, 24, true, 0 );
294 if ( !maSaveAsURL.isEmpty() )
295 InsertCheckBox( *this, "OpenNewDocument", xItemListener, getString( STR_AUTOMATICALLY_OPEN ), PAGE_POS_X, 42, PAGE_WIDTH, 1 );
296 InsertButton( *this, "button", mxActionListener, DIALOG_WIDTH / 2 - 25, nDialogHeight - 20, 50, 2, getString( STR_OK ) );
297
298 bool bOpenNewDocument = mrbOpenNewDocument;
299 setControlProperty( "OpenNewDocument", "State", Any( static_cast<sal_Int16>(bOpenNewDocument) ) );
300 }
301
302
InformationDialog(const Reference<XComponentContext> & rxContext,Reference<XFrame> const & rxFrame,const OUString & rSaveAsURL,bool & rbOpenNewDocument,sal_Int64 rSourceSize,sal_Int64 rDestSize,sal_Int64 rApproxSize)303 InformationDialog::InformationDialog( const Reference< XComponentContext > &rxContext, Reference< XFrame > const & rxFrame, const OUString& rSaveAsURL, bool& rbOpenNewDocument, sal_Int64 rSourceSize, sal_Int64 rDestSize, sal_Int64 rApproxSize ) :
304 UnoDialog( rxContext, rxFrame ),
305 ConfigurationAccess( rxContext ),
306 mxActionListener( new OKActionListener( *this ) ),
307 mnSourceSize( rSourceSize ),
308 mnDestSize( rDestSize ),
309 mnApproxSize( rApproxSize ),
310 mrbOpenNewDocument( rbOpenNewDocument ),
311 maSaveAsURL( rSaveAsURL )
312 {
313 InitDialog();
314 }
315
316
~InformationDialog()317 InformationDialog::~InformationDialog()
318 {
319 }
320
321
execute()322 void InformationDialog::execute()
323 {
324 UnoDialog::execute();
325
326 if ( !maSaveAsURL.isEmpty() )
327 {
328 sal_Int16 nInt16 = 0;
329 Any aAny( getControlProperty( "OpenNewDocument", "State" ) );
330 if ( aAny >>= nInt16 )
331 {
332 bool bOpenNewDocument = static_cast< bool >( nInt16 );
333 mrbOpenNewDocument = bOpenNewDocument;
334 }
335 }
336 }
337
338
actionPerformed(const ActionEvent & rEvent)339 void OKActionListener::actionPerformed( const ActionEvent& rEvent )
340 {
341 if ( rEvent.ActionCommand == "button" )
342 {
343 mrDialog.endExecute( true );
344 }
345 }
disposing(const css::lang::EventObject &)346 void OKActionListener::disposing( const css::lang::EventObject& /* Source */ )
347 {
348 }
349
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
351