Skip to content

fix(personalization): handle custom wallpapers#3319

Open
mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:xxx
Open

fix(personalization): handle custom wallpapers#3319
mhduiy wants to merge 1 commit into
linuxdeepin:masterfrom
mhduiy:xxx

Conversation

@mhduiy

@mhduiy mhduiy commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

修复Treeland设置自定义壁纸的时候,Treeland 可能没有权限去读, 以及原始资源如果删除,则Treeland会找不到壁纸资源的问题

Summary by Sourcery

Improve handling of custom wallpapers and wallpaper lookup, ensuring Treeland can set and track wallpapers even when using arbitrary image paths or previously unknown resources.

New Features:

  • Support adding and using custom wallpapers on-the-fly when Treeland sets a wallpaper that is not yet tracked by the personalization system.

Bug Fixes:

  • Ensure wallpaper installation uses the actual wallpaper item type, preventing mismatches in model selection and job naming.
  • Allow wallpaper lookup across all wallpaper lists when requested, fixing cases where existing wallpapers could not be found by URL.

Enhancements:

  • Make custom wallpaper saving return the stored path and optionally avoid auto-applying it, enabling more flexible reuse by Treeland and other callers.

@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.

Sorry @mhduiy, you have reached your weekly rate limit of 500000 diff characters.

Please try again later or upgrade to continue using Sourcery

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: mhduiy

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

@mhduiy mhduiy marked this pull request as draft June 26, 2026 11:18
@mhduiy mhduiy force-pushed the xxx branch 3 times, most recently from bf2fdd5 to b656fff Compare June 26, 2026 11:31
@deepin-bot

deepin-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 6.1.97
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #3322

@deepin-bot

deepin-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 6.1.98
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #3328

@mhduiy mhduiy changed the title xxx fix(personalization): handle custom wallpapers Jul 6, 2026
@mhduiy mhduiy marked this pull request as ready for review July 6, 2026 10:20
@sourcery-ai

sourcery-ai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adjusts wallpaper installation to use the actual wallpaper type from the item, improves wallpaper lookup to support cross-type and custom wallpapers, and refactors custom wallpaper handling to support non-auto-set usage in Treeland while ensuring wallpapers remain accessible even if original resources are missing or unreadable.

Sequence diagram for Treeland custom wallpaper handling

sequenceDiagram
    actor User
    participant TreeLandWorker
    participant WallpaperProvider
    participant PersonalizationWorker

    User->>TreeLandWorker: setWallpaper(monitorName, url, type)
    TreeLandWorker->>TreeLandWorker: QFile::exists(url)
    TreeLandWorker->>TreeLandWorker: QUrl::fromLocalFile / QUrl::fromUserInput
    alt type is wallpaper_source_type_image
        TreeLandWorker->>WallpaperProvider: findWallpaperItem(dest, Wallpaper_all)
        WallpaperProvider-->>TreeLandWorker: std::optional<WallpaperItemPtr>
        opt [no wallpaper item found]
            TreeLandWorker->>PersonalizationWorker: addCustomWallpaper(destUrl.toString(), false)
            PersonalizationWorker-->>TreeLandWorker: lastHashPath
            TreeLandWorker->>TreeLandWorker: dest = lastHashPath
        end
    end
    TreeLandWorker->>TreeLandWorker: [continue applying wallpaper with dest]
Loading

File-Level Changes

Change Details Files
Use the wallpaper item’s stored type for installation and job naming instead of the passed-in type.
  • Introduce a local wallpaperType variable from the found wallpaper item
  • Use wallpaperType when retrieving the wallpaper model
  • Use wallpaperType when constructing the installation job name
src/plugin-personalization/operation/wallpaperprovider.cpp
Enhance wallpaper item lookup to search across all wallpaper types when requested and avoid code duplication.
  • Add a local helper lambda to search a list of wallpaper items by URL
  • Implement Wallpaper_all handling by scanning all lists in m_wallpaperList
  • Reuse the helper lambda for type-specific wallpaper lookups
src/plugin-personalization/operation/wallpaperprovider.cpp
Support robust custom wallpaper handling in Treeland, including adding missing wallpapers and allowing caller-controlled auto-set behavior.
  • Normalize incoming wallpaper URLs into both QString path and QUrl forms
  • When setting an image wallpaper, check if it exists in wallpaper items and add it as a custom wallpaper if missing
  • Modify addCustomWallpaper to return the saved wallpaper path and accept an autoSet flag
  • Update header declaration and member ordering to match the new addCustomWallpaper signature and usage
src/plugin-personalization/operation/treelandworker.cpp
src/plugin-personalization/operation/personalizationworker.cpp
src/plugin-personalization/operation/personalizationworker.h

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

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

## Individual Comments

### Comment 1
<location path="src/plugin-personalization/operation/treelandworker.cpp" line_range="358-361" />
<code_context>
         dest = destUrl.toLocalFile();
     }

