katedocument: Add relevant MIME types to Save dialog (c72c65c3) · Commits · Frameworks / KTextEditor · GitLab
Commit c72c65c3 authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇 Committed by Christoph Cullmann
Browse files
parent a02b404e
Loading
Loading
Loading
Loading
Original line number Diff line number Diff line
@@ -1779,9 +1779,62 @@ QWidget *KTextEditor::DocumentPrivate::dialogParent()
    return w;
}

QUrl KTextEditor::DocumentPrivate::getSaveFileUrl(const QString &dialogTitle)
QUrl KTextEditor::DocumentPrivate::getSaveFileUrl(const QString &dialogTitle, QWidget *parent)
{
    return QFileDialog::getSaveFileUrl(dialogParent(), dialogTitle, startUrlForFileDialog());
    const QString plainText = QStringLiteral("text/plain");
    const QString allFiles = QStringLiteral("application/octet-stream");

    const QString currentMimeType = mimeType();
    const QUrl startUrl = startUrlForFileDialog();

    QFileDialog dialog(parent ? parent : dialogParent());
    dialog.setDirectoryUrl(startUrl.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash));
    dialog.setWindowTitle(dialogTitle);
    dialog.setAcceptMode(QFileDialog::AcceptSave);

    QStringList canonicalModeMideMimeTypeNames;

    // Keep only the canonical mime type name in the list, QMimeDatabase will return only it.
    // TODO Check whether modeManager can do this already.
    QMimeDatabase db;
    const QStringList modeMimeTypeNames = KTextEditor::EditorPrivate::self()->modeManager()->fileType(fileType()).mimetypes;
    for (const QString &mimeTypeName : modeMimeTypeNames) {
        const QMimeType mimeType = db.mimeTypeForName(mimeTypeName);
        if (!mimeType.isValid() || mimeType.isDefault() || mimeType.name() == plainText || mimeType.name() == currentMimeType) {
            continue;
        }
        if (!canonicalModeMideMimeTypeNames.contains(mimeType.name())) {
            canonicalModeMideMimeTypeNames.append(mimeType.name());
        }
    }

    QStringList mimeFilters;
    if (currentMimeType != plainText) { // keep plain text last.
        mimeFilters << currentMimeType;
    }
    mimeFilters << canonicalModeMideMimeTypeNames;
    mimeFilters << plainText;
    mimeFilters << allFiles;

    dialog.setMimeTypeFilters(mimeFilters);

    // Kate didn't use to offer any MIME types in the save dialog, keep "All files"
    // as default to not change behavior.
    // Also, e.g. a file like ".kateproject" is JSON but might not be identified as
    // such, and using Save As here would result in the file not showing by default.
    dialog.selectMimeTypeFilter(allFiles);

    if (!startUrl.fileName().isEmpty()) {
        dialog.selectFile(startUrl.fileName());
    }

    if (dialog.exec()) {
        if (const auto urls = dialog.selectedUrls(); !urls.isEmpty()) {
            return urls.first();
        }
    }

    return QUrl();
}

// BEGIN KTextEditor::HighlightingInterface stuff
Original line number Diff line number Diff line
@@ -393,15 +393,6 @@ private:
    KTEXTEDITOR_NO_EXPORT
    QWidget *dialogParent();

    /**
     * Wrapper around QFileDialog::getSaveFileUrl, will use proper dialogParent
     * and try it's best to find a good directory as start
     * @param dialogTitle dialog title string
     * @return url to save to or empty url if aborted
     */
    KTEXTEDITOR_NO_EXPORT
    QUrl getSaveFileUrl(const QString &dialogTitle);

    /*
     * Access to the mode/highlighting subsystem
     */
@@ -995,6 +986,7 @@ public Q_SLOTS:

public:
    bool saveAs(const QUrl &url) override;
    QUrl getSaveFileUrl(const QString &dialogTitle, QWidget *dialogParent = nullptr);

private:
    // helper to handle the embedded notification for externally modified files
Original line number Diff line number Diff line
@@ -540,6 +540,21 @@ public:
     */
    bool openingError() const;

    /*!
     * Wrapper around QFileDialog::getSaveFileUrl, will use proper dialogParent
     * by default and try it's best to find a good directory as start. It also
     * includes additional MIME types based on the document's format and highlighter.
     *
     * \a dialogTitle is the title for the dialog, for example "Save Document As"
     * \a dialogParent the parent window for the dialog. When nullptr, it will try to
     * determine a proper parent.
     *
     * Returns the URL to save to or an empty URL if aborted.
     *
     * \since 6.25
     */
    QUrl getSaveFileUrl(const QString &dialogTitle, QWidget *dialogParent = nullptr);

    /*
     * SIGNALS
     * Following signals should be emitted by the document if the text content
Original line number Diff line number Diff line
@@ -82,6 +82,11 @@ bool Document::openingError() const
    return d->m_openingError;
}

QUrl Document::getSaveFileUrl(const QString &dialogTitle, QWidget *parent)
{
    return d->getSaveFileUrl(dialogTitle, parent);
}

bool KTextEditor::Document::replaceText(Range range, const QString &text, bool block)
{
    bool success = true;