summaryrefslogtreecommitdiffstats
path: root/sfx2/source/bastyp/progress.cxx
blob: f0dacde858348ce2db60ea120598508a89bff67f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */


#include <sfx2/progress.hxx>
#include <com/sun/star/uno/Reference.hxx>
#include <com/sun/star/task/XStatusIndicatorFactory.hpp>

#include <svl/eitem.hxx>
#include <tools/debug.hxx>
#include <sal/log.hxx>

#include <appdata.hxx>
#include <sfx2/bindings.hxx>
#include <sfx2/frame.hxx>
#include <sfx2/viewfrm.hxx>
#include <sfx2/objsh.hxx>
#include <sfx2/app.hxx>
#include <sfxtypes.hxx>
#include <sfx2/docfile.hxx>
#include <sfx2/sfxsids.hrc>
#include <workwin.hxx>
#include <sfxbasecontroller_internal.hxx>
#include <time.h>

using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::task;

struct SfxProgress_Impl
{
    Reference < XStatusIndicator > xStatusInd;
    OUString                aText;
    sal_uInt32              nMax;
    clock_t                 nCreate;
    bool                    bWaitMode;
    bool                    bRunning;

    SfxProgress*            pActiveProgress;
    SfxObjectShellRef       xObjSh;
    SfxWorkWindow*          pWorkWin;
    SfxViewFrame*           pView;

    explicit                SfxProgress_Impl();
};

SfxProgress_Impl::SfxProgress_Impl()
    : nMax(0)
    , nCreate(0)
    , bWaitMode(false)
    , bRunning(false)
    , pActiveProgress(nullptr)
    , pWorkWin(nullptr)
    , pView(nullptr)
{
}


SfxProgress::SfxProgress
(
    SfxObjectShell*     pObjSh, /*  The action is performed on the
                                    SfxObjectShell which can be NULL.
                                    When it is then the application will be
                                    used */

    const OUString&     rText,  /* Text, which appears before the Statusmonitor
                                  in the status line */

    sal_uInt32          nRange, /* Max value for range  */

    bool                bWait    /* Activate the wait-Pointer initially (TRUE) */
)

/*  [Description]

    The constructor of the class SfxProgress switches the SfxObjectShell
    passed as parameter and SfxViewFrames which display this document in
    a progress mode. Ie as long as one of those SfxViewFrame instances is
    active the associated SfxDispatcher and associated Window is disabled.
    A progress-bar will be displayed in the status bar,
*/

:   pImpl( new SfxProgress_Impl ),
    nVal(0),
    bSuspended(true)
{
    pImpl->bRunning = true;

    pImpl->xObjSh = pObjSh;
    pImpl->aText = rText;
    pImpl->nMax = nRange;
    pImpl->bWaitMode = bWait;
    pImpl->nCreate = Get10ThSec();
    SAL_INFO(
        "sfx.bastyp",
        "SfxProgress: created for '" << rText << "' at " << pImpl->nCreate
            << "ds");
    pImpl->pWorkWin = nullptr;
    pImpl->pView = nullptr;

    pImpl->pActiveProgress = GetActiveProgress( pObjSh );
    if ( pObjSh )
        pObjSh->SetProgress_Impl(this);
    else if( !pImpl->pActiveProgress )
        SfxGetpApp()->SetProgress_Impl(this);
    Resume();
}


SfxProgress::~SfxProgress()

/*  [Description]

    The destructor of the class SfxProgress restores the old status,
    the documents are released again and the status bar shows the items again.
*/

{
    Stop();
    if ( pImpl->xStatusInd.is() )
        pImpl->xStatusInd->end();
}


void SfxProgress::Stop()

/*  [Description]

    Early Exit of <SfxProgress>.
*/

