Skip to content

Conversation

@BLumia
Copy link
Member

@BLumia BLumia commented Sep 24, 2025

确保窗口预览图会使用来自图标主题的名称作为 fallback

Summary by Sourcery

Improve the window preview icon loader to handle both base64 image data and named icons by theme, renaming the method and adding fallback behavior and logging

Enhancements:

  • Rename updatePreviewIconFromBase64 to updatePreviewIconFromString and update its usage sites accordingly
  • Extend icon loading to fall back to theme icons when input string is not base64 image data
  • Log failures when icon loading fails and use a transparent placeholder as fallback

确保窗口预览图会使用来自图标主题的名称作为 fallback

PMS: BUG-335563
Log:
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: BLumia

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
Copy link

sourcery-ai bot commented Sep 24, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Replaced the base64-only icon loader with a unified method that parses the input string, loads embedded base64 data if present, or falls back to a theme icon by name, with logging and transparent placeholder on failure.

File-Level Changes

Change Details Files
Renamed icon update function and updated its call sites
  • Renamed updatePreviewIconFromBase64 to updatePreviewIconFromString in header
  • Replaced calls in both preview lambdas to use updatePreviewIconFromString
  • Updated function signature in x11preview.h accordingly
panels/dock/taskmanager/x11preview.cpp
panels/dock/taskmanager/x11preview.h
Enhanced icon loader implementation to support theme fallbacks
  • Split input string on "base64," to detect embedded image data
  • Loaded QPixmap from base64 when appropriate
  • Used QIcon::fromTheme for non-base64 icon names
  • Logged a warning and created a transparent pixmap if loading fails
panels/dock/taskmanager/x11preview.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

@deepin-ci-robot
Copy link

deepin pr auto review

我对这段代码进行了仔细审查,以下是我的分析和改进建议:

1. 语法逻辑

  • 代码逻辑基本清晰,但函数命名和实现存在不一致。原函数名 updatePreviewIconFromBase64 暗示只处理 base64 数据,但实际实现中已经隐式支持了其他格式。
  • 改进后的函数名 updatePreviewIconFromString 更准确地反映了函数的功能,这是一个好的改进。

2. 代码质量

  • 优点

    • 函数职责更加明确,现在能同时处理 base64 编码的图片数据和图标名称
    • 增加了错误处理,当图片加载失败时使用透明图片作为回退
    • 添加了日志输出,便于调试
  • 建议改进

    • 建议在函数开头添加注释,说明函数支持的输入格式和预期行为
    • 可以考虑将图标名称的处理逻辑提取为单独的函数,提高代码可读性
    • 常量 PREVIEW_TITLE_HEIGHT 应该在类中定义为成员常量,便于维护

3. 代码性能

  • 优点

    • 使用 QIcon::fromTheme 获取系统主题图标,这是高效的系统资源利用方式
    • 在图片加载失败时快速返回,避免不必要的处理
  • 建议改进

    • 考虑添加图标缓存机制,避免重复加载相同的图标
    • 对于频繁调用的场景,可以考虑异步加载图标,避免阻塞 UI 线程

4. 代码安全

  • 优点

    • 对输入数据进行适当的边界检查
    • 在图片加载失败时有合理的回退机制
  • 建议改进

    • 对 base64 数据进行验证,确保数据完整性
    • 考虑添加输入长度限制,防止潜在的内存耗尽攻击
    • 对于从外部获取的图标数据,建议进行尺寸限制,防止过大的图标导致内存问题

5. 具体改进建议代码

void X11WindowPreviewContainer::updatePreviewIconFromString(const QString &stringData)
{
    // 支持两种格式:
    // 1. base64编码的图片数据,格式为 "data:image/xxx;base64,xxxx"
    // 2. 系统主题图标名称
    QPixmap pix;
    const QStringList strs = stringData.split("base64,", Qt::SkipEmptyParts);
    
    if (strs.size() == 2) {
        // 处理base64图片数据
        QByteArray base64Data = strs.at(1).toLatin1();
        // 验证base64数据有效性
        if (base64Data.size() > MAX_ICON_DATA_SIZE) {
            qCWarning(x11WindowPreview) << "Icon data too large, size:" << base64Data.size();
            pix = createFallbackIcon();
            goto setPixmap;
        }
        
        pix.loadFromData(QByteArray::fromBase64(base64Data));
        if (pix.isNull()) {
            qCWarning(x11WindowPreview) << "Failed to load icon from base64 data";
            pix = createFallbackIcon();
        }
    } else {
        // 处理主题图标名称
        pix = QIcon::fromTheme(stringData, QIcon()).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
        if (pix.isNull()) {
            qCWarning(x11WindowPreview) << "Failed to load theme icon:" << stringData;
            pix = createFallbackIcon();
        }
    }

setPixmap:
    m_previewIcon->setPixmap(pix);
}

