KTextAddons

extractlanguagejob.cpp
1/*
2 SPDX-FileCopyrightText: 2023-2024 Laurent Montel <montel.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "extractlanguagejob.h"
8#include "bergamotengineutils.h"
9#include "libbergamot_debug.h"
10#include <KLocalizedString>
11#include <KTar>
12#include <QDir>
13
14ExtractLanguageJob::ExtractLanguageJob(QObject *parent)
15 : QObject{parent}
16{
17}
18
19ExtractLanguageJob::~ExtractLanguageJob() = default;
20
21bool ExtractLanguageJob::canStart() const
22{
23 return !mSource.isEmpty();
24}
25
26void ExtractLanguageJob::start()
27{
28 if (!canStart()) {
29 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to start ExtractLanguageJob";
30 Q_EMIT errorText(i18n("Impossible to extract language"));
31 Q_EMIT finished();
33 return;
34 }
35 auto tar = new KTar(mSource);
36 if (!tar->open(QIODevice::ReadOnly)) {
37 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to open temporary file" << mSource;
38 Q_EMIT finished();
40 return;
41 }
42 if (!QDir().mkpath(BergamotEngineUtils::storageLanguagePath())) {
43 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to create path" << BergamotEngineUtils::storageLanguagePath();
44 Q_EMIT finished();
46 return;
47 }
48 const KArchiveDirectory *zipDir = tar->directory();
49 const QStringList lst = zipDir->entries();
50 // qDebug() << " list of files " << lst;
51 for (const QString &name : lst) {
52 const QString storeDirectory{BergamotEngineUtils::storageLanguagePath() + QLatin1Char('/') + name};
53 if (!QDir().mkpath(storeDirectory)) {
54 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to create :" << storeDirectory;
55 continue;
56 }
57 const KArchiveEntry *configPathEntry = zipDir->entry(name);
58 if (configPathEntry && configPathEntry->isDirectory()) {
59 const auto configDirectory = static_cast<const KArchiveDirectory *>(configPathEntry);
60 const QStringList entries = configDirectory->entries();
61 // qDebug() << " list of files entries " << entries;
62 for (const QString &file : entries) {
63 const KArchiveEntry *filePathEntry = zipDir->entry(name + QStringLiteral("/%1").arg(file));
64 if (filePathEntry && filePathEntry->isFile()) {
65 const auto filePath = static_cast<const KArchiveFile *>(filePathEntry);
66 if (!filePath->copyTo(storeDirectory)) {
67 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to copy to " << storeDirectory;
68 }
69 } else {
70 qCWarning(TRANSLATOR_LIBBERGAMOT_LOG) << "Impossible to import file " << file;
71 }
72 }
73 }
74 }
75 delete tar;
76 Q_EMIT finished();
78}
79
80QString ExtractLanguageJob::source() const
81{
82 return mSource;
83}
84
85void ExtractLanguageJob::setSource(const QString &newSource)
86{
87 mSource = newSource;
88}
89
90#include "moc_extractlanguagejob.cpp"
QStringList entries() const
const KArchiveEntry * entry(const QString &name) const
virtual bool isDirectory() const
virtual bool isFile() const
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT MkpathJob * mkpath(const QUrl &url, const QUrl &baseUrl=QUrl(), JobFlags flags=DefaultFlags)
QString name(StandardAction id)
Q_EMITQ_EMIT
void deleteLater()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:56:02 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.