summaryrefslogtreecommitdiff
path: root/src/filetreeitem.h
blob: c5418d438a358b431036b7565823eab3bbf93441 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef MODORGANIZER_FILETREEITEM_INCLUDED
#define MODORGANIZER_FILETREEITEM_INCLUDED

#include "directoryentry.h"
#include <QFileIconProvider>

class FileTreeItem
{
  class Sorter;

public:
  using Children = std::vector<std::unique_ptr<FileTreeItem>>;

  enum Flag
  {
    NoFlags     = 0x00,
    Directory   = 0x01,
    FromArchive = 0x02,
    Conflicted  = 0x04
  };


  Q_DECLARE_FLAGS(Flags, Flag);

  FileTreeItem(
    FileTreeItem* parent, int originID,
    std::wstring dataRelativeParentPath, std::wstring realPath, Flags flags,
    std::wstring file, std::wstring mod);

  FileTreeItem(const FileTreeItem&) = delete;
  FileTreeItem& operator=(const FileTreeItem&) = delete;
  FileTreeItem(FileTreeItem&&) = default;
  FileTreeItem& operator=(FileTreeItem&&) = default;

  void add(std::unique_ptr<FileTreeItem> child)
  {
    child->m_indexGuess = m_children.size();
    m_children.push_back(std::move(child));
  }

  void insert(std::unique_ptr<FileTreeItem> child, std::size_t at);

  template <class Itor>
  void insert(Itor begin, Itor end, std::size_t at)
  {
    std::size_t nextRowGuess = m_children.size();
    for (auto itor=begin; itor!=end; ++itor) {
      (*itor)->m_indexGuess = nextRowGuess++;
    }

    m_children.insert(m_children.begin() + at, begin, end);
  }

  void remove(std::size_t i);
  void remove(std::size_t from, std::size_t n);

  void clear()
  {
    m_children.clear();
    m_loaded = false;
  }

  const Children& children() const
  {
    return m_children;
  }

  int childIndex(const FileTreeItem& item) const
  {
    if (item.m_indexGuess < m_children.size()) {
      if (m_children[item.m_indexGuess].get() == &item) {
        return static_cast<int>(item.m_indexGuess);
      }
    }

    for (std::size_t i=0; i<m_children.size(); ++i) {
      if (m_children[i].get() == &item) {
        item.m_indexGuess = i;
        return static_cast<int>(i);
      }
    }

    return -1;
  }

  void sort(int column, Qt::SortOrder order);

  FileTreeItem* parent()
  {
    return m_parent;
  }

  int originID() const
  {
    return m_originID;
  }

  const QString& virtualParentPath() const
  {
    return m_virtualParentPath;
  }

  QString virtualPath() const;

  const QString& filename() const
  {
    return m_file;
  }

  const std::wstring& filenameWs() const
  {
    return m_wsFile;
  }

  const std::wstring& filenameWsLowerCase() const
  {
    return m_wsLcFile;
  }

  const MOShared::DirectoryEntry::FileKey& key() const
  {
    return m_key;
  }

  const QString& mod() const
  {
    return m_mod;
  }

  QFont font() const;

  std::optional<uint64_t> fileSize() const;
  std::optional<QDateTime> lastModified() const;
  std::optional<QString> fileType() const;

  std::optional<uint64_t> compressedFileSize() const
  {
    return m_compressedFileSize;
  }

  void setFileSize(uint64_t size)
  {
    m_fileSize.set(size);
  }

  void setCompressedFileSize(uint64_t compressedSize)
  {
    m_compressedFileSize = compressedSize;
  }

  const QString& realPath() const
  {
    return m_realPath;
  }

  const QString& dataRelativeParentPath() const
  {
    return m_virtualParentPath;
  }

  QString dataRelativeFilePath() const;

  QFileIconProvider::IconType icon() const;

  bool isDirectory() const
  {
    return (m_flags & Directory);
  }

  bool isFromArchive() const
  {
    return (m_flags & FromArchive);
  }

  bool isConflicted() const
  {
    return (m_flags & Conflicted);
  }

  bool isHidden() const;

  bool hasChildren() const
  {
    if (!isDirectory()) {
      return false;
    }

    if (isLoaded() && m_children.empty()) {
      return false;
    }

    return true;
  }


  void setLoaded(bool b)
  {
    m_loaded = b;
  }

  bool isLoaded() const
  {
    return m_loaded;
  }

  void unload();

  void setExpanded(bool b)
  {
    m_expanded = b;
  }

  bool isStrictlyExpanded() const
  {
    return m_expanded;
  }

  bool areChildrenVisible() const;

  QString debugName() const;

private:
  template <class T>
  struct Cached
  {
    std::optional<T> value;
    bool failed = false;

    bool empty() const
    {
      return !failed && !value;
    }

    void set(T t)
    {
      value = std::move(t);
      failed = false;
    }

    void fail()
    {
      value = {};
      failed = true;
    }
  };

  static constexpr std::size_t NoIndexGuess =
    std::numeric_limits<std::size_t>::max();

  FileTreeItem* m_parent;
  mutable std::size_t m_indexGuess;

  const int m_originID;
  const QString m_virtualParentPath;
  const std::wstring m_wsRealPath;
  const QString m_realPath;
  const Flags m_flags;
  const std::wstring m_wsFile, m_wsLcFile;
  const MOShared::DirectoryEntry::FileKey m_key;
  const QString m_file;
  const QString m_mod;

  mutable Cached<uint64_t> m_fileSize;
  mutable Cached<QDateTime> m_lastModified;
  mutable Cached<QString> m_fileType;
  mutable std::optional<uint64_t> m_compressedFileSize;

  bool m_loaded;
  bool m_expanded;
  Children m_children;

  void getFileType() const;
};

#endif // MODORGANIZER_FILETREEITEM_INCLUDED