{
    if( pImpl->pActiveProgress )
    {
        if ( pImpl->xObjSh.is() && pImpl->xObjSh->GetProgress() == this )
            pImpl->xObjSh->SetProgress_Impl(nullptr);
        return;
    }

    if ( !pImpl->bRunning )
        return;
    pImpl->bRunning = false;
    SAL_INFO(
        "sfx.bastyp", "SfxProgress: destroyed at " << Get10ThSec() << "ds");

    Suspend();
    if ( pImpl->xObjSh.is() )
        pImpl->xObjSh->SetProgress_Impl(nullptr);
    else
        SfxGetpApp()->SetProgress_Impl(nullptr);
}

void SfxProgress::SetState
(
    sal_uInt32   nNewVal,    /* new value for the progress bar */

    sal_uInt32   nNewRange   /* new maximum value, 0 for retaining the old */
)
/*  [Description]

    Setting the current status, after a time delay Reschedule is called.
*/

{
    if( pImpl->pActiveProgress ) return;

    nVal = nNewVal;

    // new Range?
    if ( nNewRange && nNewRange != pImpl->nMax )
    {
        SAL_INFO(
            "sfx.bastyp",
            "SfxProgress: range changed from " << pImpl->nMax << " to "
                << nNewRange);
        pImpl->nMax = nNewRange;
    }

    if ( !pImpl->xStatusInd.is() )
    {
        // get the active ViewFrame of the document this progress is working on
        // if it doesn't work on a document, take the current ViewFrame
        SfxObjectShell* pObjSh = pImpl->xObjSh.get();
        pImpl->pView = SfxViewFrame::Current();
        DBG_ASSERT( pImpl->pView || pObjSh, "Can't make progress bar!");
        if ( pObjSh && ( !pImpl->pView || pObjSh != pImpl->pView->GetObjectShell() ) )
        {
            // current document does not belong to current ViewFrame; take it's first visible ViewFrame
            SfxViewFrame* pDocView = SfxViewFrame::GetFirst( pObjSh );
            if ( pDocView )
                pImpl->pView = pDocView;
            else
            {
                // don't show status indicator for hidden documents (only valid while loading)
                SfxMedium* pMedium = pObjSh->GetMedium();
                const SfxBoolItem* pHiddenItem = pMedium->GetItemSet().GetItem(SID_HIDDEN, false);
                if ( !pHiddenItem || !pHiddenItem->GetValue() )
                {
                    const SfxUnoAnyItem* pIndicatorItem = pMedium->GetItemSet().GetItem(SID_PROGRESS_STATUSBAR_CONTROL, false);
                    Reference< XStatusIndicator > xInd;
                    if ( pIndicatorItem && (pIndicatorItem->GetValue()>>=xInd) )
                        pImpl->xStatusInd = xInd;
                }
            }
        }
        else if ( pImpl->pView )
        {
            pImpl->pWorkWin = SfxGetpApp()->GetWorkWindow_Impl( pImpl->pView );
            if ( pImpl->pWorkWin )
                pImpl->xStatusInd = pImpl->pWorkWin->GetStatusIndicator();
        }

        if ( pImpl->xStatusInd.is() )
        {
            pImpl->xStatusInd->start( pImpl->aText, pImpl->nMax );
            pImpl->pView = nullptr;
        }
    }

    if ( pImpl->xStatusInd.is() )
    {
        pImpl->xStatusInd->setValue( nNewVal );
    }
}


void SfxProgress::Resume()

/*  [Description]

    Resumed the status of the display after an interrupt.

    [Cross-reference]

    <SfxProgress::Suspend()>
*/

