watchdog.cpp
00001 #include "watchdog.h"
00002
00003 #include "watchdog.moc"
00004
00005 #include <iostream>
00006
00007 namespace util
00008 {
00009 void FileObject::update()
00010 {
00011 reread();
00012 emit modified();
00013 }
00014
00015 void FileObject::setFilename(std::string fn)
00016 {
00017 if(fn != filename)
00018 {
00019 filename = fn;
00020 if(!fn.empty())
00021 update();
00022 }
00023 }
00024
00025 WatchDog::WatchDog(QObject *m)
00026 : QObject()
00027 , model(m)
00028 {
00029 connect(this, SIGNAL(registerFile(std::string)), m, SLOT(registerFile(std::string)));
00030 connect(this, SIGNAL(unregisterFile(std::string)), m, SLOT(unregisterFile(std::string)));
00031 }
00032
00033 void WatchDog::addObject(FileObject* obj)
00034 {
00035 fileObjects.insert(obj);
00036 names[obj->getFilename()].insert(obj);
00037 if(!obj->getFilename().empty())
00038 emit registerFile(obj->getFilename());
00039 }
00040
00041 bool WatchDog::guarded(FileObject *obj) const
00042 {
00043 return (fileObjects.find(obj) != fileObjects.end());
00044 }
00045
00046 void WatchDog::changeFilename(FileObject* obj, std::string fn)
00047 {
00048
00049 std::string oldName = obj->getFilename();
00050 if(oldName == fn)
00051 return;
00052 std::set<FileObject*>& ns = names[oldName];
00053 if(!oldName.empty())
00054 emit unregisterFile(oldName);
00055 ns.erase(obj);
00056 if(ns.empty())
00057 names.erase(oldName);
00058
00059 names[fn].insert(obj);
00060 if(!fn.empty())
00061 emit registerFile(fn);
00062 obj->setFilename(fn);
00063 }
00064
00065 void WatchDog::removeObject(FileObject *obj)
00066 {
00067 std::string name = obj->getFilename();
00068 std::set<FileObject*>& ns = names[name];
00069 if(!name.empty())
00070 emit unregisterFile(name);
00071 ns.erase(obj);
00072 if(ns.empty())
00073 names.erase(name);
00074 fileObjects.erase(obj);
00075 }
00076
00077
00078 bool WatchDog::watch(const std::set<std::string>& filenames)
00079 {
00080 bool result = false;
00081 forall(const std::string& str, filenames)
00082 {
00083 std::map<std::string, std::set<FileObject*> >::iterator found = names.find(str);
00084 if(found != names.end())
00085 {
00086 result = true;
00087 forall(FileObject* obj, found->second)
00088 {
00089 obj->update();
00090 }
00091 }
00092 }
00093 return result;
00094 }
00095
00096 }
00097