1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26
27 #include <tools/list.hxx>
28
29 #include <vcl/svapp.hxx>
30
31 #include <idlemgr.hxx>
32
33 // =======================================================================
34
35 struct ImplIdleData
36 {
37 Link maIdleHdl;
38 sal_uInt16 mnPriority;
39 sal_Bool mbTimeout;
40 };
41
DECLARE_LIST(ImplIdleList,ImplIdleData *)42 DECLARE_LIST( ImplIdleList, ImplIdleData* )
43
44 #define IMPL_IDLETIMEOUT 350
45
46 // =======================================================================
47
48 ImplIdleMgr::ImplIdleMgr()
49 {
50 mpIdleList = new ImplIdleList( 8, 8, 8 );
51
52 maTimer.SetTimeout( IMPL_IDLETIMEOUT );
53 maTimer.SetTimeoutHdl( LINK( this, ImplIdleMgr, TimeoutHdl ) );
54 }
55
56 // -----------------------------------------------------------------------
57
~ImplIdleMgr()58 ImplIdleMgr::~ImplIdleMgr()
59 {
60 // Liste loeschen
61 ImplIdleData* pIdleData = mpIdleList->First();
62 while ( pIdleData )
63 {
64 delete pIdleData;
65 pIdleData = mpIdleList->Next();
66 }
67
68 delete mpIdleList;
69 }
70
71 // -----------------------------------------------------------------------
72
InsertIdleHdl(const Link & rLink,sal_uInt16 nPriority)73 sal_Bool ImplIdleMgr::InsertIdleHdl( const Link& rLink, sal_uInt16 nPriority )
74 {
75 sal_uLong nPos = LIST_APPEND;
76 ImplIdleData* pIdleData = mpIdleList->First();
77 while ( pIdleData )
78 {
79 // Wenn Link schon existiert, dann gebe sal_False zurueck
80 if ( pIdleData->maIdleHdl == rLink )
81 return sal_False;
82
83 // Nach Prioritaet sortieren
84 if ( nPriority <= pIdleData->mnPriority )
85 nPos = mpIdleList->GetCurPos();
86
87 // Schleife nicht beenden, da noch
88 // geprueft werden muss, ob sich der Link
89 // schon in der Liste befindet
90
91 pIdleData = mpIdleList->Next();
92 }
93
94 pIdleData = new ImplIdleData;
95 pIdleData->maIdleHdl = rLink;
96 pIdleData->mnPriority = nPriority;
97 pIdleData->mbTimeout = sal_False;
98 mpIdleList->Insert( pIdleData, nPos );
99
100 // Wenn Timer noch nicht gestartet ist, dann starten
101 if ( !maTimer.IsActive() )
102 maTimer.Start();
103
104 return sal_True;
105 }
106
107 // -----------------------------------------------------------------------
108
RemoveIdleHdl(const Link & rLink)109 void ImplIdleMgr::RemoveIdleHdl( const Link& rLink )
110 {
111 ImplIdleData* pIdleData = mpIdleList->First();
112 while ( pIdleData )
113 {
114 if ( pIdleData->maIdleHdl == rLink )
115 {
116 mpIdleList->Remove();
117 delete pIdleData;
118 break;
119 }
120
121 pIdleData = mpIdleList->Next();
122 }
123
124 // keine Handdler mehr da
125 if ( !mpIdleList->Count() )
126 maTimer.Stop();
127 }
128
129 // -----------------------------------------------------------------------
130
IMPL_LINK(ImplIdleMgr,TimeoutHdl,Timer *,EMPTYARG)131 IMPL_LINK( ImplIdleMgr, TimeoutHdl, Timer*, EMPTYARG )
132 {
133 ImplIdleData* pIdleData = mpIdleList->First();
134 while ( pIdleData )
135 {
136 if ( !pIdleData->mbTimeout )
137 {
138 pIdleData->mbTimeout = sal_True;
139 pIdleData->maIdleHdl.Call( GetpApp() );
140 // Kann im Handler entfernt worden sein
141 if ( mpIdleList->GetPos( pIdleData ) != LIST_ENTRY_NOTFOUND )
142 pIdleData->mbTimeout = sal_False;
143 }
144
145 pIdleData = mpIdleList->Next();
146 }
147
148 return 0;
149 }
150