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