CustusX  16.12
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxFileHelpers.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
33 #include "cxFileHelpers.h"
34 #include <QFile>
35 #include <QDir>
36 #include <QFileInfo>
37 
38 namespace cx
39 {
40 
41 //From http://stackoverflow.com/questions/2536524/copy-directory-using-qt
42 bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
43 {
44  QDir originDirectory(sourceDir);
45 
46  if (! originDirectory.exists())
47  {
48  return false;
49  }
50 
51  QDir destinationDirectory(destinationDir);
52 
53  if(destinationDirectory.exists() && !overWriteDirectory)
54  {
55  return false;
56  }
57  else if(destinationDirectory.exists() && overWriteDirectory)
58  {
59  destinationDirectory.removeRecursively();
60  }
61 
62  originDirectory.mkpath(destinationDir);
63 
64  foreach (QString directoryName, originDirectory.entryList(QDir::Dirs | \
65  QDir::NoDotAndDotDot))
66  {
67  QString destinationPath = destinationDir + "/" + directoryName;
68  originDirectory.mkpath(destinationPath);
69  copyRecursively(sourceDir + "/" + directoryName, destinationPath, overWriteDirectory);
70  }
71 
72  foreach (QString fileName, originDirectory.entryList(QDir::Files))
73  {
74  QFile::copy(sourceDir + "/" + fileName, destinationDir + "/" + fileName);
75  }
76 
78  QDir finalDestination(destinationDir);
79  finalDestination.refresh();
80 
81  if(finalDestination.exists())
82  {
83  return true;
84  }
85 
86  return false;
87 }
88 
89 bool removeNonemptyDirRecursively(const QString & dirName)
90 {
91  bool result = false;
92  QDir dir(dirName);
93 
94  if (dir.exists(dirName)) {
95  Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
96  if (info.isDir()) {
97  result = removeNonemptyDirRecursively(info.absoluteFilePath());
98  }
99  else {
100  result = QFile::remove(info.absoluteFilePath());
101  }
102 
103  if (!result) {
104  return result;
105  }
106  }
107  result = dir.rmdir(dirName);
108  }
109  return result;
110 }
111 
112 QFileInfoList getDirs(QString path)
113 {
114  QDir dir(path);
115  dir.setFilter(QDir::AllDirs|QDir::NoDotAndDotDot);
116  QFileInfoList retval = dir.entryInfoList();
117  return retval;
118 }
119 
120 QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
121 {
122  QStringList retval;
123  QDir dir(path);
124  dir.setFilter(QDir::Files);
125  dir.setNameFilters(nameFilters);
126 
127  foreach(QFileInfo file, dir.entryInfoList())
128  retval << file.absoluteFilePath();
129 
130  if (includeSubDirs)
131  foreach(QFileInfo directory, getDirs(path))
132  retval << getAbsolutePathToFiles(directory.absoluteFilePath(), nameFilters, includeSubDirs);
133 
134  return retval;
135 }
136 
137 QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
138 {
139  return getAbsolutePathToFiles(path, QStringList("*.xml"), includeSubDirs);
140 }
141 
142 } // namespace cx
bool removeNonemptyDirRecursively(const QString &dirName)
bool copyRecursively(QString sourceDir, QString destinationDir, bool overWriteDirectory)
QFileInfoList getDirs(QString path)
QStringList getAbsolutePathToXmlFiles(QString path, bool includeSubDirs)
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)