1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
11 #include <sal/log.hxx>
12 #include <osl/file.hxx>
14 #include <svtools/filechangedchecker.hxx>
16 #include <vcl/timer.hxx>
18 FileChangedChecker::FileChangedChecker(OUString aFilename
,
19 ::std::function
<void ()> aCallback
)
20 : mTimer("SVTools FileChangedChecker Timer")
21 , mFileName(std::move(aFilename
))
23 , mpCallback(std::move(aCallback
))
25 // Get the current last file modified Status
26 getCurrentModTime(mLastModTime
);
28 // associate the callback function for the Timer
29 mTimer
.SetInvokeHandler(LINK(this, FileChangedChecker
, TimerHandler
));
32 mTimer
.SetTimeout(100);
38 FileChangedChecker::FileChangedChecker(OUString aFilename
)
40 , mFileName(std::move(aFilename
))
44 // Get the current last file modified Status
45 getCurrentModTime(mLastModTime
);
48 void FileChangedChecker::resetTimer()
50 if (mpCallback
== nullptr)
53 // Start the Idle if it's not active
54 if (!mTimer
.IsActive())
57 // Set lowest Priority
58 mTimer
.SetPriority(TaskPriority::LOWEST
);
61 bool FileChangedChecker::getCurrentModTime(TimeValue
& o_rValue
) const
63 // Need a Directory item to fetch file status
64 osl::DirectoryItem aItem
;
65 if (osl::FileBase::E_None
!= osl::DirectoryItem::get(mFileName
, aItem
))
68 // Retrieve the status - we are only interested in last File
70 osl::FileStatus
aStatus( osl_FileStatus_Mask_ModifyTime
);
71 if (osl::FileBase::E_None
!= aItem
.getFileStatus(aStatus
))
74 o_rValue
= aStatus
.getModifyTime();
78 bool FileChangedChecker::hasFileChanged(bool bUpdate
)
80 // Get the current file Status
81 TimeValue newTime
={0,0};
82 if( !getCurrentModTime(newTime
) )
83 return true; // well. hard to answer correctly here ...
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
)
90 // Since the file has changed, set the new status as the file status and
93 mLastModTime
= newTime
;
101 IMPL_LINK_NOARG(FileChangedChecker
, TimerHandler
, Timer
*, void)
103 // If the file has changed, then update the graphic in the doc
104 SAL_INFO("svtools", "Timeout Called");
107 SAL_INFO("svtools", "File modified");
111 // Reset the Timer in any case
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */