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 | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 27 | connect(m_watcher, SIGNAL(directoryChanged(QString)), this, SLOT(watcherCallbackSlot(QString))); |
| 28 | connect(m_timer, SIGNAL(timeout()), this, SLOT(timerCallbackSlot())); |
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 |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 31 | timerCallbackSlot(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 32 | |
| 33 | // start timer |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 34 | m_timer->start(60000); |
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 | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 47 | void FileSystemWatcher::watcherCallbackSlot(QString dirPath) |
| 48 | { |
| 49 | // watcher specific steps |
| 50 | qDebug() << endl << "[WATCHER] Triggered Path: " << dirPath; |
| 51 | |
| 52 | handleCallback(dirPath); |
| 53 | } |
| 54 | |
| 55 | void FileSystemWatcher::timerCallbackSlot() |
| 56 | { |
| 57 | // timer specific steps |
| 58 | qDebug() << endl << "[TIMER] Triggered Path: " << m_dirPath; |
| 59 | |
| 60 | handleCallback(m_dirPath); |
| 61 | } |
| 62 | |
| 63 | void FileSystemWatcher::handleCallback(QString dirPath) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 64 | { |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 65 | // scan directory and populate file list |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 66 | QHash<QString, sFileInfo> currentState = scanDirectory(dirPath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 67 | |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 68 | // reconcile directory and report changes |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 69 | QVector<sEventInfo> dirChanges = reconcileDirectory(currentState, dirPath); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 70 | |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 71 | // DEBUG: Print to Gui |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 72 | printToGui(dirChanges); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 75 | QHash<QString, sFileInfo> FileSystemWatcher::scanDirectory(QString dirPath) |
| 76 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 77 | // list of files in directory |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 78 | QHash<QString, sFileInfo> currentState; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 79 | |
| 80 | // directory iterator (recursive) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 81 | QDirIterator dirIterator(dirPath, QDirIterator::Subdirectories | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 82 | QDirIterator::FollowSymlinks); |
| 83 | |
| 84 | // iterate through directory recursively |
| 85 | while(dirIterator.hasNext()) |
| 86 | { |
| 87 | // Get Next File/Dir |
| 88 | dirIterator.next(); |
| 89 | |
| 90 | // Get FileInfo |
| 91 | QFileInfo fileInfo = dirIterator.fileInfo(); |
| 92 | |
| 93 | // if not this directory or previous directory |
| 94 | if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".") |
| 95 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 96 | QString absFilePath = fileInfo.absoluteFilePath(); |
| 97 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 98 | // if this is a directory |
| 99 | if(fileInfo.isDir()) |
| 100 | { |
| 101 | QStringList dirList = m_watcher->directories(); |
| 102 | |
| 103 | // if the directory is not already being watched |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 104 | if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath)) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 105 | { |
| 106 | // add this directory to the watch list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 107 | m_watcher->addPath(absFilePath); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | else |
| 111 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 112 | // construct struct |
| 113 | sFileInfo fileInfoStruct; |
| 114 | fileInfoStruct.fileInfo = fileInfo; |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 115 | fileInfoStruct.hash = calcChecksum(absFilePath); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 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 | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 126 | QVector<sEventInfo> FileSystemWatcher::reconcileDirectory(QHash<QString, sFileInfo> currentState, QString dirPath) |
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 | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 129 | QVector<sEventInfo> dirChanges; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 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); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 133 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 134 | while(i.hasNext()) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 135 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 136 | i.next(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 137 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 138 | QString absFilePath = i.key(); |
| 139 | |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 140 | if(!absFilePath.startsWith(dirPath)) |
| 141 | { |
| 142 | continue; |
| 143 | } |
| 144 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 145 | sFileInfo storedFileInfoStruct = i.value(); |
| 146 | QFileInfo storedFileInfo = storedFileInfoStruct.fileInfo; |
| 147 | QByteArray storedHash = storedFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 148 | |
| 149 | // check file existence |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 150 | if(currentState.contains(absFilePath)) |
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 | sFileInfo currentFileInfoStruct = currentState.value(absFilePath); |
| 153 | QFileInfo currentFileInfo = currentFileInfoStruct.fileInfo; |
| 154 | QByteArray currentHash = currentFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 155 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 156 | if((storedFileInfo != currentFileInfo) || (storedHash != currentHash)) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 157 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 158 | // update stored state |
| 159 | i.setValue(currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 160 | |
| 161 | // this file has been modified |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 162 | sEventInfo eventInfo; |
| 163 | eventInfo.event = MODIFIED; |
| 164 | eventInfo.absFilePath = absFilePath; |
| 165 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 168 | // delete this file from fileList we have processed it |
| 169 | currentState.remove(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 170 | } |
| 171 | else |
| 172 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 173 | // delete from stored state |
| 174 | i.remove(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 175 | |
| 176 | // this file has been deleted |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 177 | sEventInfo eventInfo; |
| 178 | eventInfo.event = DELETED; |
| 179 | eventInfo.absFilePath = absFilePath; |
| 180 | dirChanges.push_back(eventInfo); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 181 | } |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 184 | // any files left in fileList have been added |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 185 | for(QHash<QString, sFileInfo>::iterator i = currentState.begin(); i != currentState.end(); ++i) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 186 | { |
| 187 | QString absFilePath = i.key(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 188 | sFileInfo currentFileInfoStruct = i.value(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 189 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 190 | m_storedState.insert(absFilePath, currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 191 | |
| 192 | // this file has been added |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 193 | sEventInfo eventInfo; |
| 194 | eventInfo.event = ADDED; |
| 195 | eventInfo.absFilePath = absFilePath; |
| 196 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 199 | return dirChanges; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 200 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 201 | |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame^] | 202 | QByteArray FileSystemWatcher::calcChecksum(QString absFilePath) |
| 203 | { |
| 204 | // initialize checksum |
| 205 | QCryptographicHash crypto(QCryptographicHash::Md5); |
| 206 | |
| 207 | // open file |
| 208 | QFile file(absFilePath); |
| 209 | file.open(QFile::ReadOnly); |
| 210 | |
| 211 | // calculate checksum |
| 212 | while(!file.atEnd()) |
| 213 | { |
| 214 | crypto.addData(file.read(8192)); |
| 215 | } |
| 216 | |
| 217 | return crypto.result(); |
| 218 | } |
| 219 | |
| 220 | void FileSystemWatcher::printToGui(QVector<sEventInfo> dirChanges) |
| 221 | { |
| 222 | if(!dirChanges.isEmpty()) |
| 223 | { |
| 224 | QStringList dirChangesList; |
| 225 | |
| 226 | for(int i = 0; i < dirChanges.size(); i++) |
| 227 | { |
| 228 | QString tempString; |
| 229 | |
| 230 | eEvent event = dirChanges[i].event; |
| 231 | QString absFilePath = dirChanges[i].absFilePath; |
| 232 | |
| 233 | switch(event) |
| 234 | { |
| 235 | case ADDED: |
| 236 | tempString.append("ADDED: "); |
| 237 | break; |
| 238 | case MODIFIED: |
| 239 | tempString.append("MODIFIED: "); |
| 240 | break; |
| 241 | case DELETED: |
| 242 | tempString.append("DELETED: "); |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | tempString.append(absFilePath); |
| 247 | |
| 248 | dirChangesList.append(tempString); |
| 249 | |
| 250 | qDebug() << "\t" << tempString; |
| 251 | } |
| 252 | |
| 253 | m_listViewModel->setStringList(dirChangesList); |
| 254 | } |
| 255 | } |
| 256 | |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 257 | #if WAF |
| 258 | #include "filesystemwatcher.moc" |
| 259 | #include "filesystemwatcher.cpp.moc" |
| 260 | #endif |