summaryrefslogtreecommitdiffstats
path: root/svtools/source/misc/filechangedchecker.cxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /svtools/source/misc/filechangedchecker.cxx
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'svtools/source/misc/filechangedchecker.cxx')
-rw-r--r--svtools/source/misc/filechangedchecker.cxx114
1 files changed, 114 insertions, 0 deletions
diff --git a/svtools/source/misc/filechangedchecker.cxx b/svtools/source/misc/filechangedchecker.cxx
new file mode 100644
index 000000000..dd7cf22b9
--- /dev/null
+++ b/svtools/source/misc/filechangedchecker.cxx
@@ -0,0 +1,114 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include <sal/config.h>
+#include <sal/log.hxx>
+#include <osl/file.hxx>
+
+#include <svtools/filechangedchecker.hxx>
+#include <vcl/timer.hxx>
+
+FileChangedChecker::FileChangedChecker(const OUString& rFilename,
+ const ::std::function<void ()>& rCallback)
+ : mTimer("SVTools FileChangedChecker Timer")
+ , mFileName(rFilename)
+ , mLastModTime()
+ , mpCallback(rCallback)
+{
+ // Get the current last file modified Status
+ getCurrentModTime(mLastModTime);
+
+ // associate the callback function for the Timer
+ mTimer.SetInvokeHandler(LINK(this, FileChangedChecker, TimerHandler));
+
+ // set timer interval
+ mTimer.SetTimeout(100);
+
+ // start the timer
+ resetTimer();
+}
+
+FileChangedChecker::FileChangedChecker(const OUString& rFilename)
+ : mTimer("")
+ , mFileName(rFilename)
+ , mLastModTime()
+ , mpCallback(nullptr)
+{
+ // Get the current last file modified Status
+ getCurrentModTime(mLastModTime);
+}
+
+void FileChangedChecker::resetTimer()
+{
+ if (mpCallback == nullptr)
+ return;
+
+ // Start the Idle if it's not active
+ if (!mTimer.IsActive())
+ mTimer.Start();
+
+ // Set lowest Priority
+ mTimer.SetPriority(TaskPriority::LOWEST);
+}
+
+bool FileChangedChecker::getCurrentModTime(TimeValue& o_rValue) const
+{
+ // Need a Directory item to fetch file status
+ osl::DirectoryItem aItem;
+ if (osl::FileBase::E_None != osl::DirectoryItem::get(mFileName, aItem))
+ return false;
+
+ // Retrieve the status - we are only interested in last File
+ // Modified time
+ osl::FileStatus aStatus( osl_FileStatus_Mask_ModifyTime );
+ if (osl::FileBase::E_None != aItem.getFileStatus(aStatus))
+ return false;
+
+ o_rValue = aStatus.getModifyTime();
+ return true;
+}
+
+bool FileChangedChecker::hasFileChanged(bool bUpdate)
+{
+ // Get the current file Status
+ TimeValue newTime={0,0};
+ if( !getCurrentModTime(newTime) )
+ return true; // well. hard to answer correctly here ...
+
+ // Check if the seconds time stamp has any difference
+ // If so, then our file has changed meanwhile
+ if( newTime.Seconds != mLastModTime.Seconds ||
+ newTime.Nanosec != mLastModTime.Nanosec )
+ {
+ // Since the file has changed, set the new status as the file status and
+ // return True
+ if(bUpdate)
+ mLastModTime = newTime ;
+
+ return true;
+ }
+ else
+ return false;
+}
+
+IMPL_LINK_NOARG(FileChangedChecker, TimerHandler, Timer *, void)
+{
+ // If the file has changed, then update the graphic in the doc
+ SAL_INFO("svtools", "Timeout Called");
+ if(hasFileChanged())
+ {
+ SAL_INFO("svtools", "File modified");
+ mpCallback();
+ }
+
+ // Reset the Timer in any case
+ resetTimer();
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */