]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.C
a required file
[lyx.git] / src / support / FileMonitor.C
1 /**
2  * \file FileMonitor.C
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/FileInfo.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/signals/trackable.hpp>
22
23
24 using std::string;
25
26
27 namespace lyx {
28 namespace support {
29
30 class FileMonitor::Impl : public boost::signals::trackable {
31 public:
32
33         ///
34         Impl(string const & file_with_path, int interval);
35
36         ///
37         void monitorFile();
38
39         ///
40         string 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(string 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(string 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 string 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         FileInfo finfo(pimpl_->filename_);
94         if (!finfo.isOK())
95                 return;
96
97         pimpl_->timestamp_ = finfo.getModificationTime();
98         pimpl_->checksum_ = sum(pimpl_->filename_);
99
100         if (pimpl_->timestamp_ && pimpl_->checksum_) {
101                 pimpl_->timer_.start();
102         } else {
103                 pimpl_->timestamp_ = 0;
104                 pimpl_->checksum_ = 0;
105         }
106 }
107
108
109 void FileMonitor::stop() const
110 {
111         pimpl_->timestamp_ = 0;
112         pimpl_->checksum_ = 0;
113         pimpl_->timer_.stop();
114 }
115
116
117 bool FileMonitor::monitoring() const
118 {
119         return pimpl_->timer_.running();
120 }
121
122
123 unsigned long FileMonitor::checksum() const
124 {
125         // If we aren't actively monitoring the file, then recompute the
126         // checksum explicitly.
127         if (!pimpl_->timer_.running() && !pimpl_->filename_.empty())
128                 return sum(pimpl_->filename_);
129
130         return pimpl_->checksum_;
131 }
132
133
134 boost::signals::connection FileMonitor::connect(slot_type const & slot) const
135 {
136         return pimpl_->fileChanged_.connect(slot);
137 }
138
139
140 //------------------------------
141 // Implementation details follow
142 //------------------------------
143
144
145 FileMonitor::Impl::Impl(string const & file_with_path, int interval)
146         : filename_(file_with_path),
147           timer_(interval, Timeout::ONETIME),
148           timestamp_(0),
149           checksum_(0)
150 {
151         timer_.timeout.connect(boost::bind(&Impl::monitorFile, this));
152 }
153
154
155 void FileMonitor::Impl::monitorFile()
156 {
157         bool changed = false;
158
159         FileInfo finfo(filename_);
160         if (!finfo.isOK()) {
161                 changed = timestamp_ || checksum_;
162                 timestamp_ = 0;
163                 checksum_ = 0;
164
165         } else {
166                 time_t const new_timestamp = finfo.getModificationTime();
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