jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 1 | #include "filesystemwatcher.h" |
| 2 | #include "ui_filesystemwatcher.h" |
| 3 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 4 | FileSystemWatcher::FileSystemWatcher(QString dirPath, QWidget *parent) : |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 5 | QMainWindow(parent), |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 6 | m_ui(new Ui::FileSystemWatcher), |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 7 | m_watcher(new QFileSystemWatcher()), |
| 8 | m_listViewModel(new QStringListModel()), |
| 9 | m_listView(new QListView()), |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 10 | m_timer(new QTimer(this)), |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 11 | m_dirPath(dirPath) |
| 12 | { |
| 13 | // setup user interface |
| 14 | m_ui->setupUi(this); |
| 15 | |
| 16 | // add main directory to monitor |
| 17 | m_watcher->addPath(m_dirPath); |
| 18 | |
| 19 | // create the view |
| 20 | m_listView->setModel(m_listViewModel); |
| 21 | setCentralWidget(m_listView); |
| 22 | |
| 23 | // set title |
| 24 | setWindowTitle("ChronoShare"); |
| 25 | |
| 26 | // register signals (callback functions) |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 27 | connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(handleCallback())); |
| 28 | connect(m_timer, SIGNAL(timeout()), this, SLOT(handleCallback())); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 29 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 30 | // bootstrap |
| 31 | handleCallback(); |
| 32 | |
| 33 | // start timer |
| 34 | m_timer->start(10000); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 37 | FileSystemWatcher::~FileSystemWatcher() |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 38 | { |
| 39 | // clean up |
| 40 | delete m_ui; |
| 41 | delete m_watcher; |
| 42 | delete m_listViewModel; |
| 43 | delete m_listView; |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 44 | delete m_timer; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 47 | void FileSystemWatcher::handleCallback() |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 48 | { |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 49 | // scan directory and populate file list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 50 | QHash<QString, sFileInfo> currentState = scanDirectory(m_dirPath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 51 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 52 | QStringList dirChanges = reconcileDirectory(currentState); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 53 | |
| 54 | // update gui with list of changes in this directory |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 55 | qDebug() << endl << m_watcher->directories() << endl; |
| 56 | |
| 57 | if(!dirChanges.isEmpty()) |
| 58 | m_listViewModel->setStringList(dirChanges); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 61 | QHash<QString, sFileInfo> FileSystemWatcher::scanDirectory(QString dirPath) |
| 62 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 63 | // list of files in directory |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 64 | QHash<QString, sFileInfo> currentState; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 65 | |
| 66 | // directory iterator (recursive) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 67 | QDirIterator dirIterator(dirPath, QDirIterator::Subdirectories | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 68 | QDirIterator::FollowSymlinks); |
| 69 | |
| 70 | // iterate through directory recursively |
| 71 | while(dirIterator.hasNext()) |
| 72 | { |
| 73 | // Get Next File/Dir |
| 74 | dirIterator.next(); |
| 75 | |
| 76 | // Get FileInfo |
| 77 | QFileInfo fileInfo = dirIterator.fileInfo(); |
| 78 | |
| 79 | // if not this directory or previous directory |
| 80 | if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".") |
| 81 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 82 | QString absFilePath = fileInfo.absoluteFilePath(); |
| 83 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 84 | // if this is a directory |
| 85 | if(fileInfo.isDir()) |
| 86 | { |
| 87 | QStringList dirList = m_watcher->directories(); |
| 88 | |
| 89 | // if the directory is not already being watched |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 90 | if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath)) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 91 | { |
| 92 | // add this directory to the watch list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 93 | m_watcher->addPath(absFilePath); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | else |
| 97 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 98 | // construct struct |
| 99 | sFileInfo fileInfoStruct; |
| 100 | fileInfoStruct.fileInfo = fileInfo; |
| 101 | |
| 102 | // initialize checksum |
| 103 | QCryptographicHash crypto(QCryptographicHash::Md5); |
| 104 | |
| 105 | // open file |
| 106 | QFile file(fileInfo.absoluteFilePath()); |
| 107 | file.open(QFile::ReadOnly); |
| 108 | |
| 109 | // calculate checksum |
| 110 | while(!file.atEnd()) |
| 111 | { |
| 112 | crypto.addData(file.read(8192)); |
| 113 | } |
| 114 | |
| 115 | fileInfoStruct.hash = crypto.result(); |
| 116 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 117 | // add this file to the file list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 118 | currentState.insert(absFilePath, fileInfoStruct); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 123 | return currentState; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 126 | QStringList FileSystemWatcher::reconcileDirectory(QHash<QString, sFileInfo> currentState) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 127 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 128 | // list of files changed |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 129 | QStringList dirChanges; |
| 130 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 131 | // compare result (database/stored snapshot) to fileList (current snapshot) |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 132 | QMutableHashIterator<QString, sFileInfo> i(m_storedState); |
| 133 | while(i.hasNext()) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 134 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 135 | i.next(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 136 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 137 | QString absFilePath = i.key(); |
| 138 | |
| 139 | sFileInfo storedFileInfoStruct = i.value(); |
| 140 | QFileInfo storedFileInfo = storedFileInfoStruct.fileInfo; |
| 141 | QByteArray storedHash = storedFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 142 | |
| 143 | // check file existence |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 144 | if(currentState.contains(absFilePath)) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 145 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 146 | sFileInfo currentFileInfoStruct = currentState.value(absFilePath); |
| 147 | QFileInfo currentFileInfo = currentFileInfoStruct.fileInfo; |
| 148 | QByteArray currentHash = currentFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 149 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 150 | if((storedFileInfo != currentFileInfo) || (storedHash != currentHash)) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 151 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 152 | // update stored state |
| 153 | i.setValue(currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 154 | |
| 155 | // this file has been modified |
| 156 | dirChanges.append(absFilePath); |
| 157 | } |
| 158 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 159 | // delete this file from fileList we have processed it |
| 160 | currentState.remove(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 161 | } |
| 162 | else |
| 163 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 164 | // delete from stored state |
| 165 | i.remove(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 166 | |
| 167 | // this file has been deleted |
| 168 | dirChanges.append(absFilePath); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 169 | } |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 172 | // any files left in fileList have been added |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 173 | for(QHash<QString, sFileInfo>::iterator i = currentState.begin(); i != currentState.end(); ++i) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 174 | { |
| 175 | QString absFilePath = i.key(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 176 | sFileInfo currentFileInfoStruct = i.value(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 177 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 178 | m_storedState.insert(absFilePath, currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 179 | |
| 180 | // this file has been added |
| 181 | dirChanges.append(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame^] | 184 | return dirChanges; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 185 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 186 | |
| 187 | #if WAF |
| 188 | #include "filesystemwatcher.moc" |
| 189 | #include "filesystemwatcher.cpp.moc" |
| 190 | #endif |