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