1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
/*
* Copyright (C) 2003-2008 Justin Karneges <justin@affinix.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
// since keyring files are often modified by creating a new copy and
// overwriting the original file, this messes up Qt's file watching
// capability since the original file goes away. to work around this
// problem, we'll watch the directories containing the keyring files
// instead of watching the actual files themselves.
//
// FIXME: qca 2.0.1 FileWatch has this logic already, so we can probably
// simplify this class.
#include "ringwatch.h"
#include "qca_safetimer.h"
#include "qca_support.h"
#include <QFileInfo>
using namespace QCA;
namespace gpgQCAPlugin {
RingWatch::RingWatch(QObject *parent)
: QObject(parent)
{
}
RingWatch::~RingWatch()
{
clear();
}
void RingWatch::add(const QString &filePath)
{
QFileInfo fi(filePath);
// Try to avoid symbolic links
QString path = fi.canonicalPath();
if (path.isEmpty())
path = fi.absolutePath();
// watching this path already?
DirWatch *dirWatch = nullptr;
foreach (const DirItem &di, dirs) {
if (di.dirWatch->dirName() == path) {
dirWatch = di.dirWatch;
break;
}
}
// if not, make a watcher
if (!dirWatch) {
// printf("creating dirwatch for [%s]\n", qPrintable(path));
DirItem di;
di.dirWatch = new DirWatch(path, this);
connect(di.dirWatch, &DirWatch::changed, this, &RingWatch::dirChanged);
di.changeTimer = new SafeTimer(this);
di.changeTimer->setSingleShot(true);
connect(di.changeTimer, &SafeTimer::timeout, this, &RingWatch::handleChanged);
dirWatch = di.dirWatch;
dirs += di;
}
FileItem i;
i.dirWatch = dirWatch;
i.fileName = fi.fileName();
i.exists = fi.exists();
if (i.exists) {
i.size = fi.size();
i.lastModified = fi.lastModified();
}
files += i;
// printf("watching [%s] in [%s]\n", qPrintable(fi.fileName()), qPrintable(i.dirWatch->dirName()));
}
void RingWatch::clear()
{
files.clear();
foreach (const DirItem &di, dirs) {
delete di.changeTimer;
delete di.dirWatch;
}
dirs.clear();
}
void RingWatch::dirChanged()
{
DirWatch *dirWatch = (DirWatch *)sender();
int at = -1;
for (int n = 0; n < dirs.count(); ++n) {
if (dirs[n].dirWatch == dirWatch) {
at = n;
break;
}
}
if (at == -1)
return;
// we get a ton of change notifications for the dir when
// something happens.. let's collect them and only
// report after 100ms
if (!dirs[at].changeTimer->isActive())
dirs[at].changeTimer->start(100);
}
void RingWatch::handleChanged()
{
SafeTimer *t = (SafeTimer *)sender();
int at = -1;
for (int n = 0; n < dirs.count(); ++n) {
if (dirs[n].changeTimer == t) {
at = n;
break;
}
}
if (at == -1)
return;
DirWatch *dirWatch = dirs[at].dirWatch;
const QString dir = dirWatch->dirName();
// see which files changed
QStringList changeList;
for (int n = 0; n < files.count(); ++n) {
FileItem &i = files[n];
QString filePath = dir + QLatin1Char('/') + i.fileName;
QFileInfo fi(filePath);
// if the file didn't exist, and still doesn't, skip
if (!i.exists && !fi.exists())
continue;
// size/lastModified should only get checked here if
// the file existed and still exists
if (fi.exists() != i.exists || fi.size() != i.size || fi.lastModified() != i.lastModified) {
changeList += filePath;
i.exists = fi.exists();
if (i.exists) {
i.size = fi.size();
i.lastModified = fi.lastModified();
}
}
}
foreach (const QString &s, changeList)
emit changed(s);
}
} // end namespace gpgQCAPlugin
|