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"
|
2006-11-26 21:30:39 +00:00
|
|
|
#include "support/filename.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
|
|
|
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
Impl(FileName const & file_with_path, int interval);
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
void monitorFile();
|
|
|
|
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName filename_;
|
2002-07-18 14:01:42 +00:00
|
|
|
|
|
|
|
///
|
|
|
|
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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileMonitor::FileMonitor(FileName const & file_with_path, int interval)
|
2002-07-18 14:01:42 +00:00
|
|
|
: pimpl_(new Impl(file_with_path, interval))
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
FileMonitor::~FileMonitor()
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
void FileMonitor::reset(FileName const & file_with_path) const
|
2002-07-18 14:01:42 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileName const & FileMonitor::filename() const
|
2002-07-18 14:01:42 +00:00
|
|
|
{
|
|
|
|
return pimpl_->filename_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FileMonitor::start() const
|
|
|
|
{
|
|
|
|
if (monitoring())
|
|
|
|
return;
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
if (!fs::exists(pimpl_->filename_.toFilesystemEncoding()))
|
2002-07-18 14:01:42 +00:00
|
|
|
return;
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename_.toFilesystemEncoding());
|
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
|
|
|
|
//------------------------------
|
|
|
|
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
FileMonitor::Impl::Impl(FileName const & file_with_path, int interval)
|
2002-07-18 14:01:42 +00:00
|
|
|
: 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;
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
if (!fs::exists(filename_.toFilesystemEncoding())) {
|
2002-07-18 14:01:42 +00:00
|
|
|
changed = timestamp_ || checksum_;
|
|
|
|
timestamp_ = 0;
|
|
|
|
checksum_ = 0;
|
|
|
|
|
|
|
|
} else {
|
2006-11-26 21:30:39 +00:00
|
|
|
time_t const new_timestamp = fs::last_write_time(filename_.toFilesystemEncoding());
|
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
|