Line data Source code
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 :
12 : #include "svtools/filechangedchecker.hxx"
13 :
14 0 : FileChangedChecker::FileChangedChecker(const OUString& rFilename, const ::boost::function0<void>& rCallback) :
15 : mTimer(),
16 : mFileName(rFilename),
17 : mLastModTime(),
18 0 : mpCallback(rCallback)
19 : {
20 : // Get the curren last file modified Status
21 0 : getCurrentModTime(mLastModTime);
22 :
23 : // associate the callback function for the timer
24 0 : mTimer.SetTimeoutHdl(LINK(this, FileChangedChecker, TimerHandler));
25 :
26 : //start the timer
27 0 : resetTimer();
28 0 : }
29 :
30 0 : void FileChangedChecker::resetTimer()
31 : {
32 : //Start the timer if its not active
33 0 : if(!mTimer.IsActive())
34 0 : mTimer.Start();
35 :
36 : // Set a timeout of 3 seconds
37 0 : mTimer.SetTimeout(3000);
38 0 : }
39 :
40 0 : bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
41 : {
42 : // Need a Directory item to fetch file status
43 0 : osl::DirectoryItem aItem;
44 0 : osl::DirectoryItem::get(mFileName, aItem);
45 :
46 : // Retrieve the status - we are only interested in last File
47 : // Modified time
48 0 : osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
49 0 : if( osl::FileBase::E_None != aItem.getFileStatus(aStatus) )
50 0 : return false;
51 :
52 0 : o_rValue = aStatus.getModifyTime();
53 0 : return true;
54 : }
55 :
56 0 : bool FileChangedChecker::hasFileChanged()
57 : {
58 : // Get the current file Status
59 0 : TimeValue newTime={0,0};
60 0 : if( !getCurrentModTime(newTime) )
61 0 : return true; // well. hard to answer correctly here ...
62 :
63 : // Check if the seconds time stamp has any difference
64 : // If so, then our file has changed meanwhile
65 0 : if( newTime.Seconds != mLastModTime.Seconds ||
66 0 : newTime.Nanosec != mLastModTime.Nanosec )
67 : {
68 : // Since the file has changed, set the new status as the file status and
69 : // return True
70 0 : mLastModTime = newTime ;
71 :
72 0 : return true;
73 : }
74 : else
75 0 : return false;
76 : }
77 :
78 0 : IMPL_LINK_NOARG(FileChangedChecker, TimerHandler)
79 : {
80 : // If the file has changed, then update the graphic in the doc
81 : OSL_TRACE("Timeout Called");
82 0 : if(hasFileChanged())
83 : {
84 : OSL_TRACE("File modified");
85 0 : mpCallback();
86 : }
87 :
88 : // Reset the timer in any case
89 0 : resetTimer();
90 0 : return 0;
91 : }
92 :
93 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|