Skip to content

Conversation

@Kakueeen
Copy link
Contributor

@Kakueeen Kakueeen commented Dec 30, 2025

When traversing to a file path in SMB browser, the original
implementation would incorrectly navigate to the parent directory
without selecting the file. This fix ensures that when a file path is
encountered during traversal, the file is properly opened and the parent
directory is navigated to with the file pre-selected.

The changes include:

  1. Using synchronous file info creation to ensure immediate availability
  2. Publishing kOpenFiles signal to open the target file
  3. Setting the parent URL with a query parameter to pre-select the file
  4. Maintaining the navigation to parent directory but with file
    selection

Log: Fixed file selection issue when navigating to file paths in SMB
browser

Influence:

  1. Test navigating to file paths in SMB shares - should open file and
    navigate to parent with file selected
  2. Verify file opening functionality works correctly
  3. Check that directory navigation still functions normally
  4. Test with various file types and SMB share configurations
  5. Verify query parameter handling for file selection

fix: 修复遍历到文件路径时的文件选择问题

当在SMB浏览器中遍历到文件路径时,原实现会错误地导航到父目录而不选择文
件。此修复确保当在遍历过程中遇到文件路径时,文件被正确打开并且导航到父目
录时文件被预选中。

修改包括:

  1. 使用同步文件信息创建以确保即时可用性
  2. 发布kOpenFiles信号以打开目标文件
  3. 设置父URL的查询参数以预选文件
  4. 保持导航到父目录但带有文件选择功能

Log: 修复了SMB浏览器中导航到文件路径时的文件选择问题

Influence:

  1. 测试在SMB共享中导航到文件路径 - 应打开文件并导航到父目录且文件被选中
  2. 验证文件打开功能正常工作
  3. 检查目录导航功能仍然正常
  4. 测试各种文件类型和SMB共享配置
  5. 验证文件选择的查询参数处理

BUG: https://pms.uniontech.com/bug-view-342967.html

Summary by Sourcery

Fix SMB browser traversal so navigating to a file path opens the file and moves to its parent directory with the file pre-selected.

Bug Fixes:

  • Ensure SMB mounts navigate directly to the requested subpath without using persisted last-subpath records.
  • Detect file targets synchronously during traversal so file paths are handled reliably as files rather than directories.
  • Open target files and update the view to the parent directory with the file pre-selected when a file path is traversed.

Enhancements:

  • Simplify SMB mount handling by removing INI-based subpath persistence logic.

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.

Sorry @Kakueeen, 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
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Kakueeen

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

@deepin-ci-robot
Copy link
Contributor

deepin pr auto review

我来对这段代码变更进行详细审查:

  1. 语法逻辑分析:
  • 代码语法正确,符合C++规范
  • 逻辑流程清晰:先创建FileInfo,判断是否为文件,如果是文件则打开文件并定位到父目录
  • 代码变更合理,增加了文件打开和定位功能
  1. 代码质量改进建议:
  • 建议添加错误处理:当InfoFactory::create失败时应该有适当的错误处理
  • 建议将魔法字符串"selectUrl="定义为常量,提高代码可维护性
  • 建议添加注释说明为什么需要设置selectUrl查询参数
  1. 代码性能分析:
  • InfoFactory::create添加了Global::kCreateFileInfoSync参数,这可能会影响性能,建议评估是否必须同步创建
  • QUrl的toString()操作可能产生额外开销,建议考虑使用toEncoded()或其他更高效的方法
  1. 代码安全分析:
  • url.toString()可能包含特殊字符,建议进行URL编码
  • 建议验证url的有效性,防止路径遍历攻击
  • 建议检查subPath是否包含非法字符

改进后的代码建议:

// 定义常量
static const QString kSelectUrlQuery = QStringLiteral("selectUrl=");

