1 // Copyright (C) 2014-2018 Manuel Schneider
2 
3 #pragma once
4 #include <QDateTime>
5 #include <QDebug>
6 #include <QDir>
7 #include <QString>
8 #include <QMimeDatabase>
9 #include <QRegularExpression>
10 #include <memory>
11 #include <vector>
12 #include <set>
13 #include "indexfile.h"
14 
15 namespace Files {
16 
17 class Visitor;
18 class IndexSettings;
19 
20 enum class PatternType {
21     Include,
22     Exclude
23 };
24 
25 struct IgnoreEntry {
IgnoreEntryIgnoreEntry26     IgnoreEntry(QRegularExpression regex, PatternType type) : regex(regex), type(type) {}
27     QRegularExpression regex;
28     PatternType type;
29 };
30 
31 class IndexTreeNode final : public std::enable_shared_from_this<IndexTreeNode>
32 {
33 public:
34 
35     IndexTreeNode();
36     IndexTreeNode(const IndexTreeNode & other);
37     IndexTreeNode(QString name, QDateTime lastModified, std::shared_ptr<IndexTreeNode> parent = std::shared_ptr<IndexTreeNode>());
38     IndexTreeNode(QString name, std::shared_ptr<IndexTreeNode> parent = std::shared_ptr<IndexTreeNode>());
39     ~IndexTreeNode();
40 
41     void accept(Visitor &visitor);
42 
43     void removeDownlinks();
44 
45     QString path() const;
46 
47     void update(const bool &abort, IndexSettings indexSettings);
48 
49     QJsonObject serialize();
50     void deserialize(const QJsonObject &, std::shared_ptr<IndexTreeNode> parent = std::shared_ptr<IndexTreeNode>());
51 
52     const std::vector<std::shared_ptr<IndexFile> > &items() const;
53 
54 private:
55 
56     void updateRecursion(const bool &abort,
57                          const QMimeDatabase &mimeDatabase,
58                          const IndexSettings &indexSettings,
59                          std::set<QString> *indexedDirs,
60                          const std::vector<IgnoreEntry> &ignoreEntries = std::vector<IgnoreEntry>());
61 
62     std::shared_ptr<IndexTreeNode> parent;
63     std::vector<std::shared_ptr<IndexTreeNode>> children;
64     QString name;
65     QDateTime lastModified;
66     std::vector<std::shared_ptr<Files::IndexFile>> items_;
67 
68     static constexpr const char* IGNOREFILE = ".albertignore";
69 };
70 
71 
72 /** ***********************************************************************************************/
73 class IndexSettings
74 {
75 public:
76     const std::vector<QRegExp> &filters() const;
77     void setFilters(std::vector<QRegExp> value);
78     void setFilters(QStringList value);
79 
80     bool indexHidden() const;
81     void setIndexHidden(bool value);
82 
83     bool followSymlinks() const;
84     void setFollowSymlinks(bool value);
85 
86     bool forceUpdate() const;
87     void setForceUpdate(bool value);
88 
89     bool fuzzy() const;
90     void setFuzzy(bool value);
91 
92 
93 private:
94 
95     std::vector<QRegExp> mimefilters_;
96     bool indexHidden_ = false;
97     bool followSymlinks_ = false;
98     bool fuzzy_ = false;
99     bool forceUpdate_ = false; // Ignore lastModified, force update
100 
101 };
102 
103 
104 /** ***********************************************************************************************/
105 class Visitor {
106 public:
~Visitor()107     virtual ~Visitor() { }
108     virtual void visit(IndexTreeNode *) = 0;
109 };
110 
111 }
112