Skip to content

fix: show error when print save fails to no-permission dir - #452

Closed
lzwind wants to merge 1 commit into
linuxdeepin:masterfrom
lzwind:agent/bugfix/380955
Closed

lzwind wants to merge 1 commit into
linuxdeepin:masterfrom
lzwind:agent/bugfix/380955

Conversation

@lzwind

@lzwind lzwind commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Root Cause Analysis

When a user prints a document to PDF and saves to a directory without write permission, DPrintPreviewDialog (DTK) silently fails the file write internally and closes the dialog without any error signal. The root cause is that DTK's DPrintPreviewDialog only emits paintRequested signals — there are no error/failure/saved/result signals (confirmed via Qt meta-object introspection). DocSheet::onPopPrintDialog() only connects paintRequested and has no mechanism to detect save failures, so the user gets no feedback when saving fails.

Fix Approach

Add indirect failure detection in DocSheet::onPopPrintDialog(): save the DPrinter* pointer (set in onPrintRequested()) as a member variable, connect QDialog::accepted to a lambda that checks whether outputFileName() is set and the file does not exist at that path — if so, show a DMessageBox warning. This is a symptom-level patch; the root-cause fix (adding save-result signals to DTK) is out of scope for this repository.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Only adds a QDialog::accepted signal connection (lambda), a member variable assignment in two onPrintRequested overloads, and a new private member m_printPrinter in DocSheet.h. No existing function signatures, public APIs, or call paths are modified.
  • Target functions are not products of prior bug fixes; this change does not revert any historical fix.

Business Impact Scope

Affected module: Print Preview. User scenario: saving a PDF to a no-permission directory now shows an error dialog instead of silently closing. Normal print operations (physical printer, saving to writable directories) are unaffected.

Verification Suggestion

  1. Save PDF to a writable directory — confirm no regression (no false error dialog).
  2. Save PDF to a read-only/no-permission directory — confirm DMessageBox warning appears.
  3. Cancel the print preview dialog — confirm no false error dialog.
  4. Physical printer printing — confirm no file-existence check triggers.

根因分析

用户在打印预览中保存 PDF 到无权限目录时,DTK 的 DPrintPreviewDialog 内部写入文件失败后静默关闭对话框,不发出任何错误信号。根因是 DPrintPreviewDialog 仅有 paintRequested 信号,无 error/failure/saved/result 信号(经 Qt 元对象内省确认)。DocSheet::onPopPrintDialog() 仅连接 paintRequested,无法检测保存失败,导致用户无任何反馈。

修复方案

在 DocSheet::onPopPrintDialog() 中增加间接失败检测:将 onPrintRequested() 中获取的 DPrinter* 指针保存为成员变量,连接 QDialog::accepted 信号到 lambda,检查 outputFileName() 非空且文件不存在时弹出 DMessageBox 警告。此为症状层补丁;根因层修复(DTK 增加保存结果信号)超出本仓库范围。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 仅新增 QDialog::accepted 信号连接(lambda)、两个 onPrintRequested 重载中的成员变量赋值、DocSheet.h 新增私有成员 m_printPrinter。不修改任何现有函数签名、公开 API 或调用路径。
  • 目标函数非历史修复产物,本次改动不撤销任何已有修复。

业务影响范围

受影响模块:打印预览。用户场景:保存 PDF 到无权限目录时会弹出错误提示,而非静默关闭。正常打印操作(物理打印机、保存到可写目录)不受影响。

验证建议

  1. 保存 PDF 到可写目录——确认无回归(无误报错误对话框)。
  2. 保存 PDF 到只读/无权限目录——确认弹出 DMessageBox 警告。
  3. 取消打印预览对话框——确认无误报错误对话框。
  4. 物理打印机打印——确认不触发文件存在性检查。

Summary by Sourcery

Bug Fixes:

  • Show a warning when saving a printed PDF fails, such as when the destination directory is not writable, instead of silently closing the print preview.

1. Root cause: DPrintPreviewDialog (DTK6) only exposes paintRequested
   signal, no error/failure/saved signal. DocSheet::onPopPrintDialog()
   connects only paintRequested, has no way to detect save failures.
   When user saves PDF to a no-permission directory, DTK internal save
   fails silently, dialog closes, no error shown to user.
