summaryrefslogtreecommitdiff
path: root/indra/newview/llfloatertranslationsettings.cpp
blob: 61c4e05183e2c0b10d87de4d333d305b305b440b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/**
 * @file llfloatertranslationsettings.cpp
 * @brief Machine translation settings for chat
 *
 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2011, Linden Research, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "llviewerprecompiledheaders.h"

#include "llfloatertranslationsettings.h"

// Viewer includes
#include "llfloaterimnearbychat.h"
#include "lltranslate.h"
#include "llviewercontrol.h" // for gSavedSettings

// Linden library includes
#include "llbutton.h"
#include "llcheckboxctrl.h"
#include "llcombobox.h"
#include "llfloaterreg.h"
#include "lllineeditor.h"
#include "llnotificationsutil.h"
#include "llradiogroup.h"

LLFloaterTranslationSettings::LLFloaterTranslationSettings(const LLSD& key)
:   LLFloater(key)
,   mMachineTranslationCB(NULL)
,   mAzureKeyVerified(false)
,   mGoogleKeyVerified(false)
,   mDeepLKeyVerified(false)
{
}

// virtual
BOOL LLFloaterTranslationSettings::postBuild()
{
    mMachineTranslationCB = getChild<LLCheckBoxCtrl>("translate_chat_checkbox");
    mLanguageCombo = getChild<LLComboBox>("translate_language_combo");
    mTranslationServiceRadioGroup = getChild<LLRadioGroup>("translation_service_rg");
    mAzureAPIEndpointEditor = getChild<LLComboBox>("azure_api_endpoint_combo");
    mAzureAPIKeyEditor = getChild<LLLineEditor>("azure_api_key");
    mAzureAPIRegionEditor = getChild<LLLineEditor>("azure_api_region");
    mGoogleAPIKeyEditor = getChild<LLLineEditor>("google_api_key");
    mDeepLAPIDomainCombo = getChild<LLComboBox>("deepl_api_domain_combo");
    mDeepLAPIKeyEditor = getChild<LLLineEditor>("deepl_api_key");
    mAzureVerifyBtn = getChild<LLButton>("verify_azure_api_key_btn");
    mGoogleVerifyBtn = getChild<LLButton>("verify_google_api_key_btn");
    mDeepLVerifyBtn = getChild<LLButton>("verify_deepl_api_key_btn");
    mOKBtn = getChild<LLButton>("ok_btn");

    mMachineTranslationCB->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
    mTranslationServiceRadioGroup->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
    mOKBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnOK, this));
    getChild<LLButton>("cancel_btn")->setClickedCallback(boost::bind(&LLFloater::closeFloater, this, false));
    mAzureVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnAzureVerify, this));
    mGoogleVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnGoogleVerify, this));
    mDeepLVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnDeepLVerify, this));

    mAzureAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
    mAzureAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onAzureKeyEdited, this), NULL);
    mAzureAPIRegionEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
    mAzureAPIRegionEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onAzureKeyEdited, this), NULL);

    mAzureAPIEndpointEditor->setFocusLostCallback([this](LLFocusableElement*)
                                                  {
                                                      setAzureVerified(false, false, 0);
                                                  });
    mAzureAPIEndpointEditor->setCommitCallback([this](LLUICtrl* ctrl, const LLSD& param)
                                               {
                                                   setAzureVerified(false, false, 0);
                                               });

    mGoogleAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
    mGoogleAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onGoogleKeyEdited, this), NULL);

    mDeepLAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
    mDeepLAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onDeepLKeyEdited, this), NULL);

    mDeepLAPIDomainCombo->setFocusLostCallback([this](LLFocusableElement*)
                                                  {
                                                      setDeepLVerified(false, false, 0);
                                                  });
    mDeepLAPIDomainCombo->setCommitCallback([this](LLUICtrl* ctrl, const LLSD& param)
                                               {
                                                setDeepLVerified(false, false, 0);
                                               });

    center();
    return TRUE;
}

// virtual
void LLFloaterTranslationSettings::onOpen(const LLSD& key)
{
    mMachineTranslationCB->setValue(gSavedSettings.getBOOL("TranslateChat"));
    mLanguageCombo->setSelectedByValue(gSavedSettings.getString("TranslateLanguage"), TRUE);
    mTranslationServiceRadioGroup->setSelectedByValue(gSavedSettings.getString("TranslationService"), TRUE);

    LLSD azure_key = gSavedSettings.getLLSD("AzureTranslateAPIKey");
    if (azure_key.isMap() && !azure_key["id"].asString().empty())
    {
        mAzureAPIKeyEditor->setText(azure_key["id"].asString());
        mAzureAPIKeyEditor->setTentative(false);
        if (azure_key.has("region") && !azure_key["region"].asString().empty())
        {
            mAzureAPIRegionEditor->setText(azure_key["region"].asString());
            mAzureAPIRegionEditor->setTentative(false);
        }
        else
        {
            mAzureAPIRegionEditor->setTentative(true);
        }
        mAzureAPIEndpointEditor->setValue(azure_key["endpoint"]);
        verifyKey(LLTranslate::SERVICE_AZURE, azure_key, false);
    }
    else
    {
        mAzureAPIKeyEditor->setTentative(TRUE);
        mAzureAPIRegionEditor->setTentative(true);
        mAzureKeyVerified = FALSE;
    }

    std::string google_key = gSavedSettings.getString("GoogleTranslateAPIKey");
    if (!google_key.empty())
    {
        mGoogleAPIKeyEditor->setText(google_key);
        mGoogleAPIKeyEditor->setTentative(FALSE);
        verifyKey(LLTranslate::SERVICE_GOOGLE, google_key, false);
    }
    else
    {
        mGoogleAPIKeyEditor->setTentative(TRUE);
        mGoogleKeyVerified = FALSE;
    }

    LLSD deepl_key = gSavedSettings.getLLSD("DeepLTranslateAPIKey");
    if (deepl_key.isMap() && !deepl_key["id"].asString().empty())
    {
        mDeepLAPIKeyEditor->setText(deepl_key["id"].asString());
        mDeepLAPIKeyEditor->setTentative(false);
        mDeepLAPIDomainCombo->setValue(deepl_key["domain"]);
        verifyKey(LLTranslate::SERVICE_DEEPL, deepl_key, false);
    }
    else
    {
        mDeepLAPIKeyEditor->setTentative(TRUE);
        mDeepLKeyVerified = FALSE;
    }

    updateControlsEnabledState();
}

void LLFloaterTranslationSettings::setAzureVerified(bool ok, bool alert, S32 status)
{
    if (alert)
    {
        showAlert(ok ? "azure_api_key_verified" : "azure_api_key_not_verified", status);
    }

    mAzureKeyVerified = ok;
    updateControlsEnabledState();
}

void LLFloaterTranslationSettings::setGoogleVerified(bool ok, bool alert, S32 status)
{
    if (alert)
    {
        showAlert(ok ? "google_api_key_verified" : "google_api_key_not_verified", status);
    }

    mGoogleKeyVerified = ok;
    updateControlsEnabledState();
}

void LLFloaterTranslationSettings::setDeepLVerified(bool ok, bool alert, S32 status)
{
    if (alert)
    {
        showAlert(ok ? "deepl_api_key_verified" : "deepl_api_key_not_verified", status);
    }

    mDeepLKeyVerified = ok;
    updateControlsEnabledState();
}

std::string LLFloaterTranslationSettings::getSelectedService() const
{
    return mTranslationServiceRadioGroup->getSelectedValue().asString();
}

LLSD LLFloaterTranslationSettings::getEnteredAzureKey() const
{
    LLSD key;
    if (!mAzureAPIKeyEditor->getTentative())
    {
        key["endpoint"] = mAzureAPIEndpointEditor->getValue();
        key["id"] = mAzureAPIKeyEditor->getText();
        if (!mAzureAPIRegionEditor->getTentative())
        {
            key["region"] = mAzureAPIRegionEditor->getText();
        }
    }
    return key;
}

std::string LLFloaterTranslationSettings::getEnteredGoogleKey() const
{
    return mGoogleAPIKeyEditor->getTentative() ? LLStringUtil::null : mGoogleAPIKeyEditor->getText();
}

LLSD LLFloaterTranslationSettings::getEnteredDeepLKey() const
{
    LLSD key;
    if (!mDeepLAPIKeyEditor->getTentative())
    {
        key["domain"] = mDeepLAPIDomainCombo->getValue();
        key["id"] = mDeepLAPIKeyEditor->getText();
    }
    return key;
}

void LLFloaterTranslationSettings::showAlert(const std::string& msg_name, S32 status) const
{
    LLStringUtil::format_map_t string_args;
    // For now just show an http error code, whole 'reason' string might be added later
    string_args["[STATUS]"] = llformat("%d", status);
    std::string message = getString(msg_name, string_args);

    LLSD args;
    args["MESSAGE"] = message;
    LLNotificationsUtil::add("GenericAlert", args);
}

void LLFloaterTranslationSettings::updateControlsEnabledState()
{
    // Enable/disable controls based on the checkbox value.
    bool on = mMachineTranslationCB->getValue().asBoolean();
    std::string service = getSelectedService();
    bool azure_selected = service == "azure";
    bool google_selected = service == "google";
    bool deepl_selected = service == "deepl";

    mTranslationServiceRadioGroup->setEnabled(on);
    mLanguageCombo->setEnabled(on);

    // MS Azure
    getChild<LLTextBox>("azure_api_endoint_label")->setEnabled(on);
    mAzureAPIEndpointEditor->setEnabled(on && azure_selected);
    getChild<LLTextBox>("azure_api_key_label")->setEnabled(on);
    mAzureAPIKeyEditor->setEnabled(on && azure_selected);
    getChild<LLTextBox>("azure_api_region_label")->setEnabled(on);
    mAzureAPIRegionEditor->setEnabled(on && azure_selected);

    mAzureVerifyBtn->setEnabled(on && azure_selected &&
                                !mAzureKeyVerified && getEnteredAzureKey().isMap());

    // Google
    getChild<LLTextBox>("google_api_key_label")->setEnabled(on);
    mGoogleAPIKeyEditor->setEnabled(on && google_selected);

    mGoogleVerifyBtn->setEnabled(on && google_selected &&
        !mGoogleKeyVerified && !getEnteredGoogleKey().empty());

    // DeepL
    getChild<LLTextBox>("deepl_api_domain_label")->setEnabled(on);
    mDeepLAPIDomainCombo->setEnabled(on && deepl_selected);
    getChild<LLTextBox>("deepl_api_key_label")->setEnabled(on);
    mDeepLAPIKeyEditor->setEnabled(on && deepl_selected);

    mDeepLVerifyBtn->setEnabled(on && deepl_selected &&
                                 !mDeepLKeyVerified && getEnteredDeepLKey().isMap());

    bool service_verified =
        (azure_selected && mAzureKeyVerified)
        || (google_selected && mGoogleKeyVerified)
        || (deepl_selected && mDeepLKeyVerified);
    gSavedPerAccountSettings.setBOOL("TranslatingEnabled", service_verified);

    mOKBtn->setEnabled(!on || service_verified);
}

/*static*/
void LLFloaterTranslationSettings::setVerificationStatus(int service, bool ok, bool alert, S32 status)
{
    LLFloaterTranslationSettings* floater =
        LLFloaterReg::getTypedInstance<LLFloaterTranslationSettings>("prefs_translation");

    if (!floater)
    {
        LL_WARNS() << "Cannot find translation settings floater" << LL_ENDL;
        return;
    }

    switch (service)
    {
    case LLTranslate::SERVICE_AZURE:
        floater->setAzureVerified(ok, alert, status);
        break;
    case LLTranslate::SERVICE_GOOGLE:
        floater->setGoogleVerified(ok, alert, status);
        break;
    case LLTranslate::SERVICE_DEEPL:
        floater->setDeepLVerified(ok, alert, status);
        break;
    }
}


