File indexing completed on 2024-12-22 04:28:07
0001 /* 0002 SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "exportlibreofficeautocorrection.h" 0008 #include "autocorrectionutils.h" 0009 #include "textautocorrection_debug.h" 0010 #include <KZip> 0011 #include <QDir> 0012 #include <QTemporaryFile> 0013 #include <QXmlStreamWriter> 0014 0015 using namespace TextAutoCorrectionCore; 0016 0017 ExportLibreOfficeAutocorrection::ExportLibreOfficeAutocorrection() = default; 0018 0019 ExportLibreOfficeAutocorrection::~ExportLibreOfficeAutocorrection() 0020 { 0021 delete mZip; 0022 } 0023 0024 bool ExportLibreOfficeAutocorrection::exportData(const QString &language, const QString &fileName, QString &errorMessage, const QString &writablePath) 0025 { 0026 Q_UNUSED(errorMessage); 0027 const QString libreOfficeWritableLocalAutoCorrectionPath = 0028 writablePath.isEmpty() ? AutoCorrectionUtils::libreOfficeWritableLocalAutoCorrectionPath() : writablePath; 0029 QDir().mkpath(libreOfficeWritableLocalAutoCorrectionPath); 0030 QString fixLangExtension = language; 0031 fixLangExtension.replace(QLatin1Char('_'), QLatin1Char('-')); 0032 const QString fname = fileName.isEmpty() ? libreOfficeWritableLocalAutoCorrectionPath + QStringLiteral("acor_%1.dat").arg(fixLangExtension) : fileName; 0033 // qDebug() << " fname " << fname; 0034 mZip = new KZip(fname); 0035 const bool result = mZip->open(QIODevice::WriteOnly); 0036 if (!result) { 0037 qCWarning(TEXTAUTOCORRECTION_LOG) << "Impossible to open " << fileName; 0038 return false; 0039 } 0040 if (!exportDocumentList()) { 0041 return false; 0042 } 0043 if (!exportSentenceExceptList()) { 0044 return false; 0045 } 0046 if (!exportWordExceptList()) { 0047 return false; 0048 } 0049 if (!exportManifest()) { 0050 return false; 0051 } 0052 mZip->close(); 0053 delete mZip; 0054 mZip = nullptr; 0055 return true; 0056 } 0057 0058 bool ExportLibreOfficeAutocorrection::exportDocumentList() 0059 { 0060 QTemporaryFile temporaryShareFile; 0061 temporaryShareFile.open(); 0062 QXmlStreamWriter streamWriter(&temporaryShareFile); 0063 0064 streamWriter.setAutoFormatting(true); 0065 streamWriter.setAutoFormattingIndent(2); 0066 streamWriter.writeStartDocument(); 0067 0068 streamWriter.writeStartElement(QStringLiteral("block-list:block-list")); 0069 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list")); 0070 QHashIterator<QString, QString> i(mAutocorrectEntries); 0071 while (i.hasNext()) { 0072 i.next(); 0073 streamWriter.writeStartElement(QStringLiteral("block-list:block")); 0074 streamWriter.writeAttribute(QStringLiteral("block-list:abbreviated-name"), i.key()); 0075 streamWriter.writeAttribute(QStringLiteral("block-list:name"), i.value()); 0076 streamWriter.writeEndElement(); 0077 } 0078 streamWriter.writeEndElement(); 0079 streamWriter.writeEndDocument(); 0080 temporaryShareFile.close(); 0081 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("DocumentList.xml")); 0082 return true; 0083 } 0084 0085 bool ExportLibreOfficeAutocorrection::exportSentenceExceptList() 0086 { 0087 QTemporaryFile temporaryShareFile; 0088 temporaryShareFile.open(); 0089 0090 QXmlStreamWriter streamWriter(&temporaryShareFile); 0091 0092 streamWriter.setAutoFormatting(true); 0093 streamWriter.setAutoFormattingIndent(2); 0094 streamWriter.writeStartDocument(); 0095 0096 streamWriter.writeStartElement(QStringLiteral("block-list:block-list")); 0097 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list")); 0098 0099 QSet<QString>::const_iterator upper = mUpperCaseExceptions.constBegin(); 0100 while (upper != mUpperCaseExceptions.constEnd()) { 0101 streamWriter.writeStartElement(QStringLiteral("block-list:block")); 0102 streamWriter.writeAttribute(QStringLiteral("block-list:abbreviated-name"), *upper); 0103 streamWriter.writeEndElement(); 0104 ++upper; 0105 } 0106 streamWriter.writeEndElement(); 0107 streamWriter.writeEndDocument(); 0108 temporaryShareFile.close(); 0109 0110 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("SentenceExceptList.xml")); 0111 return true; 0112 } 0113 0114 bool ExportLibreOfficeAutocorrection::exportWordExceptList() 0115 { 0116 QTemporaryFile temporaryShareFile; 0117 temporaryShareFile.open(); 0118 0119 QXmlStreamWriter streamWriter(&temporaryShareFile); 0120 0121 streamWriter.setAutoFormatting(true); 0122 streamWriter.setAutoFormattingIndent(2); 0123 streamWriter.writeStartDocument(); 0124 0125 streamWriter.writeStartElement(QStringLiteral("block-list:block-list")); 0126 streamWriter.writeAttribute(QStringLiteral("xmlns:block-list"), QStringLiteral("http://openoffice.org/2001/block-list")); 0127 0128 QSet<QString>::const_iterator twoUpper = mTwoUpperLetterExceptions.constBegin(); 0129 while (twoUpper != mTwoUpperLetterExceptions.constEnd()) { 0130 streamWriter.writeStartElement(QStringLiteral("block-list:block")); 0131 streamWriter.writeAttribute(QStringLiteral("block-list:abbreviated-name"), *twoUpper); 0132 streamWriter.writeEndElement(); 0133 ++twoUpper; 0134 } 0135 streamWriter.writeEndElement(); 0136 streamWriter.writeEndDocument(); 0137 temporaryShareFile.close(); 0138 0139 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("WordExceptList.xml")); 0140 return true; 0141 } 0142 0143 bool ExportLibreOfficeAutocorrection::exportManifest() 0144 { 0145 QTemporaryFile temporaryShareFile; 0146 temporaryShareFile.open(); 0147 0148 QXmlStreamWriter streamWriter(&temporaryShareFile); 0149 streamWriter.setAutoFormatting(true); 0150 streamWriter.setAutoFormattingIndent(2); 0151 streamWriter.writeStartDocument(); 0152 0153 streamWriter.writeStartElement(QStringLiteral("manifest:manifest")); 0154 0155 streamWriter.writeStartElement(QStringLiteral("manifest:file-entry")); 0156 streamWriter.writeAttribute(QStringLiteral("manifest:full-path"), QStringLiteral("/")); 0157 streamWriter.writeAttribute(QStringLiteral("manifest:media-type"), QString()); 0158 streamWriter.writeEndElement(); 0159 0160 streamWriter.writeStartElement(QStringLiteral("manifest:file-entry")); 0161 streamWriter.writeAttribute(QStringLiteral("manifest:full-path"), QStringLiteral("DocumentList.xml")); 0162 streamWriter.writeAttribute(QStringLiteral("manifest:media-type"), QString()); 0163 streamWriter.writeEndElement(); 0164 0165 streamWriter.writeStartElement(QStringLiteral("manifest:file-entry")); 0166 streamWriter.writeAttribute(QStringLiteral("manifest:full-path"), QStringLiteral("SentenceExceptList.xml")); 0167 streamWriter.writeAttribute(QStringLiteral("manifest:media-type"), QString()); 0168 streamWriter.writeEndElement(); 0169 0170 streamWriter.writeStartElement(QStringLiteral("manifest:file-entry")); 0171 streamWriter.writeAttribute(QStringLiteral("manifest:full-path"), QStringLiteral("WordExceptList.xml")); 0172 streamWriter.writeAttribute(QStringLiteral("manifest:media-type"), QStringLiteral("text/xml")); 0173 streamWriter.writeEndElement(); 0174 0175 streamWriter.writeEndElement(); 0176 streamWriter.writeEndDocument(); 0177 temporaryShareFile.close(); 0178 0179 // Add mimetype file 0180 mZip->setCompression(KZip::NoCompression); 0181 mZip->writeFile(QStringLiteral("mimetype"), ""); 0182 mZip->setCompression(KZip::DeflateCompression); 0183 mZip->addLocalFile(temporaryShareFile.fileName(), QStringLiteral("META-INF/manifest.xml")); 0184 return true; 0185 }