QPixmap X11WindowPreviewContainer::createFallbackIcon()
{
    QPixmap icon(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
    icon.fill(Qt::transparent);
    return icon;
}

6. 其他建议

  1. 考虑添加输入参数验证,确保传入的字符串不为空
  2. 可以添加最大图标尺寸限制,防止内存问题
  3. 建议将常量定义移到头文件中,如:
private:
    static const int MAX_ICON_DATA_SIZE = 1024 * 1024; // 1MB
    static const int PREVIEW_TITLE_HEIGHT = 24;

这些改进将提高代码的健壮性、可维护性和安全性,同时保持良好的性能表现。

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `panels/dock/taskmanager/x11preview.cpp:766-774` </location>
<code_context>
 {
-    const QStringList strs = base64Data.split("base64,");
     QPixmap pix;
+    const QStringList strs = stringData.split("base64,");
     if (strs.size() == 2) {
+        // is base64 image data
</code_context>

<issue_to_address>
**suggestion:** Splitting on 'base64,' may not handle malformed or unexpected input robustly.

If 'base64,' is missing or appears multiple times, the split may not work as intended. Input validation or a more reliable parsing method is recommended.

```suggestion
    QPixmap pix;
    int base64Idx = stringData.indexOf("base64,");
    if (base64Idx != -1) {
        QString base64Part = stringData.mid(base64Idx + 7); // 7 = length of "base64,"
        if (!base64Part.isEmpty()) {
            QByteArray decoded = QByteArray::fromBase64(base64Part.toLatin1());
            if (!decoded.isEmpty() && pix.loadFromData(decoded)) {
                // Successfully loaded base64 image data
            } else {
                // Malformed base64 or failed to load, fallback to icon theme
                pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
            }
        } else {
            // Empty base64 part, fallback to icon theme
            pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
        }
    } else {
        // "base64," not found, treat as icon name from theme
        pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
    }
```
</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 on lines 766 to 774
QPixmap pix;
const QStringList strs = stringData.split("base64,");
if (strs.size() == 2) {
// is base64 image data
pix.loadFromData(QByteArray::fromBase64(strs.at(1).toLatin1()));
} else {
// is (likely) icon name from theme
pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
}
Copy link

Choose a reason for hiding this comment

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

suggestion: Splitting on 'base64,' may not handle malformed or unexpected input robustly.

If 'base64,' is missing or appears multiple times, the split may not work as intended. Input validation or a more reliable parsing method is recommended.

Suggested change
QPixmap pix;
const QStringList strs = stringData.split("base64,");
if (strs.size() == 2) {
// is base64 image data
pix.loadFromData(QByteArray::fromBase64(strs.at(1).toLatin1()));
} else {
// is (likely) icon name from theme
pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
}
QPixmap pix;
int base64Idx = stringData.indexOf("base64,");
if (base64Idx != -1) {
QString base64Part = stringData.mid(base64Idx + 7); // 7 = length of "base64,"
if (!base64Part.isEmpty()) {
QByteArray decoded = QByteArray::fromBase64(base64Part.toLatin1());
if (!decoded.isEmpty() && pix.loadFromData(decoded)) {
// Successfully loaded base64 image data
} else {
// Malformed base64 or failed to load, fallback to icon theme
pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
}
} else {
// Empty base64 part, fallback to icon theme
pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
}
} else {
// "base64," not found, treat as icon name from theme
pix = QIcon::fromTheme(stringData).pixmap(PREVIEW_TITLE_HEIGHT, PREVIEW_TITLE_HEIGHT);
}

@BLumia BLumia merged commit 81d8a13 into linuxdeepin:master Sep 24, 2025
8 of 11 checks passed
@BLumia BLumia deleted the pms-335563 branch September 24, 2025 07:10
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.

3 participants