void LLFloaterTranslationSettings::verifyKey(int service, const LLSD& key, bool alert)
{
    LLTranslate::verifyKey(static_cast<LLTranslate::EService>(service), key,
        boost::bind(&LLFloaterTranslationSettings::setVerificationStatus, _1, _2, alert, _3));
}

void LLFloaterTranslationSettings::onEditorFocused(LLFocusableElement* control)
{
    LLLineEditor* editor = dynamic_cast<LLLineEditor*>(control);
    if (editor && editor->hasTabStop()) // if enabled. getEnabled() doesn't work
    {
        if (editor->getTentative())
        {
            editor->setText(LLStringUtil::null);
            editor->setTentative(FALSE);
        }
    }
}

void LLFloaterTranslationSettings::onAzureKeyEdited()
{
    if (mAzureAPIKeyEditor->isDirty()
        || mAzureAPIRegionEditor->isDirty())
    {
        // todo: verify mAzureAPIEndpointEditor url
        setAzureVerified(false, false, 0);
    }
}

void LLFloaterTranslationSettings::onGoogleKeyEdited()
{
    if (mGoogleAPIKeyEditor->isDirty())
    {
        setGoogleVerified(false, false, 0);
    }
}

void LLFloaterTranslationSettings::onDeepLKeyEdited()
{
    if (mDeepLAPIKeyEditor->isDirty())
    {
        setDeepLVerified(false, false, 0);
    }
}