2. Fix: save the DPrinter pointer in onPrintRequested callback, then
   connect QDialog::accepted signal in onPopPrintDialog to check if the
   output file was actually created via QFile::exists(). If the save path
   is non-empty (meaning save-to-file mode) but the file does not exist,
   show a DMessageBox warning prompting possible permission failure.
3. Impact: only affects print preview save-to-file flow; physical printer
   and normal save paths are unaffected; no API or behavior change for
   existing callers.

Log: Fix print preview not showing error when saving to no-permission directory

Influence:
1. Test print preview save to a writable directory (normal path)
2. Test print preview save to a read-only directory (error prompt shown)
3. Test print preview cancel operation (no false error prompt)
4. Test physical printer printing (no file existence check triggered)

fix: 修复打印预览保存到无权限目录时无失败提示

1. 根因:DPrintPreviewDialog(DTK6)仅暴露 paintRequested 信号,无错误/
   失败/保存成功信号。DocSheet::onPopPrintDialog() 仅连接 paintRequested,
   无法检测保存失败。用户保存 PDF 到无权限目录时,DTK 内部保存静默失败,
   对话框关闭,用户无任何错误提示。
2. 方案:在 onPrintRequested 回调中保存 DPrinter 指针,在 onPopPrintDialog
   中连接 QDialog::accepted 信号,检查输出文件是否实际创建(QFile::exists)。
   若保存路径非空(即保存到文件模式)但文件不存在,弹出 DMessageBox 提示
   可能是权限不足导致保存失败。
3. 影响:仅影响打印预览保存到文件的流程;物理打印和正常保存路径不受影响;
   对现有调用者无 API 或行为变化。

Log: 修复打印预览保存到无权限目录时无失败提示的问题

Influence:
1. 测试打印预览保存到有写权限的目录(正常路径)
2. 测试打印预览保存到只读目录(弹出错误提示)
3. 测试打印预览取消操作(无误报提示)
4. 测试物理打印机打印(不触发文件存在性检查)

PMS: BUG-380955
@lzwind
lzwind marked this pull request as draft November 15, 2026 04:59
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind

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 Nov 15, 2026

Copy link
Copy Markdown

Reviewer's Guide

UI

Track the active print request and warn after the preview is accepted when a save-to-file path was not created.

  • reader/uiframe/DocSheet.cpp L2088-2098 (+3 small edits)
  • reader/uiframe/DocSheet.h L1023

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 3 issues

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

## Individual Comments

