Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svtools / source / misc / filechangedchecker.cxx
blob7ffc5cae8364831390ea8cddb6bf652b180ba20c
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 <utility>
16 #include <vcl/timer.hxx>
18 FileChangedChecker::FileChangedChecker(OUString aFilename,
19 ::std::function<void ()> aCallback)
20 : mTimer("SVTools FileChangedChecker Timer")
21 , mFileName(std::move(aFilename))
22 , mLastModTime()
23 , mpCallback(std::move(aCallback))
25 // Get the current last file modified Status
26 getCurrentModTime(mLastModTime);
28 // associate the callback function for the Timer
29 mTimer.SetInvokeHandler(LINK(this, FileChangedChecker, TimerHandler));
31 // set timer interval
32 mTimer.SetTimeout(100);
34 // start the timer
35 resetTimer();
38 FileChangedChecker::FileChangedChecker(OUString aFilename)
39 : mTimer("")
40 , mFileName(std::move(aFilename))
41 , mLastModTime()
42 , mpCallback(nullptr)
44 // Get the current last file modified Status
45 getCurrentModTime(mLastModTime);
48 void FileChangedChecker::resetTimer()
50 if (mpCallback == nullptr)
51 return;
53 // Start the Idle if it's not active
54 if (!mTimer.IsActive())
55 mTimer.Start();
57 // Set lowest Priority
58 mTimer.SetPriority(TaskPriority::LOWEST);
61 bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
63 // Need a Directory item to fetch file status
64 osl::DirectoryItem aItem;
65 if (osl::FileBase::E_None != osl::DirectoryItem::get(mFileName, aItem))
66 return false;
68 // Retrieve the status - we are only interested in last File
69 // Modified time
70 osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
71 if (osl::FileBase::E_None != aItem.getFileStatus(aStatus))
72 return false;
74 o_rValue = aStatus.getModifyTime();
75 return true;
78 bool FileChangedChecker::hasFileChanged(bool bUpdate)
80 // Get the current file Status
81 TimeValue newTime={0,0};
82 if( !getCurrentModTime(newTime) )
83 return true; // well. hard to answer correctly here ...
85 // Check if the seconds time stamp has any difference
86 // If so, then our file has changed meanwhile
87 if( newTime.Seconds != mLastModTime.Seconds ||
88 newTime.Nanosec != mLastModTime.Nanosec )
90 // Since the file has changed, set the new status as the file status and
91 // return True
92 if(bUpdate)
93 mLastModTime = newTime ;
95 return true;
97 else
98 return false;
101 IMPL_LINK_NOARG(FileChangedChecker, TimerHandler, Timer *, void)
103 // If the file has changed, then update the graphic in the doc
104 SAL_INFO("svtools", "Timeout Called");
105 if(hasFileChanged())
107 SAL_INFO("svtools", "File modified");
108 mpCallback();
111 // Reset the Timer in any case
112 resetTimer();
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */