]> git.lyx.org Git - lyx.git/blob - src/support/FileMonitor.C
Clean up licence info and add Lars as the mysterious unknown author.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "FileMonitor.h"
18
19 #include "frontends/Timeout.h"
20
21 #include "support/FileInfo.h"
22 #include "support/lyxlib.h"
23
24 #include <boost/bind.hpp>
25 #include <boost/signals/trackable.hpp>
26
27 struct FileMonitor::Impl : public boost::signals::trackable {
28
29         ///
30         Impl(string const & file_with_path, int interval);
31
32         ///
33         void monitorFile();
34
35         ///
36         string filename_;
37
38         ///
39         Timeout timer_;
40
41         /// This signal is emitted if the file is modified (has a new checksum).
42         boost::signal0<void> fileChanged_;
43
44         /** We use these to ascertain whether a file (once loaded successfully)
45          *  has changed.
46          */
47         time_t timestamp_;
48         ///
49         unsigned long checksum_;
50 };
51
52
53 FileMonitor::FileMonitor(string const & file_with_path, int interval)
54         : pimpl_(new Impl(file_with_path, interval))
55 {}
56
57
58 FileMonitor::~FileMonitor()
59 {}
60
61
62 void FileMonitor::reset(string const & file_with_path) const
63 {
64         if (pimpl_->filename_ == file_with_path)
65                 return;
66
67         bool const monitor = pimpl_->timer_.running();
68         if (monitor)
69                 stop();
70
71         pimpl_->filename_ = file_with_path;
72
73         if (monitor)
74                 start();
75 }
76
77
78 string const & FileMonitor::filename() const
79 {
80         return pimpl_->filename_;
81 }
82
83
84 void FileMonitor::start() const
85 {
86         if (monitoring())
87                 return;
88
89         FileInfo finfo(pimpl_->filename_);
90         if (!finfo.isOK())
91                 return;
92
93         pimpl_->timestamp_ = finfo.getModificationTime();
94         pimpl_->checksum_ = lyx::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 lyx::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(string 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         FileInfo finfo(filename_);
156         if (!finfo.isOK()) {
157                 changed = timestamp_ || checksum_;
158                 timestamp_ = 0;
159                 checksum_ = 0;
160
161         } else {
162                 time_t const new_timestamp = finfo.getModificationTime();
163
164                 if (new_timestamp != timestamp_) {
165                         timestamp_ = new_timestamp;
166
167                         unsigned long const new_checksum = lyx::sum(filename_);
168                         if (new_checksum != checksum_) {
169                                 checksum_ = new_checksum;
170                                 changed = true;
171                         }
172                 }
173         }
174
175         timer_.start();
176         if (changed)
177                 fileChanged_();
178 }