PageRenderTime 30ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): LGPL-2.1
  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. #include "llviewerprecompiledheaders.h"
  27. #include "llfloatertranslationsettings.h"
  28. // Viewer includes
  29. #include "llnearbychatbar.h"
  30. #include "lltranslate.h"
  31. #include "llviewercontrol.h" // for gSavedSettings
  32. // Linden library includes
  33. #include "llbutton.h"
  34. #include "llcheckboxctrl.h"
  35. #include "llcombobox.h"
  36. #include "llfloaterreg.h"
  37. #include "lllineeditor.h"
  38. #include "llnotificationsutil.h"
  39. #include "llradiogroup.h"
  40. class EnteredKeyVerifier : public LLTranslate::KeyVerificationReceiver
  41. {
  42. public:
  43. EnteredKeyVerifier(LLTranslate::EService service, bool alert)
  44. : LLTranslate::KeyVerificationReceiver(service)
  45. , mAlert(alert)
  46. {
  47. }
  48. private:
  49. /*virtual*/ void setVerificationStatus(bool ok)
  50. {
  51. LLFloaterTranslationSettings* floater =
  52. LLFloaterReg::getTypedInstance<LLFloaterTranslationSettings>("prefs_translation");
  53. if (!floater)
  54. {
  55. llwarns << "Cannot find translation settings floater" << llendl;
  56. return;
  57. }
  58. switch (getService())
  59. {
  60. case LLTranslate::SERVICE_BING:
  61. floater->setBingVerified(ok, mAlert);
  62. break;
  63. case LLTranslate::SERVICE_GOOGLE:
  64. floater->setGoogleVerified(ok, mAlert);
  65. break;
  66. }
  67. }
  68. bool mAlert;
  69. };
  70. LLFloaterTranslationSettings::LLFloaterTranslationSettings(const LLSD& key)
  71. : LLFloater(key)
  72. , mMachineTranslationCB(NULL)
  73. , mLanguageCombo(NULL)
  74. , mTranslationServiceRadioGroup(NULL)
  75. , mBingAPIKeyEditor(NULL)
  76. , mGoogleAPIKeyEditor(NULL)
  77. , mBingVerifyBtn(NULL)
  78. , mGoogleVerifyBtn(NULL)
  79. , mOKBtn(NULL)
  80. , mBingKeyVerified(false)
  81. , mGoogleKeyVerified(false)
  82. {
  83. }
  84. // virtual
  85. BOOL LLFloaterTranslationSettings::postBuild()
  86. {
  87. mMachineTranslationCB = getChild<LLCheckBoxCtrl>("translate_chat_checkbox");
  88. mLanguageCombo = getChild<LLComboBox>("translate_language_combo");
  89. mTranslationServiceRadioGroup = getChild<LLRadioGroup>("translation_service_rg");
  90. mBingAPIKeyEditor = getChild<LLLineEditor>("bing_api_key");
  91. mGoogleAPIKeyEditor = getChild<LLLineEditor>("google_api_key");
  92. mBingVerifyBtn = getChild<LLButton>("verify_bing_api_key_btn");
  93. mGoogleVerifyBtn = getChild<LLButton>("verify_google_api_key_btn");
  94. mOKBtn = getChild<LLButton>("ok_btn");
  95. mMachineTranslationCB->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
  96. mTranslationServiceRadioGroup->setCommitCallback(boost::bind(&LLFloaterTranslationSettings::updateControlsEnabledState, this));
  97. mOKBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnOK, this));
  98. getChild<LLButton>("cancel_btn")->setClickedCallback(boost::bind(&LLFloater::closeFloater, this, false));
  99. mBingVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnBingVerify, this));
  100. mGoogleVerifyBtn->setClickedCallback(boost::bind(&LLFloaterTranslationSettings::onBtnGoogleVerify, this));
  101. mBingAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
  102. mBingAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onBingKeyEdited, this), NULL);
  103. mGoogleAPIKeyEditor->setFocusReceivedCallback(boost::bind(&LLFloaterTranslationSettings::onEditorFocused, this, _1));
  104. mGoogleAPIKeyEditor->setKeystrokeCallback(boost::bind(&LLFloaterTranslationSettings::onGoogleKeyEdited, this), NULL);
  105. center();
  106. return TRUE;
  107. }
  108. // virtual
  109. void LLFloaterTranslationSettings::onOpen(const LLSD& key)
  110. {
  111. mMachineTranslationCB->setValue(gSavedSettings.getBOOL("TranslateChat"));
  112. mLanguageCombo->setSelectedByValue(gSavedSettings.getString("TranslateLanguage"), TRUE);
  113. mTranslationServiceRadioGroup->setSelectedByValue(gSavedSettings.getString("TranslationService"), TRUE);
  114. std::string bing_key = gSavedSettings.getString("BingTranslateAPIKey");
  115. if (!bing_key.empty())
  116. {
  117. mBingAPIKeyEditor->setText(bing_key);
  118. mBingAPIKeyEditor->setTentative(FALSE);
  119. verifyKey(LLTranslate::SERVICE_BING, bing_key, false);
  120. }
  121. else
  122. {
  123. mBingAPIKeyEditor->setTentative(TRUE);
  124. mBingKeyVerified = FALSE;
  125. }
  126. std::string google_key = gSavedSettings.getString("GoogleTranslateAPIKey");
  127. if (!google_key.empty())
  128. {
  129. mGoogleAPIKeyEditor->setText(google_key);
  130. mGoogleAPIKeyEditor->setTentative(FALSE);
  131. verifyKey(LLTranslate::SERVICE_GOOGLE, google_key, false);
  132. }
  133. else
  134. {
  135. mGoogleAPIKeyEditor->setTentative(TRUE);
  136. mGoogleKeyVerified = FALSE;
  137. }
  138. updateControlsEnabledState();
  139. }
  140. void LLFloaterTranslationSettings::setBingVerified(bool ok, bool alert)
  141. {
  142. if (alert)
  143. {
  144. showAlert(ok ? "bing_api_key_verified" : "bing_api_key_not_verified");
  145. }
  146. mBingKeyVerified = ok;
  147. updateControlsEnabledState();
  148. }
  149. void LLFloaterTranslationSettings::setGoogleVerified(bool ok, bool alert)
  150. {
  151. if (alert)
  152. {
  153. showAlert(ok ? "google_api_key_verified" : "google_api_key_not_verified");
  154. }
  155. mGoogleKeyVerified = ok;
  156. updateControlsEnabledState();
  157. }
  158. std::string LLFloaterTranslationSettings::getSelectedService() const
  159. {
  160. return mTranslationServiceRadioGroup->getSelectedValue().asString();
  161. }
  162. std::string LLFloaterTranslationSettings::getEnteredBingKey() const
  163. {
  164. return mBingAPIKeyEditor->getTentative() ? LLStringUtil::null : mBingAPIKeyEditor->getText();
  165. }
  166. std::string LLFloaterTranslationSettings::getEnteredGoogleKey() const
  167. {
  168. return mGoogleAPIKeyEditor->getTentative() ? LLStringUtil::null : mGoogleAPIKeyEditor->getText();
  169. }
  170. void LLFloaterTranslationSettings::showAlert(const std::string& msg_name) const
  171. {
  172. LLSD args;
  173. args["MESSAGE"] = getString(msg_name);
  174. LLNotificationsUtil::add("GenericAlert", args);
  175. }
  176. void LLFloaterTranslationSettings::updateControlsEnabledState()
  177. {
  178. // Enable/disable controls based on the checkbox value.
  179. bool on = mMachineTranslationCB->getValue().asBoolean();
  180. std::string service = getSelectedService();
  181. bool bing_selected = service == "bing";
  182. bool google_selected = service == "google";
  183. mTranslationServiceRadioGroup->setEnabled(on);
  184. mLanguageCombo->setEnabled(on);
  185. getChild<LLTextBox>("bing_api_key_label")->setEnabled(on);
  186. mBingAPIKeyEditor->setEnabled(on);
  187. getChild<LLTextBox>("google_api_key_label")->setEnabled(on);
  188. mGoogleAPIKeyEditor->setEnabled(on);
  189. mBingAPIKeyEditor->setEnabled(on && bing_selected);
  190. mGoogleAPIKeyEditor->setEnabled(on && google_selected);
  191. mBingVerifyBtn->setEnabled(on && bing_selected &&
  192. !mBingKeyVerified && !getEnteredBingKey().empty());
  193. mGoogleVerifyBtn->setEnabled(on && google_selected &&
  194. !mGoogleKeyVerified && !getEnteredGoogleKey().empty());
  195. mOKBtn->setEnabled(
  196. !on || (
  197. (bing_selected && mBingKeyVerified) ||
  198. (google_selected && mGoogleKeyVerified)
  199. ));
  200. }
  201. void LLFloaterTranslationSettings::verifyKey(int service, const std::string& key, bool alert)
  202. {
  203. LLTranslate::KeyVerificationReceiverPtr receiver =
  204. new EnteredKeyVerifier((LLTranslate::EService) service, alert);
  205. LLTranslate::verifyKey(receiver, key);
  206. }
  207. void LLFloaterTranslationSettings::onEditorFocused(LLFocusableElement* control)
  208. {
  209. LLLineEditor* editor = dynamic_cast<LLLineEditor*>(control);
  210. if (editor && editor->hasTabStop()) // if enabled. getEnabled() doesn't work
  211. {
  212. if (editor->getTentative())
  213. {
  214. editor->setText(LLStringUtil::null);
  215. editor->setTentative(FALSE);
  216. }
  217. }
  218. }
  219. void LLFloaterTranslationSettings::onBingKeyEdited()
  220. {
  221. if (mBingAPIKeyEditor->isDirty())
  222. {
  223. setBingVerified(false, false);
  224. }
  225. }
  226. void LLFloaterTranslationSettings::onGoogleKeyEdited()
  227. {
  228. if (mGoogleAPIKeyEditor->isDirty())
  229. {
  230. setGoogleVerified(false, false);
  231. }
  232. }
  233. void LLFloaterTranslationSettings::onBtnBingVerify()
  234. {
  235. std::string key = getEnteredBingKey();
  236. if (!key.empty())
  237. {
  238. verifyKey(LLTranslate::SERVICE_BING, key);
  239. }
  240. }
  241. void LLFloaterTranslationSettings::onBtnGoogleVerify()
  242. {
  243. std::string key = getEnteredGoogleKey();
  244. if (!key.empty())
  245. {
  246. verifyKey(LLTranslate::SERVICE_GOOGLE, key);
  247. }
  248. }
  249. void LLFloaterTranslationSettings::onBtnOK()
  250. {
  251. gSavedSettings.setBOOL("TranslateChat", mMachineTranslationCB->getValue().asBoolean());
  252. gSavedSettings.setString("TranslateLanguage", mLanguageCombo->getSelectedValue().asString());
  253. gSavedSettings.setString("TranslationService", getSelectedService());
  254. gSavedSettings.setString("BingTranslateAPIKey", getEnteredBingKey());
  255. gSavedSettings.setString("GoogleTranslateAPIKey", getEnteredGoogleKey());
  256. LLNearbyChatBar::getInstance()->enableTranslationCheckbox(LLTranslate::isTranslationConfigured());
  257. closeFloater(false);
  258. }