1 /*
2     SPDX-FileCopyrightText: 2011 Michal Malek <michalm@jabster.pl>
3     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 
8 #ifndef K3BDIRITEM_H
9 #define K3BDIRITEM_H
10 
11 #include "k3bdataitem.h"
12 #include "k3b_export.h"
13 
14 #include <KIO/Global>
15 
16 #include <QList>
17 #include <QString>
18 
19 namespace K3b {
20     class DataDoc;
21 
22     class LIBK3B_EXPORT DirItem : public DataItem
23     {
24     public:
25         typedef QList<DataItem*> Children;
26 
27     public:
28         explicit DirItem( const QString& name, const ItemFlags& flags = ItemFlags() );
29 
30         /**
31          * Default copy constructor. Copies the dir including all children. However, none of the
32          * children will have set a doc and the copy dir will not have set a parent dir.
33          */
34         DirItem( const DirItem& );
35 
36         ~DirItem() override;
37 
38         DataItem* copy() const override;
39 
40         DirItem* getDirItem() const override;
41 
children()42         Children const& children() const { return m_children; }
43         DirItem* addDataItem( DataItem* item );
44         void addDataItems( Children const& items );
45         void removeDataItems( int start, int count );
46         DataItem* takeDataItem( DataItem* item );
47         Children takeDataItems( int start, int count );
48 
49         DataItem* nextSibling() const override;
50         DataItem* nextChild( DataItem* ) const;
51 
52         bool alreadyInDirectory( const QString& fileName ) const;
53         DataItem* find( const QString& filename ) const;
54         DataItem* findByPath( const QString& );
55 
56         long numFiles() const;
57         long numDirs() const;
58 
isEmpty()59         bool isEmpty() const { return ( numDirs() + numFiles() == 0 ); }
60 
61         /**
62          * returns true if item is a subItem of
63          * this dir item
64          * (returns also true if item == this
65          */
66         bool isSubItem( const DataItem* item ) const;
67 
68         bool isRemoveable() const override;
69 
70         /**
71          * Recursively creates a directory.
72          */
73         bool mkdir( const QString& dir );
74 
setLocalPath(const QString & p)75         void setLocalPath( const QString& p ) { m_localPath = p; }
localPath()76         QString localPath() const override { return m_localPath; }
77 
78         QMimeType mimeType() const override;
79 
80         /**
81          * \reimplemented
82          */
83         bool writeToCd() const override;
84 
85     protected:
86         /**
87          * Normally one does not use this method but DataItem::size()
88          *
89          * This method does not take into account the possibility to share the data
90          * between files with the same inode in an iso9660 filesystem.
91          * For that one has to use FileCompilationSizeHandler.
92          */
93         KIO::filesize_t itemSize( bool followSymlinks ) const override;
94 
95         /*
96          * Normally one does not use this method but DataItem::blocks()
97          */
98         Msf itemBlocks( bool followSymlinks ) const override;
99 
100     private:
101         /**
102          * this recursively updates the size of the directories.
103          * The size of this dir and the parent dir is updated.
104          * These values are just used for user information.
105          */
106         void updateSize( DataItem*, bool removed = false );
107         /**
108          * Updates the number of files and directories. These values are
109          * just used for user information.
110          */
111         void updateFiles( long files, long dirs );
112         /**
113          * Unsets OLD_SESSION flag when directory no longer has
114          * children from previous sessions
115          */
116         void updateOldSessionFlag();
117 
118         bool canAddDataItem( DataItem* item ) const;
119         void addDataItemImpl( DataItem* item );
120 
121         mutable Children m_children;
122 
123         // size of the items simply added
124         KIO::filesize_t m_size;
125         KIO::filesize_t m_followSymlinksSize;
126 
127         // number of blocks (2048 bytes) used by all the items
128         long m_blocks;
129         long m_followSymlinksBlocks;
130 
131         long m_files;
132         long m_dirs;
133 
134         // HACK: store the original path to be able to use it's permissions
135         //       remove this once we have a backup project
136         QString m_localPath;
137     };
138 
139 
140     class RootItem : public DirItem
141     {
142     public:
143         explicit RootItem( DataDoc& doc );
144         ~RootItem() override;
145 
146         DataDoc* getDoc() const override;
147         QString k3bName() const override;
148         void setK3bName( const QString& ) override;
149 
isMoveable()150         bool isMoveable() const override { return false; }
isRemoveable()151         bool isRemoveable() const override { return false; }
152 
153     private:
154         DataDoc& m_doc;
155     };
156 }
157 #endif
158