Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/linguistic/source/translate.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#include <linguistic/translate.hxx>
11
#include <sal/log.hxx>
12
#include <curl/curl.h>
13
#include <rtl/string.h>
14
#include <boost/property_tree/ptree.hpp>
15
#include <boost/property_tree/json_parser.hpp>
16
#include <systools/curlinit.hxx>
17
#include <vcl/htmltransferable.hxx>
18
#include <tools/long.hxx>
19
20
namespace linguistic
21
{
22
OString Translate(const OString& rTargetLang, const OString& rAPIUrl, const OString& rAuthKey,
23
                  const OString& rData)
24
0
{
25
0
    constexpr tools::Long CURL_TIMEOUT = 10L;
26
27
0
    std::unique_ptr<CURL, std::function<void(CURL*)>> curl(curl_easy_init(),
28
0
                                                           [](CURL* p) { curl_easy_cleanup(p); });
29
30
0
    ::InitCurl_easy(curl.get());
31
32
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_URL, rAPIUrl.getStr());
33
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_FAILONERROR, 1L);
34
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_TIMEOUT, CURL_TIMEOUT);
35
36
0
    std::string response_body;
37
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION,
38
0
                           +[](void* buffer, size_t size, size_t nmemb, void* userp) -> size_t {
39
0
                               if (!userp)
40
0
                                   return 0;
41
0
                               std::string* response = static_cast<std::string*>(userp);
42
0
                               size_t real_size = size * nmemb;
43
0
                               response->append(static_cast<char*>(buffer), real_size);
44
0
                               return real_size;
45
0
                           });
46
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, static_cast<void*>(&response_body));
47
0
    OString aLang(curl_easy_escape(curl.get(), rTargetLang.getStr(), rTargetLang.getLength()));
48
0
    OString aAuthKey(curl_easy_escape(curl.get(), rAuthKey.getStr(), rAuthKey.getLength()));
49
0
    OString aData(curl_easy_escape(curl.get(), rData.getStr(), rData.getLength()));
50
0
    OString aPostData("auth_key=" + aAuthKey + "&target_lang=" + aLang + "&text=" + aData);
51
52
0
    (void)curl_easy_setopt(curl.get(), CURLOPT_POSTFIELDS, aPostData.getStr());
53
0
    CURLcode cc = curl_easy_perform(curl.get());
54
0
    if (cc != CURLE_OK)
55
0
    {
56
0
        SAL_WARN("linguistic",
57
0
                 "Translate: CURL perform returned with error: " << static_cast<sal_Int32>(cc));
58
0
        return {};
59
0
    }
60
0
    tools::Long nStatusCode;
61
0
    curl_easy_getinfo(curl.get(), CURLINFO_RESPONSE_CODE, &nStatusCode);
62
0
    if (nStatusCode != 200)
63
0
    {
64
0
        SAL_WARN("linguistic",
65
0
                 "Translate: CURL request returned with status code: " << nStatusCode);
66
0
        return {};
67
0
    }
68
    // parse the response
69
0
    boost::property_tree::ptree root;
70
0
    std::stringstream aStream(response_body.data());
71
0
    boost::property_tree::read_json(aStream, root);
72
0
    boost::property_tree::ptree& translations = root.get_child("translations");
73
0
    size_t size = translations.size();
74
0
    if (size <= 0)
75
0
    {
76
0
        SAL_WARN("linguistic", "Translate: API did not return any translations");
77
0
    }
78
    // take the first one
79
0
    const boost::property_tree::ptree& translation = translations.begin()->second;
80
0
    const std::string text = translation.get<std::string>("text");
81
0
    return OString(text);
82
0
}
83
}
84
85
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */