FileMonitor.h
author František Kučera <franta-hg@frantovo.cz>
Wed, 27 Dec 2023 23:25:06 +0100
branchv_0
changeset 33 4620bba4fa40
parent 29 dc3c102e1264
permissions -rw-r--r--
import functions from the private prototype: goPage(), goHome(), goEnd(), goPageMouse()

/**
 * OHP3D
 * Copyright © 2023 František Kučera (Frantovo.cz, GlobalCode.info)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <string>
#include <memory>

class FileMonitor;

class WatchedFile {
public:
	WatchedFile();
	virtual ~WatchedFile();
	std::string getFileName() const;
	uint32_t getMask() const;
	FileMonitor getFileMonitor() const;
private:
	class Impl;
	std::shared_ptr<Impl> impl;
	friend FileMonitor;
};

class FileEvent {
public:
	std::string fileName;
	uint32_t mask;
	// TODO: more information, see: man 7 inotify
};

class FileMonitor {
public:
	FileMonitor();
	virtual ~FileMonitor();
	/**
	 * @return file descriptor that can be monitored by e.g. epoll
	 */
	int getFD() const;
	/**
	 * @param fileName file to be monitored
	 * @param mask IN_CLOSE_WRITE and other IN_*
	 * @return monitoring will continue until any copy of this object exists
	 */
	WatchedFile watch(const std::string& fileName);
	WatchedFile watch(const std::string& fileName, uint32_t mask);
	/**
	 * @param event object to be filled with information
	 * @return true if event was filled, call this method until false returned
	 */
	bool readEvent(FileEvent& event);
private:
	class Impl;
	std::shared_ptr<Impl> impl;
	FileMonitor(std::shared_ptr<Impl> impl);
	friend WatchedFile;
};