File indexing completed on 2024-03-24 15:14:41
0001 /* GCompris - File.h 0002 * 0003 * SPDX-FileCopyrightText: 2014, 2015 Holger Kaelberer <holger.k@elberer.de> 0004 * 0005 * Authors: 0006 * Holger Kaelberer <holger.k@elberer.de> 0007 * 0008 * SPDX-License-Identifier: GPL-3.0-or-later 0009 */ 0010 0011 #ifndef FILE_H 0012 #define FILE_H 0013 0014 #include <QObject> 0015 #include <QString> 0016 0017 /** 0018 * @class File 0019 * @short A helper component for accessing local files from QML. 0020 * @ingroup components 0021 * 0022 */ 0023 class File : public QObject 0024 { 0025 Q_OBJECT 0026 0027 /** 0028 * Filename 0029 * 0030 * Accepted are absolute paths and URLs starting with the schemes 0031 * 'file://' and 'qrc://'. 0032 */ 0033 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) 0034 0035 public: 0036 /** 0037 * Constructor 0038 */ 0039 explicit File(QObject *parent = nullptr); 0040 0041 /** 0042 * Reads contents of a file. 0043 * 0044 * @param name [optional] Filename to read from. If omitted reads from 0045 * the file specified by the member name. 0046 * @returns Whole file contents. 0047 * @sa name 0048 */ 0049 Q_INVOKABLE QString read(const QString &name = QString()); 0050 0051 /** 0052 * Writes @p data to a file. 0053 * 0054 * @param data Text data to write. 0055 * @param name [optional] Filename to write to. If omitted writes to 0056 * the file specified by the member name. 0057 * @returns success of the operation. 0058 * @sa name 0059 */ 0060 Q_INVOKABLE bool write(const QString &data, const QString &name = QString()); 0061 0062 /** 0063 * Appends @p data to a file. 0064 * 0065 * @param data Text data to append. 0066 * @param name [optional] Filename to append to. If omitted writes to 0067 * the file specified by the member name. 0068 * @returns success of the operation. 0069 * @sa name 0070 */ 0071 Q_INVOKABLE bool append(const QString &data, const QString &name = QString()); 0072 /** 0073 * Checks whether file @p path exists. 0074 * 0075 * @param path Filename to check. 0076 * @returns @c true if @p path exists, @c false otherwise. 0077 */ 0078 Q_INVOKABLE static bool exists(const QString &path); 0079 0080 /** 0081 * Creates directory @p path. 0082 * 0083 * Creates also all parent directories necessary to create the directory. 0084 * 0085 * @param path Directory to create. 0086 * @returns success 0087 */ 0088 Q_INVOKABLE static bool mkpath(const QString &path); 0089 0090 /** 0091 * Deletes a file @p path. 0092 * 0093 * @param path file to delete. 0094 * @returns success 0095 */ 0096 Q_INVOKABLE static bool rmpath(const QString &path); 0097 0098 /// @cond INTERNAL_DOCS 0099 QString name() const; 0100 void setName(const QString &str); 0101 /// @endcond 0102 0103 Q_SIGNALS: 0104 /** 0105 * Emitted when the name changes. 0106 */ 0107 void nameChanged(); 0108 0109 /** 0110 * Emitted when an error occurs. 0111 * 0112 * @param msg Error message. 0113 */ 0114 void error(const QString &msg); 0115 0116 private: 0117 QString m_name; 0118 0119 static QString sanitizeUrl(const QString &url); 0120 }; 0121 0122 #endif