kmail Library API Documentation

kfileio.cpp

00001 // kfileio.cpp 00002 // Author: Stefan Taferner <taferner@kde.org> 00003 00004 #ifdef HAVE_CONFIG_H 00005 #include <config.h> 00006 #endif 00007 00008 #include <kmessagebox.h> 00009 #include <kdebug.h> 00010 00011 #include <assert.h> 00012 #include <qdir.h> 00013 00014 #include <klocale.h> 00015 00016 00017 //----------------------------------------------------------------------------- 00018 static void msgDialog(const QString &msg) 00019 { 00020 KMessageBox::sorry(0, msg, i18n("File I/O Error")); 00021 } 00022 00023 00024 //----------------------------------------------------------------------------- 00025 QCString kFileToString(const QString &aFileName, bool aEnsureNL, bool aVerbose) 00026 { 00027 QCString result; 00028 QFileInfo info(aFileName); 00029 unsigned int readLen; 00030 unsigned int len = info.size(); 00031 QFile file(aFileName); 00032 00033 //assert(aFileName!=0); 00034 if( aFileName.isEmpty() ) 00035 return ""; 00036 00037 if (!info.exists()) 00038 { 00039 if (aVerbose) 00040 msgDialog(i18n("The specified file does not exist:\n%1").arg(aFileName)); 00041 return QCString(); 00042 } 00043 if (info.isDir()) 00044 { 00045 if (aVerbose) 00046 msgDialog(i18n("This is a folder and not a file:\n%1").arg(aFileName)); 00047 return QCString(); 00048 } 00049 if (!info.isReadable()) 00050 { 00051 if (aVerbose) 00052 msgDialog(i18n("You do not have read permissions " 00053 "to the file:\n%1").arg(aFileName)); 00054 return QCString(); 00055 } 00056 if (len <= 0) return QCString(); 00057 00058 if (!file.open(IO_Raw|IO_ReadOnly)) 00059 { 00060 if (aVerbose) switch(file.status()) 00061 { 00062 case IO_ReadError: 00063 msgDialog(i18n("Could not read file:\n%1").arg(aFileName)); 00064 break; 00065 case IO_OpenError: 00066 msgDialog(i18n("Could not open file:\n%1").arg(aFileName)); 00067 break; 00068 default: 00069 msgDialog(i18n("Error while reading file:\n%1").arg(aFileName)); 00070 } 00071 return QCString(); 00072 } 00073 00074 result.resize(len + (int)aEnsureNL + 1); 00075 readLen = file.readBlock(result.data(), len); 00076 if (aEnsureNL && result[len-1]!='\n') 00077 { 00078 result[len++] = '\n'; 00079 readLen++; 00080 } 00081 result[len] = '\0'; 00082 00083 if (readLen < len) 00084 { 00085 QString msg = i18n("Could only read %1 bytes of %2.") 00086 .arg(readLen).arg(len); 00087 msgDialog(msg); 00088 return QCString(); 00089 } 00090 00091 return result; 00092 } 00093 00094 //----------------------------------------------------------------------------- 00095 QByteArray kFileToBytes(const QString &aFileName, bool aVerbose) 00096 { 00097 QByteArray result; 00098 QFileInfo info(aFileName); 00099 unsigned int readLen; 00100 unsigned int len = info.size(); 00101 QFile file(aFileName); 00102 00103 //assert(aFileName!=0); 00104 if( aFileName.isEmpty() ) 00105 return result; 00106 00107 if (!info.exists()) 00108 { 00109 if (aVerbose) 00110 msgDialog(i18n("The specified file does not exist:\n%1") 00111 .arg(aFileName)); 00112 return result; 00113 } 00114 if (info.isDir()) 00115 { 00116 if (aVerbose) 00117 msgDialog(i18n("This is a folder and not a file:\n%1") 00118 .arg(aFileName)); 00119 return result; 00120 } 00121 if (!info.isReadable()) 00122 { 00123 if (aVerbose) 00124 msgDialog(i18n("You do not have read permissions " 00125 "to the file:\n%1").arg(aFileName)); 00126 return result; 00127 } 00128 if (len <= 0) return result; 00129 00130 if (!file.open(IO_Raw|IO_ReadOnly)) 00131 { 00132 if (aVerbose) switch(file.status()) 00133 { 00134 case IO_ReadError: 00135 msgDialog(i18n("Could not read file:\n%1").arg(aFileName)); 00136 break; 00137 case IO_OpenError: 00138 msgDialog(i18n("Could not open file:\n%1").arg(aFileName)); 00139 break; 00140 default: 00141 msgDialog(i18n("Error while reading file:\n%1").arg(aFileName)); 00142 } 00143 return result; 00144 } 00145 00146 result.resize(len); 00147 readLen = file.readBlock(result.data(), len); 00148 kdDebug(5006) << QString( "len %1" ).arg(len) << endl; 00149 00150 if (readLen < len) 00151 { 00152 QString msg; 00153 msg = i18n("Could only read %1 bytes of %2.") 00154 .arg(readLen).arg(len); 00155 msgDialog(msg); 00156 return result; 00157 } 00158 00159 return result; 00160 } 00161 00162 00163 //----------------------------------------------------------------------------- 00164 bool kBytesToFile(const char* aBuffer, int len, 00165 const QString &aFileName, 00166 bool aAskIfExists, bool aBackup, bool aVerbose) 00167 { 00168 QFile file(aFileName); 00169 int writeLen, rc; 00170 00171 //assert(aFileName!=0); 00172 if(aFileName.isEmpty()) 00173 return FALSE; 00174 00175 if (file.exists()) 00176 { 00177 if (aAskIfExists) 00178 { 00179 QString str; 00180 str = i18n("File %1 exists.\nDo you want to replace it?") 00181 .arg(aFileName); 00182 rc = KMessageBox::warningContinueCancel(0, 00183 str, i18n("Save to File"), i18n("&Replace")); 00184 if (rc != KMessageBox::Continue) return FALSE; 00185 } 00186 if (aBackup) 00187 { 00188 // make a backup copy 00189 QString bakName = aFileName; 00190 bakName += '~'; 00191 QFile::remove(bakName); 00192 if( !QDir::current().rename(aFileName, bakName) ) 00193 { 00194 // failed to rename file 00195 if (!aVerbose) return FALSE; 00196 rc = KMessageBox::warningContinueCancel(0, 00197 i18n("Failed to make a backup copy of %1.\nContinue anyway?") 00198 .arg(aFileName), 00199 i18n("Save to File"), KStdGuiItem::save()); 00200 if (rc != KMessageBox::Continue) return FALSE; 00201 } 00202 } 00203 } 00204 00205 if (!file.open(IO_Raw|IO_WriteOnly|IO_Truncate)) 00206 { 00207 if (aVerbose) switch(file.status()) 00208 { 00209 case IO_WriteError: 00210 msgDialog(i18n("Could not write to file:\n%1").arg(aFileName)); 00211 break; 00212 case IO_OpenError: 00213 msgDialog(i18n("Could not open file for writing:\n%1") 00214 .arg(aFileName)); 00215 break; 00216 default: 00217 msgDialog(i18n("Error while writing file:\n%1").arg(aFileName)); 00218 } 00219 return FALSE; 00220 } 00221 00222 writeLen = file.writeBlock(aBuffer, len); 00223 00224 if (writeLen < 0) 00225 { 00226 if (aVerbose) 00227 msgDialog(i18n("Could not write to file:\n%1").arg(aFileName)); 00228 return FALSE; 00229 } 00230 else if (writeLen < len) 00231 { 00232 QString msg = i18n("Could only write %1 bytes of %2.") 00233 .arg(writeLen).arg(len); 00234 if (aVerbose) 00235 msgDialog(msg); 00236 return FALSE; 00237 } 00238 00239 return TRUE; 00240 } 00241 00242 bool kCStringToFile(const QCString& aBuffer, const QString &aFileName, 00243 bool aAskIfExists, bool aBackup, bool aVerbose) 00244 { 00245 return kBytesToFile(aBuffer, aBuffer.length(), aFileName, aAskIfExists, 00246 aBackup, aVerbose); 00247 } 00248 00249 bool kByteArrayToFile(const QByteArray& aBuffer, const QString &aFileName, 00250 bool aAskIfExists, bool aBackup, bool aVerbose) 00251 { 00252 return kBytesToFile(aBuffer, aBuffer.size(), aFileName, aAskIfExists, 00253 aBackup, aVerbose); 00254 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jul 28 23:57:58 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003