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

filelight

  • sources
  • kde-4.12
  • kdeutils
  • filelight
  • src
  • part
scan.cpp
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 #include "scan.h"
23 
24 #include "remoteLister.h"
25 #include "fileTree.h"
26 #include "localLister.h"
27 
28 #include <KDebug>
29 
30 #include <QtGui/QApplication>
31 
32 namespace Filelight
33 {
34 
35 ScanManager::ScanManager(QObject *parent)
36  : QObject(parent)
37  , m_abort(false)
38  , m_files(0)
39  , m_mutex()
40  , m_thread(0)
41  , m_cache(new Chain<Folder>)
42 {
43  Filelight::LocalLister::readMounts();
44  connect(this, SIGNAL(branchCompleted(Folder*,bool)), this, SLOT(cacheTree(Folder*,bool)), Qt::QueuedConnection);
45  connect(this, SIGNAL(branchCacheHit(Folder*)), this, SLOT(foundCached(Folder*)), Qt::QueuedConnection);
46 }
47 
48 ScanManager::~ScanManager()
49 {
50  if (m_thread) {
51  kDebug() << "Attempting to abort scan operation..." << endl;
52  m_abort = true;
53  m_thread->wait();
54  }
55 
56  delete m_cache;
57 
58  //RemoteListers are QObjects and get automatically deleted
59 }
60 
61 bool ScanManager::running() const
62 {
63  return m_thread && m_thread->isRunning();
64 }
65 
66 bool ScanManager::start(const KUrl &url)
67 {
68  QMutexLocker locker(&m_mutex); // The m_mutex gets released once locker is destroyed (goes out of scope).
69 
70  //url is guaranteed clean and safe
71 
72  kDebug() << "Scan requested for: " << url.prettyUrl() << endl;
73 
74  if (running()) {
75  kWarning() << "Tried to launch two concurrent scans, aborting old one...";
76  abort();
77  }
78 
79  m_files = 0;
80  m_abort = false;
81 
82  if (url.protocol() == QLatin1String( "file" ))
83  {
84  const QString path = url.path(KUrl::AddTrailingSlash);
85 
86  Chain<Folder> *trees = new Chain<Folder>;
87 
88  /* CHECK CACHE
89  * user wants: /usr/local/
90  * cached: /usr/
91  *
92  * user wants: /usr/
93  * cached: /usr/local/, /usr/include/
94  */
95 
96  for (Iterator<Folder> it = m_cache->iterator(); it != m_cache->end(); ++it)
97  {
98  QString cachePath = (*it)->name();
99 
100  if (path.startsWith(cachePath)) //then whole tree already scanned
101  {
102  //find a pointer to the requested branch
103 
104  kDebug() << "Cache-(a)hit: " << cachePath << endl;
105 
106  QStringList split = path.mid(cachePath.length()).split(QLatin1Char( '/' ));
107  Folder *d = *it;
108  Iterator<File> jt;
109 
110  while (!split.isEmpty() && d != NULL) //if NULL we have got lost so abort!!
111  {
112  jt = d->iterator();
113 
114  const Link<File> *end = d->end();
115  QString s = split.first();
116  if (s.isEmpty()) //found the dir
117  break;
118  s += QLatin1Char( '/' );
119 
120  for (d = 0; jt != end; ++jt)
121  if (s == (*jt)->name())
122  {
123  d = (Folder*)*jt;
124  break;
125  }
126 
127  split.pop_front();
128  }
129 
130  if (d)
131  {
132  delete trees;
133 
134  //we found a completed tree, thus no need to scan
135  kDebug() << "Found cache-handle, generating map.." << endl;
136 
137  emit branchCacheHit(d);
138 
139  return true;
140  }
141  else
142  {
143  //something went wrong, we couldn't find the folder we were expecting
144  kError() << "Didn't find " << path << " in the cache!\n";
145  delete it.remove(); //safest to get rid of it
146  break; //do a full scan
147  }
148  }
149  else if (cachePath.startsWith(path)) //then part of the requested tree is already scanned
150  {
151  kDebug() << "Cache-(b)hit: " << cachePath << endl;
152  it.transferTo(*trees);
153  }
154  }
155 
156  m_url.setPath(path); //FIXME stop switching between paths and KURLs all the time
157  QApplication::setOverrideCursor(Qt::BusyCursor);
158  //starts listing by itself
159  m_thread = new Filelight::LocalLister(path, trees, this);
160  connect(m_thread, SIGNAL(branchCompleted(Folder*,bool)), this, SLOT(cacheTree(Folder*,bool)), Qt::QueuedConnection);
161  m_thread->start();
162 
163  return true;
164  }
165 
166  m_url = url;
167  QApplication::setOverrideCursor(Qt::BusyCursor);
168  //will start listing straight away
169  Filelight::RemoteLister *remoteLister = new Filelight::RemoteLister(url, (QWidget*)parent(), this);
170  connect(remoteLister, SIGNAL(branchCompleted(Folder*,bool)), this, SLOT(cacheTree(Folder*,bool)), Qt::QueuedConnection);
171  remoteLister->setParent(this);
172  remoteLister->setObjectName(QLatin1String( "remote_lister" ));
173  remoteLister->openUrl(url);
174  return true;
175 }
176 
177 bool
178 ScanManager::abort()
179 {
180  m_abort = true;
181 
182  delete findChild<RemoteLister *>(QLatin1String( "remote_lister" ));
183 
184  return m_thread && m_thread->wait();
185 }
186 
187 void
188 ScanManager::emptyCache()
189 {
190  m_abort = true;
191 
192  if (m_thread && m_thread->isRunning())
193  m_thread->wait();
194 
195  emit aboutToEmptyCache();
196 
197  m_cache->empty();
198 }
199 
200 void
201 ScanManager::cacheTree(Folder *tree, bool finished)
202 {
203  QMutexLocker locker(&m_mutex); // This gets released once it is destroyed.
204 
205  if (m_thread) {
206  kDebug() << "Waiting for thread to terminate ...";
207  m_thread->wait();
208  kDebug() << "Thread terminated!";
209  delete m_thread; //note the lister deletes itself
210  m_thread = 0;
211  }
212 
213  emit completed(tree);
214 
215  if (tree) {
216  //we don't cache foreign stuff
217  //we don't recache stuff (thus only type 1000 events)
218  if (m_url.protocol() == QLatin1String( "file" ) && finished)
219  //TODO sanity check the cache
220  m_cache->append(tree);
221  }
222  else //scan failed
223  m_cache->empty(); //FIXME this is safe but annoying
224 
225  QApplication::restoreOverrideCursor();
226 }
227 
228 void
229 ScanManager::foundCached(Folder *tree)
230 {
231  emit completed(tree);
232  QApplication::restoreOverrideCursor();
233 }
234 
235 
236 }
237 
238 #include "scan.moc"
Filelight::ScanManager::cacheTree
void cacheTree(Folder *, bool)
Definition: scan.cpp:201
Filelight::ScanManager::~ScanManager
virtual ~ScanManager()
Definition: scan.cpp:48
QWidget
Filelight::LocalLister::readMounts
static void readMounts()
Definition: localLister.cpp:228
QObject
Filelight::ScanManager::completed
void completed(Folder *)
Filelight::ScanManager::running
bool running() const
Definition: scan.cpp:61
Filelight::ScanManager::emptyCache
void emptyCache()
Definition: scan.cpp:188
Filelight::ScanManager::foundCached
void foundCached(Folder *)
Definition: scan.cpp:229
Link< File >
Filelight::RemoteLister
Definition: remoteLister.h:33
Chain::empty
void empty()
Definition: fileTree.h:197
Filelight::LocalLister
Definition: localLister.h:35
Folder
Definition: fileTree.h:271
Filelight::ScanManager::start
bool start(const KUrl &)
Definition: scan.cpp:66
fileTree.h
Filelight::ScanManager::branchCacheHit
void branchCacheHit(Folder *tree)
Filelight::ScanManager::aboutToEmptyCache
void aboutToEmptyCache()
Chain::end
const Link< T > * end() const
Definition: fileTree.h:209
Filelight::ScanManager::ScanManager
ScanManager(QObject *parent)
Definition: scan.cpp:35
Filelight::parent
http QObject * parent
Definition: part.cpp:74
Filelight::ScanManager::branchCompleted
void branchCompleted(Folder *tree, bool finished)
localLister.h
Iterator
Definition: fileTree.h:38
scan.h
Chain
Definition: fileTree.h:40
Filelight::ScanManager::abort
bool abort()
Definition: scan.cpp:178
remoteLister.h
Chain::append
void append(T *const data)
Definition: fileTree.h:170
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:08:08 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
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • 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