]> git.lyx.org Git - lyx.git/blob - src/support/Timeout.cpp
Only create a new view for lyxfiles-open if no view is open (#12894)
[lyx.git] / src / support / Timeout.cpp
1 /**
2  * \file Timeout.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/Timeout.h"
14
15 #include "support/lassert.h"
16 #include "support/debug.h"
17
18 #include <QObject>
19 #include <QTimerEvent>
20
21 #include <ctime>
22 #include <iomanip>
23 #include <sstream>
24
25 using namespace std;
26
27 namespace lyx {
28
29 /**
30  * This class executes the callback when the timeout expires
31  * using Qt mechanisms
32  */
33 class Timeout::Impl : QObject {
34 public:
35         ///
36         Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
37         ///
38         bool running() const { return timeout_id != -1; }
39         /// start the timer
40         void start();
41         /// stop the timer
42         void stop();
43         /// reset
44         void reset();
45         ///
46         unsigned int timeout_ms() const { return owner_.timeout_ms; }
47
48 protected:
49         ///
50         void timerEvent(QTimerEvent *) override { owner_.emit(); }
51
52 private:
53         ///
54         Timeout & owner_;
55         /// timeout id
56         int timeout_id;
57 };
58
59
60 void Timeout::Impl::reset()
61 {
62         if (timeout_id != -1)
63                 killTimer(timeout_id);
64         timeout_id = -1;
65 }
66
67
68 void Timeout::Impl::start()
69 {
70         if (running())
71                 lyxerr << "Timeout::start: already running!" << endl;
72         timeout_id = startTimer(timeout_ms());
73 }
74
75
76 void Timeout::Impl::stop()
77 {
78         if (running())
79                 reset();
80 }
81
82
83 //
84 // Timeout
85 //
86
87 Timeout::Timeout(unsigned int msec, Type t)
88         : pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
89 {}
90
91
92 Timeout::~Timeout()
93 {
94         pimpl_->stop();
95         delete pimpl_;
96 }
97
98
99 bool Timeout::running() const
100 {
101         return pimpl_->running();
102 }
103
104
105 void Timeout::start()
106 {
107         pimpl_->start();
108 }
109
110
111 void Timeout::stop()
112 {
113         pimpl_->stop();
114 }
115
116
117 void Timeout::restart()
118 {
119         pimpl_->stop();
120         pimpl_->start();
121 }
122
123
124 void Timeout::emit()
125 {
126         pimpl_->reset();
127         timeout();
128         if (type == CONTINUOUS)
129                 pimpl_->start();
130 }
131
132
133 Timeout & Timeout::setType(Type t)
134 {
135         type = t;
136         return *this;
137 }
138
139
140 Timeout & Timeout::setTimeout(unsigned int msec)
141 {
142         // Can't have a timeout of zero!
143         LASSERT(msec, msec = 1000);
144
145         timeout_ms = msec;
146         return *this;
147 }
148
149
150 struct Timer::Private
151 {
152         time_t start_time;
153 };
154
155
156 Timer::Timer() : d(new Private)
157 {
158         restart();
159 }
160
161
162 Timer::~Timer()
163 {
164         delete d;
165 }
166
167
168 void Timer::restart()
169 {
170         time(&d->start_time);
171 }
172
173
174 int Timer::elapsed() const
175 {
176         time_t end_time;
177         time(&end_time);
178         double diff = difftime(end_time, d->start_time);
179         return int(diff);
180 }
181
182
183 string Timer::timeStr(char separator) const
184 {
185         tm * timeinfo = localtime(&d->start_time);
186         // With less flexibility we could also use:
187         //strftime(buffer, 10, "%X", timeinfo);
188         ostringstream out;
189         out << setw(2) << setfill('0');
190         if (separator) {
191                 out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
192                     << separator << setw(2) << setfill('0') << timeinfo->tm_min
193                     << separator << setw(2) << setfill('0') << timeinfo->tm_sec;
194         } else {
195                 out << setw(2) << setfill('0') << timeinfo->tm_hour
196                     << setw(2) << setfill('0') << timeinfo->tm_min
197                     << setw(2) << setfill('0') << timeinfo->tm_sec;
198         }
199         return out.str();
200 }
201
202
203 string Timer::dateStr(char separator) const
204 {
205         tm * timeinfo = localtime(&d->start_time);
206         // With less flexibility we could also use:
207         //res = strftime(buffer, 10, "%d%m%y", timeinfo);
208         ostringstream out;
209         out << setw(2) << setfill('0') << timeinfo->tm_mday;
210         if (separator)
211                 out << separator;
212         out << setw(2) << setfill('0') << timeinfo->tm_mon;
213         if (separator)
214                 out << separator;
215         out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
216         return out.str();
217 }
218
219
220 string Timer::toStr() const
221 {
222         tm * timeinfo = localtime(&d->start_time);
223         return asctime(timeinfo);
224 }
225
226
227 string Timer::currentToStr()
228 {
229         time_t current_time;
230         time(&current_time);
231         tm * timeinfo = localtime(&current_time);
232         return asctime(timeinfo);
233 }
234
235 } // namespace lyx