File indexing completed on 2024-12-22 05:01:12
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 0006 */ 0007 0008 #include "saveasfilejob.h" 0009 #include "kmail_debug.h" 0010 #include <KLocalizedString> 0011 #include <QFileDialog> 0012 #include <QPointer> 0013 #include <QTextDocument> 0014 #include <QTextDocumentWriter> 0015 0016 SaveAsFileJob::SaveAsFileJob(QObject *parent) 0017 : QObject(parent) 0018 { 0019 } 0020 0021 SaveAsFileJob::~SaveAsFileJob() = default; 0022 0023 void SaveAsFileJob::start() 0024 { 0025 QPointer<QFileDialog> dlg = new QFileDialog(mParentWidget); 0026 dlg->setWindowTitle(i18nc("@title:window", "Save File as")); 0027 dlg->setAcceptMode(QFileDialog::AcceptSave); 0028 QStringList lst; 0029 if (mHtmlMode) { 0030 lst << QStringLiteral("text/html") << QStringLiteral("text/plain") << QStringLiteral("application/vnd.oasis.opendocument.text"); 0031 } else { 0032 lst << QStringLiteral("text/plain"); 0033 } 0034 dlg->setMimeTypeFilters(lst); 0035 0036 if (dlg->exec()) { 0037 QTextDocumentWriter writer; 0038 const QString filename = dlg->selectedFiles().at(0); 0039 writer.setFileName(filename); 0040 if (dlg->selectedNameFilter() == QLatin1StringView("text/plain") || filename.endsWith(QLatin1StringView(".txt"))) { 0041 writer.setFormat("plaintext"); 0042 } else if (dlg->selectedNameFilter() == QLatin1StringView("text/html") || filename.endsWith(QLatin1StringView(".html"))) { 0043 writer.setFormat("HTML"); 0044 } else if (dlg->selectedNameFilter() == QLatin1StringView("application/vnd.oasis.opendocument.text") || filename.endsWith(QLatin1StringView(".odf"))) { 0045 writer.setFormat("ODF"); 0046 } else { 0047 writer.setFormat("plaintext"); 0048 } 0049 if (!writer.write(mTextDocument)) { 0050 qCDebug(KMAIL_LOG) << " Error during writing"; 0051 } 0052 } 0053 delete dlg; 0054 deleteLater(); 0055 } 0056 0057 void SaveAsFileJob::setHtmlMode(bool htmlMode) 0058 { 0059 mHtmlMode = htmlMode; 0060 } 0061 0062 void SaveAsFileJob::setTextDocument(QTextDocument *textDocument) 0063 { 0064 mTextDocument = textDocument; 0065 } 0066 0067 void SaveAsFileJob::setParentWidget(QWidget *parentWidget) 0068 { 0069 mParentWidget = parentWidget; 0070 } 0071 0072 #include "moc_saveasfilejob.cpp"