-
Notifications
You must be signed in to change notification settings - Fork 43
fix: add MIME info change detection and reload #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. | ||
| // SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. | ||
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
| #include <QProcess> | ||
|
|
@@ -15,6 +15,21 @@ MimeManager1Service::MimeManager1Service(ApplicationManager1Service *parent) | |
| if (adaptor == nullptr or !registerObjectToDBus(this, DDEApplicationManager1MimeManager1ObjectPath, MimeManager1Interface)) { | ||
| std::terminate(); | ||
| } | ||
|
|
||
| // 监控用户配置目录下的 mimeapps.list 文件 | ||
| connect(&m_mimeAppsWatcher, &QFileSystemWatcher::fileChanged, this, &MimeManager1Service::onMimeAppsFileChanged); | ||
|
|
||
| // 添加用户配置目录下的 mimeapps.list 文件到监控 | ||
| QString userMimeAppsFile = getXDGConfigHome() + "/mimeapps.list"; | ||
| if (QFileInfo::exists(userMimeAppsFile)) { | ||
| m_mimeAppsWatcher.addPath(userMimeAppsFile); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 必须要存在么?这个文件会不会第一次在am起来后才创建的? |
||
| } else { | ||
| qWarning() << "User mimeapps.list file does not exist:" << userMimeAppsFile; | ||
| } | ||
|
|
||
| m_mimeAppsDebounceTimer.setSingleShot(true); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. issue (complexity): Consider replacing the shared You can simplify the control flow and remove the fragile 1. Replace
|
||
| m_mimeAppsDebounceTimer.setInterval(50); | ||
| connect(&m_mimeAppsDebounceTimer, &QTimer::timeout, this, &MimeManager1Service::handleMimeAppsFileDebounced); | ||
| } | ||
|
|
||
| MimeManager1Service::~MimeManager1Service() = default; | ||
|
|
@@ -106,12 +121,15 @@ void MimeManager1Service::setDefaultApplication(const QStringMap &defaultApps) n | |
| return; | ||
| } | ||
|
|
||
| m_internalWriteInProgress = true; | ||
| for (auto it = defaultApps.constKeyValueBegin(); it != defaultApps.constKeyValueEnd(); ++it) { | ||
| userConfig->setDefaultApplication(it->first, it->second); | ||
| } | ||
|
|
||
| if (!userConfig->writeToFile()) { | ||
| safe_sendErrorReply(QDBusError::Failed, "set default app failed, these config will be reset after re-login."); | ||
| m_internalWriteInProgress = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 有定时器,是不是不需要这个m_internalWriteInProgress这个flag了,
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. m_internalWriteInProgress这个是不是在写完就应该重置为false?而不是非要失败才重置为false, |
||
| return; | ||
| } | ||
|
Comment on lines
+124
to
133
|
||
| } | ||
|
|
||
|
|
@@ -126,12 +144,15 @@ void MimeManager1Service::unsetDefaultApplication(const QStringList &mimeTypes) | |
| return; | ||
| } | ||
|
|
||
| m_internalWriteInProgress = true; | ||
| for (const auto &mime : mimeTypes) { | ||
| userConfig->unsetDefaultApplication(mime); | ||
| } | ||
|
|
||
| if (!userConfig->writeToFile()) { | ||
| safe_sendErrorReply(QDBusError::Failed, "unset default app failed, these config will be reset after re-login."); | ||
| m_internalWriteInProgress = false; | ||
| return; | ||
| } | ||
|
Comment on lines
+147
to
156
|
||
| } | ||
|
|
||
|
|
@@ -156,3 +177,29 @@ void MimeManager1Service::updateMimeCache(QString dir) noexcept | |
| } | ||
|
|
||
| } | ||
|
|
||
| void MimeManager1Service::onMimeAppsFileChanged(const QString &path) | ||
| { | ||
| if (!m_mimeAppsWatcher.files().contains(path) && QFileInfo::exists(path)) { | ||
| m_mimeAppsWatcher.addPath(path); | ||
|
Comment on lines
+183
to
+184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (bug_risk): File-based watching may miss changes when mimeapps.list is deleted and later recreated QFileSystemWatcher will stop watching the path once mimeapps.list is removed, but your handler only re-adds it if QFileInfo::exists(path) is true at notification time. If the file is deleted and later recreated, you may never reattach the watcher and will miss subsequent changes. Consider also watching the parent directory and re-adding the file when it is (re)created, or otherwise handling the case where the file doesn’t exist at the moment of the change signal. Suggested implementation: void MimeManager1Service::onMimeAppsFileChanged(const QString &path)
{
const QString userMimeAppsFile = getXDGConfigHome() + "/mimeapps.list";
// Ensure we (re)attach a file watcher whenever mimeapps.list exists but is not watched.
// QFileSystemWatcher stops watching a file once it is removed, so this covers the
// "delete + recreate" case when combined with a directory watcher on the parent dir.
if (!m_mimeAppsWatcher.files().contains(userMimeAppsFile) && QFileInfo::exists(userMimeAppsFile)) {
m_mimeAppsWatcher.addPath(userMimeAppsFile);
}
// Debounce downstream handling of mimeapps.list changes
m_mimeAppsDebounceTimer.setSingleShot(true);
if (!m_mimeAppsDebounceTimer.isActive()) {
m_mimeAppsDebounceTimer.start();
}
}
To fully implement the suggestion and cover the delete + recreate scenario, you should also:
With these changes, the parent directory is always watched, and the file watcher is automatically reattached when
xionglinlin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // 如果是内部写入导致的文件变化,忽略 | ||
| if (m_internalWriteInProgress) { | ||
| m_internalWriteInProgress = false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 忽略了为什么还要重置它呀, |
||
| return; | ||
| } | ||
|
|
||
| m_mimeAppsDebounceTimer.start(); | ||
| } | ||
|
|
||
| void MimeManager1Service::handleMimeAppsFileDebounced() | ||
| { | ||
| auto *parentService = qobject_cast<ApplicationManager1Service *>(parent()); | ||
| if (!parentService) { | ||
| return; | ||
| } | ||
|
|
||
| qInfo() << "Reloading MIME info due to external configuration change."; | ||
| parentService->reloadMimeInfos(); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.