diff options
Diffstat (limited to 'src/FbTk/AutoReloadHelper.cc')
-rw-r--r-- | src/FbTk/AutoReloadHelper.cc | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/FbTk/AutoReloadHelper.cc b/src/FbTk/AutoReloadHelper.cc new file mode 100644 index 0000000..e3c1d59 --- /dev/null +++ b/src/FbTk/AutoReloadHelper.cc | |||
@@ -0,0 +1,66 @@ | |||
1 | // AutoReloadHelper.cc | ||
2 | // Copyright (c) 2008 Fluxbox Team (fluxgen at fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "AutoReloadHelper.hh" | ||
23 | |||
24 | #include "FileUtil.hh" | ||
25 | #include "StringUtil.hh" | ||
26 | |||
27 | namespace FbTk { | ||
28 | |||
29 | void AutoReloadHelper::checkReload() { | ||
30 | if (!m_reload_cmd.get()) | ||
31 | return; | ||
32 | TimestampMap::const_iterator it = m_timestamps.begin(); | ||
33 | TimestampMap::const_iterator it_end = m_timestamps.end(); | ||
34 | for (; it != it_end; ++it) { | ||
35 | if (FileUtil::getLastStatusChangeTimestamp(it->first.c_str()) != | ||
36 | it->second) { | ||
37 | reload(); | ||
38 | return; | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | |||
43 | void AutoReloadHelper::setMainFile(std::string file) { | ||
44 | file = StringUtil::expandFilename(file); | ||
45 | if (file == m_main_file) | ||
46 | return; | ||
47 | m_main_file = file; | ||
48 | reload(); | ||
49 | } | ||
50 | |||
51 | void AutoReloadHelper::addFile(std::string file) { | ||
52 | if (file.empty()) | ||
53 | return; | ||
54 | file = StringUtil::expandFilename(file); | ||
55 | m_timestamps[file] = FileUtil::getLastStatusChangeTimestamp(file.c_str()); | ||
56 | } | ||
57 | |||
58 | void AutoReloadHelper::reload() { | ||
59 | if (!m_reload_cmd.get()) | ||
60 | return; | ||
61 | m_timestamps.clear(); | ||
62 | addFile(m_main_file); | ||
63 | m_reload_cmd->execute(); | ||
64 | } | ||
65 | |||
66 | } // end namespace FbTk | ||