]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.cpp
remove reference to LaTeXStream
[lyx.git] / src / support / FileMonitor.cpp
1 /**
2  * \file FileMonitor.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "support/FileMonitor.h"
14 #include "support/FileName.h"
15 #include "support/lyxlib.h"
16 #include "support/Timeout.h"
17
18 #include <boost/bind.hpp>
19 #include <boost/filesystem/operations.hpp>
20 #include <boost/signals/trackable.hpp>
21
22
23 using std::string;
24
25 namespace fs = boost::filesystem;
26
27 namespace lyx {
28 namespace support {
29
30 class FileMonitor::Impl : public boost::signals::trackable {
31 public:
32
33         ///
34         Impl(FileName const & file_with_path, int interval);
35
36         ///
37         void monitorFile();
38
39         ///
40         FileName filename_;
41
42         ///
43         Timeout timer_;
44
45         /// This signal is emitted if the file is modified (has a new checksum).
46         FileMonitor::FileChangedSig fileChanged_;
47
48         /** We use these to ascertain whether a file (once loaded successfully)
49          *  has changed.
50          */
51         time_t timestamp_;
52         ///
53         unsigned long checksum_;
54 };
55
56
57 FileMonitor::FileMonitor(FileName const & file_with_path, int interval)
58         : pimpl_(new Impl(file_with_path, interval))
59 {}
60
61
62 FileMonitor::~FileMonitor()
63 {}
64
65
66 void FileMonitor::reset(FileName const & file_with_path) const
67 {
68         if (pimpl_->filename_ == file_with_path)
69                 return;
70
71         bool const monitor = pimpl_->timer_.running();
72         if (monitor)
73                 stop();
74
75         pimpl_->filename_ = file_with_path;
76
77         if (monitor)
78                 start();
79 }
80
81
82 FileName const & FileMonitor::filename() const
83 {
84         return pimpl_->filename_;
85 }
86
87
88 void FileMonitor::start() const
89 {
90         if (monitoring())
91                 return;
92
93         if (!fs::exists(pimpl_->filename_.toFilesystemEncoding()))
94                 return;
95
96         pimpl_->timestamp_ = fs::last_write_time(pimpl_->filename_.toFilesystemEncoding());
97         pimpl_->checksum_ = sum(pimpl_->filename_);
98
99         if (pimpl_->timestamp_ && pimpl_->checksum_) {
100                 pimpl_->timer_.start();
101         } else {
102                 pimpl_->timestamp_ = 0;
103                 pimpl_->checksum_ = 0;
104         }
105 }
106
107
108 void FileMonitor::stop() const
109 {
110         pimpl_->timestamp_ = 0;
111         pimpl_->checksum_ = 0;
112         pimpl_->timer_.stop();
113 }
114
115
116 bool FileMonitor::monitoring() const
117 {
118         return pimpl_->timer_.running();
119 }
120
121
122 unsigned long FileMonitor::checksum() const
123 {
124         // If we aren't actively monitoring the file, then recompute the
125         // checksum explicitly.
126         if (!pimpl_->timer_.running() && !pimpl_->filename_.empty())
127                 return sum(pimpl_->filename_);
128
129         return pimpl_->checksum_;
130 }
131
132
133 boost::signals::connection FileMonitor::connect(slot_type const & slot) const
134 {
135         return pimpl_->fileChanged_.connect(slot);
136 }
137
138
139 //------------------------------
140 // Implementation details follow
141 //------------------------------
142
143
144 FileMonitor::Impl::Impl(FileName const & file_with_path, int interval)
145         : filename_(file_with_path),
146           timer_(interval, Timeout::ONETIME),
147           timestamp_(0),
148           checksum_(0)
149 {
150         timer_.timeout.connect(boost::bind(&Impl::monitorFile, this));
151 }
152
153
154 void FileMonitor::Impl::monitorFile()
155 {
156         bool changed = false;
157
158         if (!fs::exists(filename_.toFilesystemEncoding())) {
159                 changed = timestamp_ || checksum_;
160                 timestamp_ = 0;
161                 checksum_ = 0;
162
163         } else {
164                 time_t const new_timestamp = fs::last_write_time(filename_.toFilesystemEncoding());
165
166                 if (new_timestamp != timestamp_) {
167                         timestamp_ = new_timestamp;
168
169                         unsigned long const new_checksum = sum(filename_);
170                         if (new_checksum != checksum_) {
171                                 checksum_ = new_checksum;
172                                 changed = true;
173                         }
174                 }
175         }
176
177         timer_.start();
178         if (changed)
179                 fileChanged_();
180 }
181
182 } // namespace support
183 } // namespace lyx