1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_dbaccess.hxx"
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #include "progressmixer.hxx"
28*b1cdbd2cSJim Jagielski 
29*b1cdbd2cSJim Jagielski /** === begin UNO includes === **/
30*b1cdbd2cSJim Jagielski /** === end UNO includes === **/
31*b1cdbd2cSJim Jagielski 
32*b1cdbd2cSJim Jagielski #include <osl/diagnose.h>
33*b1cdbd2cSJim Jagielski 
34*b1cdbd2cSJim Jagielski #include <map>
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski //........................................................................
37*b1cdbd2cSJim Jagielski namespace dbmm
38*b1cdbd2cSJim Jagielski {
39*b1cdbd2cSJim Jagielski //........................................................................
40*b1cdbd2cSJim Jagielski 
41*b1cdbd2cSJim Jagielski 	/** === begin UNO using === **/
42*b1cdbd2cSJim Jagielski 	/** === end UNO using === **/
43*b1cdbd2cSJim Jagielski 
44*b1cdbd2cSJim Jagielski #define OVERALL_RANGE   100000
45*b1cdbd2cSJim Jagielski 
46*b1cdbd2cSJim Jagielski     //====================================================================
47*b1cdbd2cSJim Jagielski 	//= misc types
48*b1cdbd2cSJim Jagielski 	//====================================================================
49*b1cdbd2cSJim Jagielski     struct PhaseData
50*b1cdbd2cSJim Jagielski     {
51*b1cdbd2cSJim Jagielski         // the weight of the phase, relative to all other phases
52*b1cdbd2cSJim Jagielski         PhaseWeight nWeight;
53*b1cdbd2cSJim Jagielski         // the "local"  range of the phase
54*b1cdbd2cSJim Jagielski         sal_uInt32  nRange;
55*b1cdbd2cSJim Jagielski         // this is the point in the "overall range" at which this phase starts
56*b1cdbd2cSJim Jagielski         sal_uInt32  nGlobalStart;
57*b1cdbd2cSJim Jagielski         /** the "global" range of the phase, i.e. its range after weighting with all other
58*b1cdbd2cSJim Jagielski             phases
59*b1cdbd2cSJim Jagielski         */
60*b1cdbd2cSJim Jagielski         sal_uInt32  nGlobalRange;
61*b1cdbd2cSJim Jagielski 
PhaseDatadbmm::PhaseData62*b1cdbd2cSJim Jagielski         PhaseData()
63*b1cdbd2cSJim Jagielski             :nWeight(1)
64*b1cdbd2cSJim Jagielski             ,nRange(100)
65*b1cdbd2cSJim Jagielski             ,nGlobalStart(0)
66*b1cdbd2cSJim Jagielski             ,nGlobalRange(100)
67*b1cdbd2cSJim Jagielski         {
68*b1cdbd2cSJim Jagielski         }
69*b1cdbd2cSJim Jagielski 
PhaseDatadbmm::PhaseData70*b1cdbd2cSJim Jagielski         PhaseData( const PhaseWeight _nWeight )
71*b1cdbd2cSJim Jagielski             :nWeight( _nWeight )
72*b1cdbd2cSJim Jagielski             ,nRange(100)
73*b1cdbd2cSJim Jagielski             ,nGlobalStart(0)
74*b1cdbd2cSJim Jagielski             ,nGlobalRange(100)
75*b1cdbd2cSJim Jagielski         {
76*b1cdbd2cSJim Jagielski         }
77*b1cdbd2cSJim Jagielski     };
78*b1cdbd2cSJim Jagielski 
79*b1cdbd2cSJim Jagielski     typedef ::std::map< PhaseID, PhaseData >   Phases;
80*b1cdbd2cSJim Jagielski 
81*b1cdbd2cSJim Jagielski     //====================================================================
82*b1cdbd2cSJim Jagielski 	//= ProgressMixer_Data
83*b1cdbd2cSJim Jagielski 	//====================================================================
84*b1cdbd2cSJim Jagielski     struct ProgressMixer_Data
85*b1cdbd2cSJim Jagielski     {
86*b1cdbd2cSJim Jagielski         Phases              aPhases;
87*b1cdbd2cSJim Jagielski         Phases::iterator    pCurrentPhase;
88*b1cdbd2cSJim Jagielski         sal_uInt32          nWeightSum;         /// the cached sum of the weights
89*b1cdbd2cSJim Jagielski         double              nOverallStretch;
90*b1cdbd2cSJim Jagielski         IProgressConsumer&  rConsumer;
91*b1cdbd2cSJim Jagielski 
ProgressMixer_Datadbmm::ProgressMixer_Data92*b1cdbd2cSJim Jagielski         ProgressMixer_Data( IProgressConsumer& _rConsumer )
93*b1cdbd2cSJim Jagielski             :aPhases()
94*b1cdbd2cSJim Jagielski             ,pCurrentPhase( aPhases.end() )
95*b1cdbd2cSJim Jagielski             ,nWeightSum( 0 )
96*b1cdbd2cSJim Jagielski             ,nOverallStretch( 0 )
97*b1cdbd2cSJim Jagielski             ,rConsumer( _rConsumer )
98*b1cdbd2cSJim Jagielski         {
99*b1cdbd2cSJim Jagielski         }
100*b1cdbd2cSJim Jagielski     };
101*b1cdbd2cSJim Jagielski 
102*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------
103*b1cdbd2cSJim Jagielski     namespace
104*b1cdbd2cSJim Jagielski     {
105*b1cdbd2cSJim Jagielski #if OSL_DEBUG_LEVEL > 0
106*b1cdbd2cSJim Jagielski 	    //----------------------------------------------------------------
lcl_isRunning(const ProgressMixer_Data & _rData)107*b1cdbd2cSJim Jagielski         bool lcl_isRunning( const ProgressMixer_Data& _rData )
108*b1cdbd2cSJim Jagielski         {
109*b1cdbd2cSJim Jagielski             return _rData.pCurrentPhase != _rData.aPhases.end();
110*b1cdbd2cSJim Jagielski         }
111*b1cdbd2cSJim Jagielski #endif
112*b1cdbd2cSJim Jagielski         //----------------------------------------------------------------
lcl_ensureInitialized(ProgressMixer_Data & _rData)113*b1cdbd2cSJim Jagielski         void lcl_ensureInitialized( ProgressMixer_Data& _rData )
114*b1cdbd2cSJim Jagielski         {
115*b1cdbd2cSJim Jagielski             OSL_PRECOND( _rData.nWeightSum, "lcl_ensureInitialized: we have no phases, this will crash!" );
116*b1cdbd2cSJim Jagielski 
117*b1cdbd2cSJim Jagielski             if ( _rData.nOverallStretch )
118*b1cdbd2cSJim Jagielski                 return;
119*b1cdbd2cSJim Jagielski 
120*b1cdbd2cSJim Jagielski             _rData.nOverallStretch = 1.0 * OVERALL_RANGE / _rData.nWeightSum;
121*b1cdbd2cSJim Jagielski 
122*b1cdbd2cSJim Jagielski             // tell the single phases their "overall starting point"
123*b1cdbd2cSJim Jagielski             PhaseWeight nRunningWeight( 0 );
124*b1cdbd2cSJim Jagielski             for (   Phases::iterator phase = _rData.aPhases.begin();
125*b1cdbd2cSJim Jagielski                     phase != _rData.aPhases.end();
126*b1cdbd2cSJim Jagielski                     ++phase
127*b1cdbd2cSJim Jagielski                 )
128*b1cdbd2cSJim Jagielski             {
129*b1cdbd2cSJim Jagielski                 phase->second.nGlobalStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
130*b1cdbd2cSJim Jagielski                 nRunningWeight += phase->second.nWeight;
131*b1cdbd2cSJim Jagielski 
132*b1cdbd2cSJim Jagielski                 sal_uInt32 nNextPhaseStart = (sal_uInt32)( nRunningWeight * _rData.nOverallStretch );
133*b1cdbd2cSJim Jagielski                 phase->second.nGlobalRange = nNextPhaseStart - phase->second.nGlobalStart;
134*b1cdbd2cSJim Jagielski             }
135*b1cdbd2cSJim Jagielski 
136*b1cdbd2cSJim Jagielski             _rData.rConsumer.start( OVERALL_RANGE );
137*b1cdbd2cSJim Jagielski         }
138*b1cdbd2cSJim Jagielski     }
139*b1cdbd2cSJim Jagielski 
140*b1cdbd2cSJim Jagielski 	//====================================================================
141*b1cdbd2cSJim Jagielski 	//= ProgressMixer
142*b1cdbd2cSJim Jagielski 	//====================================================================
143*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------
ProgressMixer(IProgressConsumer & _rConsumer)144*b1cdbd2cSJim Jagielski     ProgressMixer::ProgressMixer( IProgressConsumer& _rConsumer )
145*b1cdbd2cSJim Jagielski         :m_pData( new ProgressMixer_Data( _rConsumer ) )
146*b1cdbd2cSJim Jagielski     {
147*b1cdbd2cSJim Jagielski     }
148*b1cdbd2cSJim Jagielski 
149*b1cdbd2cSJim Jagielski 	//--------------------------------------------------------------------
~ProgressMixer()150*b1cdbd2cSJim Jagielski     ProgressMixer::~ProgressMixer()
151*b1cdbd2cSJim Jagielski     {
152*b1cdbd2cSJim Jagielski     }
153*b1cdbd2cSJim Jagielski 
154*b1cdbd2cSJim Jagielski     //--------------------------------------------------------------------
registerPhase(const PhaseID _nID,const PhaseWeight _nWeight)155*b1cdbd2cSJim Jagielski     void ProgressMixer::registerPhase( const PhaseID _nID, const PhaseWeight _nWeight )
156*b1cdbd2cSJim Jagielski     {
157*b1cdbd2cSJim Jagielski         OSL_PRECOND( !lcl_isRunning( *m_pData ), "ProgressMixer::registerPhase: already running!" );
158*b1cdbd2cSJim Jagielski         OSL_ENSURE( m_pData->aPhases.find( _nID ) == m_pData->aPhases.end(),
159*b1cdbd2cSJim Jagielski             "ProgressMixer::registerPhase: ID already used!" );
160*b1cdbd2cSJim Jagielski         m_pData->aPhases[ _nID ] = PhaseData( _nWeight );
161*b1cdbd2cSJim Jagielski         m_pData->nWeightSum += _nWeight;
162*b1cdbd2cSJim Jagielski     }
163*b1cdbd2cSJim Jagielski 
164*b1cdbd2cSJim Jagielski     //--------------------------------------------------------------------
startPhase(const PhaseID _nID,const sal_uInt32 _nPhaseRange)165*b1cdbd2cSJim Jagielski     void ProgressMixer::startPhase( const PhaseID _nID, const sal_uInt32 _nPhaseRange )
166*b1cdbd2cSJim Jagielski     {
167*b1cdbd2cSJim Jagielski         OSL_ENSURE( m_pData->aPhases.find( _nID ) != m_pData->aPhases.end(),
168*b1cdbd2cSJim Jagielski             "ProgresMixer::startPhase: unknown phase!" );
169*b1cdbd2cSJim Jagielski 
170*b1cdbd2cSJim Jagielski         m_pData->aPhases[ _nID ].nRange = _nPhaseRange;
171*b1cdbd2cSJim Jagielski         m_pData->pCurrentPhase = m_pData->aPhases.find( _nID );
172*b1cdbd2cSJim Jagielski     }
173*b1cdbd2cSJim Jagielski 
174*b1cdbd2cSJim Jagielski     //--------------------------------------------------------------------
advancePhase(const sal_uInt32 _nPhaseProgress)175*b1cdbd2cSJim Jagielski     void ProgressMixer::advancePhase( const sal_uInt32 _nPhaseProgress )
176*b1cdbd2cSJim Jagielski     {
177*b1cdbd2cSJim Jagielski         OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::advancePhase: not running!" );
178*b1cdbd2cSJim Jagielski 
179*b1cdbd2cSJim Jagielski         // in case this is the first call, ensure all the ranges/weights are calculated
180*b1cdbd2cSJim Jagielski         // correctly
181*b1cdbd2cSJim Jagielski         lcl_ensureInitialized( *m_pData );
182*b1cdbd2cSJim Jagielski 
183*b1cdbd2cSJim Jagielski         const PhaseData& rPhase( m_pData->pCurrentPhase->second );
184*b1cdbd2cSJim Jagielski 
185*b1cdbd2cSJim Jagielski         double nLocalProgress = 1.0 * _nPhaseProgress / rPhase.nRange;
186*b1cdbd2cSJim Jagielski         sal_uInt32 nOverallProgress = (sal_uInt32)
187*b1cdbd2cSJim Jagielski             ( rPhase.nGlobalStart + nLocalProgress * rPhase.nGlobalRange );
188*b1cdbd2cSJim Jagielski 
189*b1cdbd2cSJim Jagielski         m_pData->rConsumer.advance( nOverallProgress );
190*b1cdbd2cSJim Jagielski     }
191*b1cdbd2cSJim Jagielski 
192*b1cdbd2cSJim Jagielski     //--------------------------------------------------------------------
endPhase()193*b1cdbd2cSJim Jagielski     void ProgressMixer::endPhase()
194*b1cdbd2cSJim Jagielski     {
195*b1cdbd2cSJim Jagielski         OSL_PRECOND( lcl_isRunning( *m_pData ), "ProgresMixer::endPhase: not running!" );
196*b1cdbd2cSJim Jagielski 
197*b1cdbd2cSJim Jagielski         // in case this is the first call, ensure all the ranges/weights are calculated
198*b1cdbd2cSJim Jagielski         // correctly
199*b1cdbd2cSJim Jagielski         lcl_ensureInitialized( *m_pData );
200*b1cdbd2cSJim Jagielski 
201*b1cdbd2cSJim Jagielski         // simply assume the phase's complete range is over
202*b1cdbd2cSJim Jagielski         advancePhase( m_pData->pCurrentPhase->second.nRange );
203*b1cdbd2cSJim Jagielski 
204*b1cdbd2cSJim Jagielski         // if that's the last phase, this is the "global end", too
205*b1cdbd2cSJim Jagielski         Phases::const_iterator pNextPhase( m_pData->pCurrentPhase );
206*b1cdbd2cSJim Jagielski         ++pNextPhase;
207*b1cdbd2cSJim Jagielski         if ( pNextPhase == m_pData->aPhases.end() )
208*b1cdbd2cSJim Jagielski             m_pData->rConsumer.end();
209*b1cdbd2cSJim Jagielski     }
210*b1cdbd2cSJim Jagielski 
211*b1cdbd2cSJim Jagielski //........................................................................
212*b1cdbd2cSJim Jagielski } // namespace dbmm
213*b1cdbd2cSJim Jagielski //........................................................................
214