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 | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 52 | // reconcile directory and report changes |
| 53 | QVector<sEventInfo> dirChanges = reconcileDirectory(currentState); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 54 | |
| 55 | // update gui with list of changes in this directory |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 56 | qDebug() << endl << m_watcher->directories() << endl; |
| 57 | |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 58 | // DEBUG: Print to Gui |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 59 | if(!dirChanges.isEmpty()) |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 60 | { |
| 61 | QStringList dirChangesList; |
| 62 | |
| 63 | for(int i = 0; i < dirChanges.size(); i++) |
| 64 | { |
| 65 | QString tempString; |
| 66 | |
| 67 | eEvent event = dirChanges[i].event; |
| 68 | QString absFilePath = dirChanges[i].absFilePath; |
| 69 | |
| 70 | switch(event) |
| 71 | { |
| 72 | case ADDED: |
| 73 | tempString.append("ADDED: "); |
| 74 | break; |
| 75 | case MODIFIED: |
| 76 | tempString.append("MODIFIED: "); |
| 77 | break; |
| 78 | case DELETED: |
| 79 | tempString.append("DELETED: "); |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | tempString.append(absFilePath); |
| 84 | |
| 85 | dirChangesList.append(tempString); |
| 86 | } |
| 87 | |
| 88 | m_listViewModel->setStringList(dirChangesList); |
| 89 | } |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 92 | QHash<QString, sFileInfo> FileSystemWatcher::scanDirectory(QString dirPath) |
| 93 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 94 | // list of files in directory |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 95 | QHash<QString, sFileInfo> currentState; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 96 | |
| 97 | // directory iterator (recursive) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 98 | QDirIterator dirIterator(dirPath, QDirIterator::Subdirectories | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 99 | QDirIterator::FollowSymlinks); |
| 100 | |
| 101 | // iterate through directory recursively |
| 102 | while(dirIterator.hasNext()) |
| 103 | { |
| 104 | // Get Next File/Dir |
| 105 | dirIterator.next(); |
| 106 | |
| 107 | // Get FileInfo |
| 108 | QFileInfo fileInfo = dirIterator.fileInfo(); |
| 109 | |
| 110 | // if not this directory or previous directory |
| 111 | if(fileInfo.absoluteFilePath() != ".." && fileInfo.absoluteFilePath() != ".") |
| 112 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 113 | QString absFilePath = fileInfo.absoluteFilePath(); |
| 114 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 115 | // if this is a directory |
| 116 | if(fileInfo.isDir()) |
| 117 | { |
| 118 | QStringList dirList = m_watcher->directories(); |
| 119 | |
| 120 | // if the directory is not already being watched |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 121 | if (absFilePath.startsWith(m_dirPath) && !dirList.contains(absFilePath)) |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 122 | { |
| 123 | // add this directory to the watch list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 124 | m_watcher->addPath(absFilePath); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | else |
| 128 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 129 | // construct struct |
| 130 | sFileInfo fileInfoStruct; |
| 131 | fileInfoStruct.fileInfo = fileInfo; |
| 132 | |
| 133 | // initialize checksum |
| 134 | QCryptographicHash crypto(QCryptographicHash::Md5); |
| 135 | |
| 136 | // open file |
| 137 | QFile file(fileInfo.absoluteFilePath()); |
| 138 | file.open(QFile::ReadOnly); |
| 139 | |
| 140 | // calculate checksum |
| 141 | while(!file.atEnd()) |
| 142 | { |
| 143 | crypto.addData(file.read(8192)); |
| 144 | } |
| 145 | |
| 146 | fileInfoStruct.hash = crypto.result(); |
| 147 | |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 148 | // add this file to the file list |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 149 | currentState.insert(absFilePath, fileInfoStruct); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 154 | return currentState; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 157 | QVector<sEventInfo> FileSystemWatcher::reconcileDirectory(QHash<QString, sFileInfo> currentState) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 158 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 159 | // list of files changed |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 160 | QVector<sEventInfo> dirChanges; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 161 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 162 | // compare result (database/stored snapshot) to fileList (current snapshot) |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 163 | QMutableHashIterator<QString, sFileInfo> i(m_storedState); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 164 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 165 | while(i.hasNext()) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 166 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 167 | i.next(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 168 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 169 | QString absFilePath = i.key(); |
| 170 | |
| 171 | sFileInfo storedFileInfoStruct = i.value(); |
| 172 | QFileInfo storedFileInfo = storedFileInfoStruct.fileInfo; |
| 173 | QByteArray storedHash = storedFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 174 | |
| 175 | // check file existence |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 176 | if(currentState.contains(absFilePath)) |
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 | sFileInfo currentFileInfoStruct = currentState.value(absFilePath); |
| 179 | QFileInfo currentFileInfo = currentFileInfoStruct.fileInfo; |
| 180 | QByteArray currentHash = currentFileInfoStruct.hash; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 181 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 182 | if((storedFileInfo != currentFileInfo) || (storedHash != currentHash)) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 183 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 184 | // update stored state |
| 185 | i.setValue(currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 186 | |
| 187 | // this file has been modified |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 188 | sEventInfo eventInfo; |
| 189 | eventInfo.event = MODIFIED; |
| 190 | eventInfo.absFilePath = absFilePath; |
| 191 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 194 | // delete this file from fileList we have processed it |
| 195 | currentState.remove(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 196 | } |
| 197 | else |
| 198 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 199 | // delete from stored state |
| 200 | i.remove(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 201 | |
| 202 | // this file has been deleted |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 203 | sEventInfo eventInfo; |
| 204 | eventInfo.event = DELETED; |
| 205 | eventInfo.absFilePath = absFilePath; |
| 206 | dirChanges.push_back(eventInfo); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 207 | } |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 210 | // any files left in fileList have been added |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 211 | for(QHash<QString, sFileInfo>::iterator i = currentState.begin(); i != currentState.end(); ++i) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 212 | { |
| 213 | QString absFilePath = i.key(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 214 | sFileInfo currentFileInfoStruct = i.value(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 215 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 216 | m_storedState.insert(absFilePath, currentFileInfoStruct); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 217 | |
| 218 | // this file has been added |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame^] | 219 | sEventInfo eventInfo; |
| 220 | eventInfo.event = ADDED; |
| 221 | eventInfo.absFilePath = absFilePath; |
| 222 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 223 | } |
| 224 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 225 | return dirChanges; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 226 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 227 | |
| 228 | #if WAF |
| 229 | #include "filesystemwatcher.moc" |
| 230 | #include "filesystemwatcher.cpp.moc" |
| 231 | #endif |