void travers_prehandler::doChangeCurrentUrl(quint64 winId, const QString &mpt, const QString &subPath)
{
    // ... 前面的代码保持不变 ...

    QUrl url = QUrl::fromLocalFile(targetPath);
    FileInfoPointer fileInfo = InfoFactory::create<FileInfo>(url, Global::kCreateFileInfoSync);
    
    if (!fileInfo) {
        qWarning() << "Failed to create FileInfo for url:" << url;
        return;
    }

    if (fileInfo->isAttributes(FileInfo::FileIsType::kIsFile)) {
        // 打开文件
        dpfSignalDispatcher->publish(GlobalEventType::kOpenFiles, winId, QList<QUrl> { url });
        
        // 获取父目录URL并设置选中查询参数
        QUrl parentUrl = fileInfo->urlOf(UrlInfoType::kParentUrl);
        QString encodedUrl = QUrl::toPercentEncoding(url.toString());
        parentUrl.setQuery(kSelectUrlQuery + encodedUrl);
        url = parentUrl;
    }
    
    dpfSignalDispatcher->publish(GlobalEventType::kChangeCurrentUrl, winId, url);
    // ... 后面的代码保持不变 ...
}

这些改进主要关注:

  1. 添加了错误处理
  2. 定义了常量提高可维护性
  3. 添加了URL编码提高安全性
  4. 添加了日志输出便于调试
  5. 添加了代码注释提高可读性

@Kakueeen Kakueeen marked this pull request as draft December 30, 2025 06:21
When traversing to a file path in SMB browser, the original
implementation would incorrectly navigate to the parent directory
without selecting the file. This fix ensures that when a file path is
encountered during traversal, the file is properly opened and the parent
directory is navigated to with the file pre-selected.

The changes include:
1. Using synchronous file info creation to ensure immediate availability
2. Publishing kOpenFiles signal to open the target file
3. Setting the parent URL with a query parameter to pre-select the file
4. Maintaining the navigation to parent directory but with file
selection

Log: Fixed file selection issue when navigating to file paths in SMB
browser

Influence:
1. Test navigating to file paths in SMB shares - should open file and
navigate to parent with file selected
2. Verify file opening functionality works correctly
3. Check that directory navigation still functions normally
4. Test with various file types and SMB share configurations
5. Verify query parameter handling for file selection

fix: 修复遍历到文件路径时的文件选择问题

当在SMB浏览器中遍历到文件路径时,原实现会错误地导航到父目录而不选择文
件。此修复确保当在遍历过程中遇到文件路径时,文件被正确打开并且导航到父目
录时文件被预选中。

修改包括:
1. 使用同步文件信息创建以确保即时可用性
2. 发布kOpenFiles信号以打开目标文件
3. 设置父URL的查询参数以预选文件
4. 保持导航到父目录但带有文件选择功能

Log: 修复了SMB浏览器中导航到文件路径时的文件选择问题

Influence:
1. 测试在SMB共享中导航到文件路径 - 应打开文件并导航到父目录且文件被选中
2. 验证文件打开功能正常工作
3. 检查目录导航功能仍然正常
4. 测试各种文件类型和SMB共享配置
5. 验证文件选择的查询参数处理

BUG: https://pms.uniontech.com/bug-view-342967.html
@Kakueeen Kakueeen marked this pull request as ready for review December 30, 2025 06:30
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 30, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adjusts SMB browser traversal to treat file paths specially: on successful mount it navigates directly using the raw subPath, and when the target is a file it creates file info synchronously, opens the file via kOpenFiles, and navigates to the parent directory with the file pre‑selected via a query parameter.

Sequence diagram for SMB file path traversal and file opening

