]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.C
Use the preferred calling for Boost.Signal
[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 "FileMonitor.h"
14
15 #include "frontends/Timeout.h"
16
17 #include "support/FileInfo.h"
18 #include "support/lyxlib.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 struct FileMonitor::Impl : public boost::signals::trackable {
31
32         ///
33         Impl(string const & file_with_path, int interval);
34
35         ///
36         void monitorFile();
37
38         ///
39         string filename_;
40
41         ///
42         Timeout timer_;
43
44         /// This signal is emitted if the file is modified (has a new checksum).
45         FileMonitor::FileChangedSig fileChanged_;
46
47         /** We use these to ascertain whether a file (once loaded successfully)
48          *  has changed.
49          */
50         time_t timestamp_;
51         ///
52         unsigned long checksum_;
53 };
54
55
56 FileMonitor::FileMonitor(string const & file_with_path, int interval)
57         : pimpl_(new Impl(file_with_path, interval))
58 {}
59
60
61 FileMonitor::~FileMonitor()
62 {}
63
64
65 void FileMonitor::reset(string const & file_with_path) const
66 {
67         if (pimpl_->filename_ == file_with_path)
68                 return;
69
70         bool const monitor = pimpl_->timer_.running();
71         if (monitor)
72                 stop();
73
74         pimpl_->filename_ = file_with_path;
75
76         if (monitor)
77                 start();
78 }
79
80
81 string const & FileMonitor::filename() const
82 {
83         return pimpl_->filename_;
84 }
85
86
87 void FileMonitor::start() const
88 {
89         if (monitoring())
90                 return;
91
92         FileInfo finfo(pimpl_->filename_);
93         if (!finfo.isOK())
94                 return;
95
96         pimpl_->timestamp_ = finfo.getModificationTime();
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(string 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         FileInfo finfo(filename_);
159         if (!finfo.isOK()) {
160                 changed = timestamp_ || checksum_;
161                 timestamp_ = 0;
162                 checksum_ = 0;
163
164         } else {
165                 time_t const new_timestamp = finfo.getModificationTime();
166
167                 if (new_timestamp != timestamp_) {
168                         timestamp_ = new_timestamp;
169
170                         unsigned long const new_checksum = sum(filename_);
171                         if (new_checksum != checksum_) {
172                                 checksum_ = new_checksum;
173                                 changed = true;
174                         }
175                 }
176         }
177
178         timer_.start();
179         if (changed)
180                 fileChanged_();
181 }
182
183 } // namespace support
184 } // namespace lyx