00001 /**************************************************************** 00002 * Vidalia is distributed under the following license: 00003 * 00004 * Copyright (C) 2006, Matt Edman, Justin Hipple 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 ****************************************************************/ 00021 00022 /** 00023 * \file file.cpp 00024 * \version $Id: file.cpp 1755 2007-05-30 01:24:12Z edmanm $ 00025 * \brief Functions and definitions for common file I/O operations 00026 */ 00027 00028 #include <QDir> 00029 #include <QFile> 00030 #include "file.h" 00031 00032 #if defined(Q_OS_WIN32) 00033 #include <util/win32.h> 00034 #endif 00035 00036 00037 /** Create an empty file named <b>filename</b>. if <b>createdir</b> is true, 00038 * then the full path to <b>filename</b> will be created. Returns true on 00039 * success, or false on error and <b>errmsg</b> will be set. */ 00040 bool 00041 touch_file(QString filename, bool createdir, QString *errmsg) 00042 { 00043 /* Expand the file's path if it starts with a shortcut, like "~/" or 00044 * "%APPDATA%" */ 00045 filename = expand_filename(filename); 00046 00047 /* If the file's path doesn't exist and we're supposed to create it, do that 00048 * now. */ 00049 if (createdir && !create_path(QFileInfo(filename).absolutePath())) { 00050 return false; 00051 } 00052 00053 /* Touch the file */ 00054 QFile file(filename); 00055 if (!QFileInfo(filename).exists()) { 00056 if (!file.open(QIODevice::WriteOnly)) { 00057 return err(errmsg, file.errorString()); 00058 } 00059 } 00060 return true; 00061 } 00062 00063 /** Creates all directories in <b>path</b>, if they do not exist. */ 00064 bool 00065 create_path(QString path) 00066 { 00067 QDir dir(path); 00068 if (!dir.exists()) { 00069 path = dir.absolutePath(); 00070 if (!dir.mkpath(path)) { 00071 return false; 00072 } 00073 } 00074 return true; 00075 } 00076 00077 /** Expands <b>filename</b> if it starts with "~/". On Windows, this will 00078 * expand "%APPDATA%" and "%PROGRAMFILES%". If <b>filename</b> does not 00079 * start with a shortcut, <b>filename</b> will be returned unmodified. */ 00080 QString 00081 expand_filename(QString filename) 00082 { 00083 #if defined(Q_OS_WIN32) 00084 if (filename.startsWith("%APPDATA%\\") || 00085 filename.startsWith("%APPDATA%/")) 00086 return filename.replace(0, 9, win32_app_data_folder()); 00087 00088 if (filename.startsWith("%PROGRAMFILES%\\") || 00089 filename.startsWith("%PROGRAMFILES%/")) 00090 return filename.replace(0, 14, win32_program_files_folder()); 00091 #else 00092 if (filename.startsWith("~/")) 00093 return filename.replace(0, 1, QDir::homePath()); 00094 #endif 00095 return filename; 00096 } 00097