nss: upgrade to release 3.73
[LibreOffice.git] / svtools / source / misc / filechangedchecker.cxx
blob8536eb0bda57bae84982d93f5cdb491cb62fe435
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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)
21 , mLastModTime()
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));
30 // set timer interval
31 mTimer.SetTimeout(100);
33 // start the timer
34 resetTimer();
37 void FileChangedChecker::resetTimer()
39 // Start the Idle if it's not active
40 if(!mTimer.IsActive())
41 mTimer.Start();
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))
52 return false;
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;
60 o_rValue = aStatus.getModifyTime();
61 return true;
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
77 // return True
78 mLastModTime = newTime ;
80 return true;
82 else
83 return false;
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");
90 if(hasFileChanged())
92 SAL_INFO("svtools", "File modified");
93 mpCallback();
96 // Reset the Timer in any case
97 resetTimer();
100 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */