• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

filelight

  • sources
  • kde-4.14
  • kdeutils
  • filelight
  • src
  • part
fileTree.h
Go to the documentation of this file.
1 /***********************************************************************
2 * Copyright 2003-2004 Max Howell <max.howell@methylblue.com>
3 * Copyright 2008-2009 Martin Sandsmark <martin.sandsmark@kde.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License or (at your option) version 3 or any later version
9 * accepted by the membership of KDE e.V. (or its successor approved
10 * by the membership of KDE e.V.), which shall act as a proxy
11 * defined in Section 14 of version 3 of the license.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ***********************************************************************/
21 
22 #ifndef FILETREE_H
23 #define FILETREE_H
24 
25 #include <QtCore/QByteArray> //qstrdup
26 #include <QtCore/QFile> //decodeName()
27 #include <QtCore/QDebug>
28 #include <kglobal.h>
29 #include <klocale.h>
30 
31 #include <stdlib.h>
32 
33 //TODO these are pointlessly general purpose now, make them incredibly specific
34 
35 typedef quint64 FileSize;
36 typedef quint64 Dirsize; //**** currently unused
37 
38 template <class T> class Iterator;
39 template <class T> class ConstIterator;
40 template <class T> class Chain;
41 
42 template <class T>
43 class Link
44 {
45 public:
46  Link(T* const t) : prev(this), next(this), data(t) {}
47  Link() : prev(this), next(this), data(0) {}
48 
49 //TODO unlinking is slow and you don't use it very much in this context.
50 // ** Perhaps you can make a faster deletion system that doesn't bother tidying up first
51 // ** and then you MUST call some kind of detach() function when you remove elements otherwise
52  ~Link() {
53  delete data;
54  unlink();
55  }
56 
57  friend class Iterator<T>;
58  friend class ConstIterator<T>;
59  friend class Chain<T>;
60 
61 private:
62  void unlink() {
63  prev->next = next;
64  next->prev = prev;
65  prev = next = this;
66  }
67 
68  Link<T>* prev;
69  Link<T>* next;
70 
71  T* data; //ensure only iterators have access to this
72 };
73 
74 
75 template <class T>
76 class Iterator
77 {
78 public:
79  Iterator() : link(0) { } //**** remove this, remove this REMOVE THIS!!! dangerous as your implementation doesn't test for null links, always assumes they can be derefenced
80  Iterator(Link<T> *p) : link(p) { }
81 
82  bool operator==(const Iterator<T>& it) const {
83  return link == it.link;
84  }
85  bool operator!=(const Iterator<T>& it) const {
86  return link != it.link;
87  }
88  bool operator!=(const Link<T> *p) const {
89  return p != link;
90  }
91 
92  //here we have a choice, really I should make two classes one const the other not
93  const T* operator*() const {
94  return link->data;
95  }
96  T* operator*() {
97  return link->data;
98  }
99 
100  Iterator<T>& operator++() {
101  link = link->next; //**** does it waste time returning in places where we don't use the retval?
102  return *this;
103  }
104 
105  bool isNull() const {
106  return (link == 0); //REMOVE WITH ABOVE REMOVAL you don't want null iterators to be possible
107  }
108 
109  void transferTo(Chain<T> &chain)
110  {
111  chain.append(remove());
112  }
113 
114  T* remove() //remove from list, delete Link, data is returned NOT deleted
115 
116  {
117  T* const d = link->data;
118  Link<T>* const p = link->prev;
119 
120  link->data = 0;
121  delete link;
122  link = p; //make iterator point to previous element, YOU must check this points to an element
123 
124  return d;
125  }
126 
127 private:
128  Link<T> *link;
129 };
130 
131 
132 template <class T>
133 class ConstIterator
134 {
135 public:
136  ConstIterator(Link<T> *p) : link(p) { }
137 
138  bool operator==(const Iterator<T>& it) const {
139  return link == it.link;
140  }
141  bool operator!=(const Iterator<T>& it) const {
142  return link != it.link;
143  }
144  bool operator!=(const Link<T> *p) const {
145  return p != link;
146  }
147 
148  const T* operator*() const {
149  return link->data;
150  }
151 
152  ConstIterator<T>& operator++() {
153  link = link->next;
154  return *this;
155  }
156 
157 private:
158  const Link<T> *link;
159 };
160 
161 
162 template <class T>
163 class Chain
164 {
165 public:
166  virtual ~Chain() {
167  empty();
168  }
169 
170  void append(T* const data)
171  {
172  Link<T>* const link = new Link<T>(data);
173 
174  link->prev = head.prev;
175  link->next = &head;
176 
177  head.prev->next = link;
178  head.prev = link;
179  }
180 
181  void transferTo(Chain &c)
182  {
183  if (isEmpty()) return;
184 
185  Link<T>* const first = head.next;
186  Link<T>* const last = head.prev;
187 
188  head.unlink();
189 
190  first->prev = c.head.prev;
191  c.head.prev->next = first;
192 
193  last->next = &c.head;
194  c.head.prev = last;
195  }
196 
197  void empty() {
198  while (head.next != &head) {
199  delete head.next;
200  }
201  }
202 
203  Iterator<T> iterator() const {
204  return Iterator<T>(head.next);
205  }
206  ConstIterator<T> constIterator() const {
207  return ConstIterator<T>(head.next);
208  }
209  const Link<T> *end() const {
210  return &head;
211  }
212  bool isEmpty() const {
213  return head.next == &head;
214  }
215 
216 private:
217  Link<T> head;
218  void operator=(const Chain&);
219 };
220 
221 
222 class Folder;
223 class QString;
224 
225 class File
226 {
227 public:
228  friend class Folder;
229 
230 public:
231  File(const char *name, FileSize size) : m_parent(0), m_name(qstrdup(name)), m_size(size) {}
232  virtual ~File() {
233  delete [] m_name;
234  }
235 
236  Folder *parent() const {
237  return m_parent;
238  }
239  const char *name8Bit() const {
240  return m_name;
241  }
242  FileSize size() const {
243  return m_size;
244  }
245  QString name() const {
246  return QFile::decodeName(m_name);
247  }
248 
249  virtual bool isFolder() const {
250  return false;
251  }
252 
253  QString fullPath(const Folder* = 0) const;
254  QString humanReadableSize() const {
255  return KGlobal::locale()->formatByteSize(m_size);
256  }
257 
258 protected:
259  File(const char *name, FileSize size, Folder *parent) : m_parent(parent), m_name(qstrdup(name)), m_size(size) {}
260 
261  Folder *m_parent; //0 if this is treeRoot
262  char *m_name;
263  FileSize m_size; //in units of KiB
264 
265 private:
266  File(const File&);
267  void operator=(const File&);
268 };
269 
270 
271 class Folder : public Chain<File>, public File
272 {
273 public:
274  Folder(const char *name) : File(name, 0), m_children(0) {} //DON'T pass the full path!
275 
276  uint children() const {
277  return m_children;
278  }
279  virtual bool isFolder() const {
280  return true;
281  }
282 
284  void append(Folder *d, const char *name=0)
285  {
286  if (name) {
287  delete [] d->m_name;
288  d->m_name = qstrdup(name);
289  } //directories that had a fullpath copy just their names this way
290 
291  m_children += d->children(); //doesn't include the dir itself
292  d->m_parent = this;
293  append((File*)d); //will add 1 to filecount for the dir itself
294  }
295 
297  void append(const char *name, FileSize size)
298  {
299  append(new File(name, size, this));
300  }
301 
303  void remove(const File *f) {
304  for (Iterator<File> it = iterator(); it != end(); ++it)
305  if (f == (*it))
306  it.remove();
307 
308  for (Folder *d = this; d; d = d->parent())
309  d->m_size -= f->size();
310  }
311 
312 private:
313  void append(File *p)
314  {
315  m_children++;
316  m_size += p->size();
317  Chain<File>::append(p);
318  }
319 
320  uint m_children;
321 
322 private:
323  Folder(const Folder&); //undefined
324  void operator=(const Folder&); //undefined
325 };
326 
327 #endif
Iterator::operator!=
bool operator!=(const Link< T > *p) const
Definition: fileTree.h:88
Chain::~Chain
virtual ~Chain()
Definition: fileTree.h:166
File::isFolder
virtual bool isFolder() const
Definition: fileTree.h:249
Iterator::operator*
const T * operator*() const
Definition: fileTree.h:93
Folder::Folder
Folder(const char *name)
Definition: fileTree.h:274
Iterator::Iterator
Iterator(Link< T > *p)
Definition: fileTree.h:80
Iterator::transferTo
void transferTo(Chain< T > &chain)
Definition: fileTree.h:109
Chain::isEmpty
bool isEmpty() const
Definition: fileTree.h:212
Iterator::operator*
T * operator*()
Definition: fileTree.h:96
Dirsize
quint64 Dirsize
Definition: fileTree.h:36
File::size
FileSize size() const
Definition: fileTree.h:242
Chain::constIterator
ConstIterator< T > constIterator() const
Definition: fileTree.h:206
ConstIterator::operator==
bool operator==(const Iterator< T > &it) const
Definition: fileTree.h:138
File::humanReadableSize
QString humanReadableSize() const
Definition: fileTree.h:254
File::fullPath
QString fullPath(const Folder *=0) const
Definition: fileTree.cpp:28
FileSize
quint64 FileSize
Definition: fileTree.h:35
Link
Definition: fileTree.h:43
File::name8Bit
const char * name8Bit() const
Definition: fileTree.h:239
Chain::empty
void empty()
Definition: fileTree.h:197
Iterator::operator++
Iterator< T > & operator++()
Definition: fileTree.h:100
Folder
Definition: fileTree.h:271
File::m_parent
Folder * m_parent
Definition: fileTree.h:261
File::m_size
FileSize m_size
Definition: fileTree.h:263
Iterator::Iterator
Iterator()
Definition: fileTree.h:79
ConstIterator::ConstIterator
ConstIterator(Link< T > *p)
Definition: fileTree.h:136
Chain::end
const Link< T > * end() const
Definition: fileTree.h:209
QString
ConstIterator::operator++
ConstIterator< T > & operator++()
Definition: fileTree.h:152
Iterator::operator!=
bool operator!=(const Iterator< T > &it) const
Definition: fileTree.h:85
Folder::append
void append(const char *name, FileSize size)
appends a File
Definition: fileTree.h:297
Folder::append
void append(Folder *d, const char *name=0)
appends a Folder
Definition: fileTree.h:284
Link::Link
Link()
Definition: fileTree.h:47
Folder::children
uint children() const
Definition: fileTree.h:276
Iterator
Definition: fileTree.h:38
Link::~Link
~Link()
Definition: fileTree.h:52
ConstIterator::operator*
const T * operator*() const
Definition: fileTree.h:148
Iterator::operator==
bool operator==(const Iterator< T > &it) const
Definition: fileTree.h:82
Chain
Definition: fileTree.h:40
File::File
File(const char *name, FileSize size)
Definition: fileTree.h:231
File::File
File(const char *name, FileSize size, Folder *parent)
Definition: fileTree.h:259
File::m_name
char * m_name
Definition: fileTree.h:262
Folder::isFolder
virtual bool isFolder() const
Definition: fileTree.h:279
File::name
QString name() const
Definition: fileTree.h:245
File::~File
virtual ~File()
Definition: fileTree.h:232
ConstIterator
Definition: fileTree.h:39
Link::Link
Link(T *const t)
Definition: fileTree.h:46
Chain::transferTo
void transferTo(Chain &c)
Definition: fileTree.h:181
File::parent
Folder * parent() const
Definition: fileTree.h:236
File
Definition: fileTree.h:225
ConstIterator::operator!=
bool operator!=(const Iterator< T > &it) const
Definition: fileTree.h:141
QFile::decodeName
QString decodeName(const QByteArray &localFileName)
ConstIterator::operator!=
bool operator!=(const Link< T > *p) const
Definition: fileTree.h:144
Iterator::isNull
bool isNull() const
Definition: fileTree.h:105
Chain::append
void append(T *const data)
Definition: fileTree.h:170
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

filelight

Skip menu "filelight"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal