AlbumShaper 1.0a3
fileTools.cpp
Go to the documentation of this file.
1//==============================================
2// copyright : (C) 2003-2005 by Will Stokes
3//==============================================
4// This program is free software; you can redistribute it
5// and/or modify it under the terms of the GNU General
6// Public License as published by the Free Software
7// Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//==============================================
10
11//Systemwide includes
12#include <fstream>
13#include <qstring.h>
14#include <qfile.h>
15#include <qdir.h>
16#include <qlibrary.h>
17//#include <qglobal.h>
18
19//Projectwide includes
20#include "fileTools.h"
21
22//==============================================
23//PLATFORM_SPECIFIC_CODE
24
25//Includes and defines for getWindowsFolderLocation
26#ifdef Q_OS_WIN
27
28#include <shlobj.h>
29
30#ifndef CSIDL_APPDATA
31#define CSIDL_APPDATA 0x001a
32#endif
33
34#ifndef CSIDL_LOCAL_APPDATA
35#define CSIDL_LOCAL_APPDATA 0x001c
36#endif
37
38#endif //Q_OS_WIN
39//==============================================
40bool moveFile( QString oldName, QString newName)
41{
42 QDir rootDir;
43
44 //attempt to rename file
45 if(!rootDir.rename( oldName, newName))
46 {
47 //move failed, copy file and remove original
48
49 //copy failed! sound alert and do not remove original!!!
50 if(!copyFile(oldName, newName))
51 return false;
52
53 //copy succeded, remove original and return
54 rootDir.remove(oldName);
55 }
56
57 //move succeeded either directly or via copying and removing original file
58 return true;
59}
60//==============================================
61bool copyFile(QString oldFilePath, QString newFilePath)
62{
63 //same file, no need to copy
64 if(oldFilePath.compare(newFilePath) == 0)
65 return true;
66
67 //load both files
68 QFile oldFile(oldFilePath);
69 QFile newFile(newFilePath);
70 bool openOld = oldFile.open( QIODevice::ReadOnly );
71 bool openNew = newFile.open( QIODevice::WriteOnly );
72
73 //if either file fails to open bail
74 if(!openOld || !openNew) { return false; }
75
76 //copy contents
77 uint BUFFER_SIZE = 16000;
78 char* buffer = new char[BUFFER_SIZE];
79 while(!oldFile.atEnd())
80 {
81 Q_ULONG len = oldFile.readBlock( buffer, BUFFER_SIZE );
82 newFile.writeBlock( buffer, len );
83 }
84
85 //deallocate buffer
86 delete[] buffer;
87 buffer = NULL;
88 return true;
89}
90//==============================================
91//PLATFORM_SPECIFIC_CODE
92#ifdef Q_OS_WIN
93bool getWindowsFolderLocation(FOLDER_TYPE type, QString& path)
94{
95 //get pointer to item identifier
96 int folder;
97 switch(type)
98 {
99 case APPLICATION_DATA:
100 folder = CSIDL_APPDATA; break;
101 case LOCAL_SETTINGS_APPLICATION_DATA:
102 folder = CSIDL_LOCAL_APPDATA; break;
103 //unhandeled folder type
104 default:
105 return false;
106 }
107
108 //call unicode or local-specific functions as necessary to get folder path
109 bool success = true;
110 QT_WA(
111 {
112 //unicode version
113 unsigned short folderPath[MAX_PATH];
114 if( SHGetSpecialFolderPathW(0, folderPath, folder, false) )
115 path = QString::fromUcs2( (ushort*)folderPath );
116 else
117 success = false;
118 },
119 {
120 //non-unicode version
121 char folderPath[MAX_PATH];
122 if( SHGetSpecialFolderPathA( 0, folderPath, folder, false ) )
123 path = QString::fromLocal8Bit( folderPath );
124 else
125 success = false;
126 });
127
128 //if failed to get LOCAL_SETTINGS_APPLICATION_DATA location then
129 //try again using APPLICATION_DATA since this is likely Win95/Win98 (or ME?)
130 if( (!success) && (type == LOCAL_SETTINGS_APPLICATION_DATA) )
131 return getWindowsFolderLocation( APPLICATION_DATA, path );
132 else
133 return success;
134}
135#endif //Q_OS_WIN
136//==============================================
137QString fixFilename( QString filename )
138{
139 filename.replace( QChar(' '), "_" );
140 filename.replace( "<", "" );
141 filename.replace( ">", "" );
142 filename.replace( "&", "and" );
143 filename.replace( "\"", "" );
144 filename.replace( "\'", "" );
145 filename.replace( "?", "" );
146 return filename;
147}
148//==============================================
float * buffer
Definition blur.cpp:80
QString fixFilename(QString filename)
Replaces invalid characters in filenames with valid ones.
bool moveFile(QString oldName, QString newName)
Definition fileTools.cpp:40
bool copyFile(QString oldFilePath, QString newFilePath)
Copies a file from one location to another.
Definition fileTools.cpp:61