1/***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21#ifndef KDIRECTORYCONTENTSCOUNTER_H
22#define KDIRECTORYCONTENTSCOUNTER_H
23
24#include "kdirectorycontentscounterworker.h"
25
26#include <QSet>
27#include <QQueue>
28
29class KDirWatch;
30class KFileItemModel;
31class QString;
32
33class KDirectoryContentsCounter : public QObject
34{
35 Q_OBJECT
36
37public:
38 explicit KDirectoryContentsCounter(KFileItemModel* model, QObject* parent = 0);
39 ~KDirectoryContentsCounter();
40
41 /**
42 * Requests the number of items inside the directory \a path. The actual
43 * counting is done asynchronously, and the result is announced via the
44 * signal \a result.
45 *
46 * The directory \a path is watched for changes, and the signal is emitted
47 * again if a change occurs.
48 */
49 void addDirectory(const QString& path);
50
51 /**
52 * In contrast to \a addDirectory, this function counts the items inside
53 * the directory \a path synchronously and returns the result.
54 *
55 * The directory is watched for changes, and the signal \a result is
56 * emitted if a change occurs.
57 */
58 int countDirectoryContentsSynchronously(const QString& path);
59
60signals:
61 /**
62 * Signals that the directory \a path contains \a count items.
63 */
64 void result(const QString& path, int count);
65
66 void requestDirectoryContentsCount(const QString& path, KDirectoryContentsCounterWorker::Options options);
67
68private slots:
69 void slotResult(const QString& path, int count);
70 void slotDirWatchDirty(const QString& path);
71 void slotItemsRemoved();
72
73private:
74 void startWorker(const QString& path);
75
76private:
77 KFileItemModel* m_model;
78
79 QQueue<QString> m_queue;
80
81 static QThread* m_workerThread;
82 static int m_workersCount;
83
84 KDirectoryContentsCounterWorker* m_worker;
85 bool m_workerIsBusy;
86
87 KDirWatch* m_dirWatcher;
88 QSet<QString> m_watchedDirs; // Required as sadly KDirWatch does not offer a getter method
89 // to get all watched directories.
90};
91
92#endif