PageRenderTime 837ms CodeModel.GetById 307ms app.highlight 279ms RepoModel.GetById 247ms app.codeStats 1ms

/indra/newview/llfloatertranslationsettings.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 298 lines | 228 code | 40 blank | 30 comment | 24 complexity | 1ceed93674e52846b7c64d6aaaa01185 MD5 | raw file
  1/** 
  2 * @file llfloatertranslationsettings.cpp
  3 * @brief Machine translation settings for chat
  4 *
  5 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2011, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#include "llviewerprecompiledheaders.h"
 28
 29#include "llfloatertranslationsettings.h"
 30
 31// Viewer includes
 32#include "llnearbychatbar.h"
 33#include "lltranslate.h"
 34#include "llviewercontrol.h" // for gSavedSettings
 35
 36// Linden library includes
 37#include "llbutton.h"
 38#include "llcheckboxctrl.h"
 39#include "llcombobox.h"
 40#include "llfloaterreg.h"
 41#include "lllineeditor.h"
 42#include "llnotificationsutil.h"
 43#include "llradiogroup.h"
 44
 45class EnteredKeyVerifier : public LLTranslate::KeyVerificationReceiver
 46{
 47public:
 48	EnteredKeyVerifier(LLTranslate::EService service, bool alert)
 49	:	LLTranslate::KeyVerificationReceiver(service)
 50	,	mAlert(alert)
 51	{
 52	}
 53
 54private:
 55	/*virtual*/ void setVerificationStatus(bool ok)
 56	{
 57		LLFloaterTranslationSettings* floater =
 58			LLFloaterReg::getTypedInstance<LLFloaterTranslationSettings>("prefs_translation");
 59
 60		if (!floater)
 61		{
 62			llwarns << "Cannot find translation settings floater" << llendl;
 63			return;
 64		}
 65
 66		switch (getService())
 67		{
 68		case LLTranslate::SERVICE_BING:
 69			floater->setBingVerified(ok, mAlert);
 70			break;
 71		case LLTranslate::SERVICE_GOOGLE:
 72			floater->setGoogleVerified(ok, mAlert);
 73			break;
 74		}
 75	}
 76
 77	bool mAlert;
 78};
 79
 80LLFloaterTranslationSettings::LLFloaterTranslationSettings(const LLSD& key)
 81:	LLFloater(key)
 82,	mMachineTranslationCB(NULL)
 83,	mLanguageCombo(NULL)
 84,	mTranslationServiceRadioGroup(NULL)
 85,	mBingAPIKeyEditor(NULL)
 86,	mGoogleAPIKeyEditor(NULL)
 87,	mBingVerifyBtn(NULL)
 88,	mGoogleVerifyBtn(NULL)
 89,	mOKBtn(NULL)
 90,	mBingKeyVerified(false)
 91,	mGoogleKeyVerified(false)
 92{
 93}
 94
 95// virtual
 96BOOL LLFloaterTranslationSettings::postBuild()
 97{
 98	mMachineTranslationCB = getChild<LLCheckBoxCtrl>("translate_chat_checkbox");
 99	mLanguageCombo = getChild<LLComboBox>("translate_language_combo");
100	mTranslationServiceRadioGroup = getChild<LLRadioGroup>("translation_service_rg");
101	mBingAPIKeyEditor = getChild<LLLineEditor>("bing_api_key");
102	mGoogleAPIKeyEditor = getChild<LLLineEditor>("google_api_key");
103	mBingVerifyBtn = getChild<LLButton>("verify_bing_api_key_btn");
104	mGoogleVerifyBtn = getChild<LLButton>("verify_google_api_key_btn");
105	mOKBtn = getChild<LLButton>("ok_btn");
106
107	mMachineTranslationCB->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
108	mTranslationServiceRadioGroup->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
109	mOKBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnOK, this));
110	getChild<LLButton>("cancel_btn")->setClickedCallback(boost::bind(&LLFloater::closeFloater, this, false));
111	mBingVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnBingVerify, this));
112	mGoogleVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnGoogleVerify, this));
113
114	mBingAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
115	mBingAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onBingKeyEdited, this), NULL);
116	mGoogleAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
117	mGoogleAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onGoogleKeyEdited, this), NULL);
118
119	center();
120	return TRUE;
121}
122
123// virtual
124void LLFloaterTranslationSettings::onOpen(const LLSD& key)
125{
126	mMachineTranslationCB->setValue(gSavedSettings.getBOOL("TranslateChat"));
127	mLanguageCombo->setSelectedByValue(gSavedSettings.getString("TranslateLanguage"), TRUE);
128	mTranslationServiceRadioGroup->setSelectedByValue(gSavedSettings.getString("TranslationService"), TRUE);
129
130	std::string bing_key = gSavedSettings.getString("BingTranslateAPIKey");
131	if (!bing_key.empty())
132	{
133		mBingAPIKeyEditor->setText(bing_key);
134		mBingAPIKeyEditor->setTentative(FALSE);
135		verifyKey(LLTranslate::SERVICE_BING, bing_key, false);
136	}
137	else
138	{
139		mBingAPIKeyEditor->setTentative(TRUE);
140		mBingKeyVerified = FALSE;
141	}
142
143	std::string google_key = gSavedSettings.getString("GoogleTranslateAPIKey");
144	if (!google_key.empty())
145	{
146		mGoogleAPIKeyEditor->setText(google_key);
147		mGoogleAPIKeyEditor->setTentative(FALSE);
148		verifyKey(LLTranslate::SERVICE_GOOGLE, google_key, false);
149	}
150	else
151	{
152		mGoogleAPIKeyEditor->setTentative(TRUE);
153		mGoogleKeyVerified = FALSE;
154	}
155
156	updateControlsEnabledState();
157}
158
159void LLFloaterTranslationSettings::setBingVerified(bool ok, bool alert)
160{
161	if (alert)
162	{
163		showAlert(ok ? "bing_api_key_verified" : "bing_api_key_not_verified");
164	}
165
166	mBingKeyVerified = ok;
167	updateControlsEnabledState();
168}
169
170void LLFloaterTranslationSettings::setGoogleVerified(bool ok, bool alert)
171{
172	if (alert)
173	{
174		showAlert(ok ? "google_api_key_verified" : "google_api_key_not_verified");
175	}
176
177	mGoogleKeyVerified = ok;
178	updateControlsEnabledState();
179}
180
181std::string LLFloaterTranslationSettings::getSelectedService() const
182{
183	return mTranslationServiceRadioGroup->getSelectedValue().asString();
184}
185
186std::string LLFloaterTranslationSettings::getEnteredBingKey() const
187{
188	return mBingAPIKeyEditor->getTentative() ? LLStringUtil::null : mBingAPIKeyEditor->getText();
189}
190
191std::string LLFloaterTranslationSettings::getEnteredGoogleKey() const
192{
193	return mGoogleAPIKeyEditor->getTentative() ? LLStringUtil::null : mGoogleAPIKeyEditor->getText();
194}
195
196void LLFloaterTranslationSettings::showAlert(const std::string& msg_name) const
197{
198	LLSD args;
199	args["MESSAGE"] = getString(msg_name);
200	LLNotificationsUtil::add("GenericAlert", args);
201}
202
203void LLFloaterTranslationSettings::updateControlsEnabledState()
204{
205	// Enable/disable controls based on the checkbox value.
206	bool on = mMachineTranslationCB->getValue().asBoolean();
207	std::string service = getSelectedService();
208	bool bing_selected = service == "bing";
209	bool google_selected = service == "google";
210
211	mTranslationServiceRadioGroup->setEnabled(on);
212	mLanguageCombo->setEnabled(on);
213
214	getChild<LLTextBox>("bing_api_key_label")->setEnabled(on);
215	mBingAPIKeyEditor->setEnabled(on);
216
217	getChild<LLTextBox>("google_api_key_label")->setEnabled(on);
218	mGoogleAPIKeyEditor->setEnabled(on);
219
220	mBingAPIKeyEditor->setEnabled(on && bing_selected);
221	mGoogleAPIKeyEditor->setEnabled(on && google_selected);
222
223	mBingVerifyBtn->setEnabled(on && bing_selected &&
224		!mBingKeyVerified && !getEnteredBingKey().empty());
225	mGoogleVerifyBtn->setEnabled(on && google_selected &&
226		!mGoogleKeyVerified && !getEnteredGoogleKey().empty());
227
228	mOKBtn->setEnabled(
229		!on || (
230		(bing_selected && mBingKeyVerified) ||
231		(google_selected && mGoogleKeyVerified)
232	));
233}
234
235void LLFloaterTranslationSettings::verifyKey(int service, const std::string& key, bool alert)
236{
237	LLTranslate::KeyVerificationReceiverPtr receiver =
238		new EnteredKeyVerifier((LLTranslate::EService) service, alert);
239	LLTranslate::verifyKey(receiver, key);
240}
241
242void LLFloaterTranslationSettings::onEditorFocused(LLFocusableElement* control)
243{
244	LLLineEditor* editor = dynamic_cast<LLLineEditor*>(control);
245	if (editor && editor->hasTabStop()) // if enabled. getEnabled() doesn't work
246	{
247		if (editor->getTentative())
248		{
249			editor->setText(LLStringUtil::null);
250			editor->setTentative(FALSE);
251		}
252	}
253}
254
255void LLFloaterTranslationSettings::onBingKeyEdited()
256{
257	if (mBingAPIKeyEditor->isDirty())
258	{
259		setBingVerified(false, false);
260	}
261}
262
263void LLFloaterTranslationSettings::onGoogleKeyEdited()
264{
265	if (mGoogleAPIKeyEditor->isDirty())
266	{
267		setGoogleVerified(false, false);
268	}
269}
270
271void LLFloaterTranslationSettings::onBtnBingVerify()
272{
273	std::string key = getEnteredBingKey();
274	if (!key.empty())
275	{
276		verifyKey(LLTranslate::SERVICE_BING, key);
277	}
278}
279
280void LLFloaterTranslationSettings::onBtnGoogleVerify()
281{
282	std::string key = getEnteredGoogleKey();
283	if (!key.empty())
284	{
285		verifyKey(LLTranslate::SERVICE_GOOGLE, key);
286	}
287}
288
289void LLFloaterTranslationSettings::onBtnOK()
290{
291	gSavedSettings.setBOOL("TranslateChat", mMachineTranslationCB->getValue().asBoolean());
292	gSavedSettings.setString("TranslateLanguage", mLanguageCombo->getSelectedValue().asString());
293	gSavedSettings.setString("TranslationService", getSelectedService());
294	gSavedSettings.setString("BingTranslateAPIKey", getEnteredBingKey());
295	gSavedSettings.setString("GoogleTranslateAPIKey", getEnteredGoogleKey());
296	LLNearbyChatBar::getInstance()->enableTranslationCheckbox(LLTranslate::isTranslationConfigured());
297	closeFloater(false);
298}