Skip to content

fix: handle trash monitor initialization failures and update trash icon - #789

Closed
wjyrich wants to merge 1 commit into
linuxdeepin:masterfrom
wjyrich:fix-bug-285725
Closed

wjyrich wants to merge 1 commit into
linuxdeepin:masterfrom
wjyrich:fix-bug-285725

Conversation

@wjyrich

@wjyrich wjyrich commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor
  1. Enhanced TrashMonitor to properly handle GIO monitor initialization failures with error logging
  2. Fixed potential null pointer dereference in destructor by adding null checks
  3. Connected trash attribute changes to update the trash icon in AppsModel
  4. Added updateTrashIcon method to toggle between empty/full trash icons based on trash item count
  5. Updated SPDX copyright headers to 2023-2026

Log: Improved trash icon dynamic updates and error handling for trash monitoring

Influence:

  1. Test trash icon updates when adding/removing items from trash
  2. Verify trash icon displays correctly when trash is empty vs full
  3. Test application launchpad with trash monitor initialization failures
  4. Verify no crashes when trash monitor fails to initialize
  5. Test icon updates persist across model data refreshes
  6. Verify trash icon state matches actual trash contents

fix: 处理回收站监控初始化失败并更新回收站图标

  1. 增强 TrashMonitor 对 GIO 监控初始化失败的错误处理和日志记录
  2. 在析构函数中添加空指针检查,修复潜在的空指针解引用问题
  3. 连接回收站属性变化事件,在 AppsModel 中动态更新回收站图标
  4. 添加 updateTrashIcon 方法,根据回收站项目数量切换空/满回收站图标
  5. 更新 SPDX 版权头至 2023-2026

Log: 改进了回收站图标的动态更新和回收站监控的错误处理

Influence:

  1. 测试向回收站添加/删除项目时图标是否及时更新
  2. 验证回收站为空和满时的图标显示是否正确
  3. 测试回收站监控初始化失败情况下启动器功能
  4. 验证回收站监控初始化失败时不会崩溃
  5. 测试图标更新在模型数据刷新后是否保持
  6. 验证回收站图标状态与实际回收站内容一致

PMS: BUG-285725

Summary by Sourcery

Integrate TrashMonitor with AppsModel to keep the trash application icon in sync with actual trash contents, while improving robustness and error handling around trash monitoring and updating license headers.

New Features:

  • Add trash monitoring to AppsModel so the trash icon reflects trash contents dynamically.

Bug Fixes:

  • Handle GIO trash monitor initialization failures gracefully with error logging and avoid crashes when the monitor cannot be created.
  • Prevent potential null pointer dereference in TrashMonitor destructor by guarding the unref call.

Enhancements:

  • Update trash icon selection in AppsModel based on trash item count and avoid redundant icon updates.
  • Refresh SPDX copyright headers across touched files to cover years 2023–2026.

1. Enhanced TrashMonitor to properly handle GIO monitor initialization
failures with error logging
2. Fixed potential null pointer dereference in destructor by adding
null checks
3. Connected trash attribute changes to update the trash icon in
AppsModel
4. Added updateTrashIcon method to toggle between empty/full trash icons
based on trash item count
5. Updated SPDX copyright headers to 2023-2026

Log: Improved trash icon dynamic updates and error handling for trash
monitoring

Influence:
1. Test trash icon updates when adding/removing items from trash
2. Verify trash icon displays correctly when trash is empty vs full
3. Test application launchpad with trash monitor initialization failures
4. Verify no crashes when trash monitor fails to initialize
5. Test icon updates persist across model data refreshes
6. Verify trash icon state matches actual trash contents

fix: 处理回收站监控初始化失败并更新回收站图标

1. 增强 TrashMonitor 对 GIO 监控初始化失败的错误处理和日志记录
2. 在析构函数中添加空指针检查,修复潜在的空指针解引用问题
3. 连接回收站属性变化事件,在 AppsModel 中动态更新回收站图标
4. 添加 updateTrashIcon 方法,根据回收站项目数量切换空/满回收站图标
5. 更新 SPDX 版权头至 2023-2026

