summaryrefslogtreecommitdiff
path: root/plugins/Themes/Oxygen2/fileTree.h
blob: e0ea1c73920e02a30d50a417620c6a028f0f2beb (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
/***********************************************************************
* Copyright 2003-2004  Max Howell <max.howell@methylblue.com>
* Copyright 2008-2009  Martin Sandsmark <martin.sandsmark@kde.org>
* Copyright 2017  Harald Sitter <sitter@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#ifndef FILETREE_H
#define FILETREE_H

#include <QByteArray> //qstrdup
#include <QFile> //decodeName()
#include <QLocale>
#include <unordered_map>
#include <string>

#include <stdlib.h>
#include "../../../interface/FacilityInterface.h"

class Folder;

class File
{
public:
    friend class Folder;
    static FacilityInterface *facilityEngine;

public:
    File(const std::string &name, uint64_t size) : m_parent(nullptr), m_name(name), m_size(size) {}
    File(const std::string &name, uint64_t size, Folder * parent) : m_parent(parent), m_name(name), m_size(size) {}
    virtual ~File() {
    }

    Folder *parent() const {
        return m_parent;
    }

    /** Do not use for user visible strings. Use name instead. */
    const std::string name() const {
        return m_name;
    }
    void setName(const std::string &name) {
        m_name=name;
    }
    /** Decoded name. Use when you need a QString. */
    QString decodedName() const {
        return QString::fromStdString(m_name);
    }
    /**
     * Human readable name (including native separators where applicable).
     * Only use for display.
     */
    QString displayName() const;

    uint64_t size() const {
        return m_size;
    }

    virtual bool isFolder() const {
        return false;
    }

    /**
     * Human readable path for display (including native separators where applicable.
     * Only use for display.
     */
    QString displayPath(const Folder * = nullptr) const;
    QString humanReadableSize() const {
        return QString::fromStdString(facilityEngine->sizeToString(m_size));
    }

    /** Builds a complete QUrl by walking up to root. */
    QUrl url(const Folder *root = nullptr) const;

protected:
    File(const char *name, uint64_t size, Folder *parent) : m_parent(parent), m_name(qstrdup(name)), m_size(size) {}

    Folder *m_parent; //0 if this is treeRoot
    std::string m_name;//speed boost with std::string in compare with char * due to string size defined without read all
    uint64_t   m_size;   //in Bytes

private:
    File(const File&);
    void operator=(const File&);
};


class Folder : public File
{
public:
    Folder(const std::string &name) : File(name, 0), m_children(0) {} //DON'T pass the full path!

    uint children() const {
        return m_children;
    }
    bool isFolder() const override {
        return true;
    }

    ///appends a Folder
    void append(Folder *d, const std::string &name);
    void append(Folder *d);
    ///appends a File
    void append(const std::string &name, uint64_t size);
    /// removes a file
    void remove(const File *f);
    std::unordered_map<std::string,Folder *> folders;
    std::vector<File *> onlyFiles;
private:
    void append(File *p);

    uint m_children;
private:
    Folder(const Folder&); //undefined
    void operator=(const Folder&); //undefined
};

#endif