Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

fileTools.cpp

Go to the documentation of this file.
00001 //============================================== 00002 // copyright : (C) 2003-2005 by Will Stokes 00003 //============================================== 00004 // This program is free software; you can redistribute it 00005 // and/or modify it under the terms of the GNU General 00006 // Public License as published by the Free Software 00007 // Foundation; either version 2 of the License, or 00008 // (at your option) any later version. 00009 //============================================== 00010 00011 //Systemwide includes 00012 #include <fstream> 00013 #include <qstring.h> 00014 #include <qfile.h> 00015 #include <qdir.h> 00016 #include <qlibrary.h> 00017 //#include <qglobal.h> 00018 00019 //Projectwide includes 00020 #include "fileTools.h" 00021 00022 //============================================== 00023 //PLATFORM_SPECIFIC_CODE 00024 00025 //Includes and defines for getWindowsFolderLocation 00026 #ifdef Q_OS_WIN 00027 00028 #include <shlobj.h> 00029 00030 #ifndef CSIDL_APPDATA 00031 #define CSIDL_APPDATA 0x001a 00032 #endif 00033 00034 #ifndef CSIDL_LOCAL_APPDATA 00035 #define CSIDL_LOCAL_APPDATA 0x001c 00036 #endif 00037 00038 #endif //Q_OS_WIN 00039 //============================================== 00040 bool moveFile( QString oldName, QString newName) 00041 { 00042 QDir rootDir; 00043 00044 //attempt to rename file 00045 if(!rootDir.rename( oldName, newName)) 00046 { 00047 //move failed, copy file and remove original 00048 00049 //copy failed! sound alert and do not remove original!!! 00050 if(!copyFile(oldName, newName)) 00051 return false; 00052 00053 //copy succeded, remove original and return 00054 rootDir.remove(oldName); 00055 } 00056 00057 //move succeeded either directly or via copying and removing original file 00058 return true; 00059 } 00060 //============================================== 00061 bool copyFile(QString oldFilePath, QString newFilePath) 00062 { 00063 //same file, no need to copy 00064 if(oldFilePath.compare(newFilePath) == 0) 00065 return true; 00066 00067 //load both files 00068 QFile oldFile(oldFilePath); 00069 QFile newFile(newFilePath); 00070 bool openOld = oldFile.open( IO_ReadOnly ); 00071 bool openNew = newFile.open( IO_WriteOnly ); 00072 00073 //if either file fails to open bail 00074 if(!openOld || !openNew) { return false; } 00075 00076 //copy contents 00077 uint BUFFER_SIZE = 16000; 00078 char* buffer = new char[BUFFER_SIZE]; 00079 while(!oldFile.atEnd()) 00080 { 00081 Q_ULONG len = oldFile.readBlock( buffer, BUFFER_SIZE ); 00082 newFile.writeBlock( buffer, len ); 00083 } 00084 00085 //deallocate buffer 00086 delete[] buffer; 00087 buffer = NULL; 00088 return true; 00089 } 00090 //============================================== 00091 //PLATFORM_SPECIFIC_CODE 00092 #ifdef Q_OS_WIN 00093 bool getWindowsFolderLocation(FOLDER_TYPE type, QString& path) 00094 { 00095 //get pointer to item identifier 00096 int folder; 00097 switch(type) 00098 { 00099 case APPLICATION_DATA: 00100 folder = CSIDL_APPDATA; break; 00101 case LOCAL_SETTINGS_APPLICATION_DATA: 00102 folder = CSIDL_LOCAL_APPDATA; break; 00103 //unhandeled folder type 00104 default: 00105 return false; 00106 } 00107 00108 //call unicode or local-specific functions as necessary to get folder path 00109 bool success = true; 00110 QT_WA( 00111 { 00112 //unicode version 00113 unsigned short folderPath[MAX_PATH]; 00114 if( SHGetSpecialFolderPathW(0, folderPath, folder, false) ) 00115 path = QString::fromUcs2( (ushort*)folderPath ); 00116 else 00117 success = false; 00118 }, 00119 { 00120 //non-unicode version 00121 char folderPath[MAX_PATH]; 00122 if( SHGetSpecialFolderPathA( 0, folderPath, folder, false ) ) 00123 path = QString::fromLocal8Bit( folderPath ); 00124 else 00125 success = false; 00126 }); 00127 00128 //if failed to get LOCAL_SETTINGS_APPLICATION_DATA location then 00129 //try again using APPLICATION_DATA since this is likely Win95/Win98 (or ME?) 00130 if( (!success) && (type == LOCAL_SETTINGS_APPLICATION_DATA) ) 00131 return getWindowsFolderLocation( APPLICATION_DATA, path ); 00132 else 00133 return success; 00134 } 00135 #endif //Q_OS_WIN 00136 //============================================== 00137 QString fixFilename( QString filename ) 00138 { 00139 filename.replace( QChar(' '), "_" ); 00140 filename.replace( "<", "" ); 00141 filename.replace( ">", "" ); 00142 filename.replace( "&", "and" ); 00143 filename.replace( "\"", "" ); 00144 filename.replace( "\'", "" ); 00145 filename.replace( "?", "" ); 00146 return filename; 00147 } 00148 //==============================================

Generated on Sun Mar 4 19:42:55 2007 for AlbumShaper by doxygen 1.3.7