Log: 改进了回收站图标的动态更新和回收站监控的错误处理

Influence:
1. 测试向回收站添加/删除项目时图标是否及时更新
2. 验证回收站为空和满时的图标显示是否正确
3. 测试回收站监控初始化失败情况下启动器功能
4. 验证回收站监控初始化失败时不会崩溃
5. 测试图标更新在模型数据刷新后是否保持
6. 验证回收站图标状态与实际回收站内容一致

PMS: BUG-285725
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: wjyrich

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@sourcery-ai

sourcery-ai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Reviewer's Guide

This PR improves robustness of trash monitoring and wires trash state into the application model so the trash icon reflects actual contents, while adding basic error handling and header updates.

Sequence diagram for trash state changes updating AppsModel trash icon

sequenceDiagram
    participant GIOFileMonitor
    participant TrashMonitor
    participant AppsModel
    participant AppItem

    GIOFileMonitor->>TrashMonitor: slot_onTrashMonitorChanged
    TrashMonitor-->>AppsModel: trashAttributeChanged
    AppsModel->>AppsModel: updateTrashIcon
    AppsModel->>AppsModel: itemFromDesktopId(dde-trash.desktop)
    AppsModel->>TrashMonitor: trashItemCount
    AppsModel->>AppItem: setIconName(user-trash | user-trash-full)
Loading

File-Level Changes

Change Details Files
Integrate TrashMonitor into AppsModel and update the trash icon based on trash contents.
  • Include TrashMonitor in AppsModel implementation and forward-declare it in the header.
  • Instantiate a TrashMonitor in AppsModel and connect its trashAttributeChanged signal to a new updateTrashIcon slot.
  • Implement updateTrashIcon to locate the trash app item, compute the appropriate icon (empty vs full) from trashItemCount, and update only when the icon actually changes.
src/models/appsmodel.cpp
src/models/appsmodel.h
Harden TrashMonitor initialization and destruction to avoid crashes on GIO monitor failures.
  • Change m_trashMonitor initialization to start as nullptr and perform g_file_monitor_file inside the constructor with a GError for diagnostics.
  • Log a warning when trash monitoring fails, clear the error, and skip signal connection when no monitor is available.
  • Add a null check before g_object_unref(m_trashMonitor) in the destructor to prevent potential null pointer dereference.
src/gioutils/trashmonitor.cpp
Update SPDX copyright headers to reflect 2023-2026 across touched files.
  • Adjust SPDX-FileCopyrightText year range from 2023 to 2023-2026 in AppsModel source and header.
  • Adjust SPDX-FileCopyrightText year range from 2023 to 2023-2026 in TrashMonitor source.
src/models/appsmodel.cpp
src/models/appsmodel.h
src/gioutils/trashmonitor.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • Consider defining and documenting the behavior of TrashMonitor::trashItemCount() when the underlying GIO monitor fails to initialize, and guard updateTrashIcon() accordingly so the AppsModel does not rely on a potentially unusable monitor in that failure path.
  • Using qCInfo for every trash icon update may be noisy in normal operation; consider lowering this to qCDebug or gating it behind a condition if you only need it during troubleshooting.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Consider defining and documenting the behavior of TrashMonitor::trashItemCount() when the underlying GIO monitor fails to initialize, and guard updateTrashIcon() accordingly so the AppsModel does not rely on a potentially unusable monitor in that failure path.
- Using qCInfo for every trash icon update may be noisy in normal operation; consider lowering this to qCDebug or gating it behind a condition if you only need it during troubleshooting.

## Individual Comments

### Comment 1
<location path="src/models/appsmodel.cpp" line_range="73-77" />
<code_context>
     Q_ASSERT(duplicatedItems.isEmpty());
     qDebug() << rowCount();

