Atlas-Launcher/source/javascript/module/file_system/object/PathJsObject.hpp

114 lines
2.9 KiB
C++

#pragma once
#include <QObject>
#include <QString>
#include <QList>
#include <QDir>
#include <QSharedPointer>
#include "javascript/module/file_system/object/FileJsObject.hpp"
#include "javascript/engine/AtlasJsEngine.hpp"
/**
* Represent a file system Path as a JavaScript object
* Similar to the pathlib library in Python
*/
class PathJsObject : public QObject {
Q_OBJECT
public:
explicit PathJsObject(AtlasJsEngine* engine, const std::filesystem::path& virtualPath);
// browse
/**
* Get the parent directory
* @return the parent directory
*/
Q_INVOKABLE QJSValue getParent() const;
/**
* Get the child paths
* @return the child paths
*/
Q_INVOKABLE QList<QJSValue> getChildrens(bool recursive = false, QString pattern = "*") const;
// type
Q_INVOKABLE bool exists();
/**
* is the current path leading to a file
* @return is the current path leading to a file
*/
Q_INVOKABLE bool isFile() const;
/**
* is the current path leading to a directory
* @return is the current path leading to a directory
*/
Q_INVOKABLE bool isDirectory() const;
// operation
/**
* open the path as a file
* @return the opened file
*/
Q_INVOKABLE QJSValue open(const QString& mode, const QString& encoding = QStringLiteral("utf-8"));
Q_INVOKABLE void mkdir(bool parents);
Q_INVOKABLE void remove(bool recursive);
// component
/**
* get the full path to the file
* @return the full path to the file
*/
Q_INVOKABLE QString getPath() const;
/**
* get the name of the directory containing the file
* @return the name of the directory containing the file
*/
Q_INVOKABLE QString getDirname() const;
/**
* get the name of the file
* @return the name of the file
*/
Q_INVOKABLE QString getFilename() const;
/**
* get the name of the file without the suffix
* @return the name of the file without the suffix
*/
Q_INVOKABLE QString getBasename() const;
/**
* get the extension of the file
* @return the extension of the file
*/
Q_INVOKABLE QString getSuffix() const;
/**
* get the extensions of the file
* @return the extensions of the file
*/
Q_INVOKABLE QList<QString> getSuffixes() const;
/**
* get the segments of the path
* @return the segments of the path
*/
Q_INVOKABLE QList<QString> getSegments() const;
private:
/// the JavaScript engine
AtlasJsEngine* engine;
/// the virtual path
std::filesystem::path virtualPath;
/**
* Get the real path on the host file system of the path
* @return the real path on the host file system
* @throw rvfs::exception::FileNotFoundException if the path does not match with anything on the real file system
*/
std::filesystem::path getRealPath() const;
};