1 /*
2     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 
8 #ifndef K3BDATAITEM_H
9 #define K3BDATAITEM_H
10 
11 #include "k3bmsf.h"
12 #include "k3b_export.h"
13 
14 #include <KIO/Global>
15 
16 #include <QMimeType>
17 #include <QString>
18 
19 namespace K3b {
20     class DirItem;
21     class DataDoc;
22 
23     /**
24      *@author Sebastian Trueg
25      */
26     class LIBK3B_EXPORT DataItem
27     {
28     public:
29         enum ItemFlag {
30             DIR = 0x1,
31             FILE = 0x2,
32             SPECIALFILE = 0x4,
33             SYMLINK = 0x8,
34             OLD_SESSION = 0x10,
35             BOOT_IMAGE = 0x20
36         };
37         Q_DECLARE_FLAGS( ItemFlags, ItemFlag )
38 
39     public:
40         explicit DataItem( const ItemFlags& flags = ItemFlags() );
41 
42         /**
43          * Default copy constructor.
44          *
45          * The result is an exact copy except that no parent dir it set and, thus, also no doc.
46          */
47         DataItem( const DataItem& );
48 
49         virtual ~DataItem();
50 
51         /**
52          * Return an exact copy of this data item.
53          *
54          * The result is an exact copy except that no parent dir it set and, thus, also no doc.
55          *
56          * Implementations should use the default constructor.
57          */
58         virtual DataItem* copy() const = 0;
59 
parent()60         DirItem* parent() const { return m_parentDir; }
61 
62         /**
63          * Remove this item from it's parent and return a pointer to it.
64          */
65         DataItem* take();
66 
67         virtual DataDoc* getDoc() const;
68         virtual QString k3bName() const;
69         virtual void setK3bName( const QString& );
70 
71         /**
72          * returns the path as defined by the k3b-hierarchy, NOT starting with a slash
73          * (since this is used for graft-points!)
74          * directories have a trailing "/"
75          */
76         virtual QString k3bPath() const;
77 
78         /**
79          * Returns the name of the item as used on the CD or DVD image.
80          *
81          * This is only valid after a call to @p DataDoc::prepareFilenames()
82          */
writtenName()83         QString writtenName() const { return m_writtenName; }
84 
85         /**
86          * \return the pure name used in the Iso9660 tree.
87          *
88          * This is only valid after a call to @p DataDoc::prepareFilenames()
89          */
iso9660Name()90         QString iso9660Name() const { return m_rawIsoName; }
91 
92         /**
93          * Returns the path of the item as written to the CD or DVD image.
94          *
95          * This is suited to be used for mkisofs graftpoints.
96          *
97          * This is only valid after a call to @p DataDoc::prepareFilenames()
98          */
99         virtual QString writtenPath() const;
100 
101         virtual QString iso9660Path() const;
102 
103         /**
104          * Used to set the written name by @p DataDoc::prepareFilenames()
105          */
setWrittenName(const QString & s)106         void setWrittenName( const QString& s ) { m_writtenName = s; }
107 
108         /**
109          * Used to set the pure Iso9660 name by @p DataDoc::prepareFilenames()
110          */
setIso9660Name(const QString & s)111         void setIso9660Name( const QString& s ) { m_rawIsoName = s; }
112 
113         virtual DataItem* nextSibling() const;
114 
115         /** returns the path to the file on the local filesystem */
localPath()116         virtual QString localPath() const { return QString(); }
117 
118         /**
119          * The size of the item
120          */
121         KIO::filesize_t size() const;
122 
123         /**
124          * \return The number of blocks (2048 bytes) occupied by this item.
125          *         This value equals to ceil(size()/2048)
126          */
127         Msf blocks() const;
128 
129         /**
130          * \return the dir of the item (or the item itself if it is a dir)
131          */
getDirItem()132         virtual DirItem* getDirItem() const { return parent(); }
133 
134         virtual void reparent( DirItem* );
135 
136         const ItemFlags& flags() const;
137         bool isDir() const;
138         bool isFile() const;
139         bool isSpecialFile() const;
140         bool isSymLink() const;
141         bool isFromOldSession() const;
142         bool isBootItem() const;
143 
144         bool hideOnRockRidge() const;
145         bool hideOnJoliet() const;
146 
147         virtual void setHideOnRockRidge( bool b );
148         virtual void setHideOnJoliet( bool b );
149 
sortWeight()150         virtual long sortWeight() const { return m_sortWeight; }
setSortWeight(long w)151         virtual void setSortWeight( long w ) { m_sortWeight = w; }
152 
153         virtual int depth() const;
154 
isValid()155         virtual bool isValid() const { return true; }
156 
157         // these are all needed for special fileitems like
158         // imported sessions or the movix filesystem
isRemoveable()159         virtual bool isRemoveable() const { return m_bRemoveable; }
isMoveable()160         virtual bool isMoveable() const { return m_bMovable; }
isRenameable()161         virtual bool isRenameable() const { return m_bRenameable; }
isHideable()162         virtual bool isHideable() const { return m_bHideable; }
writeToCd()163         virtual bool writeToCd() const { return m_bWriteToCd; }
extraInfo()164         virtual QString extraInfo() const { return m_extraInfo; }
165 
166         /**
167          * Default implementation returns the default mimetype.
168          */
169         virtual QMimeType mimeType() const;
170 
setRenameable(bool b)171         void setRenameable( bool b ) { m_bRenameable = b; }
setMoveable(bool b)172         void setMoveable( bool b ) { m_bMovable = b; }
setRemoveable(bool b)173         void setRemoveable( bool b ) { m_bRemoveable = b; }
setHideable(bool b)174         void setHideable( bool b ) { m_bHideable = b; }
setWriteToCd(bool b)175         void setWriteToCd( bool b ) { m_bWriteToCd = b; }
setExtraInfo(const QString & i)176         void setExtraInfo( const QString& i ) { m_extraInfo = i; }
177 
178     protected:
179         virtual KIO::filesize_t itemSize( bool followSymlinks ) const = 0;
180 
181         /**
182          * \param followSymlinks If true symlinks will be followed and their
183          *                       size equals the size of the file they are
184          *                       pointing to.
185          *
186          * \return The number of blocks (2048 bytes) occupied by this item.
187          */
188         virtual Msf itemBlocks( bool followSymlinks ) const;
189 
190         QString m_k3bName;
191 
192         void setFlags( const ItemFlags& flags );
setParentDir(DirItem * parentDir)193         void setParentDir( DirItem* parentDir ) { m_parentDir = parentDir; }
194 
195     private:
196         class Private;
197         Private* d;
198 
199         QString m_writtenName;
200         QString m_rawIsoName;
201         QString m_extraInfo;
202 
203         DirItem* m_parentDir;
204         long m_sortWeight;
205 
206         bool m_bHideOnRockRidge;
207         bool m_bHideOnJoliet;
208         bool m_bRemoveable;
209         bool m_bRenameable;
210         bool m_bMovable;
211         bool m_bHideable;
212         bool m_bWriteToCd;
213 
214         friend class DirItem;
215     };
216 }
217 
218 #endif
219