summaryrefslogtreecommitdiff
path: root/src/qdirfiletree.cpp
blob: 691bac875732c495adbd86068ca6c8406fd4a7c0 (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
#include "qdirfiletree.h"

#include <QDirIterator>

using namespace MOBase;

class QFileTreeEntry : public virtual FileTreeEntry {
public:
  using FileTreeEntry::FileTreeEntry;

  QFileTreeEntry(std::shared_ptr<const IFileTree> parent, QFileInfo fileInfo) :
    FileTreeEntry(parent, fileInfo.fileName()), m_FileInfo(fileInfo) { }

  QDateTime time() const override {
    return m_FileInfo.lastModified();
  }

protected:
  QFileInfo m_FileInfo;
};

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(std::make_shared<QFileTreeEntry>(parent, info));
      }
    }

    // Vector is already sorted:
    return true;
  }

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

private:
  QDir qDir;

};

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