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

kleopatra

  • sources
  • kde-4.12
  • kdepim
  • kleopatra
  • utils
filesystemwatcher.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  filesystemwatcher.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "filesystemwatcher.h"
36 
37 #include <KDebug>
38 
39 #include <QFileSystemWatcher>
40 #include <QString>
41 #include <QStringList>
42 #include <QTimer>
43 #include <QDir>
44 
45 #include <kleo/stl_util.h>
46 
47 #include <boost/bind.hpp>
48 
49 #include <set>
50 #include <cassert>
51 
52 using namespace Kleo;
53 using namespace boost;
54 
55 class FileSystemWatcher::Private {
56  FileSystemWatcher* const q;
57 public:
58  explicit Private( FileSystemWatcher* qq, const QStringList& paths=QStringList() );
59  ~Private() {
60  delete m_watcher;
61  }
62 
63  void onFileChanged( const QString& path );
64  void onDirectoryChanged( const QString& path );
65  void handleTimer();
66  void onTimeout();
67 
68  void connectWatcher();
69 
70  QFileSystemWatcher* m_watcher;
71  QTimer m_timer;
72  std::set<QString> m_seenPaths;
73  std::set<QString> m_cachedDirectories;
74  std::set<QString> m_cachedFiles;
75  QStringList m_paths, m_blacklist, m_whitelist;
76 };
77 
78 FileSystemWatcher::Private::Private( FileSystemWatcher* qq, const QStringList& paths )
79  : q( qq ),
80  m_watcher( 0 ),
81  m_paths( paths )
82 {
83  m_timer.setSingleShot( true );
84  connect( &m_timer, SIGNAL(timeout()), q, SLOT(onTimeout()) );
85 }
86 
87 static bool is_matching( const QString & file, const QStringList & list ) {
88  Q_FOREACH( const QString & entry, list )
89  if ( QRegExp( entry, Qt::CaseInsensitive, QRegExp::Wildcard ).exactMatch( file ) ) {
90  return true;
91  }
92  return false;
93 }
94 
95 static bool is_blacklisted( const QString & file, const QStringList & blacklist ) {
96  return is_matching( file, blacklist );
97 }
98 
99 static bool is_whitelisted( const QString & file, const QStringList & whitelist ) {
100  if ( whitelist.empty() )
101  return true; // special case
102  return is_matching( file, whitelist );
103 }
104 
105 void FileSystemWatcher::Private::onFileChanged( const QString& path )
106 {
107  const QFileInfo fi( path );
108  if ( is_blacklisted( fi.fileName(), m_blacklist ) )
109  return;
110  if ( !is_whitelisted( fi.fileName(), m_whitelist ) )
111  return;
112  kDebug() << path;
113  m_seenPaths.insert( path );
114  m_cachedFiles.insert( path );
115  handleTimer();
116 }
117 
118 static QStringList list_dir_absolute( const QString & path, const QStringList & blacklist, const QStringList & whitelist ) {
119  QDir dir( path );
120  QStringList entries = dir.entryList( QDir::AllEntries|QDir::NoDotAndDotDot );
121  QStringList::iterator end =
122  std::remove_if( entries.begin(), entries.end(),
123  boost::bind( is_blacklisted, _1, cref( blacklist ) ) );
124  if ( !whitelist.empty() )
125  end = std::remove_if( entries.begin(), end,
126  !boost::bind( is_whitelisted, _1, cref( whitelist ) ) );
127  entries.erase( end, entries.end() );
128  kdtools::sort( entries );
129 
130  std::transform( entries.begin(), entries.end(), entries.begin(),
131  boost::bind( &QDir::absoluteFilePath, &dir, _1 ) );
132 
133  return entries;
134 }
135 
136 static QStringList find_new_files( const QStringList & current, const std::set<QString> & seen ) {
137  QStringList result;
138  std::set_difference( current.begin(), current.end(),
139  seen.begin(), seen.end(),
140  std::back_inserter( result ) );
141  return result;
142 }
143 
144 void FileSystemWatcher::Private::onDirectoryChanged( const QString& path )
145 {
146  const QFileInfo fi( path );
147  if ( is_blacklisted( fi.fileName(), m_blacklist ) )
148  return;
149  if ( !is_whitelisted( fi.fileName(), m_whitelist ) )
150  return;
151 
152  kDebug() << path;
153 
154  const QStringList newFiles = find_new_files( list_dir_absolute( path, m_blacklist, m_whitelist ), m_seenPaths );
155 
156  if ( newFiles.empty() )
157  return;
158 
159  kDebug() << "newFiles" << newFiles;
160 
161  m_cachedFiles.insert( newFiles.begin(), newFiles.end() );
162  q->addPaths( newFiles );
163 
164  m_cachedDirectories.insert( path );
165  handleTimer();
166 }
167 
168 void FileSystemWatcher::Private::onTimeout()
169 {
170  std::set<QString> dirs, files;
171 
172  dirs.swap( m_cachedDirectories );
173  files.swap( m_cachedFiles );
174 
175  if ( dirs.empty() && files.empty() )
176  return;
177 
178  emit q->triggered();
179 
180  Q_FOREACH( const QString& i, dirs )
181  emit q->directoryChanged( i );
182  Q_FOREACH( const QString& i, files )
183  emit q->fileChanged( i );
184 }
185 
186 void FileSystemWatcher::Private::handleTimer()
187 {
188  if ( m_timer.interval() == 0 ) {
189  onTimeout();
190  return;
191  }
192  m_timer.start();
193 }
194 
195 void FileSystemWatcher::Private::connectWatcher() {
196  if ( !m_watcher )
197  return;
198  connect( m_watcher, SIGNAL(directoryChanged(QString)),
199  q, SLOT(onDirectoryChanged(QString)) );
200  connect( m_watcher, SIGNAL(fileChanged(QString)),
201  q, SLOT(onFileChanged(QString)) );
202 }
203 
204 FileSystemWatcher::FileSystemWatcher( QObject* p )
205  : QObject( p ), d( new Private( this ) )
206 {
207  setEnabled( true );
208 }
209 
210 FileSystemWatcher::FileSystemWatcher( const QStringList& paths, QObject* p )
211  : QObject( p ), d( new Private( this, paths ) )
212 {
213  setEnabled( true );
214 }
215 
216 void FileSystemWatcher::setEnabled( bool enable )
217 {
218  if ( isEnabled() == enable )
219  return;
220  if ( enable ) {
221  assert( !d->m_watcher );
222  d->m_watcher = new QFileSystemWatcher;
223  if ( !d->m_paths.empty() )
224  d->m_watcher->addPaths( d->m_paths );
225  d->connectWatcher();
226  } else {
227  assert( d->m_watcher );
228  delete d->m_watcher;
229  d->m_watcher = 0;
230  }
231 }
232 
233 bool FileSystemWatcher::isEnabled() const
234 {
235  return d->m_watcher != 0;
236 }
237 
238 FileSystemWatcher::~FileSystemWatcher()
239 {
240 }
241 
242 void FileSystemWatcher::setDelay( int ms )
243 {
244  assert( ms >= 0 );
245  d->m_timer.setInterval( ms );
246 }
247 
248 int FileSystemWatcher::delay() const
249 {
250  return d->m_timer.interval();
251 }
252 
253 void FileSystemWatcher::blacklistFiles( const QStringList& paths )
254 {
255  d->m_blacklist += paths;
256  QStringList blacklisted;
257  d->m_paths.erase( kdtools::separate_if( d->m_paths.begin(), d->m_paths.end(),
258  std::back_inserter( blacklisted ), d->m_paths.begin(),
259  boost::bind( is_blacklisted, _1, cref( d->m_blacklist ) ) ).second, d->m_paths.end() );
260  if ( d->m_watcher && !blacklisted.empty() )
261  d->m_watcher->removePaths( blacklisted );
262 }
263 
264 void FileSystemWatcher::whitelistFiles( const QStringList & patterns )
265 {
266  d->m_whitelist += patterns;
267  // ### would be nice to add newly-matching paths here right away,
268  // ### but it's not as simple as blacklisting above, esp. since we
269  // ### don't want to subject addPath()'ed paths to whitelisting.
270 }
271 
272 static QStringList resolve( const QStringList & paths, const QStringList & blacklist, const QStringList & whitelist ) {
273  if ( paths.empty() )
274  return QStringList();
275  QStringList result;
276  Q_FOREACH( const QString & path, paths )
277  if ( QDir( path ).exists() )
278  result += list_dir_absolute( path, blacklist, whitelist );
279  return result + resolve( result, blacklist, whitelist );
280 }
281 
282 void FileSystemWatcher::addPaths( const QStringList& paths )
283 {
284  if ( paths.empty() )
285  return;
286  const QStringList newPaths = paths + resolve( paths, d->m_blacklist, d->m_whitelist );
287  kDebug( !newPaths.empty() ) << "adding\n " << newPaths.join( QLatin1String("\n ") ) << "\n/end";
288  d->m_paths += newPaths;
289  d->m_seenPaths.insert( newPaths.begin(), newPaths.end() );
290  if ( d->m_watcher && !newPaths.empty() )
291  d->m_watcher->addPaths( newPaths );
292 }
293 
294 void FileSystemWatcher::addPath( const QString& path )
295 {
296  addPaths( QStringList( path ) );
297 }
298 
299 void FileSystemWatcher::removePaths( const QStringList& paths )
300 {
301  if ( paths.empty() )
302  return;
303  Q_FOREACH ( const QString& i, paths )
304  d->m_paths.removeAll( i );
305  if ( d->m_watcher )
306  d->m_watcher->removePaths( paths );
307 }
308 
309 void FileSystemWatcher::removePath( const QString& path )
310 {
311  removePaths( QStringList( path ) );
312 }
313 
314 #include "filesystemwatcher.moc"
list_dir_absolute
static QStringList list_dir_absolute(const QString &path, const QStringList &blacklist, const QStringList &whitelist)
Definition: filesystemwatcher.cpp:118
is_blacklisted
static bool is_blacklisted(const QString &file, const QStringList &blacklist)
Definition: filesystemwatcher.cpp:95
Kleo::FileSystemWatcher::setEnabled
void setEnabled(bool enable)
Definition: filesystemwatcher.cpp:216
Kleo::FileSystemWatcher::addPath
void addPath(const QString &path)
Definition: filesystemwatcher.cpp:294
Kleo::FileSystemWatcher::whitelistFiles
void whitelistFiles(const QStringList &patterns)
Definition: filesystemwatcher.cpp:264
Kleo::FileSystemWatcher::isEnabled
bool isEnabled() const
Definition: filesystemwatcher.cpp:233
resolve
static QStringList resolve(const QStringList &paths, const QStringList &blacklist, const QStringList &whitelist)
Definition: filesystemwatcher.cpp:272
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::FileSystemWatcher
Definition: filesystemwatcher.h:45
Kleo::FileSystemWatcher::FileSystemWatcher
FileSystemWatcher(QObject *parent=0)
Definition: filesystemwatcher.cpp:204
Kleo::FileSystemWatcher::removePath
void removePath(const QString &path)
Definition: filesystemwatcher.cpp:309
find_new_files
static QStringList find_new_files(const QStringList &current, const std::set< QString > &seen)
Definition: filesystemwatcher.cpp:136
dir
static QString dir(const QString &id)
Definition: filedialog.cpp:53
is_matching
static bool is_matching(const QString &file, const QStringList &list)
Definition: filesystemwatcher.cpp:87
filesystemwatcher.h
Kleo::FileSystemWatcher::setDelay
void setDelay(int ms)
Definition: filesystemwatcher.cpp:242
Kleo::FileSystemWatcher::addPaths
void addPaths(const QStringList &paths)
Definition: filesystemwatcher.cpp:282
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::FileSystemWatcher::removePaths
void removePaths(const QStringList &path)
Definition: filesystemwatcher.cpp:299
Kleo::FileSystemWatcher::delay
int delay() const
Definition: filesystemwatcher.cpp:248
Kleo::FileSystemWatcher::~FileSystemWatcher
~FileSystemWatcher()
Definition: filesystemwatcher.cpp:238
is_whitelisted
static bool is_whitelisted(const QString &file, const QStringList &whitelist)
Definition: filesystemwatcher.cpp:99
Kleo::FileSystemWatcher::blacklistFiles
void blacklistFiles(const QStringList &patterns)
Definition: filesystemwatcher.cpp:253
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

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