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 | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 34 | m_timer->start(300000); |
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 | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 66 | QHash<QString, qint64> 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 | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 75 | QHash<QString, qint64> FileSystemWatcher::scanDirectory(QString dirPath) |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 76 | { |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 77 | // list of files in directory |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 78 | QHash<QString, qint64> 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 | { |
| 112 | // add this file to the file list |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 113 | currentState.insert(absFilePath, fileInfo.created().toMSecsSinceEpoch()); |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 118 | return currentState; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 121 | QVector<sEventInfo> FileSystemWatcher::reconcileDirectory(QHash<QString, qint64> currentState, QString dirPath) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 122 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 123 | // list of files changed |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 124 | QVector<sEventInfo> dirChanges; |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 125 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 126 | // compare result (database/stored snapshot) to fileList (current snapshot) |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 127 | QMutableHashIterator<QString, qint64> i(m_storedState); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 128 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 129 | while(i.hasNext()) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 130 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 131 | i.next(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 132 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 133 | QString absFilePath = i.key(); |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 134 | qint64 storedCreated = i.value(); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 135 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 136 | // if this file is in a level higher than |
| 137 | // this directory, ignore |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 138 | if(!absFilePath.startsWith(dirPath)) |
| 139 | { |
| 140 | continue; |
| 141 | } |
| 142 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 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 | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 146 | qint64 currentCreated = currentState.value(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 147 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 148 | if(storedCreated != currentCreated) |
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 | // update stored state |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 151 | i.setValue(currentCreated); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 152 | |
| 153 | // this file has been modified |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 154 | sEventInfo eventInfo; |
| 155 | eventInfo.event = MODIFIED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 156 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 157 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 158 | } |
| 159 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 160 | // delete this file from fileList we have processed it |
| 161 | currentState.remove(absFilePath); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 162 | } |
| 163 | else |
| 164 | { |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 165 | // delete from stored state |
| 166 | i.remove(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 167 | |
| 168 | // this file has been deleted |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 169 | sEventInfo eventInfo; |
| 170 | eventInfo.event = DELETED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 171 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 172 | dirChanges.push_back(eventInfo); |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 173 | } |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 176 | // any files left in fileList have been added |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 177 | for(QHash<QString, qint64>::iterator i = currentState.begin(); i != currentState.end(); ++i) |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 178 | { |
| 179 | QString absFilePath = i.key(); |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 180 | qint64 currentCreated = i.value(); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 181 | |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 182 | m_storedState.insert(absFilePath, currentCreated); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 183 | |
| 184 | // this file has been added |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 185 | sEventInfo eventInfo; |
| 186 | eventInfo.event = ADDED; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 187 | eventInfo.absFilePath = absFilePath.toStdString(); |
Jared Lindblom | 264310d | 2013-01-12 01:18:58 -0800 | [diff] [blame] | 188 | dirChanges.push_back(eventInfo); |
Jared Lindblom | 4d1d00a | 2013-01-11 01:14:23 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Jared Lindblom | a999630 | 2013-01-12 00:25:16 -0800 | [diff] [blame] | 191 | return dirChanges; |
jared | a954181 | 2013-01-09 00:08:46 -0800 | [diff] [blame] | 192 | } |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 193 | |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 194 | QByteArray FileSystemWatcher::calcChecksum(QString absFilePath) |
| 195 | { |
| 196 | // initialize checksum |
| 197 | QCryptographicHash crypto(QCryptographicHash::Md5); |
| 198 | |
| 199 | // open file |
| 200 | QFile file(absFilePath); |
| 201 | file.open(QFile::ReadOnly); |
| 202 | |
| 203 | // calculate checksum |
| 204 | while(!file.atEnd()) |
| 205 | { |
| 206 | crypto.addData(file.read(8192)); |
| 207 | } |
| 208 | |
| 209 | return crypto.result(); |
| 210 | } |
| 211 | |
| 212 | void FileSystemWatcher::printToGui(QVector<sEventInfo> dirChanges) |
| 213 | { |
| 214 | if(!dirChanges.isEmpty()) |
| 215 | { |
| 216 | QStringList dirChangesList; |
| 217 | |
| 218 | for(int i = 0; i < dirChanges.size(); i++) |
| 219 | { |
| 220 | QString tempString; |
| 221 | |
| 222 | eEvent event = dirChanges[i].event; |
Jared Lindblom | 8278b98 | 2013-01-12 15:45:09 -0800 | [diff] [blame^] | 223 | QString absFilePath = QString::fromStdString(dirChanges[i].absFilePath); |
Jared Lindblom | f8d32ad | 2013-01-12 11:32:27 -0800 | [diff] [blame] | 224 | |
| 225 | switch(event) |
| 226 | { |
| 227 | case ADDED: |
| 228 | tempString.append("ADDED: "); |
| 229 | break; |
| 230 | case MODIFIED: |
| 231 | tempString.append("MODIFIED: "); |
| 232 | break; |
| 233 | case DELETED: |
| 234 | tempString.append("DELETED: "); |
| 235 | break; |
| 236 | } |
| 237 | |
| 238 | tempString.append(absFilePath); |
| 239 | |
| 240 | dirChangesList.append(tempString); |
| 241 | |
| 242 | qDebug() << "\t" << tempString; |
| 243 | } |
| 244 | |
| 245 | m_listViewModel->setStringList(dirChangesList); |
| 246 | } |
| 247 | } |
| 248 | |
Alexander Afanasyev | ff73196 | 2013-01-09 17:16:28 -0800 | [diff] [blame] | 249 | #if WAF |
| 250 | #include "filesystemwatcher.moc" |
| 251 | #include "filesystemwatcher.cpp.moc" |
| 252 | #endif |