Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Initial Developer of the Original Code is
16 : * [ Surendran Mahendran <surenspost@gmail.com>]
17 : *
18 : * Alternatively, the contents of this file may be used under the terms of
19 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
20 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
21 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
22 : * instead of those above.
23 : */
24 :
25 : #include "sal/config.h"
26 :
27 : #include "svtools/filechangedchecker.hxx"
28 :
29 0 : FileChangedChecker::FileChangedChecker(const rtl::OUString& rFilename, const ::boost::function0<void>& rCallback) :
30 : mTimer(),
31 : mFileName(rFilename),
32 : mLastModTime(),
33 0 : mpCallback(rCallback)
34 : {
35 : // Get the curren last file modified Status
36 0 : getCurrentModTime(mLastModTime);
37 :
38 : // associate the callback function for the timer
39 0 : mTimer.SetTimeoutHdl(LINK(this, FileChangedChecker, TimerHandler));
40 :
41 : //start the timer
42 0 : resetTimer();
43 0 : }
44 :
45 0 : void FileChangedChecker::resetTimer()
46 : {
47 : //Start the timer if its not active
48 0 : if(!mTimer.IsActive())
49 0 : mTimer.Start();
50 :
51 : // Set a timeout of 3 seconds
52 0 : mTimer.SetTimeout(3000);
53 0 : }
54 :
55 0 : bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
56 : {
57 : // Need a Directory item to fetch file status
58 0 : osl::DirectoryItem aItem;
59 0 : osl::DirectoryItem::get(mFileName, aItem);
60 :
61 : // Retrieve the status - we are only interested in last File
62 : // Modified time
63 0 : osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
64 0 : if( osl::FileBase::E_None != aItem.getFileStatus(aStatus) )
65 0 : return false;
66 :
67 0 : o_rValue = aStatus.getModifyTime();
68 0 : return true;
69 : }
70 :
71 0 : bool FileChangedChecker::hasFileChanged()
72 : {
73 : // Get the current file Status
74 0 : TimeValue newTime={0,0};
75 0 : if( !getCurrentModTime(newTime) )
76 0 : return true; // well. hard to answer correctly here ...
77 :
78 : // Check if the seconds time stamp has any difference
79 : // If so, then our file has changed meanwhile
80 0 : if( newTime.Seconds != mLastModTime.Seconds ||
81 : newTime.Nanosec != mLastModTime.Nanosec )
82 : {
83 : // Since the file has changed, set the new status as the file status and
84 : // return True
85 0 : mLastModTime = newTime ;
86 :
87 0 : return true;
88 : }
89 : else
90 0 : return false;
91 : }
92 :
93 0 : IMPL_LINK_NOARG(FileChangedChecker, TimerHandler)
94 : {
95 : // If the file has changed, then update the graphic in the doc
96 : OSL_TRACE("Timeout Called");
97 0 : if(hasFileChanged())
98 : {
99 : OSL_TRACE("File modified");
100 0 : mpCallback();
101 : }
102 :
103 : // Reset the timer in any case
104 0 : resetTimer();
105 0 : return 0;
106 : }
107 :
108 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|