File indexing completed on 2024-04-21 14:43:49
0001 /* GCompris - File.cpp 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 #include "File.h" 0012 0013 #include <QFile> 0014 #include <QDir> 0015 #include <QString> 0016 #include <QTextStream> 0017 0018 File::File(QObject *parent) : 0019 QObject(parent) 0020 { 0021 } 0022 0023 QString File::name() const 0024 { 0025 return m_name; 0026 } 0027 0028 QString File::sanitizeUrl(const QString &url) 0029 { 0030 QString target(url); 0031 0032 // make sure we strip off invalid URL schemes: 0033 if (target.startsWith(QLatin1String("file://"))) 0034 target.remove(0, 7); 0035 else if (target.startsWith(QLatin1String("qrc:/"))) 0036 target.remove(0, 3); 0037 0038 return target; 0039 } 0040 0041 void File::setName(const QString &str) 0042 { 0043 QString target = sanitizeUrl(str); 0044 0045 if (target != m_name) { 0046 m_name = target; 0047 Q_EMIT nameChanged(); 0048 } 0049 } 0050 0051 QString File::read(const QString &name) 0052 { 0053 if (!name.isEmpty()) 0054 setName(name); 0055 0056 if (m_name.isEmpty()) { 0057 Q_EMIT error("source is empty"); 0058 return QString(); 0059 } 0060 0061 QFile file(m_name); 0062 QString fileContent; 0063 if (file.open(QIODevice::ReadOnly)) { 0064 QString line; 0065 QTextStream t(&file); 0066 /* Force utf-8 : for some languages, it seems to be loaded in other 0067 encoding even if the file is in utf-8 */ 0068 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0069 t.setEncoding(QStringConverter::Utf8); 0070 #else 0071 t.setCodec("UTF-8"); 0072 #endif 0073 do { 0074 line = t.readLine(); 0075 fileContent += line; 0076 } while (!line.isNull()); 0077 0078 file.close(); 0079 } 0080 else { 0081 Q_EMIT error("Unable to open the file"); 0082 return QString(); 0083 } 0084 return fileContent; 0085 } 0086 0087 bool File::write(const QString &data, const QString &name) 0088 { 0089 if (!name.isEmpty()) 0090 setName(name); 0091 0092 if (m_name.isEmpty()) { 0093 Q_EMIT error("source is empty"); 0094 return false; 0095 } 0096 0097 QFile file(m_name); 0098 if (!file.open(QFile::WriteOnly | QFile::Truncate)) { 0099 Q_EMIT error("could not open file " + m_name); 0100 return false; 0101 } 0102 0103 QTextStream out(&file); 0104 /* Force utf-8 : needed at least for Windows */ 0105 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0106 out.setEncoding(QStringConverter::Utf8); 0107 #else 0108 out.setCodec("UTF-8"); 0109 #endif 0110 out << data; 0111 0112 file.close(); 0113 0114 return true; 0115 } 0116 0117 bool File::append(const QString &data, const QString &name) 0118 { 0119 if (!name.isEmpty()) 0120 setName(name); 0121 0122 if (m_name.isEmpty()) { 0123 Q_EMIT error("source is empty"); 0124 return false; 0125 } 0126 0127 QFile file(m_name); 0128 if (!file.open(QFile::WriteOnly | QFile::Append)) { 0129 Q_EMIT error("could not open file " + m_name); 0130 return false; 0131 } 0132 0133 QTextStream out(&file); 0134 /* Force utf-8 : needed at least for Windows */ 0135 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0136 out.setEncoding(QStringConverter::Utf8); 0137 #else 0138 out.setCodec("UTF-8"); 0139 #endif 0140 0141 out << data; 0142 0143 file.close(); 0144 0145 return true; 0146 } 0147 0148 bool File::exists(const QString &path) 0149 { 0150 return QFile::exists(sanitizeUrl(path)); 0151 } 0152 0153 bool File::mkpath(const QString &path) 0154 { 0155 QDir dir; 0156 return dir.mkpath(dir.filePath(sanitizeUrl(path))); 0157 } 0158 0159 bool File::rmpath(const QString &path) 0160 { 0161 return QFile::remove(sanitizeUrl(path)); 0162 } 0163 0164 #include "moc_File.cpp"