22#include "kpimutils_export.h"
25#include <KLocalizedString>
27#include <KStandardGuiItem>
43static void msgDialog(
const QString &msg )
45 KMessageBox::sorry( 0, msg, i18n(
"File I/O Error" ) );
49QByteArray kFileToByteArray(
const QString &aFileName,
bool aEnsureNL,
53 QFileInfo info( aFileName );
55 unsigned int len = info.size();
56 QFile file( aFileName );
59 if ( aFileName.isEmpty() ) {
63 if ( !info.exists() ) {
65 msgDialog( i18n(
"The specified file does not exist:\n%1", aFileName ) );
71 msgDialog( i18n(
"This is a folder and not a file:\n%1", aFileName ) );
75 if ( !info.isReadable() ) {
77 msgDialog( i18n(
"You do not have read permissions to the file:\n%1", aFileName ) );
85 if ( !file.open( QIODevice::Unbuffered|QIODevice::ReadOnly ) ) {
87 switch ( file.error() ) {
88 case QFile::ReadError:
89 msgDialog( i18n(
"Could not read file:\n%1", aFileName ) );
91 case QFile::OpenError:
92 msgDialog( i18n(
"Could not open file:\n%1", aFileName ) );
95 msgDialog( i18n(
"Error while reading file:\n%1", aFileName ) );
101 result.resize( len +
int( aEnsureNL ) );
102 readLen = file.read( result.data(), len );
104 if ( result[readLen-1] !=
'\n' ) {
105 result[readLen++] =
'\n';
108 result.truncate( len );
112 if ( readLen < len ) {
113 QString msg = i18np(
"Could only read 1 byte of %2.",
114 "Could only read %1 bytes of %2.",
117 result.truncate( readLen );
124bool kByteArrayToFile(
const QByteArray &aBuffer,
const QString &aFileName,
125 bool aAskIfExists,
bool aBackup,
bool aVerbose )
128 QFile file( aFileName );
131 if ( aFileName.isEmpty() ) {
135 if ( file.exists() ) {
136 if ( aAskIfExists ) {
138 str = i18n(
"File %1 exists.\nDo you want to replace it?", aFileName );
140 KMessageBox::warningContinueCancel( 0, str, i18n(
"Save to File" ),
141 KGuiItem( i18n(
"&Replace" ) ) );
142 if ( rc != KMessageBox::Continue ) {
149 QString bakName = aFileName;
150 bakName += QLatin1Char(
'~');
151 QFile::remove( bakName );
152 if ( !QDir::current().rename( aFileName, bakName ) ) {
158 KMessageBox::warningContinueCancel(
160 i18n(
"Failed to make a backup copy of %1.\nContinue anyway?", aFileName ),
161 i18n(
"Save to File" ), KStandardGuiItem::save() );
163 if ( rc != KMessageBox::Continue ) {
170 if ( !file.open( QIODevice::Unbuffered|QIODevice::WriteOnly|QIODevice::Truncate ) ) {
172 switch ( file.error() ) {
173 case QFile::WriteError:
174 msgDialog( i18n(
"Could not write to file:\n%1", aFileName ) );
176 case QFile::OpenError:
177 msgDialog( i18n(
"Could not open file for writing:\n%1", aFileName ) );
180 msgDialog( i18n(
"Error while writing file:\n%1", aFileName ) );
186 const int writeLen = file.write( aBuffer.data(), aBuffer.size() );
188 if ( writeLen < 0 ) {
190 msgDialog( i18n(
"Could not write to file:\n%1", aFileName ) );
193 }
else if ( writeLen < aBuffer.size() ) {
194 QString msg = i18np(
"Could only write 1 byte of %2.",
195 "Could only write %1 bytes of %2.",
196 writeLen, aBuffer.size() );
206QString checkAndCorrectPermissionsIfPossible(
const QString &toCheck,
207 const bool recursive,
208 const bool wantItReadable,
209 const bool wantItWritable )
214 QFileInfo fiToCheck( toCheck );
215 fiToCheck.setCaching(
false );
216 QByteArray toCheckEnc = QFile::encodeName( toCheck );
218 KDE_struct_stat statbuffer;
220 if ( !fiToCheck.exists() ) {
221 error.append( i18n(
"%1 does not exist", toCheck ) + QLatin1Char(
'\n') );
225 if ( fiToCheck.isDir() ) {
226 if ( KDE_stat( toCheckEnc, &statbuffer ) != 0 ) {
227 kDebug() <<
"wantItA: Can't read perms of" << toCheck;
230 if ( !g.isReadable() ) {
231 if ( chmod( toCheckEnc, statbuffer.st_mode + S_IXUSR ) != 0 ) {
232 error.append( i18n(
"%1 is not accessible and that is "
233 "unchangeable.", toCheck ) + QLatin1Char(
'\n') );
235 kDebug() <<
"Changed access bit for" << toCheck;
242 if ( fiToCheck.isFile() || fiToCheck.isDir() ) {
244 if ( !fiToCheck.isReadable() && wantItReadable ) {
247 if ( KDE_stat( toCheckEnc, &statbuffer ) != 0 ) {
248 kDebug() <<
"wantItR: Can't read perms of" << toCheck;
252 if ( chmod( toCheckEnc, statbuffer.st_mode + S_IRUSR ) != 0 ) {
253 error.append( i18n(
"%1 is not readable and that is unchangeable.",
254 toCheck ) + QLatin1Char(
'\n') );
256 kDebug() <<
"Changed the read bit for" << toCheck;
260 if ( !fiToCheck.isWritable() && wantItWritable ) {
263 if ( KDE_stat( toCheckEnc, &statbuffer ) != 0 ) {
264 kDebug() <<
"wantItW: Can't read perms of" << toCheck;
268 if ( chmod ( toCheckEnc, statbuffer.st_mode + S_IWUSR ) != 0 ) {
269 error.append( i18n(
"%1 is not writable and that is unchangeable.", toCheck ) + QLatin1Char(
'\n') );
271 kDebug() <<
"Changed the write bit for" << toCheck;
278 if ( fiToCheck.isDir() && recursive ) {
282 if ( !g.isReadable() ) {
283 error.append( i18n(
"Folder %1 is inaccessible.", toCheck ) + QLatin1Char(
'\n') );
285 foreach (
const QFileInfo &fi, g.entryInfoList() ) {
286 QString newToCheck = toCheck + QLatin1Char(
'/') + fi.fileName();
287 if ( fi.fileName() != QLatin1String(
".") && fi.fileName() != QLatin1String(
"..") ) {
289 checkAndCorrectPermissionsIfPossible( newToCheck, recursive,
290 wantItReadable, wantItWritable ) );
298bool checkAndCorrectPermissionsIfPossibleWithErrorHandling( QWidget *parent,
299 const QString &toCheck,
300 const bool recursive,
301 const bool wantItReadable,
302 const bool wantItWritable )
305 checkAndCorrectPermissionsIfPossible( toCheck, recursive, wantItReadable, wantItWritable );
310 if ( !error.isEmpty() ) {
311 kDebug() <<
"checkPermissions found:" << error;
312 KMessageBox::detailedSorry( parent,
313 i18n(
"Some files or folders do not have the "
314 "necessary permissions, please correct "
316 error, i18n(
"Permissions Check" ), 0 );
323bool removeDirAndContentsRecursively(
const QString & path )
329 d.setFilter( QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks );
331 QFileInfoList list = d.entryInfoList();
333 Q_FOREACH (
const QFileInfo &fi, list ) {
335 if ( fi.fileName() != QLatin1String(
".") && fi.fileName() != QLatin1String(
"..") ) {
336 success = success && removeDirAndContentsRecursively( fi.absoluteFilePath() );
339 success = success && d.remove( fi.absoluteFilePath() );
344 success = success && d.rmdir( path );