LibreOffice Module svtools (master) 1
filechangedchecker.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10#include <sal/config.h>
11#include <sal/log.hxx>
12#include <osl/file.hxx>
13
15#include <utility>
16#include <vcl/timer.hxx>
17
19 ::std::function<void ()> aCallback)
20 : mTimer("SVTools FileChangedChecker Timer")
21 , mFileName(std::move(aFilename))
22 , mLastModTime()
23 , mpCallback(std::move(aCallback))
24{
25 // Get the current last file modified Status
27
28 // associate the callback function for the Timer
29 mTimer.SetInvokeHandler(LINK(this, FileChangedChecker, TimerHandler));
30
31 // set timer interval
32 mTimer.SetTimeout(100);
33
34 // start the timer
35 resetTimer();
36}
37
39 : mTimer("")
40 , mFileName(std::move(aFilename))
41 , mLastModTime()
42 , mpCallback(nullptr)
43{
44 // Get the current last file modified Status
46}
47
49{
50 if (mpCallback == nullptr)
51 return;
52
53 // Start the Idle if it's not active
54 if (!mTimer.IsActive())
55 mTimer.Start();
56
57 // Set lowest Priority
58 mTimer.SetPriority(TaskPriority::LOWEST);
59}
60
61bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
62{
63 // Need a Directory item to fetch file status
64 osl::DirectoryItem aItem;
65 if (osl::FileBase::E_None != osl::DirectoryItem::get(mFileName, aItem))
66 return false;
67
68 // Retrieve the status - we are only interested in last File
69 // Modified time
70 osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
71 if (osl::FileBase::E_None != aItem.getFileStatus(aStatus))
72 return false;
73
74 o_rValue = aStatus.getModifyTime();
75 return true;
76}
77
79{
80 // Get the current file Status
81 TimeValue newTime={0,0};
82 if( !getCurrentModTime(newTime) )
83 return true; // well. hard to answer correctly here ...
84
85 // Check if the seconds time stamp has any difference
86 // If so, then our file has changed meanwhile
87 if( newTime.Seconds != mLastModTime.Seconds ||
88 newTime.Nanosec != mLastModTime.Nanosec )
89 {
90 // Since the file has changed, set the new status as the file status and
91 // return True
92 if(bUpdate)
93 mLastModTime = newTime ;
94
95 return true;
96 }
97 else
98 return false;
99}
100
102{
103 // If the file has changed, then update the graphic in the doc
104 SAL_INFO("svtools", "Timeout Called");
105 if(hasFileChanged())
106 {
107 SAL_INFO("svtools", "File modified");
108 mpCallback();
109 }
110
111 // Reset the Timer in any case
112 resetTimer();
113}
114
115/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Periodically checks if a file has been modified.
bool SVT_DLLPRIVATE getCurrentModTime(TimeValue &o_rValue) const
bool hasFileChanged(bool bUpdate=true)
FileChangedChecker(OUString aFilename, ::std::function< void()> aCallback)
::std::function< void()> mpCallback
bool IsActive() const
void SetPriority(TaskPriority ePriority)
void SetTimeout(sal_uInt64 nTimeoutMs)
void SetInvokeHandler(const Link< Timer *, void > &rLink)
virtual void Start(bool bStartTimer=true) override
IMPL_LINK_NOARG(FileChangedChecker, TimerHandler, Timer *, void)
#define SAL_INFO(area, stream)