2007-08-11 22:37:09 +00:00
|
|
|
/**
|
2007-11-12 20:18:19 +00:00
|
|
|
* \file Timeout.cpp
|
2007-08-11 22:37:09 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/Timeout.h"
|
|
|
|
#include "support/debug.h"
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
#include <QObject>
|
|
|
|
#include <QTimerEvent>
|
|
|
|
|
2010-12-18 18:40:21 +00:00
|
|
|
#include <ctime>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class executes the callback when the timeout expires
|
|
|
|
* using Qt mechanisms
|
|
|
|
*/
|
2007-11-12 20:18:19 +00:00
|
|
|
class Timeout::Impl : QObject {
|
2007-08-11 22:37:09 +00:00
|
|
|
public:
|
|
|
|
///
|
2007-11-12 20:18:19 +00:00
|
|
|
Impl(Timeout & owner) : owner_(owner), timeout_id(-1) {}
|
2007-08-11 22:37:09 +00:00
|
|
|
///
|
2008-04-11 12:12:57 +00:00
|
|
|
bool running() const { return timeout_id != -1; }
|
2007-08-11 22:37:09 +00:00
|
|
|
/// start the timer
|
2007-11-12 20:18:19 +00:00
|
|
|
void start();
|
2007-08-11 22:37:09 +00:00
|
|
|
/// stop the timer
|
2007-11-12 20:18:19 +00:00
|
|
|
void stop();
|
2007-08-11 22:37:09 +00:00
|
|
|
/// reset
|
2007-11-12 20:18:19 +00:00
|
|
|
void reset();
|
|
|
|
///
|
|
|
|
unsigned int timeout_ms() const { return owner_.timeout_ms; }
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
protected:
|
2008-04-11 12:12:57 +00:00
|
|
|
///
|
2007-11-12 20:18:19 +00:00
|
|
|
void timerEvent(QTimerEvent *) { owner_.emit(); }
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
private:
|
2007-11-12 20:18:19 +00:00
|
|
|
///
|
|
|
|
Timeout & owner_;
|
2007-08-11 22:37:09 +00:00
|
|
|
/// timout id
|
|
|
|
int timeout_id;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::reset()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (timeout_id != -1)
|
|
|
|
killTimer(timeout_id);
|
|
|
|
timeout_id = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::start()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (running())
|
2007-12-12 19:28:07 +00:00
|
|
|
lyxerr << "Timeout::start: already running!" << endl;
|
2007-08-11 22:37:09 +00:00
|
|
|
timeout_id = startTimer(timeout_ms());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
void Timeout::Impl::stop()
|
2007-08-11 22:37:09 +00:00
|
|
|
{
|
|
|
|
if (running())
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2007-11-12 20:18:19 +00:00
|
|
|
// Timeout
|
2007-08-11 22:37:09 +00:00
|
|
|
//
|
|
|
|
|
2007-11-12 20:18:19 +00:00
|
|
|
Timeout::Timeout(unsigned int msec, Type t)
|
|
|
|
: pimpl_(new Impl(*this)), type(t), timeout_ms(msec)
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
Timeout::~Timeout()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
2007-11-12 20:18:19 +00:00
|
|
|
delete pimpl_;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Timeout::running() const
|
|
|
|
{
|
|
|
|
return pimpl_->running();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::start()
|
|
|
|
{
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::stop()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::restart()
|
|
|
|
{
|
|
|
|
pimpl_->stop();
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Timeout::emit()
|
|
|
|
{
|
|
|
|
pimpl_->reset();
|
|
|
|
timeout();
|
|
|
|
if (type == CONTINUOUS)
|
|
|
|
pimpl_->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timeout & Timeout::setType(Type t)
|
|
|
|
{
|
|
|
|
type = t;
|
2007-11-12 20:18:19 +00:00
|
|
|
return *this;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Timeout & Timeout::setTimeout(unsigned int msec)
|
|
|
|
{
|
|
|
|
// Can't have a timeout of zero!
|
2013-04-25 21:27:10 +00:00
|
|
|
LASSERT(msec, msec = 1000);
|
2007-08-11 22:37:09 +00:00
|
|
|
|
|
|
|
timeout_ms = msec;
|
2007-11-12 20:18:19 +00:00
|
|
|
return *this;
|
2007-08-11 22:37:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-18 18:40:21 +00:00
|
|
|
struct Timer::Private
|
|
|
|
{
|
|
|
|
time_t start_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Timer::Timer() : d(new Private)
|
|
|
|
{
|
|
|
|
restart();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-20 14:39:45 +00:00
|
|
|
Timer::~Timer()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-18 18:40:21 +00:00
|
|
|
void Timer::restart()
|
|
|
|
{
|
|
|
|
time(&d->start_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int Timer::elapsed() const
|
|
|
|
{
|
|
|
|
time_t end_time;
|
|
|
|
time(&end_time);
|
|
|
|
double diff = difftime(end_time, d->start_time);
|
|
|
|
return int(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Timer::timeStr(char separator) const
|
|
|
|
{
|
|
|
|
tm * timeinfo = localtime(&d->start_time);
|
|
|
|
// With less flexibility we could also use:
|
|
|
|
//strftime(buffer, 10, "%X", timeinfo);
|
|
|
|
ostringstream out;
|
|
|
|
out << setw(2) << setfill('0');
|
|
|
|
if (separator) {
|
|
|
|
out << separator << setw(2) << setfill('0') << timeinfo->tm_hour
|
|
|
|
<< separator << setw(2) << setfill('0') << timeinfo->tm_min
|
|
|
|
<< separator << setw(2) << setfill('0') << timeinfo->tm_sec;
|
|
|
|
} else {
|
|
|
|
out << setw(2) << setfill('0') << timeinfo->tm_hour
|
|
|
|
<< setw(2) << setfill('0') << timeinfo->tm_min
|
|
|
|
<< setw(2) << setfill('0') << timeinfo->tm_sec;
|
|
|
|
}
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Timer::dateStr(char separator) const
|
|
|
|
{
|
|
|
|
tm * timeinfo = localtime(&d->start_time);
|
|
|
|
// With less flexibility we could also use:
|
|
|
|
//res = strftime(buffer, 10, "%d%m%y", timeinfo);
|
|
|
|
ostringstream out;
|
|
|
|
out << setw(2) << setfill('0') << timeinfo->tm_mday;
|
|
|
|
if (separator)
|
|
|
|
out << separator;
|
|
|
|
out << setw(2) << setfill('0') << timeinfo->tm_mon;
|
|
|
|
if (separator)
|
|
|
|
out << separator;
|
|
|
|
out << setw(2) << setfill('0') << timeinfo->tm_year - 100;
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Timer::toStr() const
|
|
|
|
{
|
|
|
|
tm * timeinfo = localtime(&d->start_time);
|
|
|
|
return asctime(timeinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string Timer::currentToStr()
|
|
|
|
{
|
|
|
|
time_t current_time;
|
|
|
|
time(¤t_time);
|
|
|
|
tm * timeinfo = localtime(¤t_time);
|
|
|
|
return asctime(timeinfo);
|
|
|
|
}
|
|
|
|
|
2007-08-11 22:37:09 +00:00
|
|
|
} // namespace lyx
|