fix(index):添加/media后插入u盘,u盘内文件没被索引.

This commit is contained in:
JunjieBai 2024-11-05 11:20:14 +08:00 committed by iaom
parent 479daeb699
commit 16011de197
9 changed files with 54 additions and 6 deletions

View File

@ -38,6 +38,7 @@ DirWatcher::DirWatcher(QObject *parent)
} else { } else {
connect(m_dbusInterface, SIGNAL(appendIndexItem(QString, QStringList)), this, SLOT(sendAppendSignal(QString, QStringList))); connect(m_dbusInterface, SIGNAL(appendIndexItem(QString, QStringList)), this, SLOT(sendAppendSignal(QString, QStringList)));
connect(m_dbusInterface, SIGNAL(removeIndexItem(QString)), this, SLOT(sendRemoveSignal(QString))); connect(m_dbusInterface, SIGNAL(removeIndexItem(QString)), this, SLOT(sendRemoveSignal(QString)));
connect(m_dbusInterface, SIGNAL(mountAdded(QString)), this, SIGNAL(mountAdded(QString)));
} }
} }

View File

@ -50,6 +50,7 @@ public Q_SLOTS:
Q_SIGNALS: Q_SIGNALS:
void appendIndexItem(const QString&, const QStringList&); void appendIndexItem(const QString&, const QStringList&);
void removeIndexItem(const QString&); void removeIndexItem(const QString&);
void mountAdded(const QString&);
private: private:
DirWatcher(QObject *parent = nullptr); DirWatcher(QObject *parent = nullptr);
~DirWatcher(); ~DirWatcher();

View File

@ -56,6 +56,7 @@ private:
QStringList m_watchedRootPaths; QStringList m_watchedRootPaths;
FileSystemWatcher *q = nullptr; FileSystemWatcher *q = nullptr;
bool m_recursive = true; bool m_recursive = true;
QStringList m_failedWatchPaths;
}; };

View File

