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 "optimizerdialog.hxx"
22 #include "impoptimizer.hxx"
23 #include "fileopendialog.hxx"
24 #include <com/sun/star/awt/XItemEventBroadcaster.hpp>
25 #include <com/sun/star/beans/XPropertySet.hpp>
26 #include <com/sun/star/container/XIndexContainer.hpp>
27 #include <com/sun/star/frame/XStorable.hpp>
28 #include <com/sun/star/frame/XTitle.hpp>
29 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
30 #include <com/sun/star/ui/dialogs/ExecutableDialogResults.hpp>
31 #include <com/sun/star/ucb/XSimpleFileAccess.hpp>
32 #include <com/sun/star/io/IOException.hpp>
33 #include <com/sun/star/util/XModifiable.hpp>
34 #include <sal/macros.h>
35 #include <osl/time.h>
36 #include <vcl/errinf.hxx>
37 #include <vcl/svapp.hxx>
38 #include <vcl/weld.hxx>
39 #include <svtools/sfxecode.hxx>
40 #include <svtools/ehdl.hxx>
41 #include <tools/urlobj.hxx>
42 #include <bitmaps.hlst>
43 
44 using namespace ::com::sun::star::io;
45 using namespace ::com::sun::star::ui;
46 using namespace ::com::sun::star::awt;
47 using namespace ::com::sun::star::ucb;
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::container;
54 
55 
InitDialog()56 void OptimizerDialog::InitDialog()
57 {
58    // setting the dialog properties
59     OUString pNames[] = {
60         OUString("Closeable"),
61         OUString("Height"),
62         OUString("Moveable"),
63         OUString("PositionX"),
64         OUString("PositionY"),
65         OUString("Title"),
66         OUString("Width") };
67 
68     Any pValues[] = {
69         Any( true ),
70         Any( sal_Int32( DIALOG_HEIGHT ) ),
71         Any( true ),
72         Any( sal_Int32( 200 ) ),
73         Any( sal_Int32( 52 ) ),
74         Any( getString( STR_SUN_OPTIMIZATION_WIZARD2 ) ),
75         Any( sal_Int32( OD_DIALOG_WIDTH ) ) };
76 
77     sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
78 
79     Sequence< OUString >   aNames( pNames, nCount );
80     Sequence< Any >             aValues( pValues, nCount );
81 
82     setPropertyValues(aNames, aValues);
83 }
84 
85 
InitRoadmap()86 void OptimizerDialog::InitRoadmap()
87 {
88     try
89     {
90         OUString pNames[] = {
91             OUString("Height"),
92             OUString("PositionX"),
93             OUString("PositionY"),
94             OUString("Step"),
95             OUString("TabIndex"),
96             OUString("Width") };
97 
98         Any pValues[] = {
99             Any( sal_Int32( DIALOG_HEIGHT - 26 ) ),
100             Any( sal_Int32( 0 ) ),
101             Any( sal_Int32( 0 ) ),
102             Any( sal_Int32( 0 ) ),
103             Any( mnTabIndex++ ),
104             Any( sal_Int32( 85 ) ) };
105 
106         sal_Int32 nCount = SAL_N_ELEMENTS( pNames );
107 
108         Sequence< OUString >   aNames( pNames, nCount );
109         Sequence< Any >        aValues( pValues, nCount );
110 
111         mxRoadmapControlModel = insertControlModel( "com.sun.star.awt.UnoControlRoadmapModel",
112                                                               "rdmNavi", aNames, aValues  );
113 
114         Reference< XPropertySet > xPropertySet( mxRoadmapControlModel, UNO_QUERY_THROW );
115         xPropertySet->setPropertyValue( "Name", Any( OUString("rdmNavi") ) );
116         mxRoadmapControl = getControl( "rdmNavi" );
117         InsertRoadmapItem( 0, getString( STR_INTRODUCTION ), ITEM_ID_INTRODUCTION );
118         InsertRoadmapItem( 1, getString( STR_SLIDES ), ITEM_ID_SLIDES );
119         InsertRoadmapItem( 2, getString( STR_IMAGE_OPTIMIZATION ), ITEM_ID_GRAPHIC_OPTIMIZATION );
120         InsertRoadmapItem( 3, getString( STR_OLE_OBJECTS ), ITEM_ID_OLE_OPTIMIZATION );
121         InsertRoadmapItem( 4, getString( STR_SUMMARY ), ITEM_ID_SUMMARY );
122 
123         xPropertySet->setPropertyValue( "ImageURL", Any( OUString("private:graphicrepository/" BMP_PRESENTATION_MINIMIZER) ) );
124         xPropertySet->setPropertyValue( "Activated", Any( true ) );
125         xPropertySet->setPropertyValue( "Complete", Any( true ) );
126         xPropertySet->setPropertyValue( "CurrentItemID", Any( sal_Int16(ITEM_ID_INTRODUCTION) ) );
127         xPropertySet->setPropertyValue( "Text", Any( getString( STR_STEPS ) ) );
128     }
129     catch( Exception& )
130     {
131     }
132 }
133 
134 
InsertRoadmapItem(const sal_Int32 nIndex,const OUString & rLabel,const sal_Int32 nItemID)135 void OptimizerDialog::InsertRoadmapItem( const sal_Int32 nIndex, const OUString& rLabel, const sal_Int32 nItemID )
136 {
137     try
138     {
139         Reference< XSingleServiceFactory > xSFRoadmap( mxRoadmapControlModel, UNO_QUERY_THROW );
140         Reference< XIndexContainer > aIndexContainerRoadmap( mxRoadmapControlModel, UNO_QUERY_THROW );
141         Reference< XInterface > xRoadmapItem( xSFRoadmap->createInstance(), UNO_SET_THROW );
142         Reference< XPropertySet > xPropertySet( xRoadmapItem, UNO_QUERY_THROW );
143         xPropertySet->setPropertyValue( "Label", Any( rLabel ) );
144         xPropertySet->setPropertyValue( "Enabled", Any( true ) );
145         xPropertySet->setPropertyValue( "ID", Any( nItemID ) );
146         aIndexContainerRoadmap->insertByIndex( nIndex, Any( xRoadmapItem ) );
147     }
148     catch( Exception& )
149     {
150 
151     }
152 }
153 
154 
UpdateConfiguration()155 void OptimizerDialog::UpdateConfiguration()
156 {
157     sal_Int16   nInt16 = 0;
158     Any         aAny;
159 
160     Sequence< sal_Int16 > aSelectedItems;
161     Sequence< OUString > aStringItemList;
162 
163     // page0
164     aAny = getControlProperty( "ListBox0Pg0", "SelectedItems" );
165     if ( aAny >>= aSelectedItems )
166     {
167         if ( aSelectedItems.hasElements() )
168         {
169             sal_Int16 nSelectedItem = aSelectedItems[ 0 ];
170             aAny = getControlProperty( "ListBox0Pg0", "StringItemList" );
171             if ( aAny >>= aStringItemList )
172             {
173                 if ( aStringItemList.getLength() > nSelectedItem )
174                     SetConfigProperty( TK_Name, Any( aStringItemList[ nSelectedItem ] ) );
175             }
176         }
177     }
178 
179     aAny = getControlProperty( "CheckBox3Pg3", "State" );
180     if ( !((aAny >>= nInt16) && nInt16) )
181         return;
182 
183     aAny = getControlProperty( "ListBox0Pg3", "SelectedItems" );
184     if ( !(aAny >>= aSelectedItems) )
185         return;
186 
187     if ( aSelectedItems.hasElements() )
188     {
189         sal_Int16 nSelectedItem = aSelectedItems[ 0 ];
190         aAny = getControlProperty( "ListBox0Pg3", "StringItemList" );
191         if ( aAny >>= aStringItemList )
192         {
193             if ( aStringItemList.getLength() > nSelectedItem )
194                 SetConfigProperty( TK_CustomShowName, Any( aStringItemList[ nSelectedItem ] ) );
195         }
196     }
197 }
198 
199 
OptimizerDialog(const Reference<XComponentContext> & rxContext,Reference<XFrame> const & rxFrame,Reference<XDispatch> const & rxStatusDispatcher)200 OptimizerDialog::OptimizerDialog( const Reference< XComponentContext > &rxContext, Reference< XFrame > const & rxFrame, Reference< XDispatch > const & rxStatusDispatcher ) :
201     UnoDialog( rxContext, rxFrame ),
202     ConfigurationAccess( rxContext ),
203     mnCurrentStep( 0 ),
204     mnTabIndex( 0 ),
205     mxFrame( rxFrame ),
206     mxItemListener( new ItemListener( *this ) ),
207     mxActionListener( new ActionListener( *this ) ),
208     mxActionListenerListBox0Pg0( new ActionListenerListBox0Pg0( *this ) ),
209     mxTextListenerFormattedField0Pg1( new TextListenerFormattedField0Pg1( *this ) ),
210     mxTextListenerComboBox0Pg1( new TextListenerComboBox0Pg1( *this ) ),
211     mxSpinListenerFormattedField0Pg1( new SpinListenerFormattedField0Pg1( *this ) ),
212     mxStatusDispatcher( rxStatusDispatcher )
213 {
214     Reference< XStorable > xStorable( mxController->getModel(), UNO_QUERY_THROW );
215     mbIsReadonly = xStorable->isReadonly();
216 
217     InitDialog();
218     InitRoadmap();
219     InitNavigationBar();
220     InitPage0();
221     InitPage1();
222     InitPage2();
223     InitPage3();
224     InitPage4();
225     ActivatePage( 0 );
226 
227     OptimizationStats aStats;
228     aStats.InitializeStatusValuesFromDocument( mxController->getModel() );
229     Sequence< PropertyValue > aStatusSequence( aStats.GetStatusSequence() );
230     UpdateStatus( aStatusSequence );
231 }
232 
233 
~OptimizerDialog()234 OptimizerDialog::~OptimizerDialog()
235 {
236     // not saving configuration if the dialog has been finished via cancel or close window
237     if ( endStatus() )
238         SaveConfiguration();
239 }
240 
241 
execute()242 void OptimizerDialog::execute()
243 {
244     Reference< XItemEventBroadcaster > xRoadmapBroadcaster( mxRoadmapControl, UNO_QUERY_THROW );
245     xRoadmapBroadcaster->addItemListener( mxItemListener );
246     UnoDialog::execute();
247     UpdateConfiguration();          // taking actual control settings for the configuration
248     xRoadmapBroadcaster->removeItemListener( mxItemListener );
249 }
250 
251 
SwitchPage(sal_Int16 nNewStep)252 void OptimizerDialog::SwitchPage( sal_Int16 nNewStep )
253 {
254     if ( !(( nNewStep != mnCurrentStep ) && ( nNewStep <= MAX_STEP ) && ( nNewStep >= 0 )) )
255         return;
256 
257     sal_Int16 nOldStep = mnCurrentStep;
258     if ( nNewStep == 0 )
259         disableControl( "btnNavBack" );
260     else if ( nOldStep == 0 )
261         enableControl( "btnNavBack" );
262 
263     if ( nNewStep == MAX_STEP )
264         disableControl( "btnNavNext" );
265     else if ( nOldStep == MAX_STEP )
266         enableControl( "btnNavNext" );
267 
268     setControlProperty( "rdmNavi", "CurrentItemID", Any( nNewStep ) );
269 
270     DeactivatePage( nOldStep );
271     UpdateControlStates( nNewStep );
272 
273     ActivatePage( nNewStep );
274     mnCurrentStep = nNewStep;
275 }
276 
UpdateControlStates(sal_Int16 nPage)277 void OptimizerDialog::UpdateControlStates( sal_Int16 nPage )
278 {
279     switch( nPage )
280     {
281         case 0 : UpdateControlStatesPage0(); break;
282         case 1 : UpdateControlStatesPage1(); break;
283         case 2 : UpdateControlStatesPage2(); break;
284         case 3 : UpdateControlStatesPage3(); break;
285         case 4 : UpdateControlStatesPage4(); break;
286         default:
287         {
288             UpdateControlStatesPage0();
289             UpdateControlStatesPage1();
290             UpdateControlStatesPage2();
291             UpdateControlStatesPage3();
292             UpdateControlStatesPage4();
293         }
294     }
295 }
296 
297 
GetSelectedString(OUString const & token)298 OUString OptimizerDialog::GetSelectedString( OUString const & token )
299 {
300     OUString aSelectedItem;
301     Sequence< sal_Int16 > sSelectedItems;
302     Sequence< OUString >  sItemList;
303 
304     if ( ( getControlProperty( token, "SelectedItems" ) >>= sSelectedItems ) &&
305             ( getControlProperty( token, "StringItemList" ) >>= sItemList ) )
306     {
307         if ( sSelectedItems.getLength() == 1 )
308         {
309             sal_Int16 nSelectedItem = sSelectedItems[ 0 ];
310             if ( nSelectedItem < sItemList.getLength() )
311                 aSelectedItem = sItemList[ nSelectedItem ];
312         }
313     }
314     return aSelectedItem;
315 }
316 
317 
UpdateStatus(const css::uno::Sequence<css::beans::PropertyValue> & rStatus)318 void OptimizerDialog::UpdateStatus( const css::uno::Sequence< css::beans::PropertyValue >& rStatus )
319 {
320     maStats.InitializeStatusValues( rStatus );
321     const Any* pVal( maStats.GetStatusValue( TK_Status ) );
322     if ( pVal )
323     {
324         OUString sStatus;
325         if ( *pVal >>= sStatus )
326         {
327             setControlProperty( "FixedText1Pg4", "Enabled", Any( true ) );
328             setControlProperty( "FixedText1Pg4", "Label", Any( getString( TKGet( sStatus ) ) ) );
329         }
330     }
331     pVal = maStats.GetStatusValue( TK_Progress );
332     if ( pVal )
333     {
334         sal_Int32 nProgress = 0;
335         if ( *pVal >>= nProgress )
336             setControlProperty( "Progress", "ProgressValue", Any( nProgress ) );
337     }
338     pVal = maStats.GetStatusValue( TK_OpenNewDocument );
339     if ( pVal )
340         SetConfigProperty( TK_OpenNewDocument, *pVal );
341 
342     reschedule();
343 }
344 
345 
itemStateChanged(const ItemEvent & Event)346 void ItemListener::itemStateChanged( const ItemEvent& Event )
347 {
348     try
349     {
350         sal_Int16 nState;
351         Reference< XControl > xControl;
352         Any aSource( Event.Source );
353         if ( aSource >>= xControl )
354         {
355             Reference< XPropertySet > xPropertySet( xControl->getModel(), UNO_QUERY_THROW );
356             OUString aControlName;
357             xPropertySet->getPropertyValue( "Name" ) >>= aControlName;
358             PPPOptimizerTokenEnum eControl( TKGet( aControlName ) );
359             switch( eControl )
360             {
361                 case TK_rdmNavi :
362                 {
363                     mrOptimizerDialog.SwitchPage( static_cast< sal_Int16 >( Event.ItemId ) );
364                 }
365                 break;
366                 case TK_CheckBox1Pg1 :
367                 {
368                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
369                         mrOptimizerDialog.SetConfigProperty( TK_RemoveCropArea, Any( nState != 0 ) );
370                 }
371                 break;
372                 case TK_CheckBox2Pg1 :
373                 {
374                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
375                         mrOptimizerDialog.SetConfigProperty( TK_EmbedLinkedGraphics, Any( nState != 0 ) );
376                 }
377                 break;
378                 case TK_CheckBox0Pg2 :
379                 {
380                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
381                     {
382                         mrOptimizerDialog.SetConfigProperty( TK_OLEOptimization, Any( nState != 0 ) );
383                         mrOptimizerDialog.setControlProperty( "RadioButton0Pg2", "Enabled", Any( nState != 0 ) );
384                         mrOptimizerDialog.setControlProperty( "RadioButton1Pg2", "Enabled", Any( nState != 0 ) );
385                     }
386                 }
387                 break;
388                 case TK_RadioButton0Pg1 :
389                 {
390                     sal_Int16 nInt16 = 0;
391                     if ( xPropertySet->getPropertyValue( "State" ) >>= nInt16 )
392                     {
393                         nInt16 ^= 1;
394                         mrOptimizerDialog.SetConfigProperty( TK_JPEGCompression, Any( nInt16 != 0 ) );
395                         mrOptimizerDialog.setControlProperty( "FixedText1Pg1", "Enabled", Any( nInt16 != 0 ) );
396                         mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "Enabled", Any( nInt16 != 0 ) );
397                     }
398                 }
399                 break;
400                 case TK_RadioButton1Pg1 :
401                 {
402                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
403                     {
404                         mrOptimizerDialog.SetConfigProperty( TK_JPEGCompression, Any( nState != 0 ) );
405                         mrOptimizerDialog.setControlProperty( "FixedText1Pg1", "Enabled", Any( nState != 0 ) );
406                         mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "Enabled", Any( nState != 0 ) );
407                     }
408                 }
409                 break;
410                 case TK_RadioButton0Pg2 :
411                 {
412                     sal_Int16 nInt16;
413                     if ( xPropertySet->getPropertyValue( "State" ) >>= nInt16 )
414                     {
415                         nInt16 ^= 1;
416                         mrOptimizerDialog.SetConfigProperty( TK_OLEOptimizationType, Any( nInt16 ) );
417                     }
418                 }
419                 break;
420                 case TK_RadioButton1Pg2 :
421                 {
422                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
423                         mrOptimizerDialog.SetConfigProperty( TK_OLEOptimizationType, Any( nState ) );
424                 }
425                 break;
426                 case TK_CheckBox0Pg3 :
427                 {
428                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
429                         mrOptimizerDialog.SetConfigProperty( TK_DeleteUnusedMasterPages, Any( nState != 0 ) );
430                 }
431                 break;
432                 case TK_CheckBox1Pg3 :
433                 {
434                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
435                         mrOptimizerDialog.SetConfigProperty( TK_DeleteNotesPages, Any( nState != 0 ) );
436                 }
437                 break;
438                 case TK_CheckBox2Pg3 :
439                 {
440                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
441                         mrOptimizerDialog.SetConfigProperty( TK_DeleteHiddenSlides, Any( nState != 0 ) );
442                 }
443                 break;
444                 case TK_CheckBox3Pg3 :
445                 {
446                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
447                         mrOptimizerDialog.setControlProperty( "ListBox0Pg3", "Enabled", Any( nState != 0 ) );
448                 }
449                 break;
450                 case TK_CheckBox1Pg4 :
451                 {
452                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
453                         mrOptimizerDialog.setControlProperty( "ComboBox0Pg4", "Enabled", Any( nState != 0 ) );
454                 }
455                 break;
456                 case TK_RadioButton0Pg4 :
457                 case TK_RadioButton1Pg4 :
458                 {
459                     if ( xPropertySet->getPropertyValue( "State" ) >>= nState )
460                         mrOptimizerDialog.SetConfigProperty( TK_SaveAs, Any( eControl == TK_RadioButton1Pg4 ? nState != 0 : nState == 0 ) );
461                 }
462                 break;
463                 default:
464                 break;
465             }
466         }
467     }
468     catch ( Exception& )
469     {
470 
471     }
472 }
disposing(const css::lang::EventObject &)473 void ItemListener::disposing( const css::lang::EventObject& /* Source */ )
474 {
475 }
476 
actionPerformed(const ActionEvent & rEvent)477 void ActionListener::actionPerformed( const ActionEvent& rEvent )
478 {
479     switch( TKGet( rEvent.ActionCommand ) )
480     {
481         case TK_btnNavBack :    mrOptimizerDialog.SwitchPage( mrOptimizerDialog.mnCurrentStep - 1 ); break;
482         case TK_btnNavNext :    mrOptimizerDialog.SwitchPage( mrOptimizerDialog.mnCurrentStep + 1 ); break;
483         case TK_btnNavFinish :
484         {
485             mrOptimizerDialog.UpdateConfiguration();
486 
487             mrOptimizerDialog.SwitchPage( ITEM_ID_SUMMARY );
488             mrOptimizerDialog.DisablePage( ITEM_ID_SUMMARY );
489             mrOptimizerDialog.setControlProperty( "btnNavBack", "Enabled", Any( false ) );
490             mrOptimizerDialog.setControlProperty( "btnNavNext", "Enabled", Any( false ) );
491             mrOptimizerDialog.setControlProperty( "btnNavFinish", "Enabled", Any( false ) );
492             mrOptimizerDialog.setControlProperty( "btnNavCancel", "Enabled", Any( false ) );
493             mrOptimizerDialog.setControlProperty( "FixedText0Pg4", "Enabled", Any( true ) );
494 
495             // check if we have to open the FileDialog
496             bool    bSuccessfullyExecuted = true;
497             sal_Int16   nInt16 = 0;
498             mrOptimizerDialog.getControlProperty( "RadioButton1Pg4", "State" ) >>= nInt16;
499             if ( nInt16 )
500             {
501                 // Duplicate presentation before applying changes
502                 OUString aSaveAsURL;
503                 FileOpenDialog aFileOpenDialog( mrOptimizerDialog.GetComponentContext() );
504 
505                 // generating default file name
506                 OUString aName;
507                 Reference< XStorable > xStorable( mrOptimizerDialog.controller()->getModel(), UNO_QUERY );
508                 if ( xStorable.is() && xStorable->hasLocation() )
509                 {
510                     INetURLObject aURLObj( xStorable->getLocation() );
511                     if ( !aURLObj.hasFinalSlash() )
512                     {
513                         // tdf#105382 uri-decode file name
514                         aURLObj.removeExtension(INetURLObject::LAST_SEGMENT, false);
515                         aName = aURLObj.getName(INetURLObject::LAST_SEGMENT, false,
516                                                 INetURLObject::DecodeMechanism::WithCharset);
517                     }
518                 }
519                 else
520                 {
521                     // If no filename, try to use model title ("Untitled 1" or something like this)
522                     Reference<XTitle> xTitle(
523                         mrOptimizerDialog.GetFrame()->getController()->getModel(), UNO_QUERY);
524                     aName = xTitle->getTitle();
525                 }
526 
527                 if (!aName.isEmpty())
528                 {
529                     aName += " " + mrOptimizerDialog.getString(STR_FILENAME_SUFFIX);
530                     aFileOpenDialog.setDefaultName(aName);
531                 }
532 
533                 if (aFileOpenDialog.execute() == dialogs::ExecutableDialogResults::OK)
534                 {
535                     aSaveAsURL = aFileOpenDialog.getURL();
536                     mrOptimizerDialog.SetConfigProperty( TK_SaveAsURL, Any( aSaveAsURL ) );
537                     mrOptimizerDialog.SetConfigProperty( TK_FilterName, Any( aFileOpenDialog.getFilterName() ) );
538                 }
539                 if ( aSaveAsURL.isEmpty() )
540                 {
541                     // something goes wrong...
542                     bSuccessfullyExecuted = false;
543                 }
544 
545                 // waiting for 500ms
546                 mrOptimizerDialog.reschedule();
547                 for ( sal_uInt32 i = osl_getGlobalTimer(); ( i + 500 ) > ( osl_getGlobalTimer() ); )
548                 mrOptimizerDialog.reschedule();
549             }
550             else
551             {
552                 // Apply changes to current presentation
553                 Reference<XModifiable> xModifiable(mrOptimizerDialog.controller()->getModel(),
554                                                    UNO_QUERY_THROW );
555                 if ( xModifiable->isModified() )
556                 {
557                     SolarMutexGuard aSolarGuard;
558                     std::unique_ptr<weld::MessageDialog> popupDlg(Application::CreateMessageDialog(
559                         nullptr, VclMessageType::Question, VclButtonsType::YesNo,
560                         mrOptimizerDialog.getString(STR_WARN_UNSAVED_PRESENTATION)));
561                     if (popupDlg->run() != RET_YES)
562                     {
563                         // Selected not "yes" ("no" or dialog was cancelled) so return to previous step
564                         mrOptimizerDialog.setControlProperty("btnNavBack", "Enabled",
565                                                                   Any(true));
566                         mrOptimizerDialog.setControlProperty("btnNavNext", "Enabled", Any(false));
567                         mrOptimizerDialog.setControlProperty("btnNavFinish", "Enabled", Any(true));
568                         mrOptimizerDialog.setControlProperty("btnNavCancel", "Enabled", Any(true));
569                         mrOptimizerDialog.EnablePage(ITEM_ID_SUMMARY);
570                         return;
571                     }
572                 }
573             }
574             if ( bSuccessfullyExecuted )
575             {   // now check if we have to store a session template
576                 nInt16 = 0;
577                 OUString aSettingsName;
578                 mrOptimizerDialog.getControlProperty( "CheckBox1Pg4", "State" ) >>= nInt16;
579                 mrOptimizerDialog.getControlProperty( "ComboBox0Pg4", "Text" ) >>= aSettingsName;
580                 if ( nInt16 && !aSettingsName.isEmpty() )
581                 {
582                     std::vector< OptimizerSettings >::iterator aIter( mrOptimizerDialog.GetOptimizerSettingsByName( aSettingsName ) );
583                     std::vector< OptimizerSettings >& rSettings( mrOptimizerDialog.GetOptimizerSettings() );
584                     OptimizerSettings aNewSettings( rSettings[ 0 ] );
585                     aNewSettings.maName = aSettingsName;
586                     if ( aIter == rSettings.end() )
587                         rSettings.push_back( aNewSettings );
588                     else
589                         *aIter = aNewSettings;
590                 }
591             }
592             if ( bSuccessfullyExecuted )
593             {
594                 URL aURL;
595                 aURL.Protocol = "vnd.com.sun.star.comp.PPPOptimizer:";
596                 aURL.Path = "optimize";
597 
598                 Sequence< PropertyValue > lArguments( 3 );
599                 lArguments[ 0 ].Name = "Settings";
600                 lArguments[ 0 ].Value <<= mrOptimizerDialog.GetConfigurationSequence();
601                 lArguments[ 1 ].Name = "StatusDispatcher";
602                 lArguments[ 1 ].Value <<= mrOptimizerDialog.GetStatusDispatcher();
603                 lArguments[ 2 ].Name = "InformationDialog";
604                 lArguments[ 2 ].Value <<= mrOptimizerDialog.GetFrame();
605 
606 
607                 ErrCode errorCode;
608                 try
609                 {
610                     ImpOptimizer aOptimizer(
611                         mrOptimizerDialog.GetComponentContext(),
612                         mrOptimizerDialog.GetFrame()->getController()->getModel());
613                     aOptimizer.Optimize(lArguments);
614                 }
615                 catch (css::io::IOException&)
616                 {
617                     // We always receive just ERRCODE_IO_CANTWRITE in case of problems, so no need to bother
618                     // about extracting error code from exception text
619                     errorCode = ERRCODE_IO_CANTWRITE;
620                 }
621                 catch (css::uno::Exception&)
622                 {
623                     // Other general exception
624                     errorCode = ERRCODE_IO_GENERAL;
625                 }
626 
627                 if (errorCode != ERRCODE_NONE)
628                 {
629                     // Restore wizard controls
630                     mrOptimizerDialog.maStats.SetStatusValue(TK_Progress,
631                                                              Any(static_cast<sal_Int32>(0)));
632                     mrOptimizerDialog.setControlProperty("btnNavBack", "Enabled", Any(true));
633                     mrOptimizerDialog.setControlProperty("btnNavNext", "Enabled", Any(false));
634                     mrOptimizerDialog.setControlProperty("btnNavFinish", "Enabled", Any(true));
635                     mrOptimizerDialog.setControlProperty("btnNavCancel", "Enabled", Any(true));
636 
637                     OUString aFileName;
638                     mrOptimizerDialog.GetConfigProperty(TK_SaveAsURL) >>= aFileName;
639                     SfxErrorContext aEc(ERRCTX_SFX_SAVEASDOC, aFileName);
640                     ErrorHandler::HandleError(errorCode);
641                     break;
642                 }
643 
644                 mrOptimizerDialog.endExecute( bSuccessfullyExecuted );
645             }
646             else
647             {
648                 mrOptimizerDialog.setControlProperty( "btnNavBack", "Enabled", Any( true ) );
649                 mrOptimizerDialog.setControlProperty( "btnNavNext", "Enabled", Any( false ) );
650                 mrOptimizerDialog.setControlProperty( "btnNavFinish", "Enabled", Any( true ) );
651                 mrOptimizerDialog.setControlProperty( "btnNavCancel", "Enabled", Any( true ) );
652                 mrOptimizerDialog.EnablePage( ITEM_ID_SUMMARY );
653             }
654         }
655         break;
656         case TK_btnNavCancel :  mrOptimizerDialog.endExecute( false ); break;
657         case TK_Button0Pg0 :    // delete configuration
658         {
659             OUString aSelectedItem( mrOptimizerDialog.GetSelectedString( "ListBox0Pg0" ) );
660             if ( !aSelectedItem.isEmpty() )
661             {
662                 std::vector< OptimizerSettings >::iterator aIter( mrOptimizerDialog.GetOptimizerSettingsByName( aSelectedItem ) );
663                 std::vector< OptimizerSettings >& rList( mrOptimizerDialog.GetOptimizerSettings() );
664                 if ( aIter != rList.end() )
665                 {
666                     rList.erase( aIter );
667                     mrOptimizerDialog.UpdateControlStates();
668                 }
669             }
670         }
671         break;
672         default: break;
673     }
674 }
disposing(const css::lang::EventObject &)675 void ActionListener::disposing( const css::lang::EventObject& /* Source */ )
676 {
677 }
678 
679 
actionPerformed(const ActionEvent & rEvent)680 void ActionListenerListBox0Pg0::actionPerformed( const ActionEvent& rEvent )
681 {
682     if ( !rEvent.ActionCommand.isEmpty() )
683     {
684         std::vector< OptimizerSettings >::iterator aIter( mrOptimizerDialog.GetOptimizerSettingsByName( rEvent.ActionCommand ) );
685         std::vector< OptimizerSettings >& rList( mrOptimizerDialog.GetOptimizerSettings() );
686         if ( aIter != rList.end() )
687             rList[ 0 ] = *aIter;
688     }
689     mrOptimizerDialog.UpdateControlStates();
690 }
disposing(const css::lang::EventObject &)691 void ActionListenerListBox0Pg0::disposing( const css::lang::EventObject& /* Source */ )
692 {
693 }
694 
695 
textChanged(const TextEvent &)696 void TextListenerFormattedField0Pg1::textChanged( const TextEvent& /* rEvent */ )
697 {
698     double fDouble = 0;
699     Any aAny = mrOptimizerDialog.getControlProperty( "FormattedField0Pg1", "EffectiveValue" );
700     if ( aAny >>= fDouble )
701         mrOptimizerDialog.SetConfigProperty( TK_JPEGQuality, Any( static_cast<sal_Int32>(fDouble) ) );
702 }
disposing(const css::lang::EventObject &)703 void TextListenerFormattedField0Pg1::disposing( const css::lang::EventObject& /* Source */ )
704 {
705 }
706 
707 namespace
708 {
709 
lcl_mapResolution(OUString & rResolution,const OUString & rImageResolution)710 bool lcl_mapResolution(OUString& rResolution, const OUString& rImageResolution)
711 {
712     if (rImageResolution.getToken(1, ';')!=rResolution)
713         return false;
714     rResolution = rImageResolution.getToken(0, ';');
715     return true;
716 }
717 
718 }
719 
textChanged(const TextEvent &)720 void TextListenerComboBox0Pg1::textChanged( const TextEvent& /* rEvent */ )
721 {
722     OUString aString;
723     Any aAny = mrOptimizerDialog.getControlProperty( "ComboBox0Pg1", "Text" );
724     if ( !(aAny >>= aString) )
725         return;
726 
727     for (int nIR{ STR_IMAGE_RESOLUTION_0 }; nIR <= STR_IMAGE_RESOLUTION_3; ++nIR)
728     {
729         if (lcl_mapResolution(aString, mrOptimizerDialog.getString(static_cast<PPPOptimizerTokenEnum>(nIR))))
730             break;
731     }
732 
733     mrOptimizerDialog.SetConfigProperty( TK_ImageResolution, Any( aString.toInt32() ) );
734 }
disposing(const css::lang::EventObject &)735 void TextListenerComboBox0Pg1::disposing( const css::lang::EventObject& /* Source */ )
736 {
737 }
738 
739 
up(const SpinEvent &)740 void SpinListenerFormattedField0Pg1::up( const SpinEvent& /* rEvent */ )
741 {
742     double fDouble;
743     Any aAny = mrOptimizerDialog.getControlProperty( "FormattedField0Pg1", "EffectiveValue" );
744     if ( aAny >>= fDouble )
745     {
746         fDouble += 9;
747         if ( fDouble > 100 )
748             fDouble = 100;
749         mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "EffectiveValue", Any( fDouble ) );
750         mrOptimizerDialog.SetConfigProperty( TK_JPEGQuality, Any( static_cast<sal_Int32>(fDouble) ) );
751     }
752 }
down(const SpinEvent &)753 void SpinListenerFormattedField0Pg1::down( const SpinEvent& /* rEvent */ )
754 {
755     double fDouble;
756     Any aAny = mrOptimizerDialog.getControlProperty( "FormattedField0Pg1", "EffectiveValue" );
757     if ( aAny >>= fDouble )
758     {
759         fDouble -= 9;
760         if ( fDouble < 0 )
761             fDouble = 0;
762         mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "EffectiveValue", Any( fDouble ) );
763         mrOptimizerDialog.SetConfigProperty( TK_JPEGQuality, Any( static_cast<sal_Int32>(fDouble) ) );
764     }
765 }
first(const SpinEvent &)766 void SpinListenerFormattedField0Pg1::first( const SpinEvent& /* rEvent */ )
767 {
768     mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "EffectiveValue", Any( static_cast< double >( 0 ) ) );
769     mrOptimizerDialog.SetConfigProperty( TK_JPEGQuality, Any( sal_Int32(0) ) );
770 }
last(const SpinEvent &)771 void SpinListenerFormattedField0Pg1::last( const SpinEvent& /* rEvent */ )
772 {
773     mrOptimizerDialog.setControlProperty( "FormattedField0Pg1", "EffectiveValue", Any( static_cast< double >( 100 ) ) );
774     mrOptimizerDialog.SetConfigProperty( TK_JPEGQuality, Any( sal_Int32(100) ) );
775 }
disposing(const css::lang::EventObject &)776 void SpinListenerFormattedField0Pg1::disposing( const css::lang::EventObject& /* Source */ )
777 {
778 }
779 
780 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
781