+    if (type == WallpaperContext::wallpaper_source_type_image && Q_LIKELY(m_wallpaperWorker)) {
+        if (m_wallpaperWorker->findWallpaperItem(dest, WallpaperEnums::Wallpaper_all) == std::nullopt) {
+            qInfo(DdcPersonnalizationTreelandWorker) << "cannot find wallpaper item for url:" << dest << ", adding as custom wallpaper.";
+            dest = addCustomWallpaper(destUrl.toString(), false);
+            qInfo(DdcPersonnalizationTreelandWorker) << "added custom wallpaper, new path:" << dest;
+        }
</code_context>
<issue_to_address>
**issue (bug_risk):** Matching on `dest` (local path) against `wallpaperItem->url` may fail and cause duplicate custom wallpapers.

`findWallpaperItem` is passed `dest` (a local file path from `toLocalFile()`), but internally it compares the argument to `wallpaperItem->url`. If stored items use `file://` URLs or remote URIs rather than plain paths, this lookup will always fail and a new custom wallpaper will be added each time. Please normalize the identifier used for matching (e.g., always canonical URL or always local path) consistently when storing and searching wallpaper items.
</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 +358 to +361
if (type == WallpaperContext::wallpaper_source_type_image && Q_LIKELY(m_wallpaperWorker)) {
if (m_wallpaperWorker->findWallpaperItem(dest, WallpaperEnums::Wallpaper_all) == std::nullopt) {
qInfo(DdcPersonnalizationTreelandWorker) << "cannot find wallpaper item for url:" << dest << ", adding as custom wallpaper.";
dest = addCustomWallpaper(destUrl.toString(), false);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

issue (bug_risk): Matching on dest (local path) against wallpaperItem->url may fail and cause duplicate custom wallpapers.

findWallpaperItem is passed dest (a local file path from toLocalFile()), but internally it compares the argument to wallpaperItem->url. If stored items use file:// URLs or remote URIs rather than plain paths, this lookup will always fail and a new custom wallpaper will be added each time. Please normalize the identifier used for matching (e.g., always canonical URL or always local path) consistently when storing and searching wallpaper items.

1. Return the saved path when adding custom wallpapers.
2. Allow custom wallpapers to be added without applying them immediately.
3. Register unknown Treeland image wallpapers before setting them.
4. Find wallpapers across all types and install packages with the matched type.

Log: Handle custom wallpaper paths correctly when setting Treeland wallpapers.

fix(personalization): 处理自定义壁纸

1. 添加自定义壁纸时返回保存后的路径。
2. 支持添加自定义壁纸但不立即应用。
3. 在设置 Treeland 图片壁纸前注册未知壁纸。
4. 跨全部类型查找壁纸,并使用匹配到的类型安装包。

Log: 修正 Treeland 设置壁纸时自定义壁纸路径的处理。
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

★ 总体评分:75分

■ 【总体评价】

代码修复了变量遮蔽和类型搜索等核心BUG,但存在URL格式不匹配导致的非致命逻辑瑕疵
逻辑正确性存在瑕疵因URL格式转换导致已有壁纸可能被错误重复添加扣25分

■ 【详细分析】

  • 1.语法逻辑(存在错误)✕

在treelandworker.cpp的setWallpaper函数中,当本地文件存在时,使用QUrl::fromLocalFile(url)构造destUrl,随后调用findWallpaperItem(destUrl.toString(), ...)进行查找。此时toString()会返回带有file://协议前缀的字符串,而m_wallpaperList中存储的wallpaperItem->url通常是纯本地绝对路径。这种格式不匹配会导致原本已存在于列表中的壁纸无法被正确匹配,从而错误地触发addCustomWallpaper逻辑,造成壁纸被重复添加和拷贝。
潜在问题:已有壁纸被错误识别为未添加状态;产生不必要的文件拷贝;用户界面可能出现重复的壁纸项。
建议:在调用findWallpaperItem时传入纯本地路径dest而非destUrl.toString();或者在findWallpaperItem内部统一对传入的URL进行标准化处理去除file://前缀。

  • 2.代码质量(良好)✓

代码通过提取findInItems lambda表达式有效消除了findWallpaperItem函数中的重复遍历逻辑,提升了代码复用率。installWallpaper函数中使用wallpaperType局部变量解决了参数遮蔽问题,命名清晰且符合规范。
建议:无

  • 3.代码性能(无性能问题)✓

新增的QImageReader::canRead()调用仅读取文件头部信息,不会导致大量IO开销。findWallpaperItem在处理Wallpaper_all类型时虽需遍历所有列表,但属于正常业务需求,且通过提前返回优化了最坏情况下的执行路径。
建议:无

  • 4.代码安全(存在0个安全漏洞)✓

漏洞对比统计:新增漏洞 0 个,减少漏洞 0 个,持平 0 个
代码引入的QImageReader校验增强了自定义壁纸的输入验证,有效防止了非图片文件被当作壁纸处理。未发现命令注入、路径遍历或其他安全漏洞。

  • 建议:保持当前的输入校验逻辑,确保后续文件操作均基于已校验的文件句柄。

■ 【改进建议代码示例】

diff --git a/src/plugin-personalization/operation/treelandworker.cpp b/src/plugin-personalization/operation/treelandworker.cpp
index c79e7e0b96..corrected_hash 100644
--- a/src/plugin-personalization/operation/treelandworker.cpp
+++ b/src/plugin-personalization/operation/treelandworker.cpp
@@ -414,7 +414,7 @@ bool TreeLandWorker::setWallpaper(const QString &monitorName, const QString &url
 
     if (type == WallpaperContext::wallpaper_source_type_image && Q_LIKELY(m_wallpaperWorker)) {
-        if (m_wallpaperWorker->findWallpaperItem(destUrl.toString(), WallpaperEnums::Wallpaper_all) == std::nullopt) {
+        if (m_wallpaperWorker->findWallpaperItem(dest, WallpaperEnums::Wallpaper_all) == std::nullopt) {
             qInfo(DdcPersonnalizationTreelandWorker) << "cannot find wallpaper item for url:" << dest << ", adding as custom wallpaper.";
-            dest = addCustomWallpaper(destUrl.toString(), false);
+            dest = addCustomWallpaper(dest, false);
             qInfo(DdcPersonnalizationTreelandWorker) << "added custom wallpaper, new path:" << dest;
         }
     }

@deepin-bot

deepin-bot Bot commented Jul 9, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 6.1.99
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #3348

@deepin-bot

deepin-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

TAG Bot

New tag: 6.1.100
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #3354

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