@ -30,6 +30,7 @@
#include "ukui-search-qdbus.h" #include "ukui-search-qdbus.h"
#include "file-utils.h" #include "file-utils.h"
#include "dir-watcher.h"
using namespace UkuiSearch; using namespace UkuiSearch;
FileSystemWatcherPrivate::FileSystemWatcherPrivate(FileSystemWatcher *parent) : q(parent) FileSystemWatcherPrivate::FileSystemWatcherPrivate(FileSystemWatcher *parent) : q(parent)
{ {
@ -183,6 +184,21 @@ FileSystemWatcher::FileSystemWatcher(bool recursive, WatchEvents events, WatchFl
d->m_watchEvents = events; d->m_watchEvents = events;
d->m_watchFlags = flags; d->m_watchFlags = flags;
d->m_recursive = recursive; d->m_recursive = recursive;
connect(DirWatcher::getDirWatcher(), &DirWatcher::mountAdded, this, [ & ] (const QString& mountPoint) {
for (const auto &path: d->m_failedWatchPaths) {
if (FileUtils::isOrUnder(path, mountPoint)) {
qDebug() << "the path that fail to watch has been mounted: " << path;
d->m_failedWatchPaths.removeAll(path);
for(const QString &changedPath : traverse(path)) {
QFileInfo info(changedPath);
if (info.isReadable() && info.isExecutable()) {
Q_EMIT accessibleChanged(changedPath, info.isDir(), true);
}
}
break;
}
}
});
} }
FileSystemWatcher::~FileSystemWatcher() FileSystemWatcher::~FileSystemWatcher()
@ -400,10 +416,17 @@ void FileSystemWatcher::eventProcess(int socket)
free(buf); free(buf);
} }
QStringList FileSystemWatcher::traverse(QString &path) QStringList FileSystemWatcher::traverse(const QString &path)
{ {
QStringList paths; QStringList paths;
d->addWatch(path); QFileInfo info(path);
if (info.isReadable() && info.isExecutable()) {
d->addWatch(path);
} else {
qWarning() << "Fail to watch" << path << ".Permission Dined";
d->m_failedWatchPaths.append(path);
}
if(!d->m_recursive || QFileInfo(path).isSymLink()) { if(!d->m_recursive || QFileInfo(path).isSymLink()) {
return paths; return paths;
} }
@ -419,8 +442,13 @@ QStringList FileSystemWatcher::traverse(QString &path)
list = dir.entryInfoList(); list = dir.entryInfoList();
for(auto i : list) { for(auto i : list) {
if(i.isDir() && !(i.isSymLink())) { if(i.isDir() && !(i.isSymLink())) {
queue.enqueue(i.absoluteFilePath()); if (i.isReadable() && i.isExecutable()) {
d->addWatch(i.absoluteFilePath()); queue.enqueue(i.absoluteFilePath());
d->addWatch(i.absoluteFilePath());
} else {
qWarning() << "Fail to watch" << i.absoluteFilePath();
d->m_failedWatchPaths.append(i.absoluteFilePath());
}
} }
paths.append(i.absoluteFilePath()); paths.append(i.absoluteFilePath());
} }

View File

@ -164,7 +164,7 @@ Q_SIGNALS:
void accessibleChanged(const QString& path, bool isDir, bool accessable); void accessibleChanged(const QString& path, bool isDir, bool accessable);
private Q_SLOTS: private Q_SLOTS:
void eventProcess(int socket); void eventProcess(int socket);
QStringList traverse(QString &path); QStringList traverse(const QString &path);
private: private:

View File

@ -28,6 +28,7 @@
#include <QMutexLocker> #include <QMutexLocker>
#include "config.h" #include "config.h"
#include "fileindexserviceadaptor.h" #include "fileindexserviceadaptor.h"
#include "volume-manager.h"
#define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf" #define CURRENT_INDEXABLE_DIR_SETTINGS QDir::homePath() + "/.config/org.ukui/ukui-search/ukui-search-current-indexable-dir.conf"
#define INDEXABLE_DIR_VALUE "IndexableDir" #define INDEXABLE_DIR_VALUE "IndexableDir"
@ -41,6 +42,7 @@ DirWatcher::DirWatcher(QObject *parent) : QObject(parent)
//兼容旧版配置 //兼容旧版配置
Config::self()->processCompatibleCache(); Config::self()->processCompatibleCache();
connect(VolumeManager::self(), &VolumeManager::MountAdded, this, &DirWatcher::mountAdded);
new FileindexAdaptor(this); new FileindexAdaptor(this);
} }

View File

@ -83,6 +83,7 @@ private:
Q_SIGNALS: Q_SIGNALS:
void appendIndexItem(const QString&, const QStringList&); void appendIndexItem(const QString&, const QStringList&);
void removeIndexItem(const QString&); void removeIndexItem(const QString&);
void mountAdded(const QString&);
//abondoned //abondoned
void udiskRemoved(); void udiskRemoved();
void indexDirsChanged(); void indexDirsChanged();

View File

@ -163,7 +163,20 @@ void VolumeManager::refresh()
void VolumeManager::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, VolumeManager *pThis) void VolumeManager::mountAddCallback(GVolumeMonitor *monitor, GMount *gmount, VolumeManager *pThis)
{ {
Q_UNUSED(monitor) Q_UNUSED(monitor)
Q_UNUSED(gmount) QString mountPoint;
auto rootFile = g_mount_get_root(gmount);
if (rootFile) {
auto gMountPoint = g_file_get_uri(rootFile);
mountPoint = gMountPoint;
g_free(gMountPoint);
if(!(mountPoint.startsWith("smb://"))){
gMountPoint = g_filename_from_uri(mountPoint.toUtf8().constData(), nullptr, nullptr);
mountPoint = gMountPoint;
g_free(gMountPoint);
}
g_object_unref(rootFile);
}
Q_EMIT pThis->MountAdded(mountPoint);
//TODO 识别U盘等移动设备 //TODO 识别U盘等移动设备
pThis->refresh(); pThis->refresh();
} }

View File

@ -61,6 +61,7 @@ public:
void refresh(); void refresh();
Q_SIGNALS: Q_SIGNALS:
void VolumeDataUpdated(); void VolumeDataUpdated();
void MountAdded(const QString&);
private: private:
explicit VolumeManager(QObject *parent = nullptr); explicit VolumeManager(QObject *parent = nullptr);