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 <time.h>
21 #include <osl/diagnose.h>
22 
23 #include <address.hxx>
24 #include <autostyl.hxx>
25 #include <docsh.hxx>
26 
TimeNow()27 static sal_uLong TimeNow()          // seconds
28 {
29     return static_cast<sal_uLong>(time(nullptr));
30 }
31 
32 namespace {
33 
34 class FindByRange
35 {
36     ScRange const maRange;
37 public:
FindByRange(const ScRange & r)38     explicit FindByRange(const ScRange& r) : maRange(r) {}
operator ()(const ScAutoStyleData & rData) const39     bool operator() (const ScAutoStyleData& rData) const { return rData.aRange == maRange; }
40 };
41 
42 class FindByTimeout
43 {
44     sal_uLong const mnTimeout;
45 public:
FindByTimeout(sal_uLong n)46     explicit FindByTimeout(sal_uLong n) : mnTimeout(n) {}
operator ()(const ScAutoStyleData & rData) const47     bool operator() (const ScAutoStyleData& rData) const { return rData.nTimeout >= mnTimeout; }
48 };
49 
50 struct FindNonZeroTimeout
51 {
operator ()__anonfe38ab9a0111::FindNonZeroTimeout52     bool operator() (const ScAutoStyleData& rData) const
53     {
54         return rData.nTimeout != 0;
55     }
56 };
57 
58 }
59 
ScAutoStyleList(ScDocShell * pShell)60 ScAutoStyleList::ScAutoStyleList(ScDocShell* pShell)
61     : pDocSh(pShell)
62     , aTimer("ScAutoStyleList Timer")
63     , aInitIdle("ScAutoStyleList InitIdle")
64     , nTimerStart(0)
65 {
66     aTimer.SetInvokeHandler( LINK( this, ScAutoStyleList, TimerHdl ) );
67     aInitIdle.SetInvokeHandler( LINK( this, ScAutoStyleList, InitHdl ) );
68     aInitIdle.SetPriority( TaskPriority::HIGHEST );
69 }
70 
~ScAutoStyleList()71 ScAutoStyleList::~ScAutoStyleList()
72 {
73 }
74 
75 //  initial short delay (asynchronous call)
76 
AddInitial(const ScRange & rRange,const OUString & rStyle1,sal_uLong nTimeout,const OUString & rStyle2)77 void ScAutoStyleList::AddInitial( const ScRange& rRange, const OUString& rStyle1,
78                                     sal_uLong nTimeout, const OUString& rStyle2 )
79 {
80     aInitials.emplace_back( rRange, rStyle1, nTimeout, rStyle2 );
81     aInitIdle.Start();
82 }
83 
IMPL_LINK_NOARG(ScAutoStyleList,InitHdl,Timer *,void)84 IMPL_LINK_NOARG(ScAutoStyleList, InitHdl, Timer *, void)
85 {
86     for (const auto& rInitial : aInitials)
87     {
88         //  apply first style immediately
89         pDocSh->DoAutoStyle(rInitial.aRange, rInitial.aStyle1);
90 
91         //  add second style to list
92         if (rInitial.nTimeout)
93             AddEntry(rInitial.nTimeout, rInitial.aRange, rInitial.aStyle2 );
94     }
95 
96     aInitials.clear();
97 }
98 
AddEntry(sal_uLong nTimeout,const ScRange & rRange,const OUString & rStyle)99 void ScAutoStyleList::AddEntry( sal_uLong nTimeout, const ScRange& rRange, const OUString& rStyle )
100 {
101     aTimer.Stop();
102     sal_uLong nNow = TimeNow();
103 
104     // Remove the first item with the same range.
105     std::vector<ScAutoStyleData>::iterator itr =
106         ::std::find_if(aEntries.begin(), aEntries.end(), FindByRange(rRange));
107 
108     if (itr != aEntries.end())
109         aEntries.erase(itr);
110 
111     //  adjust timeouts of all entries
112 
113     if (!aEntries.empty() && nNow != nTimerStart)
114     {
115         OSL_ENSURE(nNow>nTimerStart, "Time is running backwards?");
116         AdjustEntries((nNow-nTimerStart)*1000);
117     }
118 
119     //  find insert position
120     std::vector<ScAutoStyleData>::iterator iter =
121         ::std::find_if(aEntries.begin(), aEntries.end(), FindByTimeout(nTimeout));
122 
123     aEntries.insert(iter, ScAutoStyleData(nTimeout,rRange,rStyle));
124 
125     //  execute expired, restart timer
126 
127     ExecuteEntries();
128     StartTimer(nNow);
129 }
130 
AdjustEntries(sal_uLong nDiff)131 void ScAutoStyleList::AdjustEntries( sal_uLong nDiff )  // milliseconds
132 {
133     for (auto& rEntry : aEntries)
134     {
135         if (rEntry.nTimeout <= nDiff)
136             rEntry.nTimeout = 0;                 // expired
137         else
138             rEntry.nTimeout -= nDiff;                // continue counting
139     }
140 }
141 
ExecuteEntries()142 void ScAutoStyleList::ExecuteEntries()
143 {
144     // Execute and remove all items with timeout == 0 from the begin position
145     // until the first item with non-zero timeout value.
146     std::vector<ScAutoStyleData>::iterator itr = aEntries.begin(), itrEnd = aEntries.end();
147     for (; itr != itrEnd; ++itr)
148     {
149         if (itr->nTimeout)
150             break;
151 
152         pDocSh->DoAutoStyle(itr->aRange, itr->aStyle);
153     }
154     // At this point itr should be on the first item with non-zero timeout, or
155     // the end position in case all items have timeout == 0.
156     aEntries.erase(aEntries.begin(), itr);
157 }
158 
ExecuteAllNow()159 void ScAutoStyleList::ExecuteAllNow()
160 {
161     aTimer.Stop();
162 
163     for (const auto& rEntry : aEntries)
164         pDocSh->DoAutoStyle(rEntry.aRange, rEntry.aStyle);
165 
166     aEntries.clear();
167 }
168 
StartTimer(sal_uLong nNow)169 void ScAutoStyleList::StartTimer( sal_uLong nNow )      // seconds
170 {
171     // find first entry with Timeout != 0
172     std::vector<ScAutoStyleData>::iterator iter =
173         ::std::find_if(aEntries.begin(),aEntries.end(), FindNonZeroTimeout());
174 
175     if (iter != aEntries.end())
176     {
177         aTimer.SetTimeout(iter->nTimeout);
178         aTimer.Start();
179     }
180 
181     nTimerStart = nNow;
182 }
183 
IMPL_LINK_NOARG(ScAutoStyleList,TimerHdl,Timer *,void)184 IMPL_LINK_NOARG(ScAutoStyleList, TimerHdl, Timer *, void)
185 {
186     sal_uLong nNow = TimeNow();
187     AdjustEntries(aTimer.GetTimeout());             // the set waiting time
188     ExecuteEntries();
189     StartTimer(nNow);
190 }
191 
192 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
193