### Comment 1
<location path="reader/uiframe/DocSheet.cpp" line_range="2093" />
<code_context>
+    connect(preview, &QDialog::accepted, this, [this]() {
+        if (m_printPrinter && !m_printPrinter->outputFileName().isEmpty()) {
+            QString savePath = m_printPrinter->outputFileName();
+            if (!QFile::exists(savePath)) {
+                DMessageBox::warning(this, tr("打印"), tr("保存失败,目标目录可能没有写入权限。"));
+            }
</code_context>
<issue_to_address>
**Save feedback misreports outcomes**

When the destination exists, a failed write leaves a partial file, a non-permission failure leaves no file, or a volume disconnects after writing, `QFile::exists(savePath)` suppresses warnings for failed overwrites or partial writes, but warns after a successful write followed by disconnection and labels other missing-output failures as permission problems; users get incorrect save feedback.

Use the print/write result to decide whether to warn, not file existence.

Also at `reader/uiframe/DocSheet.cpp:2095`.
</issue_to_address>

### Comment 2
<location path="reader/uiframe/DocSheet.cpp" line_range="2089" />
<code_context>
 #include <DPrintPreviewDialog>
 #include <DWidgetUtil>
+#include <DMessageBox>

 #include <QJsonDocument>
 #include <QJsonObject>
</code_context>
<issue_to_address>
**Earlier preview can crash**

When two preview dialogs for the same `DocSheet` remain active and the later one closes first, `m_printPrinter` is shared and reset or overwritten by the later request, so the earlier dialog's accepted handler reads its printer, possibly after deletion, missing its save result or dereferencing a stale pointer and crashing.

Capture and use each preview's own printer in its accepted handler instead of reading `m_printPrinter`.

Also at `reader/uiframe/DocSheet.cpp:1035`, `reader/uiframe/DocSheet.cpp:2091`.
</issue_to_address>

### Comment 3
<location path="reader/uiframe/DocSheet.cpp" line_range="1035" />
<code_context>

 void DocSheet::onPrintRequested(DPrinter *printer, const QVector<int> &pageRange)
 {
+    m_printPrinter = printer;
     qCDebug(appLog) << "Print requested for pages:" << pageRange;
     if (pageRange.isEmpty()) {
</code_context>
<issue_to_address>
**Direct-path saves skip detection**

When DTK handles a `setPrintFromPath()` print without emitting `paintRequested`, `m_printPrinter` stays null because its only assignment is in `onPrintRequested`; the accepted handler then skips the warning, leaving failed saves on this route silent.

Capture the printer independently of `paintRequested`, or connect failure detection to the direct-path save operation.

Also at `reader/uiframe/DocSheet.cpp:2091`.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

connect(preview, &QDialog::accepted, this, [this]() {
if (m_printPrinter && !m_printPrinter->outputFileName().isEmpty()) {
QString savePath = m_printPrinter->outputFileName();
if (!QFile::exists(savePath)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Save feedback misreports outcomes

When the destination exists, a failed write leaves a partial file, a non-permission failure leaves no file, or a volume disconnects after writing, QFile::exists(savePath) suppresses warnings for failed overwrites or partial writes, but warns after a successful write followed by disconnection and labels other missing-output failures as permission problems; users get incorrect save feedback.

Use the print/write result to decide whether to warn, not file existence.

Also at reader/uiframe/DocSheet.cpp:2095.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 2093:

**Save feedback misreports outcomes**

When the destination exists, a failed write leaves a partial file, a non-permission failure leaves no file, or a volume disconnects after writing, `QFile::exists(savePath)` suppresses warnings for failed overwrites or partial writes, but warns after a successful write followed by disconnection and labels other missing-output failures as permission problems; users get incorrect save feedback.

Use the print/write result to decide whether to warn, not file existence.

Also at `reader/uiframe/DocSheet.cpp:2095`.

connect(preview, QOverload<DPrinter *>::of(&DPrintPreviewDialog::paintRequested), this, QOverload<DPrinter *>::of(&DocSheet::onPrintRequested));
#endif
m_printPrinter = nullptr;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Earlier preview can crash

When two preview dialogs for the same DocSheet remain active and the later one closes first, m_printPrinter is shared and reset or overwritten by the later request, so the earlier dialog's accepted handler reads its printer, possibly after deletion, missing its save result or dereferencing a stale pointer and crashing.

Capture and use each preview's own printer in its accepted handler instead of reading m_printPrinter.

Also at reader/uiframe/DocSheet.cpp:1035, reader/uiframe/DocSheet.cpp:2091.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 2089:

**Earlier preview can crash**

When two preview dialogs for the same `DocSheet` remain active and the later one closes first, `m_printPrinter` is shared and reset or overwritten by the later request, so the earlier dialog's accepted handler reads its printer, possibly after deletion, missing its save result or dereferencing a stale pointer and crashing.

Capture and use each preview's own printer in its accepted handler instead of reading `m_printPrinter`.

Also at `reader/uiframe/DocSheet.cpp:1035`, `reader/uiframe/DocSheet.cpp:2091`.


void DocSheet::onPrintRequested(DPrinter *printer, const QVector<int> &pageRange)
{
m_printPrinter = printer;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟠 High · Direct-path saves skip detection

When DTK handles a setPrintFromPath() print without emitting paintRequested, m_printPrinter stays null because its only assignment is in onPrintRequested; the accepted handler then skips the warning, leaving failed saves on this route silent.

Capture the printer independently of paintRequested, or connect failure detection to the direct-path save operation.

Also at reader/uiframe/DocSheet.cpp:2091.

Prompt for AI agents
In `reader/uiframe/DocSheet.cpp` at line 1035:

**Direct-path saves skip detection**

When DTK handles a `setPrintFromPath()` print without emitting `paintRequested`, `m_printPrinter` stays null because its only assignment is in `onPrintRequested`; the accepted handler then skips the warning, leaving failed saves on this route silent.

Capture the printer independently of `paintRequested`, or connect failure detection to the direct-path save operation.

Also at `reader/uiframe/DocSheet.cpp:2091`.

@lzwind lzwind closed this Nov 17, 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