PDA

View Full Version : QFileSystemWatcher best way to detect change



hakermania
12th October 2011, 11:47
Hello.
This library allows you to detect whether a folder or a file has changed.
Specifically, if you watch a file, whatever the change, it will send the signal that the file has changed, but you don't know whether only the content has changed or the file was deleted.
If you watch a folder, it will say you that something has changed only if a file has changed (removed/deleted/renamed) on the top folder you've set (not inside its subfolders), and again, you can't even know what kind of change has been made inside the folder.

What I want to do is to add all the files of the given folder at startup of the program and add/remove any file that has been added/removed from the given folder to a list (for example to a list_widget)
What I've done to the program so far is to add all the files recursively to a list (let's say a QStringList) on startup of a selected folder and then track every single file for changes and also track and all the subfolders of the given one and itself for changes. It works.

The main problem is that it turns sometimes to be very slow if there are too many changes in too many files so as to add to the list every file that is added to the given folder or its subfolders and remove any file from the list if it is removed from the given folder or its subfolders as well.
Deletion is quite easy, because I get the signal that the file changed, I detect whether it exists using the QFile library and if it doesn't it has simply changed and I don't remove it from the list.
Addition is challenging. First of all, I have to detect whether a whole folder has been added or simply (a) file(s). So I list all the subfolders of the given (main) folder and I compare it with the list of all the tracked folders (one-by-one). If there's a folder that doesn't exist on the tracked ones I add it. Then, if there are not differences, I get the files of the folder to a list and I compare it to the list containing all the tracked files. Again, if there's a file that's not present to the list with all the tracked files, its being added.

Well, this is quite complicated but I cannot think of a better way considering that qt don't tell me what kind of change happened automatically.

So, I'd like to see if there's any other better way to do such a thing. I hope I was understandable :P

Thanks in advance for any response!

totem
12th October 2011, 12:52
Well, it depends on the number of files beeing watched, but if there is not too much, I'd choose too watch the directories only, and whenever a change occures I reload the entire directory contents.
To avoid too much work when multiple files are added/removed at the same time, I would link directory-changed-signals to a timer. Thus, any change (re)start the timer and I reload the directory only after a certain interval is elapsed.

hakermania
12th October 2011, 15:51
Yeah, the problem is that the files can be up to 1000, I can never know what the user may do, he'll maybe add 10.000 files at once :P
I thought of having a timer resetting each time a folder's contents have changed. The timer, if running will restart to 1 second for example. When the timer finally timeouts (=means that there hasn't been any change inside the folder for one second) I will recursively list again the contents of the folder and re-add them to the list... This also includes the process of removing the files of the folder and re-add them so as to remove duplicate entries but it's maybe quicker rather than comparing huge string lists between them.:p