void LLFloaterTranslationSettings::onBtnAzureVerify()
{
    LLSD key = getEnteredAzureKey();
    if (key.isMap())
    {
        verifyKey(LLTranslate::SERVICE_AZURE, key);
    }
}

void LLFloaterTranslationSettings::onBtnGoogleVerify()
{
    std::string key = getEnteredGoogleKey();
    if (!key.empty())
    {
        verifyKey(LLTranslate::SERVICE_GOOGLE, LLSD(key));
    }
}

void LLFloaterTranslationSettings::onBtnDeepLVerify()
{
    LLSD key = getEnteredDeepLKey();
    if (key.isMap())
    {
        verifyKey(LLTranslate::SERVICE_DEEPL, key);
    }
}

void LLFloaterTranslationSettings::onClose(bool app_quitting)
{
    std::string service = gSavedSettings.getString("TranslationService");
    bool azure_selected = service == "azure";
    bool google_selected = service == "google";
    bool deepl_selected = service == "deepl";

    bool service_verified =
        (azure_selected && mAzureKeyVerified)
        || (google_selected && mGoogleKeyVerified)
        || (deepl_selected && mDeepLKeyVerified);
    gSavedPerAccountSettings.setBOOL("TranslatingEnabled", service_verified);
}
void LLFloaterTranslationSettings::onBtnOK()
{
    gSavedSettings.setBOOL("TranslateChat", mMachineTranslationCB->getValue().asBoolean());
    gSavedSettings.setString("TranslateLanguage", mLanguageCombo->getSelectedValue().asString());
    gSavedSettings.setString("TranslationService", getSelectedService());
    gSavedSettings.setLLSD("AzureTranslateAPIKey", getEnteredAzureKey());
    gSavedSettings.setString("GoogleTranslateAPIKey", getEnteredGoogleKey());
    gSavedSettings.setLLSD("DeepLTranslateAPIKey", getEnteredDeepLKey());

    closeFloater(false);
}