9 #include <tdemessagebox.h>
15 #include <tdelocale.h>
16 #include <kstdguiitem.h>
20 #include <tqfileinfo.h>
22 #include <sys/types.h>
24 #include <tdemacros.h>
29 static void msgDialog(
const TQString &msg)
31 KMessageBox::sorry(0, msg, i18n(
"File I/O Error"));
36 TDE_EXPORT TQCString
kFileToString(
const TQString &aFileName,
bool aEnsureNL,
bool aVerbose)
39 TQFileInfo info(aFileName);
41 unsigned int len = info.size();
42 TQFile file(aFileName);
45 if( aFileName.isEmpty() )
51 msgDialog(i18n(
"The specified file does not exist:\n%1").arg(aFileName));
57 msgDialog(i18n(
"This is a folder and not a file:\n%1").arg(aFileName));
60 if (!info.isReadable())
63 msgDialog(i18n(
"You do not have read permissions "
64 "to the file:\n%1").arg(aFileName));
67 if (len <= 0)
return TQCString();
69 if (!file.open(IO_Raw|IO_ReadOnly))
71 if (aVerbose)
switch(file.status())
74 msgDialog(i18n(
"Could not read file:\n%1").arg(aFileName));
77 msgDialog(i18n(
"Could not open file:\n%1").arg(aFileName));
80 msgDialog(i18n(
"Error while reading file:\n%1").arg(aFileName));
85 result.resize(len + (
int)aEnsureNL + 1);
86 readLen = file.readBlock(result.data(), len);
87 if (aEnsureNL && result[len-1]!=
'\n')
96 TQString msg = i18n(
"Could only read %1 bytes of %2.")
97 .arg(readLen).arg(len);
107 TQByteArray kFileToBytes(
const TQString &aFileName,
bool aVerbose)
110 TQFileInfo info(aFileName);
111 unsigned int readLen;
112 unsigned int len = info.size();
113 TQFile file(aFileName);
116 if( aFileName.isEmpty() )
122 msgDialog(i18n(
"The specified file does not exist:\n%1")
129 msgDialog(i18n(
"This is a folder and not a file:\n%1")
133 if (!info.isReadable())
136 msgDialog(i18n(
"You do not have read permissions "
137 "to the file:\n%1").arg(aFileName));
140 if (len <= 0)
return result;
142 if (!file.open(IO_Raw|IO_ReadOnly))
144 if (aVerbose)
switch(file.status())
147 msgDialog(i18n(
"Could not read file:\n%1").arg(aFileName));
150 msgDialog(i18n(
"Could not open file:\n%1").arg(aFileName));
153 msgDialog(i18n(
"Error while reading file:\n%1").arg(aFileName));
159 readLen = file.readBlock(result.data(), len);
160 kdDebug(5300) << TQString(
"len %1" ).arg(len) << endl;
165 msg = i18n(
"Could only read %1 bytes of %2.")
166 .arg(readLen).arg(len);
177 const TQString &aFileName,
178 bool aAskIfExists,
bool aBackup,
bool aVerbose)
181 TQFile file(aFileName);
185 if(aFileName.isEmpty())
193 str = i18n(
"File %1 exists.\nDo you want to replace it?")
195 rc = KMessageBox::warningContinueCancel(0,
196 str, i18n(
"Save to File"), i18n(
"&Replace"));
197 if (rc != KMessageBox::Continue)
return FALSE;
203 TQString bakName = aFileName;
205 TQFile::remove(bakName);
206 if( !TQDir::current().rename(aFileName, bakName) )
209 if (!aVerbose)
return FALSE;
210 rc = KMessageBox::warningContinueCancel(0,
211 i18n(
"Failed to make a backup copy of %1.\nContinue anyway?")
213 i18n(
"Save to File"), KStdGuiItem::save() );
214 if (rc != KMessageBox::Continue)
return FALSE;
219 if (!file.open(IO_Raw|IO_WriteOnly|IO_Truncate))
221 if (aVerbose)
switch(file.status())
224 msgDialog(i18n(
"Could not write to file:\n%1").arg(aFileName));
227 msgDialog(i18n(
"Could not open file for writing:\n%1")
231 msgDialog(i18n(
"Error while writing file:\n%1").arg(aFileName));
236 writeLen = file.writeBlock(aBuffer, len);
241 msgDialog(i18n(
"Could not write to file:\n%1").arg(aFileName));
244 else if (writeLen < len)
246 TQString msg = i18n(
"Could only write %1 bytes of %2.")
247 .arg(writeLen).arg(len);
256 TDE_EXPORT
bool kCStringToFile(
const TQCString& aBuffer,
const TQString &aFileName,
257 bool aAskIfExists,
bool aBackup,
bool aVerbose)
259 return kBytesToFile(aBuffer, aBuffer.length(), aFileName, aAskIfExists,
264 bool aAskIfExists,
bool aBackup,
bool aVerbose)
266 return kBytesToFile(aBuffer, aBuffer.size(), aFileName, aAskIfExists,
272 const bool recursive,
const bool wantItReadable,
273 const bool wantItWritable )
278 TQFileInfo fiToCheck(toCheck);
279 fiToCheck.setCaching(
false);
280 TQCString toCheckEnc = TQFile::encodeName(toCheck);
282 struct stat statbuffer;
284 if ( !fiToCheck.exists() ) {
285 error.append( i18n(
"%1 does not exist")
286 .arg(toCheck) +
"\n");
290 if ( fiToCheck.isDir() ) {
291 if ( stat( toCheckEnc,&statbuffer ) != 0 ) {
292 kdDebug() <<
"wantItA: Can't read perms of " << toCheck << endl;
295 if ( !g.isReadable() ) {
296 if ( chmod( toCheckEnc, statbuffer.st_mode + S_IXUSR ) != 0 ) {
297 error.append( i18n(
"%1 is not accessible and that is "
298 "unchangeable.").arg(toCheck) +
"\n");
300 kdDebug() <<
"Changed access bit for " << toCheck << endl;
307 if ( fiToCheck.isFile() || fiToCheck.isDir() ) {
309 if ( !fiToCheck.isReadable() && wantItReadable ) {
312 if ( stat(toCheckEnc,&statbuffer) != 0 ) {
313 kdDebug() <<
"wantItR: Can't read perms of " << toCheck << endl;
317 if ( chmod( toCheckEnc, statbuffer.st_mode + S_IRUSR ) != 0 ) {
318 error.append( i18n(
"%1 is not readable and that is unchangeable.")
319 .arg(toCheck) +
"\n");
321 kdDebug() <<
"Changed the read bit for " << toCheck << endl;
325 if ( !fiToCheck.isWritable() && wantItWritable ) {
328 if (stat(toCheckEnc,&statbuffer) != 0) {
329 kdDebug() <<
"wantItW: Can't read perms of " << toCheck << endl;
333 if ( chmod (toCheckEnc, statbuffer.st_mode + S_IWUSR ) != 0 ) {
334 error.append( i18n(
"%1 is not writable and that is unchangeable.")
335 .arg(toCheck) +
"\n");
337 kdDebug() <<
"Changed the write bit for " << toCheck << endl;
344 if ( fiToCheck.isDir() && recursive ){
348 if ( !g.isReadable() ){
349 error.append(i18n(
"Folder %1 is inaccessible.").arg(toCheck) +
"\n");
351 const TQFileInfoList *list = g.entryInfoList();
352 TQFileInfoListIterator it( *list );
354 while ((fi = it.current()) != 0) {
355 TQString newToCheck = toCheck +
"/" + fi->fileName();
356 TQFileInfo fiNewToCheck(newToCheck);
357 if ( fi->fileName() !=
"." && fi->fileName() !=
".." ) {
359 recursive, wantItReadable, wantItWritable) );
369 const TQString &toCheck,
const bool recursive,
const bool wantItReadable,
370 const bool wantItWritable )
373 wantItReadable, wantItWritable);
377 if ( !error.isEmpty() ) {
378 kdDebug() <<
"checkPermissions found:" << error << endl;
379 KMessageBox::detailedSorry(parent,
380 i18n(
"Some files or folders do not have "
381 "the right permissions, please correct them "
383 error, i18n(
"Permissions Check"),
false);
TDEPIM classes for drag and drop of mails.
TDE_EXPORT bool kBytesToFile(const char *aBuffer, int len, const TQString &aFileName, bool aAskIfExists, bool aBackup, bool aVerbose)
Save a file.
TDE_EXPORT TQCString kFileToString(const TQString &aFileName, bool aEnsureNL, bool aVerbose)
Load a file.
TDE_EXPORT bool kByteArrayToFile(const TQByteArray &aBuffer, const TQString &aFileName, bool aAskIfExists, bool aBackup, bool aVerbose)
Does not stop at NUL.
bool checkAndCorrectPermissionsIfPossibleWithErrorHandling(TQWidget *parent, const TQString &toCheck, const bool recursive, const bool wantItReadable, const bool wantItWritable)
Checks and corrects the permissions of a file or folder, and if requested all files and folders below...
TQString checkAndCorrectPermissionsIfPossible(const TQString &toCheck, const bool recursive, const bool wantItReadable, const bool wantItWritable)
Checks and corrects the permissions of a file or folder, and if requested all files and folders below...