Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / app / idlemgr.cxx
blob9463acd5b02108be9808ec8206b7e6fef8593da6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <vcl/svapp.hxx>
22 #include <idlemgr.hxx>
24 struct ImplIdleData
26 Link<> maIdleHdl;
27 sal_uInt16 mnPriority;
28 bool mbTimeout;
31 #define IMPL_IDLETIMEOUT 350
33 ImplIdleMgr::ImplIdleMgr():
34 mbInDestruction(false)
36 mpIdleList = new ImplIdleList();
38 maTimer.SetTimeout( IMPL_IDLETIMEOUT );
39 maTimer.SetTimeoutHdl( LINK( this, ImplIdleMgr, TimeoutHdl ) );
42 ImplIdleMgr::~ImplIdleMgr()
44 mbInDestruction = true;
45 // Liste loeschen
46 for ( size_t i = 0, n = mpIdleList->size(); i < n; ++i ) {
47 ImplIdleData* pIdleData = (*mpIdleList)[ i ];
48 pIdleData->maIdleHdl.Call( GetpApp() );
49 delete pIdleData;
51 mpIdleList->clear();
52 delete mpIdleList;
55 bool ImplIdleMgr::InsertIdleHdl( const Link<>& rLink, sal_uInt16 nPriority )
57 size_t nPos = (size_t)-1;
58 size_t n = mpIdleList->size();
59 for ( size_t i = 0; i < n; ++i ) {
60 // we need to check each element to verify that rLink isn't in the array
61 if ( (*mpIdleList)[ i ]->maIdleHdl == rLink ) {
62 return false;
64 if ( nPriority <= (*mpIdleList)[ i ]->mnPriority ) {
65 nPos = i;
69 ImplIdleData* pIdleData = new ImplIdleData;
70 pIdleData->maIdleHdl = rLink;
71 pIdleData->mnPriority = nPriority;
72 pIdleData->mbTimeout = false;
74 if ( nPos < mpIdleList->size() ) {
75 ImplIdleList::iterator it = mpIdleList->begin();
76 ::std::advance( it, nPos );
77 mpIdleList->insert( it, pIdleData );
78 } else {
79 mpIdleList->push_back( pIdleData );
82 // if Timer was not started already then start it now
83 if ( !maTimer.IsActive() )
84 maTimer.Start();
86 return true;
89 void ImplIdleMgr::RemoveIdleHdl( const Link<>& rLink )
91 if (mbInDestruction)
92 return;
94 for ( ImplIdleList::iterator it = mpIdleList->begin(); it != mpIdleList->end(); ++it ) {
95 if ( (*it)->maIdleHdl == rLink ) {
96 delete *it;
97 mpIdleList->erase( it );
98 break;
102 // there are no more handlers...
103 if ( mpIdleList->empty() )
104 maTimer.Stop();
107 IMPL_LINK_NOARG_TYPED(ImplIdleMgr, TimeoutHdl, Timer *, void)
109 for ( size_t i = 0; i < mpIdleList->size(); ++i ) {
110 ImplIdleData* pIdleData = (*mpIdleList)[ i ];
111 if ( !pIdleData->mbTimeout ) {
112 pIdleData->mbTimeout = true;
113 pIdleData->maIdleHdl.Call( GetpApp() );
114 // May have been removed in the handler
115 for ( size_t j = 0; j < mpIdleList->size(); ++j ) {
116 if ( (*mpIdleList)[ j ] == pIdleData ) {
117 pIdleData->mbTimeout = false;
118 break;
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */