File indexing completed on 2024-05-26 16:09:09

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "translatetextjob.h"
0008 #include "ruqola_debug.h"
0009 #include "translatetext/translatorenginemanager.h"
0010 #include <KLocalizedString>
0011 #include <TextTranslator/TranslatorEnginePlugin>
0012 
0013 TranslateTextJob::TranslateTextJob(QObject *parent)
0014     : QObject(parent)
0015 {
0016     connect(TranslatorEngineManager::self(), &TranslatorEngineManager::translateDone, this, &TranslateTextJob::translateDone);
0017     connect(TranslatorEngineManager::self(), &TranslatorEngineManager::translateFailed, this, &TranslateTextJob::translateFailed);
0018 }
0019 
0020 TranslateTextJob::~TranslateTextJob() = default;
0021 
0022 void TranslateTextJob::translate()
0023 {
0024     if (mInfo.isValid()) {
0025         auto translatorEngine = TranslatorEngineManager::self()->translatorEngineBase();
0026         translatorEngine->setInputText(mInfo.inputText);
0027         translatorEngine->setFrom(mInfo.from);
0028         translatorEngine->setTo(mInfo.to);
0029         translatorEngine->translate();
0030     } else {
0031         Q_EMIT translateFailed(i18n("Missing translator info. It's a bug"));
0032         qCDebug(RUQOLA_LOG) << " Invalid translate info " << mInfo;
0033     }
0034 }
0035 
0036 const TranslateTextJob::TranslateInfo &TranslateTextJob::info() const
0037 {
0038     return mInfo;
0039 }
0040 
0041 void TranslateTextJob::setInfo(const TranslateInfo &newInfo)
0042 {
0043     mInfo = newInfo;
0044 }
0045 
0046 bool TranslateTextJob::TranslateInfo::isValid() const
0047 {
0048     return !from.isEmpty() && !to.isEmpty() && !inputText.isEmpty();
0049 }
0050 
0051 QDebug operator<<(QDebug d, const TranslateTextJob::TranslateInfo &t)
0052 {
0053     d << "From " << t.from;
0054     d << "To " << t.to;
0055     d << "inputtext " << t.inputText;
0056     return d;
0057 }
0058 
0059 #include "moc_translatetextjob.cpp"