//=============================================================================
void qkFolderWatch::recurseDirectories()
{
QFileInfoList list = dir.entryInfoList();
{
if (contains(info.filePath())) continue;
if (info.fileName() == ".." || info.fileName() == ".") continue;
if (info.isHidden()) continue;
if (info.isDir())
{
qkFolderWatch folder(info.filePath(), mWatcher);
mFolderWatchs.push_back(folder);
newFolders += info.fileName();
}
else if (info.isFile())
{
qkFileWatch file(info.filePath(), mWatcher);
mFileWatchs.push_back(file);
newFiles += info.fileName();
}
}
if (newFolders.size())
{
request_foldersCreated(mPath, newFolders);
}
if (newFiles.size())
{
request_filesCreated(mPath, newFiles);
}
}
void qkFolderWatch::checkDeleted()
{
for (int i = 0; i < mFolderWatchs.size(); i++)
{
if (mFolderWatchs[i].exists() == false)
{
deletedFolders += mFolderWatchs[i].name();
mFolderWatchs.removeAt(i);
i = -1;
}
}
for (int i = 0; i < mFileWatchs.size(); i++)
{
if (mFileWatchs[i].exists() == false)
{
deletedFiles += mFileWatchs[i].name();
mFileWatchs.removeAt(i);
i = -1;
}
}
if (deletedFolders.size())
{
request_foldersDeleted(mPath, deletedFolders);
}
if (deletedFiles.size())
{
request_filesDeleted(mPath, deletedFiles);
}
}
//=============================================================================
int qkFolderWatch
::getFolderIndex_from_path(const QString & path
) {
for (int i = 0; i < mFolderWatchs.size(); i++)
{
if (mFolderWatchs[i].path() == path) return i;
}
return -1;
}
int qkFolderWatch
::getFileIndex_from_path(const QString & path
) {
for (int i = 0; i < mFileWatchs.size(); i++)
{
if (mFileWatchs[i].path() == path) return i;
}
return -1;
}
//=============================================================================
// Private
//=============================================================================
#include <qkGlobalHeader_p.h>
class qkFileWatcherPrivate : public qkPrivate
{
protected:
QK_DECLARE_PUBLIC(qkFileWatcher);
public:
qkFileWatcherPrivate(qkFileWatcher * p);
void init();
private: // Functions
void addPath
(const QString & path
);
void removePath
(const QString & path
);
int getFolderIndex_from_path
(const QString & path
);
int getFileIndex_from_path
(const QString & path
);
private: // Variables
QList<qkFileWatch> fileWatchs;
QList<qkFolderWatch> folderWatchs;
};
qkFileWatcherPrivate::qkFileWatcherPrivate(qkFileWatcher * p)
: qkPrivate(p)
{
}
void qkFileWatcherPrivate::init()
{
Q_Q(qkFileWatcher);
QObject::connect(&timer,
SIGNAL(timeout
()), q,
SLOT(onCheckForChange
()));
timer.start(100);
}
//=============================================================================
void qkFileWatcherPrivate
::addPath(const QString & path
) {
Q_Q(qkFileWatcher);
if (info.isDir())
{
qkFolderWatch folder(path, q);
folderWatchs.push_back(folder);
}
else
{
qkFileWatch file(path, q);
fileWatchs.push_back(file);
}
}
void qkFileWatcherPrivate
::removePath(const QString & path
) {
int index = getFolderIndex_from_path(path);
if (index != -1) folderWatchs.removeAt(index);
index = getFileIndex_from_path(path);
if (index != -1) fileWatchs.removeAt(index);
}
//=============================================================================
int qkFileWatcherPrivate
::getFolderIndex_from_path(const QString & path
) {
for (int i = 0; i < folderWatchs.size(); i++)
{
if (folderWatchs[i].path() == path) return i;
}
return -1;
}
int qkFileWatcherPrivate
::getFileIndex_from_path(const QString & path
) {
for (int i = 0; i < fileWatchs.size(); i++)
{
if (fileWatchs[i].path() == path) return i;
}
return -1;
}
//=============================================================================
// Ctor / dtor
//=============================================================================
qkFileWatcher
::qkFileWatcher(QObject * parent
) : QObject(parent
), qkPrivatable
(new qkFileWatcherPrivate
(this)) {
Q_D(qkFileWatcher);
d->init();
}
qkFileWatcher::~qkFileWatcher()
{
}
//=============================================================================
// Interface
//=============================================================================
void qkFileWatcher
::addPath(const QString & path
) {
Q_D(qkFileWatcher);
if (contains(path)) return;
d->addPath(path);
}
void qkFileWatcher
::removePath(const QString & path
) {
Q_D(qkFileWatcher);
if (contains(path) == false) return;
d->removePath(path);
}
bool qkFileWatcher
::contains(const QString & path
) {
Q_D(qkFileWatcher);
for (int i = 0; i < d->folderWatchs.size(); i++)
{
if (d->folderWatchs[i].path() == path) return true;
}
for (int i = 0; i < d->fileWatchs.size(); i++)
{
if (d->fileWatchs[i].path() == path) return true;
}
return false;
}
//=============================================================================
// Private slots
//=============================================================================
void qkFileWatcher::onCheckForChange()
{
Q_D(qkFileWatcher);
for (int i = 0; i < d->fileWatchs.size(); i++)
{
d->fileWatchs[i].checkChange();
if (d->fileWatchs[i].exists() == false)
{
request_filesDeleted(d->fileWatchs[i].absolutePath(),
d->fileWatchs.removeAt(i);
i = -1;
}
}
for (int i = 0; i < d->folderWatchs.size(); i++)
{
d->folderWatchs[i].checkChange();
if (d->folderWatchs[i].exists() == false)
{
request_foldersDeleted(d->folderWatchs[i].absolutePath(),
d->folderWatchs.removeAt(i);
i = -1;
}
}
}
//=============================================================================
// Private functions
//=============================================================================
void qkFileWatcher
::request_folderModified(const QString parentPath,
const QString folderName
) {
qkDebug("Folder modified %s %s", parentPath.C_STR, folderName.C_STR);
emit folderModified(parentPath, folderName);
}
void qkFileWatcher
::request_foldersCreated(const QString parentPath,
const QStringList folderNames
) {
foreach
(QString name, folderNames
) {
qkDebug("Folder created %s %s", parentPath.C_STR, name.C_STR);
}
emit foldersCreated(parentPath, folderNames);
}
void qkFileWatcher
::request_foldersDeleted(const QString parentPath,
const QStringList folderNames
) {
foreach
(QString name, folderNames
) {
qkDebug("Folder deleted %s %s", parentPath.C_STR, name.C_STR);
}
emit foldersDeleted(parentPath, folderNames);
}
//=============================================================================
void qkFileWatcher
::request_fileModified(const QString parentPath,
const QString fileName
) {
qkDebug("File modified %s %s", parentPath.C_STR, fileName.C_STR);
emit fileModified(parentPath, fileName);
}
void qkFileWatcher
::request_filesCreated(const QString parentPath,
const QStringList fileNames
) {
{
qkDebug("File created %s %s", parentPath.C_STR, name.C_STR);
}
emit filesCreated(parentPath, fileNames);
}
void qkFileWatcher
::request_filesDeleted(const QString parentPath,
const QStringList fileNames
) {
{
qkDebug("File deleted %s %s", parentPath.C_STR, name.C_STR);
}
emit filesDeleted(parentPath, fileNames);
}
//=============================================================================
void qkFolderWatch::recurseDirectories()
{
QDir dir(mPath);
QFileInfoList list = dir.entryInfoList();
QStringList newFolders;
QStringList newFiles;
foreach (QFileInfo info, list)
{
if (contains(info.filePath())) continue;
if (info.fileName() == ".." || info.fileName() == ".") continue;
if (info.isHidden()) continue;
if (info.isDir())
{
qkFolderWatch folder(info.filePath(), mWatcher);
mFolderWatchs.push_back(folder);
newFolders += info.fileName();
}
else if (info.isFile())
{
qkFileWatch file(info.filePath(), mWatcher);
mFileWatchs.push_back(file);
newFiles += info.fileName();
}
}
if (newFolders.size())
{
request_foldersCreated(mPath, newFolders);
}
if (newFiles.size())
{
request_filesCreated(mPath, newFiles);
}
}
void qkFolderWatch::checkDeleted()
{
QStringList deletedFolders;
QStringList deletedFiles;
for (int i = 0; i < mFolderWatchs.size(); i++)
{
if (mFolderWatchs[i].exists() == false)
{
deletedFolders += mFolderWatchs[i].name();
mFolderWatchs.removeAt(i);
i = -1;
}
}
for (int i = 0; i < mFileWatchs.size(); i++)
{
if (mFileWatchs[i].exists() == false)
{
deletedFiles += mFileWatchs[i].name();
mFileWatchs.removeAt(i);
i = -1;
}
}
if (deletedFolders.size())
{
request_foldersDeleted(mPath, deletedFolders);
}
if (deletedFiles.size())
{
request_filesDeleted(mPath, deletedFiles);
}
}
//=============================================================================
int qkFolderWatch::getFolderIndex_from_path(const QString & path)
{
for (int i = 0; i < mFolderWatchs.size(); i++)
{
if (mFolderWatchs[i].path() == path) return i;
}
return -1;
}
int qkFolderWatch::getFileIndex_from_path(const QString & path)
{
for (int i = 0; i < mFileWatchs.size(); i++)
{
if (mFileWatchs[i].path() == path) return i;
}
return -1;
}
//=============================================================================
// Private
//=============================================================================
#include <qkGlobalHeader_p.h>
class qkFileWatcherPrivate : public qkPrivate
{
protected:
QK_DECLARE_PUBLIC(qkFileWatcher);
public:
qkFileWatcherPrivate(qkFileWatcher * p);
void init();
private: // Functions
void addPath(const QString & path);
void removePath(const QString & path);
int getFolderIndex_from_path(const QString & path);
int getFileIndex_from_path(const QString & path);
private: // Variables
QTimer timer;
QList<qkFileWatch> fileWatchs;
QList<qkFolderWatch> folderWatchs;
};
qkFileWatcherPrivate::qkFileWatcherPrivate(qkFileWatcher * p)
: qkPrivate(p)
{
}
void qkFileWatcherPrivate::init()
{
Q_Q(qkFileWatcher);
QObject::connect(&timer, SIGNAL(timeout()), q, SLOT(onCheckForChange()));
timer.start(100);
}
//=============================================================================
void qkFileWatcherPrivate::addPath(const QString & path)
{
Q_Q(qkFileWatcher);
QFileInfo info(path);
if (info.isDir())
{
qkFolderWatch folder(path, q);
folderWatchs.push_back(folder);
}
else
{
qkFileWatch file(path, q);
fileWatchs.push_back(file);
}
}
void qkFileWatcherPrivate::removePath(const QString & path)
{
int index = getFolderIndex_from_path(path);
if (index != -1) folderWatchs.removeAt(index);
index = getFileIndex_from_path(path);
if (index != -1) fileWatchs.removeAt(index);
}
//=============================================================================
int qkFileWatcherPrivate::getFolderIndex_from_path(const QString & path)
{
for (int i = 0; i < folderWatchs.size(); i++)
{
if (folderWatchs[i].path() == path) return i;
}
return -1;
}
int qkFileWatcherPrivate::getFileIndex_from_path(const QString & path)
{
for (int i = 0; i < fileWatchs.size(); i++)
{
if (fileWatchs[i].path() == path) return i;
}
return -1;
}
//=============================================================================
// Ctor / dtor
//=============================================================================
qkFileWatcher::qkFileWatcher(QObject * parent)
: QObject(parent), qkPrivatable(new qkFileWatcherPrivate(this))
{
Q_D(qkFileWatcher);
d->init();
}
qkFileWatcher::~qkFileWatcher()
{
}
//=============================================================================
// Interface
//=============================================================================
void qkFileWatcher::addPath(const QString & path)
{
Q_D(qkFileWatcher);
if (contains(path)) return;
d->addPath(path);
}
void qkFileWatcher::removePath(const QString & path)
{
Q_D(qkFileWatcher);
if (contains(path) == false) return;
d->removePath(path);
}
bool qkFileWatcher::contains(const QString & path)
{
Q_D(qkFileWatcher);
for (int i = 0; i < d->folderWatchs.size(); i++)
{
if (d->folderWatchs[i].path() == path) return true;
}
for (int i = 0; i < d->fileWatchs.size(); i++)
{
if (d->fileWatchs[i].path() == path) return true;
}
return false;
}
//=============================================================================
// Private slots
//=============================================================================
void qkFileWatcher::onCheckForChange()
{
Q_D(qkFileWatcher);
QFileInfo info;
for (int i = 0; i < d->fileWatchs.size(); i++)
{
d->fileWatchs[i].checkChange();
if (d->fileWatchs[i].exists() == false)
{
request_filesDeleted(d->fileWatchs[i].absolutePath(),
QStringList() << d->fileWatchs[i].name());
d->fileWatchs.removeAt(i);
i = -1;
}
}
for (int i = 0; i < d->folderWatchs.size(); i++)
{
d->folderWatchs[i].checkChange();
if (d->folderWatchs[i].exists() == false)
{
request_foldersDeleted(d->folderWatchs[i].absolutePath(),
QStringList() << d->folderWatchs[i].name());
d->folderWatchs.removeAt(i);
i = -1;
}
}
}
//=============================================================================
// Private functions
//=============================================================================
void qkFileWatcher::request_folderModified(const QString parentPath, const QString folderName)
{
qkDebug("Folder modified %s %s", parentPath.C_STR, folderName.C_STR);
emit folderModified(parentPath, folderName);
}
void qkFileWatcher::request_foldersCreated(const QString parentPath, const QStringList folderNames)
{
foreach (QString name, folderNames)
{
qkDebug("Folder created %s %s", parentPath.C_STR, name.C_STR);
}
emit foldersCreated(parentPath, folderNames);
}
void qkFileWatcher::request_foldersDeleted(const QString parentPath, const QStringList folderNames)
{
foreach (QString name, folderNames)
{
qkDebug("Folder deleted %s %s", parentPath.C_STR, name.C_STR);
}
emit foldersDeleted(parentPath, folderNames);
}
//=============================================================================
void qkFileWatcher::request_fileModified(const QString parentPath, const QString fileName)
{
qkDebug("File modified %s %s", parentPath.C_STR, fileName.C_STR);
emit fileModified(parentPath, fileName);
}
void qkFileWatcher::request_filesCreated(const QString parentPath, const QStringList fileNames)
{
foreach (QString name, fileNames)
{
qkDebug("File created %s %s", parentPath.C_STR, name.C_STR);
}
emit filesCreated(parentPath, fileNames);
}
void qkFileWatcher::request_filesDeleted(const QString parentPath, const QStringList fileNames)
{
foreach (QString name, fileNames)
{
qkDebug("File deleted %s %s", parentPath.C_STR, name.C_STR);
}
emit filesDeleted(parentPath, fileNames);
}
To copy to clipboard, switch view to plain text mode
Bookmarks