sequenceDiagram
    actor User
    participant SmbBrowserUI
    participant travers_prehandler
    participant DevMngIns
    participant DFMMOUNT
    participant InfoFactory
    participant FileInfo
    participant dpfSignalDispatcher

    User->>SmbBrowserUI: NavigateToSmbPath(winId, netUrl)
    SmbBrowserUI->>travers_prehandler: networkAccessPrehandler(winId, netUrl, after)

    travers_prehandler->>DevMngIns: mountNetworkDeviceAsync(mountSource, callback)
    DevMngIns-->>travers_prehandler: callback(ok, err, mpt)

    alt mountPointAvailable
        travers_prehandler->>travers_prehandler: doChangeCurrentUrl(winId, mpt, subPath, netUrl)
        travers_prehandler->>InfoFactory: create_FileInfo(urlFrom(mpt + subPath), kCreateFileInfoSync)
        InfoFactory-->>travers_prehandler: fileInfo

        alt targetIsFile
            travers_prehandler->>dpfSignalDispatcher: publish(kOpenFiles, winId, [fileUrl])
            travers_prehandler->>FileInfo: urlOf(kParentUrl)
            FileInfo-->>travers_prehandler: parentUrl
            travers_prehandler->>travers_prehandler: parentUrl.setQuery(selectUrl=fileUrl)
            travers_prehandler->>dpfSignalDispatcher: publish(kChangeCurrentUrl, winId, parentUrlWithSelection)
        else targetIsDirectoryOrNoInfo
            travers_prehandler->>dpfSignalDispatcher: publish(kChangeCurrentUrl, winId, dirUrl)
        end
    else alreadyMountedOrError
        travers_prehandler->>travers_prehandler: onSmbRootMounted(mountSource, after) or handleError
    end

    dpfSignalDispatcher-->>SmbBrowserUI: kChangeCurrentUrl event
    dpfSignalDispatcher-->>SmbBrowserUI: kOpenFiles event (when file)
    SmbBrowserUI-->>User: Parent directory shown with file opened and preselected
Loading

Flow diagram for updated doChangeCurrentUrl behavior

flowchart TD
    A["start doChangeCurrentUrl(winId, mpt, subPath, netUrl)"] --> B["Build targetPath from mpt and subPath"]
    B --> C["Create url from targetPath"]
    C --> D["InfoFactory::create FileInfo(url, kCreateFileInfoSync)"]
    D --> E{fileInfo exists and isFile?}

    E -- Yes --> F["publish kOpenFiles(winId, [url])"]
    F --> G["parentUrl = fileInfo.urlOf(kParentUrl)"]
    G --> H["parentUrl.setQuery(selectUrl=url.toString)"]
    H --> I["publish kChangeCurrentUrl(winId, parentUrl)"]

    E -- No --> J["publish kChangeCurrentUrl(winId, url)"]

    I --> K[end]
    J --> K[end]
Loading

File-Level Changes

Change Details Files
Simplify post-mount traversal to always use the provided subPath instead of recording/restoring last subpaths per SMB root.
  • Remove helper lambdas that persisted per-host subpaths in an INI file using QSettings.
  • Stop reading or writing the last-used subpath on successful mount.
  • Call doChangeCurrentUrl with the raw subPath returned from URL parsing.
src/plugins/filemanager/dfmplugin-smbbrowser/events/traversprehandler.cpp
Handle traversal to file paths by opening the file and navigating to its parent directory with the file pre-selected.
  • Create FileInfo objects synchronously using Global::kCreateFileInfoSync to ensure immediate availability of file metadata.
  • When the target is identified as a file, publish the GlobalEventType::kOpenFiles signal with the file URL to open it.
  • Derive the parent directory URL from the file info, set a selectUrl query parameter containing the file URL, and use that as the navigation target.
  • Retain the existing GlobalEventType::kChangeCurrentUrl signal publication with the possibly adjusted URL.
