1 /*************************************************************************** 2 Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org> 3 ***************************************************************************/ 4 5 /*************************************************************************** 6 * * 7 * This program is free software; you can redistribute it and/or * 8 * modify it under the terms of the GNU General Public License as * 9 * published by the Free Software Foundation; either version 2 of * 10 * the License or (at your option) version 3 or any later version * 11 * accepted by the membership of KDE e.V. (or its successor approved * 12 * by the membership of KDE e.V.), which shall act as a proxy * 13 * defined in Section 14 of version 3 of the license. * 14 * * 15 * This program is distributed in the hope that it will be useful, * 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 18 * GNU General Public License for more details. * 19 * * 20 * You should have received a copy of the GNU General Public License * 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. * 22 * * 23 ***************************************************************************/ 24 25 #ifndef TELLICO_FILEHANDLER_H 26 #define TELLICO_FILEHANDLER_H 27 28 #include <QString> 29 #include <QByteArray> 30 31 class QUrl; 32 33 namespace KIO { 34 class Job; 35 } 36 37 class QDomDocument; 38 class QIODevice; 39 class QSaveFile; 40 41 namespace Tellico { 42 class ImageFactory; 43 class ImageDirectory; 44 namespace Data { 45 class Image; 46 } 47 48 /** 49 * The FileHandler class contains some utility functions for reading files. 50 * 51 * @author Robby Stephenson 52 */ 53 class FileHandler { 54 55 friend class ImageFactory; 56 friend class ImageDirectory; 57 58 public: 59 /** 60 * An internal class to handle KIO stuff. Exposed so a FileRef pointer 61 * can be returned from FileHandler. 62 */ 63 class FileRef { 64 public: 65 bool open(bool quiet=false); file()66 QIODevice* file() const { return m_device; } fileName()67 const QString& fileName() const { return m_filename; } isValid()68 bool isValid() const { return m_isValid; } 69 ~FileRef(); 70 71 private: 72 friend class FileHandler; 73 explicit FileRef(const QUrl& url, bool quiet=false); 74 QIODevice* m_device; 75 QString m_filename; 76 bool m_isValid; 77 }; 78 friend class FileRef; 79 80 /** 81 * Creates a FileRef for a given url. It's not meant to be used by methods in the class, 82 * Rather by a class wanting direct access to a file. The caller takes ownership of the pointer. 83 * 84 * @param url The url 85 * @param quiet Whether error messages should be shown 86 * @return The fileref 87 */ 88 static FileRef* fileRef(const QUrl& url, bool quiet=false); 89 /** 90 * Read contents of a file into a string. 91 * 92 * @param url The URL of the file 93 * @param quiet whether the importer should report errors or not 94 * @param useUTF8 Whether the file should be read as UTF8 or use user locale 95 * @return A string containing the contents of a file 96 */ 97 static QString readTextFile(const QUrl& url, bool quiet=false, bool useUTF8=false); 98 /** 99 * Read contents of an XML file into a string, checking for encoding. 100 * 101 * @param url The URL of the file 102 * @param quiet whether the importer should report errors or not 103 * @return A string containing the contents of a file 104 */ 105 static QString readXMLFile(const QUrl& url, bool quiet=false); 106 /** 107 * Read contents of an XML file into a QDomDocument. 108 * 109 * @param url The URL of the file 110 * @param processNamespace Whether to process the namespace of the XML file 111 * @param quiet Whether error messages should be shown 112 * @return A QDomDocument containing the contents of a file 113 */ 114 static QDomDocument readXMLDocument(const QUrl& url, bool processNamespace, bool quiet=false); 115 /** 116 * Read contents of a data file into a QByteArray. 117 * 118 * @param url The URL of the file 119 * @param quiet Whether error messages should be shown 120 * @return A QByteArray of the file's contents 121 */ 122 static QByteArray readDataFile(const QUrl& url, bool quiet=false); 123 /** 124 * Writes the contents of a string to a url. If the file already exists, a "~" is appended 125 * and the existing file is moved. If the file is remote, a temporary file is written and 126 * then uploaded. 127 * 128 * @param url The url 129 * @param text The text 130 * @param encodeUTF8 Whether to use UTF-8 encoding, or Locale 131 * @param force Whether to force the write 132 * @return A boolean indicating success 133 */ 134 static bool writeTextURL(const QUrl& url, const QString& text, bool encodeUTF8, bool force=false, bool quiet=false); 135 /** 136 * Writes data to a url. If the file already exists, a "~" is appended 137 * and the existing file is moved. If the file is remote, a temporary file is written and 138 * then uploaded. 139 * 140 * @param url The url 141 * @param data The data 142 * @param force Whether to force the write 143 * @return A boolean indicating success 144 */ 145 static bool writeDataURL(const QUrl& url, const QByteArray& data, bool force=false, bool quiet=false); 146 /** 147 * Checks to see if a URL exists already, and if so, queries the user. 148 * 149 * @param url The target URL 150 * @return True if it is ok to continue, false otherwise. 151 */ 152 static bool queryExists(const QUrl& url); 153 /** 154 * Write a backup file with '~' extension 155 * 156 * Returns true on success 157 */ 158 static bool writeBackupFile(const QUrl& url); 159 160 private: 161 /** 162 * Writes the contents of a string to a file. 163 * 164 * @param file The file object 165 * @param text The string 166 * @param encodeUTF8 Whether to use UTF-8 encoding, or Locale 167 * @return A boolean indicating success 168 */ 169 static bool writeTextFile(QSaveFile& file, const QString& text, bool encodeUTF8); 170 /** 171 * Writes data to a file. 172 * 173 * @param file The file object 174 * @param data The data 175 * @return A boolean indicating success 176 */ 177 static bool writeDataFile(QSaveFile& file, const QByteArray& data); 178 }; 179 180 } // end namespace 181 #endif 182