{
    if( pImpl->pActiveProgress ) return;
    if ( !bSuspended )
        return;

    SAL_INFO("sfx.bastyp", "SfxProgress: resumed");
    if ( pImpl->xStatusInd.is() )
    {
        pImpl->xStatusInd->start( pImpl->aText, pImpl->nMax );
        pImpl->xStatusInd->setValue( nVal );
    }

    if ( pImpl->bWaitMode )
    {
        if ( pImpl->xObjSh.is() )
        {
            for ( SfxViewFrame *pFrame = SfxViewFrame::GetFirst(pImpl->xObjSh.get() );
                    pFrame;
                    pFrame = SfxViewFrame::GetNext( *pFrame, pImpl->xObjSh.get() ) )
                pFrame->GetWindow().EnterWait();
        }
    }

    if ( pImpl->xObjSh.is() )
    {
        SfxViewFrame *pFrame = SfxViewFrame::GetFirst(pImpl->xObjSh.get());
        if ( pFrame )
            pFrame->GetBindings().ENTERREGISTRATIONS();
    }

    bSuspended = false;
}


void SfxProgress::Suspend()

/*  [Description]

    Interrupts the status of the display

    [Cross-reference]

    <SfxProgress::Resume()>
*/

{
    if( pImpl->pActiveProgress ) return;
    if ( bSuspended )
        return;

    SAL_INFO("sfx.bastyp", "SfxProgress: suspended");
    bSuspended = true;

    if ( pImpl->xStatusInd.is() )
    {
        pImpl->xStatusInd->reset();
    }

    if ( pImpl->xObjSh.is() )
    {
        for ( SfxViewFrame *pFrame =
                SfxViewFrame::GetFirst(pImpl->xObjSh.get());
                pFrame;
                pFrame = SfxViewFrame::GetNext( *pFrame, pImpl->xObjSh.get() ) )
            pFrame->GetWindow().LeaveWait();
    }
    if ( pImpl->xObjSh.is() )
    {
        SfxViewFrame *pFrame = SfxViewFrame::GetFirst( pImpl->xObjSh.get() );
        if ( pFrame )
            pFrame->GetBindings().LEAVEREGISTRATIONS();
    }
}


void SfxProgress::Reschedule()

/*  [Description]

    Reschedule, callable from the outside
*/

{
    SFX_STACK(SfxProgress::Reschedule);
}


SfxProgress* SfxProgress::GetActiveProgress
(
    SfxObjectShell const * pDocSh /*  the <SfxObjectShell>, which should be
                                      queried after a current <SfxProgress>,
                                      or 0 if a current SfxProgress for the
                                      entire application should be obtained.
                                      The pointer only needs at the time of
                                      the call to be valid.
                                  */
)

/*  [Description]

    This method is used to check whether and which <SfxProgress> is currently
    active for a specific instance of SfxObjectShell or even an entire
    application. This can for example be used to check for Time-Out-Events, etc.

    Instead of a pointer to the SfxProgress the SfxObjectShell may be
    pointed at the SfxProgress of the application, with the query
    'SfxProgress:: GetActiveProgress (pMyDocSh)' thus the current
    SfxProgress of 'pMyDocSh' is delivered, otherwise the SfxProgress of
    the application or a 0-pointer.

    [Note]

    If no SfxProgress is running in the application and also not at the
    specified SfxObjectShell, then this method will always return 0,
    even if one SfxProgress runs on another SfxObjectShell.

    [Cross-reference]

    <SfxApplication::GetProgress()const>
    <SfxObjectShell::GetProgress()const>
*/

{
    if ( !SfxApplication::Get() )
        return nullptr;

    SfxProgress *pProgress = nullptr;
    if ( pDocSh )
        pProgress = pDocSh->GetProgress();
    if ( !pProgress )
        pProgress = SfxGetpApp()->GetProgress();
    return pProgress;
}


void SfxProgress::EnterLock()
{
    SfxGetpApp()->Get_Impl()->nRescheduleLocks++;
}


void SfxProgress::LeaveLock()
{
    SfxAppData_Impl *pImp = SfxGetpApp()->Get_Impl();
    DBG_ASSERT( 0 != pImp->nRescheduleLocks, "SFxProgress::LeaveLock but no locks" );
    pImp->nRescheduleLocks--;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */