Line data Source code
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 : #include <vcl/svapp.hxx>
21 :
22 : #include <idlemgr.hxx>
23 :
24 : // =======================================================================
25 :
26 171 : struct ImplIdleData
27 : {
28 : Link maIdleHdl;
29 : sal_uInt16 mnPriority;
30 : sal_Bool mbTimeout;
31 : };
32 :
33 : #define IMPL_IDLETIMEOUT 350
34 :
35 : // =======================================================================
36 :
37 2 : ImplIdleMgr::ImplIdleMgr()
38 : {
39 2 : mpIdleList = new ImplIdleList();
40 :
41 2 : maTimer.SetTimeout( IMPL_IDLETIMEOUT );
42 2 : maTimer.SetTimeoutHdl( LINK( this, ImplIdleMgr, TimeoutHdl ) );
43 2 : }
44 :
45 : // -----------------------------------------------------------------------
46 :
47 4 : ImplIdleMgr::~ImplIdleMgr()
48 : {
49 : // Liste loeschen
50 6 : for ( size_t i = 0, n = mpIdleList->size(); i < n; ++i ) {
51 4 : delete (*mpIdleList)[ i ];
52 : }
53 2 : mpIdleList->clear();
54 2 : delete mpIdleList;
55 2 : }
56 :
57 : // -----------------------------------------------------------------------
58 :
59 171 : sal_Bool ImplIdleMgr::InsertIdleHdl( const Link& rLink, sal_uInt16 nPriority )
60 : {
61 171 : size_t nPos = (size_t)-1;
62 171 : size_t n = mpIdleList->size();
63 5992 : for ( size_t i = 0; i < n; ++i ) {
64 : // we need to check each element to verify that rLink isn't in the array
65 5821 : if ( (*mpIdleList)[ i ]->maIdleHdl == rLink ) {
66 0 : return sal_False;
67 : }
68 5821 : if ( nPriority <= (*mpIdleList)[ i ]->mnPriority ) {
69 5821 : nPos = i;
70 : }
71 : }
72 :
73 171 : ImplIdleData* pIdleData = new ImplIdleData;
74 171 : pIdleData->maIdleHdl = rLink;
75 171 : pIdleData->mnPriority = nPriority;
76 171 : pIdleData->mbTimeout = sal_False;
77 :
78 171 : if ( nPos < mpIdleList->size() ) {
79 169 : ImplIdleList::iterator it = mpIdleList->begin();
80 169 : ::std::advance( it, nPos );
81 169 : mpIdleList->insert( it, pIdleData );
82 : } else {
83 2 : mpIdleList->push_back( pIdleData );
84 : }
85 :
86 : // if Timer was not started already then start it now
87 171 : if ( !maTimer.IsActive() )
88 2 : maTimer.Start();
89 :
90 171 : return sal_True;
91 : }
92 :
93 : // -----------------------------------------------------------------------
94 :
95 167 : void ImplIdleMgr::RemoveIdleHdl( const Link& rLink )
96 : {
97 2082 : for ( ImplIdleList::iterator it = mpIdleList->begin(); it != mpIdleList->end(); ++it ) {
98 2082 : if ( (*it)->maIdleHdl == rLink ) {
99 167 : delete *it;
100 167 : mpIdleList->erase( it );
101 167 : break;
102 : }
103 : }
104 :
105 : // keine Handdler mehr da
106 167 : if ( mpIdleList->empty() )
107 1 : maTimer.Stop();
108 167 : }
109 :
110 : // -----------------------------------------------------------------------
111 :
112 26 : IMPL_LINK_NOARG(ImplIdleMgr, TimeoutHdl)
113 : {
114 180 : for ( size_t i = 0; i < mpIdleList->size(); ++i ) {
115 167 : ImplIdleData* pIdleData = (*mpIdleList)[ i ];
116 167 : if ( !pIdleData->mbTimeout ) {
117 167 : pIdleData->mbTimeout = sal_True;
118 167 : pIdleData->maIdleHdl.Call( GetpApp() );
119 : // May have been removed in the handler
120 5982 : for ( size_t j = 0; j < mpIdleList->size(); ++j ) {
121 5815 : if ( (*mpIdleList)[ j ] == pIdleData ) {
122 0 : pIdleData->mbTimeout = sal_False;
123 0 : break;
124 : }
125 : }
126 : }
127 : }
128 :
129 13 : return 0;
130 : }
131 :
132 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|