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