22 #include "kpimutils_export.h"
25 #include <KLocalizedString>
26 #include <KMessageBox>
27 #include <KStandardGuiItem>
37 #include <sys/types.h>
43 static void msgDialog(
const QString &msg )
45 KMessageBox::sorry( 0, msg, i18n(
"File I/O Error" ) );
55 unsigned int len = info.size();
56 QFile file( aFileName );
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';
112 if ( readLen < len ) {
113 QString msg = i18np(
"Could only read 1 byte of %2.",
114 "Could only read %1 bytes of %2.",
125 bool aAskIfExists,
bool aBackup,
bool aVerbose )
128 QFile file( aFileName );
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 ) {
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() );
206 QString checkAndCorrectPermissionsIfPossible(
const QString &toCheck,
207 const bool recursive,
208 const bool wantItReadable,
209 const bool wantItWritable )
215 fiToCheck.setCaching(
false );
218 KDE_struct_stat statbuffer;
220 if ( !fiToCheck.exists() ) {
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 "
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.",
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() ) {
289 checkAndCorrectPermissionsIfPossible( newToCheck, recursive,
290 wantItReadable, wantItWritable ) );
298 bool checkAndCorrectPermissionsIfPossibleWithErrorHandling(
QWidget *parent,
300 const bool recursive,
301 const bool wantItReadable,
302 const bool wantItWritable )
305 checkAndCorrectPermissionsIfPossible( toCheck, recursive, wantItReadable, wantItWritable );
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 );
323 bool removeDirAndContentsRecursively(
const QString & path )
329 d.
setFilter( QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks );
333 Q_FOREACH (
const QFileInfo &fi, list ) {
336 success = success && removeDirAndContentsRecursively( fi.
absoluteFilePath() );
344 success = success && d.
rmdir( path );
QString & append(QChar ch)
bool remove(const QString &fileName)
void setFilter(QFlags< QDir::Filter > filters)
bool rmdir(const QString &dirName) const
QFileInfoList entryInfoList(QFlags< QDir::Filter > filters, QFlags< QDir::SortFlag > sort) const
QString absoluteFilePath() const
void setPath(const QString &path)
QByteArray encodeName(const QString &fileName)