| 379 | } |
| 380 | |
| 381 | bool TextDocument::save(DocumentSaveMode mode) |
| 382 | { |
| 383 | Q_D(TextDocument); |
| 384 | |
| 385 | if (!d->document) |
| 386 | return true; |
| 387 | |
| 388 | if (mode & Discard) |
| 389 | return true; |
| 390 | |
| 391 | switch (d->state) |
| 392 | { |
| 393 | case IDocument::Clean: |
| 394 | return true; |
| 395 | |
| 396 | case IDocument::Modified: |
| 397 | break; |
| 398 | |
| 399 | case IDocument::Dirty: |
| 400 | case IDocument::DirtyAndModified: |
| 401 | if (!(mode & Silent)) |
| 402 | { |
| 403 | int code = KMessageBox::warningTwoActionsCancel( |
| 404 | Core::self()->uiController()->activeMainWindow(), |
| 405 | i18n("The file \"%1\" is modified on disk.\n\nAre " |
| 406 | "you sure you want to overwrite it? (External " |
| 407 | "changes will be lost.)", |
| 408 | d->document->url().toLocalFile()), |
| 409 | i18nc("@title:window", "Document Externally Modified"), |
| 410 | KGuiItem(i18nc("@action:button", "Overwrite External Changes"), QStringLiteral("document-save")), |
| 411 | KStandardGuiItem::discard()); |
| 412 | if (code == KMessageBox::SecondaryAction) { |
| 413 | return true; // Discard |
| 414 | } else if (code == KMessageBox::Cancel) { |
| 415 | return false; |
| 416 | } |
| 417 | mode = Silent; // prevent documentSave() from asking essentially the same question again |
| 418 | } |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | if (!KDevelop::ensureWritable(QList<QUrl>() << url())) { |
| 423 | return false; |
| 424 | } |
| 425 | |
| 426 | if (mode & Silent) { |
| 427 | // Set the KTextEditor equivalent to DocumentSaveMode::Silent. |
| 428 | d->document->setModifiedOnDiskWarning(false); |
| 429 | } |
| 430 | const auto saved = d->document->documentSave(); |
| 431 | if (mode & Silent) { |
| 432 | // Restore the default value. Unfortunately, a getter to query the previous value is missing. |
| 433 | d->document->setModifiedOnDiskWarning(true); |
| 434 | } |
| 435 | return saved; |
| 436 | } |
| 437 | |
| 438 | IDocument::DocumentState TextDocument::state() const |
nothing calls this directly
no test coverage detected