src/plugins/filemanager/dfmplugin-smbbrowser/events/traversprehandler.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

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 - I've found 2 issues, and left some high level feedback:

  • Building the query as parentUrl.setQuery("selectUrl=" + url.toString()); risks breaking when the file URL contains ?, & or non-ASCII characters; consider using QUrlQuery and addQueryItem("selectUrl", url.toString()) to ensure proper encoding and preservation of any existing query parameters.
  • The removal of the recordSubPath/readSubPath logic changes behavior when subPath is empty (no longer restoring the last visited sub-path for a share); if this persistence is still desired in some scenarios, it may be worth explicitly deciding whether this regression is acceptable or reintroducing a scoped version of that logic.
  • Switching InfoFactory::create<FileInfo>(url) to synchronous creation via Global::kCreateFileInfoSync may introduce UI blocking on slow SMB shares; if this code runs on the UI thread, consider at least guarding for long-running calls or adding a comment/rationale where this trade-off is acceptable.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- Building the query as `parentUrl.setQuery("selectUrl=" + url.toString());` risks breaking when the file URL contains `?`, `&` or non-ASCII characters; consider using `QUrlQuery` and `addQueryItem("selectUrl", url.toString())` to ensure proper encoding and preservation of any existing query parameters.
- The removal of the `recordSubPath`/`readSubPath` logic changes behavior when `subPath` is empty (no longer restoring the last visited sub-path for a share); if this persistence is still desired in some scenarios, it may be worth explicitly deciding whether this regression is acceptable or reintroducing a scoped version of that logic.
- Switching `InfoFactory::create<FileInfo>(url)` to synchronous creation via `Global::kCreateFileInfoSync` may introduce UI blocking on slow SMB shares; if this code runs on the UI thread, consider at least guarding for long-running calls or adding a comment/rationale where this trade-off is acceptable.

## Individual Comments

### Comment 1
<location> `src/plugins/filemanager/dfmplugin-smbbrowser/events/traversprehandler.cpp:97` </location>
<code_context>
-            if (jumpTo.isEmpty())
-                jumpTo = readSubPath(mountSource);
-            doChangeCurrentUrl(winId, mpt, jumpTo, netUrl);
+            doChangeCurrentUrl(winId, mpt, subPath, netUrl);
         } else if (ok || err.code == DFMMOUNT::DeviceError::kGIOErrorAlreadyMounted) {
             if (isSmb) onSmbRootMounted(mountSource, after);
</code_context>

<issue_to_address>
**question (bug_risk):** Removing the persisted `subPath` fallback changes navigation semantics for callers that pass an empty `subPath`.

Previously, an empty `subPath` caused us to reuse the last recorded subPath for that `mountSource` via `readSubPath`; now we always navigate using the (possibly empty) `subPath`, so flows that relied on “remember last location on this share” will instead land at the root. Is this behavior change intentional for all call sites? If not, consider reintroducing the fallback in a more explicit/centralized way.
</issue_to_address>

### Comment 2
<location> `src/plugins/filemanager/dfmplugin-smbbrowser/events/traversprehandler.cpp:159-160` </location>
<code_context>
+    if (fileInfo && fileInfo->isAttributes(FileInfo::FileIsType::kIsFile)) {
+        dpfSignalDispatcher->publish(GlobalEventType::kOpenFiles, winId, QList<QUrl> { url });
+
+        QUrl parentUrl = fileInfo->urlOf(UrlInfoType::kParentUrl);
+        parentUrl.setQuery("selectUrl=" + url.toString());
+        url = parentUrl;
+    }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Building the `selectUrl` query parameter via string concatenation may produce incorrect or lossy URLs.

`url.toString()` is being interpolated directly into the query, which can mis-handle characters like spaces, `&`, `?`, or `#`, and it also drops any existing query on `parentUrl`. Consider using `QUrlQuery` to add a `selectUrl` parameter with proper encoding, and explicitly choose the encoding for the URL (`toString()` with a mode or `toEncoded()`) to avoid malformed URLs and unintentionally overwriting other query parameters.
</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.

@Johnson-zs
Copy link
Contributor

/forcemerge

@deepin-bot
Copy link
Contributor

deepin-bot bot commented Jan 4, 2026

This pr force merged! (status: behind)

@deepin-bot deepin-bot bot merged commit e80e89b into linuxdeepin:master Jan 4, 2026
21 checks passed
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.

4 participants