File indexing completed on 2024-05-12 05:09:25
0001 /*************************************************************************** 0002 Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org> 0003 ***************************************************************************/ 0004 0005 /*************************************************************************** 0006 * * 0007 * This program is free software; you can redistribute it and/or * 0008 * modify it under the terms of the GNU General Public License as * 0009 * published by the Free Software Foundation; either version 2 of * 0010 * the License or (at your option) version 3 or any later version * 0011 * accepted by the membership of KDE e.V. (or its successor approved * 0012 * by the membership of KDE e.V.), which shall act as a proxy * 0013 * defined in Section 14 of version 3 of the license. * 0014 * * 0015 * This program is distributed in the hope that it will be useful, * 0016 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0018 * GNU General Public License for more details. * 0019 * * 0020 * You should have received a copy of the GNU General Public License * 0021 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 0022 * * 0023 ***************************************************************************/ 0024 0025 #ifndef TELLICO_FILEHANDLER_H 0026 #define TELLICO_FILEHANDLER_H 0027 0028 #include <QString> 0029 #include <QByteArray> 0030 0031 class QUrl; 0032 0033 namespace KIO { 0034 class Job; 0035 } 0036 0037 class QDomDocument; 0038 class QIODevice; 0039 class QSaveFile; 0040 0041 namespace Tellico { 0042 class ImageFactory; 0043 class ImageDirectory; 0044 namespace Data { 0045 class Image; 0046 } 0047 0048 /** 0049 * The FileHandler class contains some utility functions for reading files. 0050 * 0051 * @author Robby Stephenson 0052 */ 0053 class FileHandler { 0054 0055 friend class ImageFactory; 0056 friend class ImageDirectory; 0057 0058 public: 0059 /** 0060 * An internal class to handle KIO stuff. Exposed so a FileRef pointer 0061 * can be returned from FileHandler. 0062 */ 0063 class FileRef { 0064 public: 0065 bool open(bool quiet=false); 0066 QIODevice* file() const { return m_device; } 0067 const QString& fileName() const { return m_filename; } 0068 bool isValid() const { return m_isValid; } 0069 ~FileRef(); 0070 0071 private: 0072 friend class FileHandler; 0073 explicit FileRef(const QUrl& url, bool quiet=false); 0074 QIODevice* m_device; 0075 QString m_filename; 0076 bool m_isValid; 0077 }; 0078 friend class FileRef; 0079 0080 /** 0081 * Creates a FileRef for a given url. It's not meant to be used by methods in the class, 0082 * Rather by a class wanting direct access to a file. The caller takes ownership of the pointer. 0083 * 0084 * @param url The url 0085 * @param quiet Whether error messages should be shown 0086 * @return The fileref 0087 */ 0088 static FileRef* fileRef(const QUrl& url, bool quiet=false); 0089 /** 0090 * Read contents of a file into a string. 0091 * 0092 * @param url The URL of the file 0093 * @param quiet whether the importer should report errors or not 0094 * @param useUTF8 Whether the file should be read as UTF8 or use user locale 0095 * @return A string containing the contents of a file 0096 */ 0097 static QString readTextFile(const QUrl& url, bool quiet=false, bool useUTF8=false); 0098 /** 0099 * Read contents of an XML file into a string, checking for encoding. 0100 * 0101 * @param url The URL of the file 0102 * @param quiet whether the importer should report errors or not 0103 * @return A string containing the contents of a file 0104 */ 0105 static QString readXMLFile(const QUrl& url, bool quiet=false); 0106 /** 0107 * Read contents of an XML file into a QDomDocument. 0108 * 0109 * @param url The URL of the file 0110 * @param processNamespace Whether to process the namespace of the XML file 0111 * @param quiet Whether error messages should be shown 0112 * @return A QDomDocument containing the contents of a file 0113 */ 0114 static QDomDocument readXMLDocument(const QUrl& url, bool processNamespace, bool quiet=false); 0115 /** 0116 * Read contents of a data file into a QByteArray. 0117 * 0118 * @param url The URL of the file 0119 * @param quiet Whether error messages should be shown 0120 * @return A QByteArray of the file's contents 0121 */ 0122 static QByteArray readDataFile(const QUrl& url, bool quiet=false); 0123 /** 0124 * Writes the contents of a string to a url. If the file already exists, a "~" is appended 0125 * and the existing file is moved. If the file is remote, a temporary file is written and 0126 * then uploaded. 0127 * 0128 * @param url The url 0129 * @param text The text 0130 * @param encodeUTF8 Whether to use UTF-8 encoding, or Locale 0131 * @param force Whether to force the write 0132 * @return A boolean indicating success 0133 */ 0134 static bool writeTextURL(const QUrl& url, const QString& text, bool encodeUTF8, bool force=false, bool quiet=false); 0135 /** 0136 * Writes data to a url. If the file already exists, a "~" is appended 0137 * and the existing file is moved. If the file is remote, a temporary file is written and 0138 * then uploaded. 0139 * 0140 * @param url The url 0141 * @param data The data 0142 * @param force Whether to force the write 0143 * @return A boolean indicating success 0144 */ 0145 static bool writeDataURL(const QUrl& url, const QByteArray& data, bool force=false, bool quiet=false); 0146 /** 0147 * Checks to see if a URL exists already, and if so, queries the user. 0148 * 0149 * @param url The target URL 0150 * @return True if it is ok to continue, false otherwise. 0151 */ 0152 static bool queryExists(const QUrl& url); 0153 /** 0154 * Write a backup file with '~' extension 0155 * 0156 * Returns true on success 0157 */ 0158 static bool writeBackupFile(const QUrl& url); 0159 0160 private: 0161 /** 0162 * Writes the contents of a string to a file. 0163 * 0164 * @param file The file object 0165 * @param text The string 0166 * @param encodeUTF8 Whether to use UTF-8 encoding, or Locale 0167 * @return A boolean indicating success 0168 */ 0169 static bool writeTextFile(QSaveFile& file, const QString& text, bool encodeUTF8); 0170 /** 0171 * Writes data to a file. 0172 * 0173 * @param file The file object 0174 * @param data The data 0175 * @return A boolean indicating success 0176 */ 0177 static bool writeDataFile(QSaveFile& file, const QByteArray& data); 0178 }; 0179 0180 } // end namespace 0181 #endif