directoryutilities.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include <QDir>
  2. #include <QApplication>
  3. #include <QDebug>
  4. #include "defines.h"
  5. #include "directoryutilities.h"
  6. #include "generatoroutputview.h"
  7. DirectoryUtilities::DirectoryUtilities(GeneratorOutputView &outputView, GeneratorData &generatorData)
  8. : m_outputView(outputView),
  9. m_generatorData(generatorData)
  10. {
  11. }
  12. void DirectoryUtilities::getFiles(const QString &directory, QDir::Filters &filters, const QStringList &nameFilters, int pathLengthToSubstract, const QString &filePrefixToAdd, QStringList &foundFiles)
  13. {
  14. QDir dir(directory);
  15. dir.setFilter(filters);
  16. dir.setNameFilters(nameFilters);
  17. foreach (QFileInfo entry, dir.entryInfoList())
  18. {
  19. if (entry.isDir())
  20. {
  21. getFiles(entry.canonicalFilePath(), filters, nameFilters, pathLengthToSubstract, filePrefixToAdd, foundFiles);
  22. }
  23. else
  24. {
  25. QString fileName = entry.absoluteFilePath().mid(pathLengthToSubstract);
  26. qDebug() << filePrefixToAdd << ", " << fileName;
  27. foundFiles.append(filePrefixToAdd + fileName);
  28. m_outputView.printOutput("Widget file: " + filePrefixToAdd + fileName);
  29. }
  30. }
  31. }
  32. bool DirectoryUtilities::getFile(const QString &directory, QDir::Filters &filters, const QStringList &nameFilters, QString &foundFile)
  33. {
  34. QStringList files;
  35. getFiles(directory, filters, nameFilters, directory.length(), directory, files);
  36. if(files.count() == 0)
  37. return false;
  38. foundFile = files.at(0);
  39. return true;
  40. }
  41. void DirectoryUtilities::getDirs(const QString &directory, QDir::Filters &filters, const QStringList &nameFilters, int pathLengthToSubstract, const QString &filePrefixToAdd, QStringList &foundDirs)
  42. {
  43. QDir dir(directory);
  44. dir.setFilter(filters);
  45. dir.setNameFilters(nameFilters);
  46. foreach(QFileInfo entry, dir.entryInfoList()) {
  47. if(entry.isDir()) {
  48. foundDirs.append(filePrefixToAdd + entry.baseName());
  49. getFiles(entry.canonicalFilePath(), filters, nameFilters, pathLengthToSubstract, filePrefixToAdd, foundDirs);
  50. }
  51. }
  52. }
  53. void DirectoryUtilities::cleanDirectory(const QString &directory, bool cleanSubdir, const QString &rootDirectory)
  54. {
  55. QDir dir(directory);
  56. dir.setFilter(QDir::Files | QDir::AllDirs | QDir::NoDotAndDotDot);
  57. foreach(QFileInfo entry, dir.entryInfoList()) {
  58. if(entry.isDir()) {
  59. if(cleanSubdir)
  60. cleanDirectory(entry.absoluteFilePath(), cleanSubdir, rootDirectory);
  61. else
  62. continue;
  63. }
  64. else {
  65. dir.remove(entry.absoluteFilePath());
  66. }
  67. }
  68. if(rootDirectory != directory)
  69. dir.rmdir(directory);
  70. }
  71. bool DirectoryUtilities::getSubdirectory(const QString &directory, QString &subdir)
  72. {
  73. bool retval = false;
  74. QDir dir(directory);
  75. dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
  76. foreach(QFileInfo entry, dir.entryInfoList()) {
  77. if(entry.isDir()) {
  78. subdir = entry.absoluteFilePath();
  79. retval = true;
  80. break;
  81. }
  82. }
  83. return retval;
  84. }
  85. bool DirectoryUtilities::copyDirectory(const QString &srcDirectory, const QString &destDirectory, bool deleteOriginal/*= false*/)
  86. {
  87. QDir dir(srcDirectory);
  88. dir.setFilter(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
  89. foreach(QFileInfo entry, dir.entryInfoList()) {
  90. if(entry.isDir()) {
  91. QString destSubdir(entry.absoluteFilePath());
  92. destSubdir.replace(srcDirectory, "");
  93. copyDirectory(entry.absoluteFilePath(), destDirectory + destSubdir);
  94. if(deleteOriginal) {
  95. cleanDirectory(entry.absoluteFilePath(), true, srcDirectory);
  96. }
  97. }
  98. else {
  99. QString destDir(destDirectory+"/");
  100. QFile srcFile(entry.absoluteFilePath());
  101. if(!srcFile.open(QIODevice::ReadOnly)) {
  102. QString errMsg = QString(ERR_FILE_OPEN_READ_FAILED_CANNOT_CONTINUE).arg(entry.absoluteFilePath());
  103. m_outputView.printOutput(errMsg);
  104. return false;
  105. }
  106. QString destFilename(destDirectory+"/"+entry.fileName());
  107. if(srcFile.exists(destFilename))
  108. srcFile.remove(destFilename);
  109. else if(!srcFile.exists(destDirectory)) {
  110. QDir destDir;
  111. destDir.mkpath(destDirectory);
  112. }
  113. if(!srcFile.copy(destFilename)) {
  114. QString errMsg = QString(ERR_FILE_COPY_FAILED).arg(entry.absoluteFilePath()).arg(destFilename);
  115. m_outputView.printOutput(errMsg);
  116. return false;
  117. }
  118. if(deleteOriginal) {
  119. srcFile.remove(entry.absoluteFilePath());
  120. }
  121. }
  122. }
  123. return true;
  124. }