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 
14 #include <svtools/filechangedchecker.hxx>
15 #include <vcl/timer.hxx>
16 
FileChangedChecker(const OUString & rFilename,const::std::function<void ()> & rCallback)17 FileChangedChecker::FileChangedChecker(const OUString& rFilename,
18         const ::std::function<void ()>& rCallback)
19     : mTimer("SVTools FileChangedChecker Timer")
20     , mFileName(rFilename)
21     , mLastModTime()
22     , mpCallback(rCallback)
23 {
24     // Get the curren last file modified Status
25     getCurrentModTime(mLastModTime);
26 
27     // associate the callback function for the Timer
28     mTimer.SetInvokeHandler(LINK(this, FileChangedChecker, TimerHandler));
29 
30     // set timer interval
31     mTimer.SetTimeout(100);
32 
33     // start the timer
34     resetTimer();
35 }
36 
resetTimer()37 void FileChangedChecker::resetTimer()
38 {
39     // Start the Idle if it's not active
40     if(!mTimer.IsActive())
41         mTimer.Start();
42 
43     // Set lowest Priority
44     mTimer.SetPriority(TaskPriority::LOWEST);
45 }
46 
getCurrentModTime(TimeValue & o_rValue) const47 bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
48 {
49     // Need a Directory item to fetch file status
50     osl::DirectoryItem aItem;
51     if (osl::FileBase::E_None != osl::DirectoryItem::get(mFileName, aItem))
52         return false;
53 
54     // Retrieve the status - we are only interested in last File
55     // Modified time
56     osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
57     if (osl::FileBase::E_None != aItem.getFileStatus(aStatus))
58         return false;
59 
60     o_rValue = aStatus.getModifyTime();
61     return true;
62 }
63 
hasFileChanged()64 bool FileChangedChecker::hasFileChanged()
65 {
66     // Get the current file Status
67     TimeValue newTime={0,0};
68     if( !getCurrentModTime(newTime) )
69         return true; // well. hard to answer correctly here ...
70 
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 )
75     {
76         // Since the file has changed, set the new status as the file status and
77         // return True
78         mLastModTime = newTime ;
79 
80         return true;
81     }
82     else
83         return false;
84 }
85 
IMPL_LINK_NOARG(FileChangedChecker,TimerHandler,Timer *,void)86 IMPL_LINK_NOARG(FileChangedChecker, TimerHandler, Timer *, void)
87 {
88     // If the file has changed, then update the graphic in the doc
89     SAL_INFO("svtools", "Timeout Called");
90     if(hasFileChanged())
91     {
92         SAL_INFO("svtools", "File modified");
93         mpCallback();
94     }
95 
96     // Reset the Timer in any case
97     resetTimer();
98 }
99 
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
101