Vidalia 0.2.10
|
00001 /* 00002 ** This file is part of Vidalia, and is subject to the license terms in the 00003 ** LICENSE file, found in the top level directory of this distribution. If you 00004 ** did not receive the LICENSE file with this file, you may obtain it from the 00005 ** Vidalia source package distributed by the Vidalia Project at 00006 ** http://www.vidalia-project.net/. No part of Vidalia, including this file, 00007 ** may be copied, modified, propagated, or distributed except according to the 00008 ** terms described in the LICENSE file. 00009 */ 00010 00011 /* 00012 ** \file file.cpp 00013 ** \version $Id: file.cpp 3735 2009-04-28 20:28:01Z edmanm $ 00014 ** \brief Functions and definitions for common file I/O operations 00015 */ 00016 00017 #include "file.h" 00018 #include "stringutil.h" 00019 00020 #if defined(Q_OS_WIN32) 00021 #include "win32.h" 00022 #endif 00023 00024 #include <QDir> 00025 #include <QFile> 00026 00027 00028 /** Create an empty file named <b>filename</b>. if <b>createdir</b> is true, 00029 * then the full path to <b>filename</b> will be created. Returns true on 00030 * success, or false on error and <b>errmsg</b> will be set. */ 00031 bool 00032 touch_file(const QString &filename, bool createdir, QString *errmsg) 00033 { 00034 /* Expand the file's path if it starts with a shortcut, like "~/" or 00035 * "%APPDATA%" */ 00036 QString expanded = expand_filename(filename); 00037 00038 /* If the file's path doesn't exist and we're supposed to create it, do that 00039 * now. */ 00040 if (createdir && !create_path(QFileInfo(expanded).absolutePath())) { 00041 return false; 00042 } 00043 00044 /* Touch the file */ 00045 QFile file(expanded); 00046 if (!QFileInfo(expanded).exists()) { 00047 if (!file.open(QIODevice::WriteOnly)) { 00048 return err(errmsg, file.errorString()); 00049 } 00050 } 00051 return true; 00052 } 00053 00054 /** Creates all directories in <b>path</b>, if they do not exist. */ 00055 bool 00056 create_path(const QString &path) 00057 { 00058 QDir dir(path); 00059 if (!dir.exists()) { 00060 if (!dir.mkpath(dir.absolutePath())) { 00061 return false; 00062 } 00063 } 00064 return true; 00065 } 00066 00067 /** Recursively copy the contents of one directory to another. The 00068 * destination must already exist. Returns true on success, and false 00069 * otherwise. */ 00070 bool 00071 copy_dir(const QString &source, const QString &dest) 00072 { 00073 /* Source and destination as QDir's */ 00074 QDir src(source); 00075 QDir dst(dest); 00076 00077 /* Get contents of the directory */ 00078 QFileInfoList contents = src.entryInfoList(QDir::Files | QDir::Dirs 00079 | QDir::NoDotAndDotDot); 00080 00081 /* Copy each entry in src to dst */ 00082 foreach (QFileInfo fileInfo, contents) { 00083 /* Get absolute path of source and destination */ 00084 QString fileName = fileInfo.fileName(); 00085 QString srcFilePath = src.absoluteFilePath(fileName); 00086 QString dstFilePath = dst.absoluteFilePath(fileName); 00087 00088 if (fileInfo.isDir()) { 00089 /* This is a directory, make it and recurse */ 00090 if (!dst.mkdir(fileName)) 00091 return false; 00092 if (!copy_dir(srcFilePath, dstFilePath)) 00093 return false; 00094 } else if (fileInfo.isFile()) { 00095 /* This is a file, copy it */ 00096 if (!QFile::copy(srcFilePath, dstFilePath)) 00097 return false; 00098 } 00099 /* Ignore special files (e.g. symlinks, devices) */ 00100 00101 } 00102 return true; 00103 } 00104 00105 /** Expands <b>filename</b> if it starts with "~/". On Windows, this will 00106 * expand "%APPDATA%" and "%PROGRAMFILES%". If <b>filename</b> does not 00107 * start with a shortcut, <b>filename</b> will be returned unmodified. */ 00108 QString 00109 expand_filename(const QString &filename) 00110 { 00111 QString fname = filename; 00112 #if defined(Q_OS_WIN32) 00113 if (fname.startsWith("%APPDATA%\\") || 00114 fname.startsWith("%APPDATA%/")) 00115 return fname.replace(0, 9, win32_app_data_folder()); 00116 00117 if (fname.startsWith("%PROGRAMFILES%\\") || 00118 fname.startsWith("%PROGRAMFILES%/")) 00119 return fname.replace(0, 14, win32_program_files_folder()); 00120 #else 00121 if (fname.startsWith("~/")) 00122 return fname.replace(0, 1, QDir::homePath()); 00123 #endif 00124 return fname; 00125 } 00126