QFileInfoGatherer/Win32: Connect to drive arrival/removal signals of watcher

Retrieve instance of QWindowsRemovableDriveListener via dynamic property
of QFileSystemWatcher and connect to its signals to add/remove drives.

Task-number: QTBUG-18729
Task-number: QTBUG-14290
Change-Id: I603a6f99aca8e6fb3e28d4bc997d8e49d7d0a7c8
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
bb10
Friedemann Kleint 2016-05-24 11:15:16 +02:00
parent 41b27047ea
commit 8e79806d08
2 changed files with 40 additions and 12 deletions

View File

@ -65,6 +65,18 @@ Q_AUTOTEST_EXPORT bool qt_test_isFetchedRoot()
}
#endif
static QString translateDriveName(const QFileInfo &drive)
{
QString driveName = drive.absoluteFilePath();
#ifdef Q_OS_WIN
if (driveName.startsWith(QLatin1Char('/'))) // UNC host
return drive.fileName();
if (driveName.endsWith(QLatin1Char('/')))
driveName.chop(1);
#endif // Q_OS_WIN
return driveName;
}
/*!
Creates thread
*/
@ -82,6 +94,16 @@ QFileInfoGatherer::QFileInfoGatherer(QObject *parent)
watcher = new QFileSystemWatcher(this);
connect(watcher, SIGNAL(directoryChanged(QString)), this, SLOT(list(QString)));
connect(watcher, SIGNAL(fileChanged(QString)), this, SLOT(updateFile(QString)));
# if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
const QVariant listener = watcher->property("_q_driveListener");
if (listener.canConvert<QObject *>()) {
if (QObject *driveListener = listener.value<QObject *>()) {
connect(driveListener, SIGNAL(driveAdded()), this, SLOT(driveAdded()));
connect(driveListener, SIGNAL(driveRemoved(QString)), this, SLOT(driveRemoved()));
}
}
# endif // Q_OS_WIN && !Q_OS_WINRT
#endif
start(LowPriority);
}
@ -106,6 +128,20 @@ void QFileInfoGatherer::setResolveSymlinks(bool enable)
#endif
}
void QFileInfoGatherer::driveAdded()
{
fetchExtendedInformation(QString(), QStringList());
}
void QFileInfoGatherer::driveRemoved()
{
QStringList drives;
const QFileInfoList driveInfoList = QDir::drives();
for (const QFileInfo &fi : driveInfoList)
drives.append(translateDriveName(fi));
newListOfFiles(QString(), drives);
}
bool QFileInfoGatherer::resolveSymlinks() const
{
#ifdef Q_OS_WIN
@ -260,18 +296,6 @@ QExtendedInformation QFileInfoGatherer::getInfo(const QFileInfo &fileInfo) const
return info;
}
static QString translateDriveName(const QFileInfo &drive)
{
QString driveName = drive.absoluteFilePath();
#if defined(Q_OS_WIN)
if (driveName.startsWith(QLatin1Char('/'))) // UNC host
return drive.fileName();
if (driveName.endsWith(QLatin1Char('/')))
driveName.chop(1);
#endif
return driveName;
}
/*
Get specific file info's, batch the files so update when we have 100
items and every 200ms after that

View File

@ -180,6 +180,10 @@ public Q_SLOTS:
void setResolveSymlinks(bool enable);
void setIconProvider(QFileIconProvider *provider);
private Q_SLOTS:
void driveAdded();
void driveRemoved();
private:
void run() Q_DECL_OVERRIDE;
// called by run():