]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.cpp
cosmetics
[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 using namespace std;
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         delete pimpl_;
62 }
63
64
65 void FileMonitor::reset(FileName 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 FileName 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         if (!pimpl_->filename_.exists())
93                 return;
94
95         pimpl_->timestamp_ = pimpl_->filename_.lastModified();
96         pimpl_->checksum_ = pimpl_->filename_.checksum();
97
98         if (pimpl_->timestamp_ && pimpl_->checksum_) {
99                 pimpl_->timer_.start();
100         } else {
101                 pimpl_->timestamp_ = 0;
102                 pimpl_->checksum_ = 0;
103         }
104 }
105
106
107 void FileMonitor::stop() const
108 {
109         pimpl_->timestamp_ = 0;
110         pimpl_->checksum_ = 0;
111         pimpl_->timer_.stop();
112 }
113
114
115 bool FileMonitor::monitoring() const
116 {
117         return pimpl_->timer_.running();
118 }
119
120
121 unsigned long FileMonitor::checksum() const
122 {
123         // If we aren't actively monitoring the file, then recompute the
124         // checksum explicitly.
125         if (!pimpl_->timer_.running() && !pimpl_->filename_.empty())
126                 return pimpl_->filename_.checksum();
127
128         return pimpl_->checksum_;
129 }
130
131
132 boost::signals::connection FileMonitor::connect(slot_type const & slot) const
133 {
134         return pimpl_->fileChanged_.connect(slot);
135 }
136
137
138 //------------------------------
139 // Implementation details follow
140 //------------------------------
141
142
143 FileMonitor::Impl::Impl(FileName const & file_with_path, int interval)
144         : filename_(file_with_path),
145           timer_(interval, Timeout::ONETIME),
146           timestamp_(0),
147           checksum_(0)
148 {
149         timer_.timeout.connect(boost::bind(&Impl::monitorFile, this));
150 }
151
152
153 void FileMonitor::Impl::monitorFile()
154 {
155         bool changed = false;
156
157         if (!filename_.exists()) {
158                 changed = timestamp_ || checksum_;
159                 timestamp_ = 0;
160                 checksum_ = 0;
161
162         } else {
163                 time_t const new_timestamp = filename_.lastModified();
164
165                 if (new_timestamp != timestamp_) {
166                         timestamp_ = new_timestamp;
167
168                         unsigned long const new_checksum = filename_.checksum();
169                         if (new_checksum != checksum_) {
170                                 checksum_ = new_checksum;
171                                 changed = true;
172                         }
173                 }
174         }
175
176         timer_.start();
177         if (changed)
178                 fileChanged_();
179 }
180
181 } // namespace support
182 } // namespace lyx