PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llpanelsnapshotpostcard.cpp

https://bitbucket.org/lindenlab/mesh-development/
C++ | 269 lines | 176 code | 48 blank | 45 comment | 14 complexity | 17270f8847029050dc557df6c4e098a4 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. /**
  2. * @file llpanelsnapshotpostcard.cpp
  3. * @brief Postcard sending panel.
  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 "llcombobox.h"
  28. #include "llnotificationsutil.h"
  29. #include "llsidetraypanelcontainer.h"
  30. #include "llsliderctrl.h"
  31. #include "llspinctrl.h"
  32. #include "lltexteditor.h"
  33. #include "llagent.h"
  34. #include "llagentui.h"
  35. #include "llfloatersnapshot.h" // FIXME: replace with a snapshot storage model
  36. #include "llpanelsnapshot.h"
  37. #include "llpostcard.h"
  38. #include "llviewercontrol.h" // gSavedSettings
  39. #include "llviewerwindow.h"
  40. #include <boost/regex.hpp>
  41. /**
  42. * Sends postcard via email.
  43. */
  44. class LLPanelSnapshotPostcard
  45. : public LLPanelSnapshot
  46. {
  47. LOG_CLASS(LLPanelSnapshotPostcard);
  48. public:
  49. LLPanelSnapshotPostcard();
  50. /*virtual*/ BOOL postBuild();
  51. /*virtual*/ void onOpen(const LLSD& key);
  52. /*virtual*/ S32 notify(const LLSD& info);
  53. private:
  54. /*virtual*/ std::string getWidthSpinnerName() const { return "postcard_snapshot_width"; }
  55. /*virtual*/ std::string getHeightSpinnerName() const { return "postcard_snapshot_height"; }
  56. /*virtual*/ std::string getAspectRatioCBName() const { return "postcard_keep_aspect_check"; }
  57. /*virtual*/ std::string getImageSizeComboName() const { return "postcard_size_combo"; }
  58. /*virtual*/ LLFloaterSnapshot::ESnapshotFormat getImageFormat() const { return LLFloaterSnapshot::SNAPSHOT_FORMAT_JPEG; }
  59. /*virtual*/ void updateControls(const LLSD& info);
  60. bool missingSubjMsgAlertCallback(const LLSD& notification, const LLSD& response);
  61. void sendPostcard();
  62. void onMsgFormFocusRecieved();
  63. void onFormatComboCommit(LLUICtrl* ctrl);
  64. void onQualitySliderCommit(LLUICtrl* ctrl);
  65. void onTabButtonPress(S32 btn_idx);
  66. void onSend();
  67. bool mHasFirstMsgFocus;
  68. std::string mAgentEmail;
  69. };
  70. static LLRegisterPanelClassWrapper<LLPanelSnapshotPostcard> panel_class("llpanelsnapshotpostcard");
  71. LLPanelSnapshotPostcard::LLPanelSnapshotPostcard()
  72. : mHasFirstMsgFocus(false)
  73. {
  74. mCommitCallbackRegistrar.add("Postcard.Send", boost::bind(&LLPanelSnapshotPostcard::onSend, this));
  75. mCommitCallbackRegistrar.add("Postcard.Cancel", boost::bind(&LLPanelSnapshotPostcard::cancel, this));
  76. mCommitCallbackRegistrar.add("Postcard.Message", boost::bind(&LLPanelSnapshotPostcard::onTabButtonPress, this, 0));
  77. mCommitCallbackRegistrar.add("Postcard.Settings", boost::bind(&LLPanelSnapshotPostcard::onTabButtonPress, this, 1));
  78. }
  79. // virtual
  80. BOOL LLPanelSnapshotPostcard::postBuild()
  81. {
  82. // pick up the user's up-to-date email address
  83. gAgent.sendAgentUserInfoRequest();
  84. std::string name_string;
  85. LLAgentUI::buildFullname(name_string);
  86. getChild<LLUICtrl>("name_form")->setValue(LLSD(name_string));
  87. // For the first time a user focuses to .the msg box, all text will be selected.
  88. getChild<LLUICtrl>("msg_form")->setFocusChangedCallback(boost::bind(&LLPanelSnapshotPostcard::onMsgFormFocusRecieved, this));
  89. getChild<LLUICtrl>("to_form")->setFocus(TRUE);
  90. getChild<LLUICtrl>("image_quality_slider")->setCommitCallback(boost::bind(&LLPanelSnapshotPostcard::onQualitySliderCommit, this, _1));
  91. getChild<LLButton>("message_btn")->setToggleState(TRUE);
  92. return LLPanelSnapshot::postBuild();
  93. }
  94. // virtual
  95. void LLPanelSnapshotPostcard::onOpen(const LLSD& key)
  96. {
  97. LLPanelSnapshot::onOpen(key);
  98. }
  99. // virtual
  100. S32 LLPanelSnapshotPostcard::notify(const LLSD& info)
  101. {
  102. if (!info.has("agent-email"))
  103. {
  104. llassert(info.has("agent-email"));
  105. return 0;
  106. }
  107. if (mAgentEmail.empty())
  108. {
  109. mAgentEmail = info["agent-email"].asString();
  110. }
  111. return 1;
  112. }
  113. // virtual
  114. void LLPanelSnapshotPostcard::updateControls(const LLSD& info)
  115. {
  116. getChild<LLUICtrl>("image_quality_slider")->setValue(gSavedSettings.getS32("SnapshotQuality"));
  117. updateImageQualityLevel();
  118. const bool have_snapshot = info.has("have-snapshot") ? info["have-snapshot"].asBoolean() : true;
  119. getChild<LLUICtrl>("send_btn")->setEnabled(have_snapshot);
  120. }
  121. bool LLPanelSnapshotPostcard::missingSubjMsgAlertCallback(const LLSD& notification, const LLSD& response)
  122. {
  123. S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
  124. if(0 == option)
  125. {
  126. // User clicked OK
  127. if((getChild<LLUICtrl>("subject_form")->getValue().asString()).empty())
  128. {
  129. // Stuff the subject back into the form.
  130. getChild<LLUICtrl>("subject_form")->setValue(getString("default_subject"));
  131. }
  132. if (!mHasFirstMsgFocus)
  133. {
  134. // The user never switched focus to the message window.
  135. // Using the default string.
  136. getChild<LLUICtrl>("msg_form")->setValue(getString("default_message"));
  137. }
  138. sendPostcard();
  139. }
  140. return false;
  141. }
  142. void LLPanelSnapshotPostcard::sendPostcard()
  143. {
  144. std::string to(getChild<LLUICtrl>("to_form")->getValue().asString());
  145. std::string subject(getChild<LLUICtrl>("subject_form")->getValue().asString());
  146. LLSD postcard = LLSD::emptyMap();
  147. postcard["pos-global"] = LLFloaterSnapshot::getPosTakenGlobal().getValue();
  148. postcard["to"] = to;
  149. postcard["from"] = mAgentEmail;
  150. postcard["name"] = getChild<LLUICtrl>("name_form")->getValue().asString();
  151. postcard["subject"] = subject;
  152. postcard["msg"] = getChild<LLUICtrl>("msg_form")->getValue().asString();
  153. LLPostCard::send(LLFloaterSnapshot::getImageData(), postcard);
  154. // Give user feedback of the event.
  155. gViewerWindow->playSnapshotAnimAndSound();
  156. LLFloaterSnapshot::postSave();
  157. }
  158. void LLPanelSnapshotPostcard::onMsgFormFocusRecieved()
  159. {
  160. LLTextEditor* msg_form = getChild<LLTextEditor>("msg_form");
  161. if (msg_form->hasFocus() && !mHasFirstMsgFocus)
  162. {
  163. mHasFirstMsgFocus = true;
  164. msg_form->setText(LLStringUtil::null);
  165. }
  166. }
  167. void LLPanelSnapshotPostcard::onFormatComboCommit(LLUICtrl* ctrl)
  168. {
  169. // will call updateControls()
  170. LLFloaterSnapshot::getInstance()->notify(LLSD().with("image-format-change", true));
  171. }
  172. void LLPanelSnapshotPostcard::onQualitySliderCommit(LLUICtrl* ctrl)
  173. {
  174. updateImageQualityLevel();
  175. LLSliderCtrl* slider = (LLSliderCtrl*)ctrl;
  176. S32 quality_val = llfloor((F32)slider->getValue().asReal());
  177. LLSD info;
  178. info["image-quality-change"] = quality_val;
  179. LLFloaterSnapshot::getInstance()->notify(info); // updates the "SnapshotQuality" setting
  180. }
  181. void LLPanelSnapshotPostcard::onTabButtonPress(S32 btn_idx)
  182. {
  183. LLButton* buttons[2] = {
  184. getChild<LLButton>("message_btn"),
  185. getChild<LLButton>("settings_btn"),
  186. };
  187. // Switch between Message and Settings tabs.
  188. LLButton* clicked_btn = buttons[btn_idx];
  189. LLButton* other_btn = buttons[!btn_idx];
  190. LLSideTrayPanelContainer* container =
  191. getChild<LLSideTrayPanelContainer>("postcard_panel_container");
  192. container->selectTab(clicked_btn->getToggleState() ? btn_idx : !btn_idx);
  193. //clicked_btn->setEnabled(FALSE);
  194. other_btn->toggleState();
  195. //other_btn->setEnabled(TRUE);
  196. lldebugs << "Button #" << btn_idx << " (" << clicked_btn->getName() << ") clicked" << llendl;
  197. }
  198. void LLPanelSnapshotPostcard::onSend()
  199. {
  200. // Validate input.
  201. std::string to(getChild<LLUICtrl>("to_form")->getValue().asString());
  202. boost::regex email_format("[A-Za-z0-9.%+-_]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}(,[ \t]*[A-Za-z0-9.%+-_]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,})*");
  203. if (to.empty() || !boost::regex_match(to, email_format))
  204. {
  205. LLNotificationsUtil::add("PromptRecipientEmail");
  206. return;
  207. }
  208. if (mAgentEmail.empty() || !boost::regex_match(mAgentEmail, email_format))
  209. {
  210. LLNotificationsUtil::add("PromptSelfEmail");
  211. return;
  212. }
  213. std::string subject(getChild<LLUICtrl>("subject_form")->getValue().asString());
  214. if(subject.empty() || !mHasFirstMsgFocus)
  215. {
  216. LLNotificationsUtil::add("PromptMissingSubjMsg", LLSD(), LLSD(), boost::bind(&LLPanelSnapshotPostcard::missingSubjMsgAlertCallback, this, _1, _2));
  217. return;
  218. }
  219. // Send postcard.
  220. sendPostcard();
  221. }