Skip to content

feat: use DDesktopServices to open files and folders#493

Merged
wyu71 merged 1 commit into
linuxdeepin:develop/snipefrom
wyu71:develop/snipe
Mar 3, 2026
Merged

feat: use DDesktopServices to open files and folders#493
wyu71 merged 1 commit into
linuxdeepin:develop/snipefrom
wyu71:develop/snipe

Conversation

@wyu71
Copy link
Copy Markdown
Contributor

@wyu71 wyu71 commented Mar 3, 2026

Replace dde-file-manager command line calls with DDesktopServices::showFolder and DDesktopServices::showFileItem for better integration. Sync from master commit #333.

Log: 改用DDesktopService的方法打开文件或文件夹
Influence: 使用DTK标准接口打开文件管理器,提升系统集成性

Replace dde-file-manager command line calls with DDesktopServices::showFolder
and DDesktopServices::showFileItem for better integration.
Sync from master commit linuxdeepin#333.

Log: 改用DDesktopService的方法打开文件或文件夹
Influence: 使用DTK标准接口打开文件管理器,提升系统集成性
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这份代码变更主要是将文件管理器的打开方式从直接调用 QProcess 启动 dde-file-manager 进程,改为使用 DTK 框架提供的封装接口 DDesktopServices

以下是对该代码变更的详细审查意见:

1. 语法逻辑

  • 通过:代码逻辑清晰,if-else 分支结构完整,覆盖了以下三种情况:
    1. 未选择文本:打开当前工作目录。
    2. 选择文本是文件夹:打开该文件夹。
    3. 选择文本是文件:打开文件管理器并选中该文件。
    4. 选择文本既非文件也非文件夹:回退到打开当前工作目录。
  • 改进建议:在 else if (fi.isFile()) 分支中,return 语句是显式写出的,而 fi.isDir() 分支中也有 return。最后的默认情况(非文件非文件夹)没有 return,但由于这是函数末尾,逻辑上是正确的。为了保持代码风格的一致性和防御性编程,建议在最后一行也加上 return;,或者保持现状也可以接受。

2. 代码质量

  • 改进点
    • API 封装性更好:使用 DDesktopServices 是更好的选择。它封装了底层的进程调用细节,使得代码更简洁,减少了直接管理 QProcess 对象的样板代码(如 process.startDetached)。
    • 可读性提升:移除了注释掉的旧代码和关于命令行参数的注释,代码看起来更干净。
    • 日志完善:保留了 qCDebug 日志,有助于调试追踪,且日志信息准确反映了当前的代码逻辑。

3. 代码性能

  • 评估:性能影响微乎其微。
  • 分析
    • 旧代码使用 QProcess::startDetached,新代码调用 DDesktopServices。在底层,DDesktopServices 通常也是通过 DBus 调用或启动进程来实现的。
    • QFileInfo fi(...) 的构造和检查(isFile, isDir)在旧代码中也是存在的,因此这部分开销没有变化。
    • 总体而言,这是一个功能替换,不涉及高频循环或计算密集型操作,不会对程序性能产生负面影响。

4. 代码安全

  • 潜在风险(命令注入)
    • 旧代码process.startDetached("dde-file-manager", {"--show-item", workingDirectory() + "/" + selectedText()});
      这里直接将 selectedText() 拼接到命令参数中。虽然 startDetached 的参数列表形式通常能避免直接的 Shell 命令注入(因为它不通过 /bin/sh -c 执行),但如果 selectedText() 包含特殊字符或路径非常奇怪,可能会传递给文件管理器意外的参数。
    • 新代码QFileInfo fi(workingDirectory() + "/" + selectedText()); 然后 DDesktopServices::showFileItem(QUrl::fromLocalFile(fi.filePath()));
      使用 QUrl::fromLocalFile 是处理文件路径的标准且安全的方式。它会自动处理路径中的特殊字符、转义以及本地文件系统的编码问题。这比直接字符串拼接更安全、更规范。

总结

这是一个高质量的代码重构。

  1. 安全性:通过引入 QUrl 处理路径,增强了对特殊字符的防护。
  2. 规范性:使用框架封装的高级 API (DDesktopServices) 替代了直接调用外部二进制文件,符合现代 Qt/DTK 开发的最佳实践。
  3. 可维护性:代码更简洁,逻辑分支更清晰。

建议
代码已经很好,如果非要吹毛求疵,可以在函数最后一行添加 return; 以明确控制流,但这不是必须的。

// 建议微调(可选)
// ...
// 选择的文本不是文件也不是文件夹
qCDebug(views) << "Branch: selectedText is not a file or directory, using DDesktopServices::showFolder";
DDesktopServices::showFolder(QUrl::fromLocalFile(workingDirectory()));
return; // 添加这一行保持风格一致
}

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 3, 2026

  • 敏感词检查失败, 检测到1个文件存在敏感词
详情
{
    "src/views/termwidget.cpp": [
        {
            "line": "    QString strUrl = \"https://cn.bing.com/search?q=\" + selectedText();",
            "line_number": 709,
            "rule": "S35",
            "reason": "Url link | fd576f862d"
        },
        {
            "line": "    QString strUrl = \"https://www.baidu.com/s?wd=\" + selectedText();",
            "line_number": 716,
            "rule": "S35",
            "reason": "Url link | 6f8d292d20"
        },
        {
            "line": "    QString strUrl = \"https://github.com/search?q=\" + selectedText();",
            "line_number": 723,
            "rule": "S35",
            "reason": "Url link | e1ba2d7c70"
        },
        {
            "line": "    QString strUrl = \"https://stackoverflow.com/search?q=\" + selectedText();",
            "line_number": 730,
            "rule": "S35",
            "reason": "Url link | bd659725f2"
        }
    ]
}

@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, wyu71

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

@wyu71 wyu71 merged commit 0b917a4 into linuxdeepin:develop/snipe Mar 3, 2026
10 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.

3 participants