2002-09-25 10:03:41 +00:00
|
|
|
/**
|
2002-07-18 14:01:42 +00:00
|
|
|
* \file FileMonitor.C
|
2002-09-25 10:03:41 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-07-18 14:01:42 +00:00
|
|
|
*
|
2002-09-25 10:03:41 +00:00
|
|
|
* \author Angus Leeming
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-07-18 14:01:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
#include "support/FileMonitor.h"
|
2002-07-18 14:01:42 +00:00
|
|
|
#include "support/lyxlib.h"
|
|
|
|
|
2004-11-07 13:22:51 +00:00
|
|
|
// FIXME Interface violation
|
|
|
|
#include "frontends/Timeout.h"
|
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
#include <boost/bind.hpp>
|
2005-01-31 10:42:26 +00:00
|
|
|
#include <boost/filesystem/operations.hpp>
|
2002-07-18 14:01:42 +00:00
|
|
|
#include <boost/signals/trackable.hpp>
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
namespace lyx {
|
|
|
|
namespace support {
|
|
|
|
|
2005-01-19 15:03:31 +00:00
|
|
|
class FileMonitor::Impl : public boost::signals::trackable {
|
|
|
|
public:
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
Impl(string const & file_with_path, int interval);
|
|
|
|
|
|
|
|
///
|
|
|
|
void monitorFile();
|
|
|
|
|
|
|
|
///
|
|
|
|
string filename_;
|
|
|
|
|
|
|
|
///
|
|
|
|
Timeout timer_;
|
|
|
|
|
|
|
|
/// This signal is emitted if the file is modified (has a new checksum).
|
2004-09-26 14:19:47 +00:00
|
|
|
FileMonitor::FileChangedSig fileChanged_;
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
/** We use these to ascertain whether a file (once loaded successfully)
|
|
|
|
* has changed.
|
|
|
|
*/
|
|
|
|
time_t timestamp_;
|
|
|
|
///
|
|
|
|
unsigned long checksum_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
FileMonitor::FileMonitor(string const & file_with_path, int interval)
|
|
|
|
: pimpl_(new Impl(file_with_path, interval))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
FileMonitor::~FileMonitor()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
void FileMonitor::reset(string const & file_with_path) const
|
|
|
|
{
|
|
|
|
if (pimpl_->filename_ == file_with_path)
|
|
|
|
return;
|
|
|
|
|
|
|
|
bool const monitor = pimpl_->timer_.running();
|
|
|
|
if (monitor)
|
|
|
|
stop();
|
2002-09-25 10:03:41 +00:00
|
|
|
|
2002-07-18 14:01:42 +00:00
|
|
|
pimpl_->filename_ = file_with_path;
|
|
|
|
|
|
|
|
if (monitor)
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string const & FileMonitor::filename() const
|
|
|
|
{
|
|
|
|
return pimpl_->filename_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileMonitor::start() const
|
|
|
|
{
|
|
|
|
if (monitoring())
|
|
|
|
return;
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
if (!fs::exists(pimpl_->filename_))
|
2002-07-18 14:01:42 +00:00
|
|
|
return;
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename_);
|
2003-06-30 23:56:22 +00:00
|
|
|
pimpl_->checksum_ = sum(pimpl_->filename_);
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
if (pimpl_->timestamp_ && pimpl_->checksum_) {
|
|
|
|
pimpl_->timer_.start();
|
|
|
|
} else {
|
|
|
|
pimpl_->timestamp_ = 0;
|
|
|
|
pimpl_->checksum_ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileMonitor::stop() const
|
|
|
|
{
|
|
|
|
pimpl_->timestamp_ = 0;
|
|
|
|
pimpl_->checksum_ = 0;
|
|
|
|
pimpl_->timer_.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool FileMonitor::monitoring() const
|
|
|
|
{
|
|
|
|
return pimpl_->timer_.running();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long FileMonitor::checksum() const
|
|
|
|
{
|
|
|
|
// If we aren't actively monitoring the file, then recompute the
|
|
|
|
// checksum explicitly.
|
|
|
|
if (!pimpl_->timer_.running() && !pimpl_->filename_.empty())
|
2003-06-30 23:56:22 +00:00
|
|
|
return sum(pimpl_->filename_);
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
return pimpl_->checksum_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
boost::signals::connection FileMonitor::connect(slot_type const & slot) const
|
|
|
|
{
|
|
|
|
return pimpl_->fileChanged_.connect(slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------
|
|
|
|
// Implementation details follow
|
|
|
|
//------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
FileMonitor::Impl::Impl(string const & file_with_path, int interval)
|
|
|
|
: filename_(file_with_path),
|
|
|
|
timer_(interval, Timeout::ONETIME),
|
|
|
|
timestamp_(0),
|
|
|
|
checksum_(0)
|
|
|
|
{
|
|
|
|
timer_.timeout.connect(boost::bind(&Impl::monitorFile, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileMonitor::Impl::monitorFile()
|
|
|
|
{
|
|
|
|
bool changed = false;
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
if (!fs::exists(filename_)) {
|
2002-07-18 14:01:42 +00:00
|
|
|
changed = timestamp_ || checksum_;
|
|
|
|
timestamp_ = 0;
|
|
|
|
checksum_ = 0;
|
|
|
|
|
|
|
|
} else {
|
2005-01-31 10:42:26 +00:00
|
|
|
time_t const new_timestamp = fs::last_write_time(filename_);
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
if (new_timestamp != timestamp_) {
|
|
|
|
timestamp_ = new_timestamp;
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
unsigned long const new_checksum = sum(filename_);
|
2002-07-18 14:01:42 +00:00
|
|
|
if (new_checksum != checksum_) {
|
|
|
|
checksum_ = new_checksum;
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer_.start();
|
|
|
|
if (changed)
|
|
|
|
fileChanged_();
|
|
|
|
}
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
|
|
} // namespace support
|
|
|
|
} // namespace lyx
|