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