xref: /aoo42x/main/vcl/source/app/idlemgr.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #include <tools/list.hxx>
32 
33 #include <vcl/svapp.hxx>
34 
35 #include <idlemgr.hxx>
36 
37 // =======================================================================
38 
39 struct ImplIdleData
40 {
41     Link        maIdleHdl;
42     sal_uInt16      mnPriority;
43     sal_Bool        mbTimeout;
44 };
45 
46 DECLARE_LIST( ImplIdleList, ImplIdleData* )
47 
48 #define IMPL_IDLETIMEOUT         350
49 
50 // =======================================================================
51 
52 ImplIdleMgr::ImplIdleMgr()
53 {
54     mpIdleList  = new ImplIdleList( 8, 8, 8 );
55 
56     maTimer.SetTimeout( IMPL_IDLETIMEOUT );
57     maTimer.SetTimeoutHdl( LINK( this, ImplIdleMgr, TimeoutHdl ) );
58 }
59 
60 // -----------------------------------------------------------------------
61 
62 ImplIdleMgr::~ImplIdleMgr()
63 {
64     // Liste loeschen
65     ImplIdleData* pIdleData = mpIdleList->First();
66     while ( pIdleData )
67     {
68         delete pIdleData;
69         pIdleData = mpIdleList->Next();
70     }
71 
72     delete mpIdleList;
73 }
74 
75 // -----------------------------------------------------------------------
76 
77 sal_Bool ImplIdleMgr::InsertIdleHdl( const Link& rLink, sal_uInt16 nPriority )
78 {
79     sal_uLong           nPos = LIST_APPEND;
80     ImplIdleData*   pIdleData = mpIdleList->First();
81     while ( pIdleData )
82     {
83         // Wenn Link schon existiert, dann gebe sal_False zurueck
84         if ( pIdleData->maIdleHdl == rLink )
85             return sal_False;
86 
87         // Nach Prioritaet sortieren
88         if ( nPriority <= pIdleData->mnPriority )
89             nPos = mpIdleList->GetCurPos();
90 
91         // Schleife nicht beenden, da noch
92         // geprueft werden muss, ob sich der Link
93         // schon in der Liste befindet
94 
95         pIdleData = mpIdleList->Next();
96     }
97 
98     pIdleData               = new ImplIdleData;
99     pIdleData->maIdleHdl    = rLink;
100     pIdleData->mnPriority   = nPriority;
101     pIdleData->mbTimeout    = sal_False;
102     mpIdleList->Insert( pIdleData, nPos );
103 
104     // Wenn Timer noch nicht gestartet ist, dann starten
105     if ( !maTimer.IsActive() )
106         maTimer.Start();
107 
108     return sal_True;
109 }
110 
111 // -----------------------------------------------------------------------
112 
113 void ImplIdleMgr::RemoveIdleHdl( const Link& rLink )
114 {
115     ImplIdleData* pIdleData = mpIdleList->First();
116     while ( pIdleData )
117     {
118         if ( pIdleData->maIdleHdl == rLink )
119         {
120             mpIdleList->Remove();
121             delete pIdleData;
122             break;
123         }
124 
125         pIdleData = mpIdleList->Next();
126     }
127 
128     // keine Handdler mehr da
129     if ( !mpIdleList->Count() )
130         maTimer.Stop();
131 }
132 
133 // -----------------------------------------------------------------------
134 
135 IMPL_LINK( ImplIdleMgr, TimeoutHdl, Timer*, EMPTYARG )
136 {
137     ImplIdleData* pIdleData = mpIdleList->First();
138     while ( pIdleData )
139     {
140         if ( !pIdleData->mbTimeout )
141         {
142             pIdleData->mbTimeout = sal_True;
143             pIdleData->maIdleHdl.Call( GetpApp() );
144             // Kann im Handler entfernt worden sein
145             if ( mpIdleList->GetPos( pIdleData ) != LIST_ENTRY_NOTFOUND )
146                 pIdleData->mbTimeout = sal_False;
147         }
148 
149         pIdleData = mpIdleList->Next();
150     }
151 
152     return 0;
153 }
154