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