All pastes #1685922 Raw Edit

Stuff

public text v1 · immutable
#1685922 ·published 2009-11-24 20:28 UTC
rendered paste body
#ifndef M_FILE_SYSTEM_WATCHER_H_
#define M_FILE_SYSTEM_WATCHER_H_

#include <QFileSystemWatcher>

class MFileSystemWatcher : public QFileSystemWatcher
{
    Q_OBJECT
  public:
    MFileSystemWatcher();
   ~MFileSystemWatcher();

    void addPath(const QString &path);

	void removePath(const QString &path);

	static addDir(const QString &dir);
	static removeDir(const QString &dir);

  public slots:
    void dirModified(const QString dir);

  private:
    QHash <QString, int> m_dirList;

};

#endif

=============================================================================

#include "mythcontext.h"
#include "mfilesystemwatcher.h"

MFileSystemWatcher::MFileSystemWatcher()
  : QFileSystemWatcher()
{
    gContext->addListener(this);
}

MFileSystemWatcher::~MFileSystemWatcher()
{
    gContext->removeListener(this);

	QHash<QString, int>::iterator it = m_dirList.begin();
	while (it != m_dirList.end())
	{
	    QFileSystemWatcher::removePath(it.key());
	}
	m_dirList.clear();
}

void MFileSystemWatcher::addPath(const QString &path)
{
    if (m_dirList.contains(path))
	    m_dirList[path]++;
	else
	    m_dirList[path] = 1;

	QFileSystemWatcher::addPath(path);
}

void MFileSystemWatcher::removePath(const QString &path)
{
    if (!m_dirList.contains(path))
	    return;

    if (m_dirList[path] > 1)
	    m_dirList[path]--;
	else
	    m_dirList.remove(path);

	QFileSystemWatcher::removePath(path);
}

void MFileSystemWatcher::addDir(const QString &host, const QString &dir)
{
    QString msg = QString("DIRWATCHER ADDDIR %1 %2").arg(host).arg(dir);

    if (gContext->IsMasterBackend())
	{
	    MythEvent me(msg);
		gContext->dispatch(me);
	}
	else
	{
	    RemoteSendMessage(tmpMsg);
	}
}

void MFileSystemWatcher::removeDir(const QString &host, const QString &dir)
{
    QString msg = QString("DIRWATCHER REMOVEDIR %1 %2").arg(host).arg(dir);

    if (gContext->IsMasterBackend())
	{
	    MythEvent me(msg);
		gContext->dispatch(me);
	}
	else
	{
	    RemoteSendMessage(tmpMsg);
	}
}

void MFileSystemWatcher::dirModified(const QString &dir)
{
    QString msg = QString("DIRWATCHER DIRCHANGED %1 %2")
	                      .arg(gContext->GetHostName()).arg(dir);

    if (gContext->IsMasterBackend())
	{
	    MythEvent me(msg);
		gContext->dispatch(me);
	}
	else
	{
	    RemoteSendMessage(tmpMsg);
	}
}

void MFileSystemWatcher::customEvent(QEvent *event)
{
    if ((MythEvent::Type)(event->type()) == MythEvent::MythEventMessage)
    {
        MythEvent *me = (MythEvent *)event;
        QString message = me->Message();

        if (message.startsWith("DIRWATCHER "))
		{
		    QStringList tokens = message.split(' ', QString::SkipEmptyParts);

			if (tokens[2] != gContext->GetHostName())
			    return;

			if (tokens[1] == "ADDDIR")
			{
			    addPath(tokens[2]);
			}
			else if (tokens[1] == "REMOVEDIR")
			{
			    removePath(tokens[2]);
			}
		}
    }
}



=============================================================================

In mythbackend's main.cpp, somewhere around the exec() near the bottom you'll
do something like this:

    MFileSystemWatcher mfswatcher = new MFileSystemWatcher();
	...
	blah.exec();
	...
	delete mfswatcher;


In your object:

1) keep a QStringList of directoires you want to watch.
2) #include "mfilesystemwatcher.h"
3) call the static MFileSystemWatcher::addDir(host, dir) for each dir
   that you want to monitor
4) profit!!
5) in your destructor, call the static MFileSystemWatcher::removeDir(host, dir)
   for each dir that you were monitoring

In your object's ::customEvent() method:


if (message.startsWith("DIRWATCHER "))
{
    QStringList tokens = message.split(' ', QString::SkipEmptyParts);

	if (tokens[1] != "DIRCHANGED")
	    return;

    QString hostname = tokens[2];
	QString dirname  = tokens[3];

	// check to see if this is a dir we are monitoring and act appropriately
}