+    connect(m_trashMonitor, &TrashMonitor::trashAttributeChanged, this, &AppsModel::updateTrashIcon);
+
     m_tmUpdateCache->setInterval(1000);
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Consider updating the trash icon once during initialization to reflect the current state before any changes occur.

Currently the icon is only updated when `trashAttributeChanged` is emitted, so the launchpad can start with an incorrect trash icon until something changes. Please call `updateTrashIcon()` once after the `connect` (or at the end of the constructor) so the initial UI reflects the current trash state.

```suggestion
    qDebug() << rowCount();

    connect(m_trashMonitor, &TrashMonitor::trashAttributeChanged, this, &AppsModel::updateTrashIcon);
    updateTrashIcon();

    m_tmUpdateCache->setInterval(1000);
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread src/models/appsmodel.cpp
Comment on lines 73 to 77
qDebug() << rowCount();

connect(m_trashMonitor, &TrashMonitor::trashAttributeChanged, this, &AppsModel::updateTrashIcon);

m_tmUpdateCache->setInterval(1000);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider updating the trash icon once during initialization to reflect the current state before any changes occur.

Currently the icon is only updated when trashAttributeChanged is emitted, so the launchpad can start with an incorrect trash icon until something changes. Please call updateTrashIcon() once after the connect (or at the end of the constructor) so the initial UI reflects the current trash state.

Suggested change
qDebug() << rowCount();
connect(m_trashMonitor, &TrashMonitor::trashAttributeChanged, this, &AppsModel::updateTrashIcon);
m_tmUpdateCache->setInterval(1000);
qDebug() << rowCount();
connect(m_trashMonitor, &TrashMonitor::trashAttributeChanged, this, &AppsModel::updateTrashIcon);
updateTrashIcon();
m_tmUpdateCache->setInterval(1000);

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:100分

■ 【总体评价】

代码成功修复了文件监视器创建失败导致的空指针解引用崩溃,并规范地实现了回收站图标动态更新功能
逻辑严密、质量优秀、无性能损耗且无安全漏洞,符合满分标准

■ 【详细分析】

  • 1.语法逻辑(完全正确)✓
    修复了trashmonitor.cpp构造函数中g_file_monitor_file返回NULL直接传入g_signal_connect导致的段错误,增加GError处理与提前return,析构函数同步增加空判断,逻辑闭环完美

  • 2.代码质量(优秀)✓
    使用前向声明解耦头文件依赖,日志分级输出规范,updateTrashIcon采用卫语句控制流扁平化,完全符合UOS与Qt高阶编码规范

  • 3.代码性能(高效)✓
    基于GFileMonitor事件驱动机制替代轮询,updateTrashIcon的O(N)查找在桌面应用数量级下无性能损耗

  • 4.代码安全(存在0个安全漏洞)✓
    漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
    代码彻底消除了原有的空指针解引用拒绝服务风险,未引入任何命令注入、越界或信息泄露等安全缺陷

  • 建议:继续保持防御性编程习惯,在所有底层C接口调用处增加返回值校验

■ 【改进建议代码示例】

// appsmodel.cpp - 增加对 m_trashMonitor 指针本身的防御性校验,提升极端场景下的鲁棒性
void AppsModel::updateTrashIcon()
{
    if (!m_trashMonitor) {
        qCWarning(logModels) << "TrashMonitor instance is null, skipping icon update";
        return;
    }

    auto *trashItem = itemFromDesktopId(QStringLiteral("dde-trash.desktop"));
    if (!trashItem) {
        qCDebug(logModels) << "Trash item is not present in AppsModel";
        return;
    }

    const int count = m_trashMonitor->trashItemCount();
    const QString iconName = count > 0
        ? QStringLiteral("user-trash-full")
        : QStringLiteral("user-trash");
        
    if (trashItem->iconName() == iconName) {
        return;
    }

    qCInfo(logModels) << "Updating trash icon to:" << iconName;
    trashItem->setIconName(iconName);
}

@wjyrich wjyrich closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants