summaryrefslogtreecommitdiffstats
path: root/konq-plugins/fsview/scan.h
blob: 38b015c300d13323e63a71fa46aef8aaff7e2ac1 (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
/* This file is part of FSView.
   Copyright (C) 2002, 2003 Josef Weidendorfer <Josef.Weidendorfer@gmx.de>

   KCachegrind 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, version 2.

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

/*
 * Classes for breadth-first search in local filesystem
 */

#ifndef FSDIR_H
#define FSDIR_H

#include <qptrlist.h>
#include <qvaluevector.h>
#include <qfile.h>

/* Use KDE_lstat and KIO::fileoffset_t for 64-bit sizes */
#include <klargefile.h>
#include <kio/global.h>

class ScanDir;
class ScanFile;

class ScanItem
{
 public:
  ScanItem(const QString& p, ScanDir* d)
    { absPath = p; dir = d; }

  QString absPath;
  ScanDir* dir;
};

typedef QPtrList<ScanItem> ScanItemList;


/**
 * Listener for events from directory scanning.
 *
 * You can register a listener for the ScanManager to get
 * all scan events and a listener for every ScanDir for
 * directory specific scan events.
 *
 * sizeChanged is called when a scan of a subdirectory
 * finished.
 */
class ScanListener
{
 public:
  virtual void scanStarted(ScanDir*) {}
  virtual void sizeChanged(ScanDir*) {}
  virtual void scanFinished(ScanDir*) {}
  // destroyed events are not delivered to listeners of ScanManager
  virtual void destroyed(ScanDir*) {}
  virtual void destroyed(ScanFile*) {}
};



/**
 * ScanManager
 *
 * Start/Stop/Restart Scans. Example:
 *
 *   ScanManager m("/opt");
 *   m.startScan();
 *   while(m.scan());
 */
class ScanManager
{
 public:
  ScanManager();
  ScanManager(const QString& path);
  ~ScanManager();

  /** Set the top path for scanning
   * The ScanDir object created gets attribute data.
   */
  ScanDir* setTop(const QString& path, int data = 0);
  ScanDir* top() { return _topDir; }

  bool scanRunning();
  unsigned int scanLength() { return _list.count(); }
  
  /**
   * Starts the scan. Stop previous scan if running.
   * For the actual scan to happen, you have to call
   * scan() peridically.
   *
   * If from !=0, restart scan at given position; from must
   * be from the previous scan of this manager.
   */
  void startScan(ScanDir* from = 0);

  /** Stop a current running scan.
   * Make all directories to finish their scan.
   */
  void stopScan();

  /**
   * Scan first directory from todo list.
   * Directories added to the todo list are attributed with data. 
   * Returns the number of new subdirectories created for scanning.
   */
  int scan(int data);

  /* set listener to get a callbacks from this ScanDir */
  void setListener(ScanListener*);
  ScanListener* listener() { return _listener; }

 private:
  ScanItemList _list;
  ScanDir* _topDir;
  ScanListener* _listener;
};

class ScanFile
{
 public:
  ScanFile();
  ScanFile(const QString& n, KIO::fileoffset_t s);
  ~ScanFile();

  const QString& name() { return _name; }
  KIO::fileoffset_t size() { return _size; }

  /* set listener to get callbacks from this ScanDir */
  void setListener(ScanListener* l) { _listener = l; }
  ScanListener* listener() { return _listener; }

 private:
  QString _name;
  KIO::fileoffset_t _size;
  ScanListener* _listener;
};

typedef QValueVector<ScanFile> ScanFileVector;
typedef QValueVector<ScanDir> ScanDirVector;

/**
 * A directory to scan.
 * You can attribute a directory to scan with a
 * integer data attribute.
 */
class ScanDir
{
 public:
  ScanDir();
  ScanDir(const QString& n, ScanManager* m,
	  ScanDir* p = 0, int data = 0);
  ~ScanDir();
  
  /* Get items of this directory
   * and append subdirectories to todo list.
   *
   * Directories added to the todo list are attributed with data.
   * Returns the number of new subdirectories created for scanning.
   */
  int scan(ScanItem* si, ScanItemList& list, int data);

  /* clear scan objects below */
  void clear();

  /*
   * Setup for child rescan
   */
  void setupChildRescan();

  /* Absolute path. Warning: Slow, loops to top parent. */
  QString path();

  /* get integer data attribute */
  int data() { return _data; }
  void setData(int d) { _data = d; }

  ScanFileVector& files() { return _files; }
  ScanDirVector& dirs() { return _dirs; }
  const QString& name() { return _name; }
  KIO::fileoffset_t size() { update(); return _size; }
  unsigned int fileCount() { update(); return _fileCount; }
  unsigned int dirCount() { update(); return _dirCount; }
  ScanDir* parent() { return _parent; }
  bool scanStarted() { return (_dirsFinished >= 0); }
  bool scanFinished() { return (_dirsFinished == (int)_dirs.count()); }
  bool scanRunning() { return scanStarted() && !scanFinished(); }
  
  /* set listener to get a callbacks from this ScanDir */
  void setListener(ScanListener*);
  ScanListener* listener() { return _listener; }
  ScanManager* manager() { return _manager; }

  /* force current scan to be finished */
  void finish();

 private:
  void update();

  /* this propagates file count and size to upper dirs */
  void subScanFinished();
  void callScanStarted();
  void callSizeChanged();
  void callScanFinished();
  
  ScanFileVector _files;
  ScanDirVector _dirs;

  QString _name;
  bool _dirty; /* needs a call to update() */
  KIO::fileoffset_t _size, _fileSize;
  unsigned int _fileCount, _dirCount;
  int _dirsFinished, _data;
  ScanDir* _parent;
  ScanListener* _listener;
  ScanManager* _manager;
};

#endif