Exemplo n.º 1
0
void ChatMessagePanel::on_networkReplyError(QNetworkReply::NetworkError)
{
    QString errorString = (qobject_cast<QNetworkReply *>(QObject::sender()))->errorString();
    qCritical() << "Translation error:" << errorString;

    emit translationFinished();

    setMessageLabelText(originalText); // restore the original text
}
Exemplo n.º 2
0
void ChatMessagePanel::on_networkReplyFinished(QNetworkReply *reply)
{
    QString data(reply->readAll());
    ChatMessagePanel *messagePanel
        = qobject_cast<ChatMessagePanel *>(reply->request().originatingObject());
    int startSlash = data.indexOf(QRegExp("\""));
    int endSlash = data.indexOf(QRegExp("\""), startSlash + 1);
    QString translatedText = data.mid(startSlash+1, endSlash - startSlash - 1);
    if (translatedText.isEmpty())
        translatedText = "translation error!";
    messagePanel->setTranslatedMessage(translatedText);

    reply->manager()->deleteLater();
    reply->deleteLater();

    emit translationFinished();
}
Exemplo n.º 3
0
void QGoogleTranslator::finishedSlot(QNetworkReply* reply)
{
    // Reathising attributes of the reply
    // e.g. the HTTP    QTextStream stream ( &file );

    QVariant statusCodV =
            reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
    // Or the target URL if it was a rethisirect:
    QVariant redirectionTargetUrl =
            reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    QTextStream stream ( reply );
    stream.setCodec("UTF-8");
    QString strTmp;
    strTmp =  stream.readAll();
    QVariant response = Json::parse(strTmp);
    emit translationFinished(response.toMap ().value ("responseData")
                             .toMap ().value ("translatedText").toString ());
}
Exemplo n.º 4
0
void TranslationInterface::createService(uint id)
{
    m_suppressRetranslate = true;

    delete m_sourceLanguage;
    m_sourceLanguage = NULL;
    delete m_targetLanguage;
    m_targetLanguage = NULL;
    delete m_service;
    delete m_serviceItem;

    switch (id) {
    case MicrosoftTranslatorService:
        m_service = new MicrosoftTranslator(this);
        m_serviceItem = new TranslationServiceItem(MicrosoftTranslatorService,
                                                   MicrosoftTranslator::displayName(), this);
        break;
    case YandexTranslateService:
        m_service = new YandexTranslate(this);
        m_serviceItem = new TranslationServiceItem(YandexTranslateService,
                                                   YandexTranslate::displayName(), this);
        break;
    case YandexDictionariesService:
        m_service = new YandexDictionaries(m_dict, this);
        m_serviceItem = new TranslationServiceItem(YandexDictionariesService,
                                                   YandexDictionaries::displayName(), this);
        break;
    case GoogleTranslateService:
    default:
        m_service = new GoogleTranslate(m_dict, this);
        m_serviceItem = new TranslationServiceItem(GoogleTranslateService,
                                                   GoogleTranslate::displayName(), this);
        m_settings->setValue("SelectedService", GoogleTranslateService);
    }
    connect(m_service, SIGNAL(translationFinished()), SLOT(onTranslationFinished()));
    emit selectedServiceChanged();

    const LanguagePair defaults = m_service->defaultLanguagePair();

    m_sourceLanguages->setLanguageList(m_service->sourceLanguages());
    m_settings->beginGroup(m_service->uid() + "/SourceLanguage");
    if (m_settings->contains("Info")) {
        const QVariant info = m_service->deserializeLanguageInfo(m_settings->value("Info").toString());
        Language lang(info, m_service->getLanguageName(info));
        if (m_service->sourceLanguages().contains(lang))
            m_sourceLanguage = new LanguageItem(lang, m_sourceLanguages->indexOf(lang));
    }
    m_settings->endGroup();
    if (!m_sourceLanguage)
        m_sourceLanguage = new LanguageItem(defaults.first,
                                            m_sourceLanguages->indexOf(defaults.first));

    m_targetLanguages->setLanguageList(m_service->targetLanguages(m_sourceLanguage->language()));
    m_settings->beginGroup(m_service->uid() + "/TargetLanguage");
    if (m_settings->contains("Info")) {
        const QVariant info = m_service->deserializeLanguageInfo(m_settings->value("Info").toString());
        Language lang(info, m_service->getLanguageName(info));
        if (m_service->targetLanguages(m_sourceLanguage->language()).contains(lang))
            m_targetLanguage = new LanguageItem(lang, m_targetLanguages->indexOf(lang));
    }
    m_settings->endGroup();
    if (!m_targetLanguage)
        m_targetLanguage = new LanguageItem(defaults.second,
                                            m_targetLanguages->indexOf(defaults.second));

    emit sourceLanguageChanged();
    emit targetLanguageChanged();

    m_suppressRetranslate = false;
    retranslate();
}