file.cpp

Go to the documentation of this file.
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 3511 2009-02-05 00:56:16Z edmanm $
00014 ** \brief Functions and definitions for common file I/O operations
00015 */
00016 
00017 #include <QDir>
00018 #include <QFile>
00019 #include "stringutil.h"
00020 #include "file.h"
00021 
00022 #if defined(Q_OS_WIN32)
00023 #include "win32.h"
00024 #endif
00025 
00026 
00027 /** Create an empty file named <b>filename</b>. if <b>createdir</b> is true,
00028  * then the full path to <b>filename</b> will be created. Returns true on 
00029  * success, or false on error and <b>errmsg</b> will be set. */
00030 bool
00031 touch_file(QString filename, bool createdir, QString *errmsg)
00032 {
00033   /* Expand the file's path if it starts with a shortcut, like "~/" or
00034    * "%APPDATA%" */
00035   filename = expand_filename(filename);
00036     
00037   /* If the file's path doesn't exist and we're supposed to create it, do that
00038    * now. */
00039   if (createdir && !create_path(QFileInfo(filename).absolutePath())) {
00040     return false;
00041   }
00042  
00043   /* Touch the file */
00044   QFile file(filename);
00045   if (!QFileInfo(filename).exists()) {
00046     if (!file.open(QIODevice::WriteOnly)) {
00047       return err(errmsg, file.errorString());
00048     } 
00049   }
00050   return true;
00051 }
00052 
00053 /** Creates all directories in <b>path</b>, if they do not exist. */
00054 bool
00055 create_path(QString path)
00056 {
00057   QDir dir(path);
00058   if (!dir.exists()) {
00059     path = dir.absolutePath();
00060     if (!dir.mkpath(path)) {
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(QString source, 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 | QDir::NoDotAndDotDot);
00079 
00080   /* Copy each entry in src to dst */
00081   foreach (QFileInfo fileInfo, contents) {
00082     /* Get absolute path of source and destination */
00083     QString fileName = fileInfo.fileName();
00084     QString srcFilePath = src.absoluteFilePath(fileName);
00085     QString dstFilePath = dst.absoluteFilePath(fileName);
00086 
00087     if (fileInfo.isDir()) {
00088       /* This is a directory, make it and recurse */
00089       if (!dst.mkdir(fileName))
00090         return false;
00091       if (!copy_dir(srcFilePath, dstFilePath))
00092         return false;
00093     } else if (fileInfo.isFile()) {
00094       /* This is a file, copy it */
00095       if (!QFile::copy(srcFilePath, dstFilePath))
00096         return false;
00097     } 
00098     /* Ignore special files (e.g. symlinks, devices) */
00099 
00100   }
00101   return true;
00102 }
00103 
00104 /** Expands <b>filename</b> if it starts with "~/". On Windows, this will
00105  * expand "%APPDATA%" and "%PROGRAMFILES%". If <b>filename</b> does not
00106  * start with a shortcut, <b>filename</b> will be returned unmodified. */
00107 QString
00108 expand_filename(QString filename)
00109 {
00110 #if defined(Q_OS_WIN32)
00111   if (filename.startsWith("%APPDATA%\\") ||
00112       filename.startsWith("%APPDATA%/"))
00113     return filename.replace(0, 9, win32_app_data_folder());
00114     
00115   if (filename.startsWith("%PROGRAMFILES%\\") ||
00116       filename.startsWith("%PROGRAMFILES%/"))
00117     return filename.replace(0, 14, win32_program_files_folder());
00118 #else
00119   if (filename.startsWith("~/"))
00120     return filename.replace(0, 1, QDir::homePath());
00121 #endif
00122   return filename;
00123 }
00124 

Generated on Tue Jul 7 16:58:11 2009 for Vidalia by  doxygen 1.4.7