1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3*cdf0e10cSrcweir  *
4*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
7*cdf0e10cSrcweir  *
8*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
9*cdf0e10cSrcweir  *
10*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
11*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
12*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
13*cdf0e10cSrcweir  *
14*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
15*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
18*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
19*cdf0e10cSrcweir  *
20*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
21*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
22*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
23*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
24*cdf0e10cSrcweir  *
25*cdf0e10cSrcweir ************************************************************************/
26*cdf0e10cSrcweir 
27*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
28*cdf0e10cSrcweir #include "precompiled_dbaccess.hxx"
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir #include "progressmixer.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir /** === begin UNO includes === **/
33*cdf0e10cSrcweir /** === end UNO includes === **/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <osl/diagnose.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <map>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //........................................................................
40*cdf0e10cSrcweir namespace dbmm
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir //........................................................................
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	/** === begin UNO using === **/
45*cdf0e10cSrcweir 	/** === end UNO using === **/
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #define OVERALL_RANGE   100000
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir     //====================================================================
50*cdf0e10cSrcweir 	//= misc types
51*cdf0e10cSrcweir 	//====================================================================
52*cdf0e10cSrcweir     struct PhaseData
53*cdf0e10cSrcweir     {
54*cdf0e10cSrcweir         // the weight of the phase, relative to all other phases
55*cdf0e10cSrcweir         PhaseWeight nWeight;
56*cdf0e10cSrcweir         // the "local"  range of the phase
57*cdf0e10cSrcweir         sal_uInt32  nRange;
58*cdf0e10cSrcweir         // this is the point in the "overall range" at which this phase starts
59*cdf0e10cSrcweir         sal_uInt32  nGlobalStart;
60*cdf0e10cSrcweir         /** the "global" range of the phase, i.e. its range after weighting with all other
61*cdf0e10cSrcweir             phases
62*cdf0e10cSrcweir         */
63*cdf0e10cSrcweir         sal_uInt32  nGlobalRange;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         PhaseData()
66*cdf0e10cSrcweir             :nWeight(1)
67*cdf0e10cSrcweir             ,nRange(100)
68*cdf0e10cSrcweir             ,nGlobalStart(0)
69*cdf0e10cSrcweir             ,nGlobalRange(100)
70*cdf0e10cSrcweir         {
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir         PhaseData( const PhaseWeight _nWeight )
74*cdf0e10cSrcweir             :nWeight( _nWeight )
75*cdf0e10cSrcweir             ,nRange(100)
76*cdf0e10cSrcweir             ,nGlobalStart(0)
77*cdf0e10cSrcweir             ,nGlobalRange(100)
78*cdf0e10cSrcweir         {
79*cdf0e10cSrcweir         }
80*cdf0e10cSrcweir     };
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     typedef ::std::map< PhaseID, PhaseData >   Phases;
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir     //====================================================================
85*cdf0e10cSrcweir 	//= ProgressMixer_Data
86*cdf0e10cSrcweir 	//====================================================================
87*cdf0e10cSrcweir     struct ProgressMixer_Data
88*cdf0e10cSrcweir     {
89*cdf0e10cSrcweir         Phases              aPhases;
90*cdf0e10cSrcweir         Phases::iterator    pCurrentPhase;
91*cdf0e10cSrcweir         sal_uInt32          nWeightSum;         /// the cached sum of the weights
92*cdf0e10cSrcweir         double              nOverallStretch;
93*cdf0e10cSrcweir         IProgressConsumer&  rConsumer;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         ProgressMixer_Data( IProgressConsumer& _rConsumer )
96*cdf0e10cSrcweir             :aPhases()
97*cdf0e10cSrcweir             ,pCurrentPhase( aPhases.end() )
98*cdf0e10cSrcweir             ,nWeightSum( 0 )
99*cdf0e10cSrcweir             ,nOverallStretch( 0 )
100*cdf0e10cSrcweir             ,rConsumer( _rConsumer )
101*cdf0e10cSrcweir         {
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir     };
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 	//--------------------------------------------------------------------
106*cdf0e10cSrcweir     namespace
107*cdf0e10cSrcweir     {
108*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 0
109*cdf0e10cSrcweir 	    //----------------------------------------------------------------
110*cdf0e10cSrcweir         bool lcl_isRunning( const ProgressMixer_Data& _rData )
111*cdf0e10cSrcweir         {
112*cdf0e10cSrcweir             return _rData.pCurrentPhase != _rData.aPhases.end();
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir #endif
115*cdf0e10cSrcweir         //----------------------------------------------------------------
116*cdf0e10cSrcweir         void lcl_ensureInitialized( ProgressMixer_Data& _rData )
117*cdf0e10cSrcweir         {
118*cdf0e10cSrcweir             OSL_PRECOND( _rData.nWeightSum, "lcl_ensureInitialized: we have no phases, this will crash!" );
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir             if ( _rData.nOverallStretch )
121*cdf0e10cSrcweir                 return;
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir             _rData.nOverallStretch = 1.0 * OVERALL_RANGE / _rData.nWeightSum;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir             // tell the single phases their "overall starting point"
126*cdf0e10cSrcweir             PhaseWeight nRunningWeight( 0 );
127*cdf0e10cSrcweir             for (   Phases::iterator phase = _rData.aPhases.begin();
128*cdf0e10cSrcweir                     phase != _rData.aPhases.end();
129*cdf0e10cSrcweir                     ++phase
130*cdf0e10cSrcweir                 )
131*cdf0e10cSrcweir             {
132*cdf0e10cSrcweir                 phase->second.nGlobalStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
133*cdf0e10cSrcweir                 nRunningWeight += phase->second.nWeight;
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir                 sal_uInt32 nNextPhaseStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
136*cdf0e10cSrcweir                 phase->second.nGlobalRange = nNextPhaseStart - phase->second.nGlobalStart;
137*cdf0e10cSrcweir             }
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir             _rData.rConsumer.start( OVERALL_RANGE );
140*cdf0e10cSrcweir         }
141*cdf0e10cSrcweir     }
142*cdf0e10cSrcweir 
143*cdf0e10cSrcweir 	//====================================================================
144*cdf0e10cSrcweir 	//= ProgressMixer
145*cdf0e10cSrcweir 	//====================================================================
146*cdf0e10cSrcweir 	//--------------------------------------------------------------------
147*cdf0e10cSrcweir     ProgressMixer::ProgressMixer( IProgressConsumer& _rConsumer )
148*cdf0e10cSrcweir         :m_pData( new ProgressMixer_Data( _rConsumer ) )
149*cdf0e10cSrcweir     {
150*cdf0e10cSrcweir     }
151*cdf0e10cSrcweir 
152*cdf0e10cSrcweir 	//--------------------------------------------------------------------
153*cdf0e10cSrcweir     ProgressMixer::~ProgressMixer()
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir     }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir     //--------------------------------------------------------------------
158*cdf0e10cSrcweir     void ProgressMixer::registerPhase( const PhaseID _nID, const PhaseWeight _nWeight )
159*cdf0e10cSrcweir     {
160*cdf0e10cSrcweir         OSL_PRECOND( !lcl_isRunning( *m_pData ), "ProgressMixer::registerPhase: already running!" );
161*cdf0e10cSrcweir         OSL_ENSURE( m_pData->aPhases.find( _nID ) == m_pData->aPhases.end(),
162*cdf0e10cSrcweir             "ProgressMixer::registerPhase: ID already used!" );
163*cdf0e10cSrcweir         m_pData->aPhases[ _nID ] = PhaseData( _nWeight );
164*cdf0e10cSrcweir         m_pData->nWeightSum += _nWeight;
165*cdf0e10cSrcweir     }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir     //--------------------------------------------------------------------
168*cdf0e10cSrcweir     void ProgressMixer::startPhase( const PhaseID _nID, const sal_uInt32 _nPhaseRange )
169*cdf0e10cSrcweir     {
170*cdf0e10cSrcweir         OSL_ENSURE( m_pData->aPhases.find( _nID ) != m_pData->aPhases.end(),
171*cdf0e10cSrcweir             "ProgresMixer::startPhase: unknown phase!" );
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir         m_pData->aPhases[ _nID ].nRange = _nPhaseRange;
174*cdf0e10cSrcweir         m_pData->pCurrentPhase = m_pData->aPhases.find( _nID );
175*cdf0e10cSrcweir     }
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir     //--------------------------------------------------------------------
178*cdf0e10cSrcweir     void ProgressMixer::advancePhase( const sal_uInt32 _nPhaseProgress )
179*cdf0e10cSrcweir     {
180*cdf0e10cSrcweir         OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::advancePhase: not running!" );
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir         // in case this is the first call, ensure all the ranges/weights are calculated
183*cdf0e10cSrcweir         // correctly
184*cdf0e10cSrcweir         lcl_ensureInitialized( *m_pData );
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir         const PhaseData& rPhase( m_pData->pCurrentPhase->second );
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir         double nLocalProgress = 1.0 * _nPhaseProgress / rPhase.nRange;
189*cdf0e10cSrcweir         sal_uInt32 nOverallProgress = (sal_uInt32)
190*cdf0e10cSrcweir             ( rPhase.nGlobalStart + nLocalProgress * rPhase.nGlobalRange );
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         m_pData->rConsumer.advance( nOverallProgress );
193*cdf0e10cSrcweir     }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir     //--------------------------------------------------------------------
196*cdf0e10cSrcweir     void ProgressMixer::endPhase()
197*cdf0e10cSrcweir     {
198*cdf0e10cSrcweir         OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::endPhase: not running!" );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         // in case this is the first call, ensure all the ranges/weights are calculated
201*cdf0e10cSrcweir         // correctly
202*cdf0e10cSrcweir         lcl_ensureInitialized( *m_pData );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir         // simply assume the phase's complete range is over
205*cdf0e10cSrcweir         advancePhase( m_pData->pCurrentPhase->second.nRange );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         // if that's the last phase, this is the "global end", too
208*cdf0e10cSrcweir         Phases::const_iterator pNextPhase( m_pData->pCurrentPhase );
209*cdf0e10cSrcweir         ++pNextPhase;
210*cdf0e10cSrcweir         if ( pNextPhase == m_pData->aPhases.end() )
211*cdf0e10cSrcweir             m_pData->rConsumer.end();
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir //........................................................................
215*cdf0e10cSrcweir } // namespace dbmm
216*cdf0e10cSrcweir //........................................................................
217