File indexing completed on 2022-11-29 13:00:36

0001 /*
0002     SPDX-FileCopyrightText: 2021 Andreas Cord-Landwehr <cordlandwehr@kde.org>
0003     SPDX-License-Identifier: LGPL-2.1-or-later
0004 */
0005 
0006 #include "translateshelladapter.h"
0007 #include <QDebug>
0008 #include <QProcess>
0009 #include <QtConcurrent>
0010 
0011 bool TranslateShellAdapter::isTranslateShellAvailable() const
0012 {
0013     if (!m_translateShellAvailable) {
0014         QProcess process;
0015         process.start(QStringLiteral("trans"), QStringList() << "--version");
0016         process.waitForFinished(1000);
0017         if (process.error() != QProcess::ProcessError::FailedToStart) {
0018             qDebug() << "Translateshell process found";
0019             m_translateShellAvailable = true;
0020         }
0021     }
0022     return m_translateShellAvailable;
0023 }
0024 
0025 QFuture<TranslateShellAdapter::Translation>
0026 TranslateShellAdapter::translateAsync(const QString &word, const QString &sourceLanguage, const QString &targetLanguage)
0027 {
0028     QFuture<TranslateShellAdapter::Translation> result = QtConcurrent::run([word, sourceLanguage, targetLanguage]() {
0029         return TranslateShellAdapter::translate(word, sourceLanguage, targetLanguage);
0030     });
0031     return result;
0032 }
0033 
0034 TranslateShellAdapter::Translation TranslateShellAdapter::translate(const QString &word, const QString &sourceLanguage, const QString &targetLanguage)
0035 {
0036     TranslateShellAdapter::Translation translation;
0037 
0038     QProcess process;
0039     process.start(QStringLiteral("trans"),
0040                   {"-l",
0041                    "en", // output language of CLI
0042                    "-t",
0043                    targetLanguage,
0044                    "-s",
0045                    sourceLanguage,
0046                    word,
0047                    "-no-ansi",
0048                    "-show-alternatives=y",
0049                    "-show-original=n",
0050                    "-show-languages=n",
0051                    "-show-original-dictionary=n",
0052                    "-show-dictionary=y",
0053                    "-no-warn"});
0054     process.waitForFinished();
0055     if (process.exitCode() != 0) {
0056         TranslateShellAdapter::Translation translation;
0057         translation.m_error = true;
0058         return {};
0059     } else {
0060         return TranslateShellAdapter::parseTranslateShellResult(process.readAll());
0061     }
0062 }
0063 
0064 QFuture<bool> TranslateShellAdapter::downloadSoundFile(const QString &word, const QString &language, const QString &filePath)
0065 {
0066     if (!filePath.endsWith(".ts")) {
0067         qWarning() << "Sound file will have TS format and should have that suffix";
0068     }
0069     if (QFile::exists(filePath)) {
0070         qWarning() << "File already exists, this operation will override the file" << filePath;
0071     }
0072     QDir directory = QFileInfo(filePath).absoluteDir();
0073     if (!directory.exists(directory.absolutePath())) {
0074         qWarning() << "Output directory does not exist, creating it" << directory;
0075         directory.mkpath(directory.absolutePath());
0076     }
0077 
0078     QFuture<bool> result = QtConcurrent::run([word, language, filePath]() {
0079         QProcess process;
0080         process.start(QStringLiteral("trans"),
0081                       {"-l",
0082                        "en", // output language of CLI
0083                        "-t",
0084                        language,
0085                        word,
0086                        "-no-translate",
0087                        "-download-audio-as",
0088                        filePath});
0089         process.waitForFinished();
0090         return process.error() != QProcess::ProcessError::FailedToStart && process.exitCode() == 0;
0091     });
0092     return result;
0093 }
0094 
0095 TranslateShellAdapter::Translation TranslateShellAdapter::parseTranslateShellResult(const QString &output)
0096 {
0097     const QStringList lines = output.split('\n');
0098 
0099     Translation result;
0100     if (lines.count() < 1) {
0101         result.m_error = true;
0102         return result;
0103     }
0104 
0105     // magic line parsing, since current version of translate-shell does not support stable CLI API
0106     result.m_suggestions = QStringList() << lines.at(0).trimmed();
0107     for (int i = 5; i < lines.count(); ++i) {
0108         result.m_synonyms << lines.at(i).trimmed();
0109     }
0110 
0111     return result;
0112 }