]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.cpp
lyxlib.h:sum -> FileName::checksum()
[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
15 #include "support/FileName.h"
16 #include "support/lyxlib.h"
17 #include "support/Timeout.h"
18
19 #include <boost/bind.hpp>
20 #include <boost/signals/trackable.hpp>
21
22
23 using std::string;
24
25 namespace lyx {
26 namespace support {
27
28 class FileMonitor::Impl : public boost::signals::trackable {
29 public:
30
31         ///
32         Impl(FileName const & file_with_path, int interval);
33
34         ///
35         void monitorFile();
36
37         ///
38         FileName filename_;
39
40         ///
41         Timeout timer_;
42
43         /// This signal is emitted if the file is modified (has a new checksum).
44         FileMonitor::FileChangedSig fileChanged_;
45
46         /** We use these to ascertain whether a file (once loaded successfully)
47          *  has changed.
48          */
49         time_t timestamp_;
50         ///
51         unsigned long checksum_;
52 };
53
54
55 FileMonitor::FileMonitor(FileName const & file_with_path, int interval)
56         : pimpl_(new Impl(file_with_path, interval))
57 {}
58
59
60 FileMonitor::~FileMonitor()
61 {
62         delete pimpl_;
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 (!pimpl_->filename_.exists())
94                 return;
95
96         pimpl_->timestamp_ = pimpl_->filename_.lastModified();
97         pimpl_->checksum_ = pimpl_->filename_.checksum();
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 pimpl_->filename_.checksum();
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 (!filename_.exists()) {
159                 changed = timestamp_ || checksum_;
160                 timestamp_ = 0;
161                 checksum_ = 0;
162
163         } else {
164                 time_t const new_timestamp = filename_.lastModified();
165
166                 if (new_timestamp != timestamp_) {
167                         timestamp_ = new_timestamp;
168
169                         unsigned long const new_checksum = filename_.checksum();
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