File indexing completed on 2024-11-24 04:16:55

0001 /*
0002   SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "extractlanguagejob.h"
0008 #include "bergamotengineutils.h"
0009 #include "libbergamot_debug.h"
0010 #include <KLocalizedString>
0011 #include <KTar>
0012 #include <QDir>
0013 
0014 ExtractLanguageJob::ExtractLanguageJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 ExtractLanguageJob::~ExtractLanguageJob() = default;
0020 
0021 bool ExtractLanguageJob::canStart() const
0022 {
0023     return !mSource.isEmpty();
0024 }
0025 
0026 void ExtractLanguageJob::start()
0027 {
0028     if (!canStart()) {
0029         qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to start ExtractLanguageJob";
0030         Q_EMIT errorText(i18n("Impossible to extract language"));
0031         Q_EMIT finished();
0032         deleteLater();
0033         return;
0034     }
0035     auto tar = new KTar(mSource);
0036     if (!tar->open(QIODevice::ReadOnly)) {
0037         qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open temporary file" << mSource;
0038         Q_EMIT finished();
0039         deleteLater();
0040         return;
0041     }
0042     if (!QDir().mkpath(BergamotEngineUtils::storageLanguagePath())) {
0043         qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to create path" << BergamotEngineUtils::storageLanguagePath();
0044         Q_EMIT finished();
0045         deleteLater();
0046         return;
0047     }
0048     const KArchiveDirectory *zipDir = tar->directory();
0049     const QStringList lst = zipDir->entries();
0050     // qDebug() << " list of files " << lst;
0051     for (const QString &name : lst) {
0052         const QString storeDirectory{BergamotEngineUtils::storageLanguagePath() + QLatin1Char('/') + name};
0053         if (!QDir().mkpath(storeDirectory)) {
0054             qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to create :" << storeDirectory;
0055             continue;
0056         }
0057         const KArchiveEntry *configPathEntry = zipDir->entry(name);
0058         if (configPathEntry && configPathEntry->isDirectory()) {
0059             const auto configDirectory = static_cast<const KArchiveDirectory *>(configPathEntry);
0060             const QStringList entries = configDirectory->entries();
0061             // qDebug() << " list of files entries " << entries;
0062             for (const QString &file : entries) {
0063                 const KArchiveEntry *filePathEntry = zipDir->entry(name + QStringLiteral("/%1").arg(file));
0064                 if (filePathEntry && filePathEntry->isFile()) {
0065                     const auto filePath = static_cast<const KArchiveFile *>(filePathEntry);
0066                     if (!filePath->copyTo(storeDirectory)) {
0067                         qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to copy to " << storeDirectory;
0068                     }
0069                 } else {
0070                     qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to import file " << file;
0071                 }
0072             }
0073         }
0074     }
0075     delete tar;
0076     Q_EMIT finished();
0077     deleteLater();
0078 }
0079 
0080 QString ExtractLanguageJob::source() const
0081 {
0082     return mSource;
0083 }
0084 
0085 void ExtractLanguageJob::setSource(const QString &newSource)
0086 {
0087     mSource = newSource;
0088 }
0089 
0090 #include "moc_extractlanguagejob.cpp"