jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 1 | #include "filesystemwatcher.h" |
| 2 | #include "ui_filesystemwatcher.h" |
| 3 | |
| 4 | filesystemwatcher::filesystemwatcher(QString dirPath, QWidget *parent) : |
| 5 | QMainWindow(parent), |
| 6 | m_ui(new Ui::filesystemwatcher), |
| 7 | m_watcher(new QFileSystemWatcher()), |
| 8 | m_listViewModel(new QStringListModel()), |
| 9 | m_listView(new QListView()), |
| 10 | m_dirPath(dirPath) |
| 11 | { |
| 12 | // setup user interface |
| 13 | m_ui->setupUi(this); |
| 14 | |
| 15 | // add main directory to monitor |
| 16 | m_watcher->addPath(m_dirPath); |
| 17 | |
| 18 | // create the view |
| 19 | m_listView->setModel(m_listViewModel); |
| 20 | setCentralWidget(m_listView); |
| 21 | |
| 22 | // set title |
| 23 | setWindowTitle("ChronoShare"); |
| 24 | |
| 25 | // register signals (callback functions) |
| 26 | connect(m_watcher,SIGNAL(fileChanged(QString)),this,SLOT(fileChangedSlot(QString))); |
| 27 | connect(m_watcher,SIGNAL(directoryChanged(QString)),this,SLOT(dirChangedSlot(QString))); |
| 28 | |
| 29 | // bootstrap file list |
| 30 | dirChangedSlot(m_dirPath); |
| 31 | } |
| 32 | |
| 33 | filesystemwatcher::~filesystemwatcher() |
| 34 | { |
| 35 | // clean up |
| 36 | delete m_ui; |
| 37 | delete m_watcher; |
| 38 | delete m_listViewModel; |
| 39 | delete m_listView; |
| 40 | } |
| 41 | |
| 42 | void filesystemwatcher::fileChangedSlot(QString filePath) |
| 43 | { |
| 44 | QStringList fileList; |
| 45 | fileList.append(filePath); |
| 46 | } |
| 47 | |
| 48 | void filesystemwatcher::dirChangedSlot(QString dirPath) |
| 49 | { |
| 50 | // list of files in directory |
| 51 | QStringList fileList; |
| 52 | |
| 53 | // directory iterator (recursive) |
| 54 | QDirIterator dirIterator(m_dirPath, QDirIterator::Subdirectories | |
| 55 | QDirIterator::FollowSymlinks); |
| 56 | |
| 57 | // iterate through directory recursively |
| 58 | while(dirIterator.hasNext()) |
| 59 | { |
| 60 | // Get Next File/Dir |
| 61 | dirIterator.next(); |
| 62 | |
| 63 | // Get FileInfo |
| 64 | QFileInfo fileInfo = dirIterator.fileInfo(); |
| 65 | |
| 66 | // if not this directory or previous directory |
| 67 | if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".") |
| 68 | { |
| 69 | // if this is a directory |
| 70 | if(fileInfo.isDir()) |
| 71 | { |
| 72 | QStringList dirList = m_watcher->directories(); |
| 73 | |
| 74 | // if the directory is not already being watched |
| 75 | if (!dirList.contains(fileInfo.absoluteFilePath())) |
| 76 | { |
| 77 | // add this directory to the watch list |
| 78 | m_watcher->addPath(fileInfo.absoluteFilePath()); |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | // add this file to the file list |
| 84 | fileList.append(fileInfo.absoluteFilePath()); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // update gui with list of files in this directory |
| 90 | m_listViewModel->setStringList(fileList); |
| 91 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame^] | 92 | |
| 93 | #if WAF |
| 94 | #include "filesystemwatcher.moc" |
| 95 | #include "filesystemwatcher.cpp.moc" |
| 96 | #endif |