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>
15 #include <vcl/timer.hxx>
17 FileChangedChecker::FileChangedChecker(const OUString
& rFilename
,
18 const ::std::function
<void ()>& rCallback
)
19 : mTimer("SVTools FileChangedChecker Timer")
20 , mFileName(rFilename
)
22 , mpCallback(rCallback
)
24 // Get the curren last file modified Status
25 getCurrentModTime(mLastModTime
);
27 // associate the callback function for the Timer
28 mTimer
.SetInvokeHandler(LINK(this, FileChangedChecker
, TimerHandler
));
31 mTimer
.SetTimeout(100);
37 void FileChangedChecker::resetTimer()
39 // Start the Idle if it's not active
40 if(!mTimer
.IsActive())
43 // Set lowest Priority
44 mTimer
.SetPriority(TaskPriority::LOWEST
);
47 bool FileChangedChecker::getCurrentModTime(TimeValue
& o_rValue
) const
49 // Need a Directory item to fetch file status
50 osl::DirectoryItem aItem
;
51 if (osl::FileBase::E_None
!= osl::DirectoryItem::get(mFileName
, aItem
))
54 // Retrieve the status - we are only interested in last File
56 osl::FileStatus
aStatus( osl_FileStatus_Mask_ModifyTime
);
57 if (osl::FileBase::E_None
!= aItem
.getFileStatus(aStatus
))
60 o_rValue
= aStatus
.getModifyTime();
64 bool FileChangedChecker::hasFileChanged()
66 // Get the current file Status
67 TimeValue newTime
={0,0};
68 if( !getCurrentModTime(newTime
) )
69 return true; // well. hard to answer correctly here ...
71 // Check if the seconds time stamp has any difference
72 // If so, then our file has changed meanwhile
73 if( newTime
.Seconds
!= mLastModTime
.Seconds
||
74 newTime
.Nanosec
!= mLastModTime
.Nanosec
)
76 // Since the file has changed, set the new status as the file status and
78 mLastModTime
= newTime
;
86 IMPL_LINK_NOARG(FileChangedChecker
, TimerHandler
, Timer
*, void)
88 // If the file has changed, then update the graphic in the doc
89 SAL_INFO("svtools", "Timeout Called");
92 SAL_INFO("svtools", "File modified");
96 // Reset the Timer in any case
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */