summaryrefslogtreecommitdiff
path: root/src/qdirfiletree.cpp
blob: f7bb11a7195cf80575007e38cc2e42864ae08cb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "qdirfiletree.h"

#include <QDirIterator>

using namespace MOBase;

class QDirFileTreeImpl : public QDirFileTree
{
public:
  QDirFileTreeImpl(std::shared_ptr<const IFileTree> parent, QDir dir)
      : FileTreeEntry(parent, dir.dirName()), QDirFileTree(), qDir(dir)
  {}

protected:
  /**
   * No mutable operations allowed.
   */
  bool beforeReplace(IFileTree const* dstTree, FileTreeEntry const* destination,
                     FileTreeEntry const* source) override
  {
    return false;
  }
  bool beforeInsert(IFileTree const* entry, FileTreeEntry const* name) override
  {
    return false;
  }
  bool beforeRemove(IFileTree const* entry, FileTreeEntry const* name) override
  {
    return false;
  }
  std::shared_ptr<FileTreeEntry> makeFile(std::shared_ptr<const IFileTree> parent,
                                          QString name) const override
  {
    return nullptr;
  }
  std::shared_ptr<IFileTree> makeDirectory(std::shared_ptr<const IFileTree> parent,
                                           QString name) const override
  {
    return nullptr;
  }

  bool doPopulate(std::shared_ptr<const IFileTree> parent,
                  std::vector<std::shared_ptr<FileTreeEntry>>& entries) const override
  {
    auto infoList = qDir.entryInfoList(qDir.filter() | QDir::NoDotAndDotDot,
                                       QDir::Name | QDir::DirsFirst | QDir::IgnoreCase);
    for (auto& info : infoList) {
      if (info.isDir()) {
        entries.push_back(
            std::make_shared<QDirFileTreeImpl>(parent, QDir(info.absoluteFilePath())));
      } else {
        entries.push_back(createFileEntry(parent, info.fileName()));
      }
    }

    // Vector is already sorted:
    return true;
  }

  std::shared_ptr<IFileTree> QDirFileTree::doClone() const
  {
    return std::make_shared<QDirFileTreeImpl>(nullptr, qDir);
  }

protected:
  QDir qDir;
};

// subclass of QDirFileTreeImpl that ignores meta.ini
//
// only used for the root folder, subdirectories are actually QDirFileTreeImpl
class QDirRootFileTreeImpl : public QDirFileTreeImpl
{
public:
  QDirRootFileTreeImpl(QDir dir)
      : FileTreeEntry(nullptr, dir.dirName()), QDirFileTreeImpl(nullptr, dir)
  {}

protected:
  bool doPopulate(std::shared_ptr<const IFileTree> parent,
                  std::vector<std::shared_ptr<FileTreeEntry>>& entries) const override
  {
    auto infoList = qDir.entryInfoList(qDir.filter() | QDir::NoDotAndDotDot,
                                       QDir::Name | QDir::DirsFirst | QDir::IgnoreCase);
    for (auto& info : infoList) {
      if (info.isDir()) {
        entries.push_back(
            std::make_shared<QDirFileTreeImpl>(parent, QDir(info.absoluteFilePath())));
      } else if (info.fileName().compare("meta.ini", Qt::CaseInsensitive) != 0) {
        entries.push_back(createFileEntry(parent, info.fileName()));
      }
    }

    // Vector is already sorted:
    return true;
  }

  std::shared_ptr<IFileTree> QDirFileTree::doClone() const
  {
    return std::make_shared<QDirRootFileTreeImpl>(qDir);
  }
};

/**
 *
 */
std::shared_ptr<const QDirFileTree> QDirFileTree::makeTree(QDir directory,
                                                           bool ignoreRootMeta)
{
  if (ignoreRootMeta) {
    return std::make_shared<QDirRootFileTreeImpl>(directory);
  } else {
    return std::make_shared<QDirFileTreeImpl>(nullptr, directory);
  }
}