llfloaterpostcard.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /**
  2. * @file llfloaterpostcard.cpp
  3. * @brief Postcard send floater, allows setting name, e-mail address, etc.
  4. *
  5. * $LicenseInfo:firstyear=2004&license=viewergpl$
  6. *
  7. * Copyright (c) 2004-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include <regex>
  34. #include "llfloaterpostcard.h"
  35. #include "llgl.h"
  36. #include "llimagejpeg.h"
  37. #include "lllineeditor.h"
  38. #include "lltexteditor.h"
  39. #include "lluictrlfactory.h"
  40. #include "llagent.h"
  41. #include "llgridmanager.h" // For gIsInSecondLife
  42. #include "llviewerassetupload.h"
  43. #include "llviewertexture.h"
  44. #include "llviewerwindow.h"
  45. // static variables
  46. LLFloaterPostcard::instance_list_t LLFloaterPostcard::sInstances;
  47. std::string LLFloaterPostcard::sUserEmail;
  48. class LLPostcardUploadInfo : public LLBufferedAssetUploadInfo
  49. {
  50. public:
  51. LLPostcardUploadInfo(const std::string& email_from,
  52. const std::string& name_from,
  53. const std::string& email_to,
  54. const std::string& subject,
  55. const std::string& message,
  56. const LLVector3d& position,
  57. LLPointer<LLImageFormatted> image,
  58. inv_uploaded_cb_t finish)
  59. : LLBufferedAssetUploadInfo(LLUUID::null, image, finish),
  60. mEmailFrom(email_from),
  61. mNameFrom(name_from),
  62. mEmailTo(email_to),
  63. mSubject(subject),
  64. mMessage(message),
  65. mGlobalPosition(position)
  66. {
  67. }
  68. LLSD generatePostBody() override
  69. {
  70. LLSD postcard = LLSD::emptyMap();
  71. postcard["to"] = mEmailTo;
  72. if (!gIsInSecondLife)
  73. {
  74. postcard["from"] = mEmailFrom;
  75. }
  76. postcard["name"] = mNameFrom;
  77. postcard["subject"] = mSubject;
  78. postcard["msg"] = mMessage;
  79. postcard["pos-global"] = mGlobalPosition.getValue();
  80. return postcard;
  81. }
  82. private:
  83. std::string mEmailFrom;
  84. std::string mNameFrom;
  85. std::string mEmailTo;
  86. std::string mSubject;
  87. std::string mMessage;
  88. LLVector3d mGlobalPosition;
  89. };
  90. //static
  91. LLFloaterPostcard* LLFloaterPostcard::showFromSnapshot(LLImageJPEG* jpeg,
  92. LLViewerTexture* img,
  93. const LLVector2& scale,
  94. const LLVector3d& pos)
  95. {
  96. // Take the images from the caller. It is now our job to clean them up.
  97. return new LLFloaterPostcard(jpeg, img, scale, pos);
  98. }
  99. LLFloaterPostcard::LLFloaterPostcard(LLImageJPEG* jpeg, LLViewerTexture* img,
  100. const LLVector2& img_scale,
  101. const LLVector3d& pos_taken_global)
  102. : LLFloater("postcard"),
  103. mJPEGImage(jpeg),
  104. mViewerImage(img),
  105. mImageScale(img_scale),
  106. mPosTakenGlobal(pos_taken_global),
  107. mHasFirstMsgFocus(false)
  108. {
  109. LLUICtrlFactory::getInstance()->buildFloater(this, "floater_postcard.xml");
  110. sInstances.insert(this);
  111. }
  112. LLFloaterPostcard::~LLFloaterPostcard()
  113. {
  114. sInstances.erase(this);
  115. mJPEGImage = NULL; // dereferences the image (LLPointer)
  116. }
  117. bool LLFloaterPostcard::postBuild()
  118. {
  119. if (gIsInSecondLife)
  120. {
  121. sUserEmail = getString("undisclosed");
  122. }
  123. else if (sUserEmail.empty())
  124. {
  125. gAgent.sendAgentUserInfoRequest();
  126. }
  127. childSetAction("cancel_btn", onClickCancel, this);
  128. childSetAction("send_btn", onClickSend, this);
  129. mFromLine = getChild<LLLineEditor>("from_form");
  130. mFromLine->setText(sUserEmail);
  131. mFromLine->setEnabled(sUserEmail.empty());
  132. LLUIString subject = getString("default_subject");
  133. subject.setArg("[GRID]", LLGridManager::getInstance()->getGridLabel());
  134. childSetValue("subject_form", LLSD::String(subject));
  135. std::string name_string;
  136. gAgent.buildFullname(name_string);
  137. childSetValue("name_form", LLSD(name_string));
  138. mMessageText = getChild<LLTextEditor>("msg_form");
  139. mMessageText->setWordWrap(true);
  140. // The first time a user focuses to the msg box, all text will be selected.
  141. mMessageText->setFocusChangedCallback(onMsgFormFocusReceived, this);
  142. childSetFocus("to_form", true);
  143. return true;
  144. }
  145. void LLFloaterPostcard::draw()
  146. {
  147. LLGLSUIDefault gls_ui;
  148. LLFloater::draw();
  149. if (!isMinimized() && mViewerImage.notNull() && mJPEGImage.notNull())
  150. {
  151. LLRect rect(getRect());
  152. // first set the max extents of our preview
  153. rect.translate(-rect.mLeft, -rect.mBottom);
  154. rect.mLeft += 280;
  155. rect.mRight -= 10;
  156. rect.mTop -= 20;
  157. rect.mBottom = rect.mTop - 130;
  158. // then fix the aspect ratio
  159. F32 ratio = (F32)mJPEGImage->getWidth() / (F32)mJPEGImage->getHeight();
  160. if ((F32)rect.getWidth() / (F32)rect.getHeight() >= ratio)
  161. {
  162. rect.mRight = (S32)((F32)rect.mLeft + (F32)rect.getHeight() * ratio);
  163. }
  164. else
  165. {
  166. rect.mBottom = (S32)((F32)rect.mTop - (F32)rect.getWidth() / ratio);
  167. }
  168. gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
  169. gl_rect_2d(rect, LLColor4(0.f, 0.f, 0.f, 1.f));
  170. rect.stretch(-1);
  171. gGL.matrixMode(LLRender::MM_TEXTURE);
  172. gGL.pushMatrix();
  173. {
  174. gGL.scalef(mImageScale.mV[VX], mImageScale.mV[VY], 1.f);
  175. gGL.matrixMode(LLRender::MM_MODELVIEW);
  176. gl_draw_scaled_image(rect.mLeft, rect.mBottom, rect.getWidth(),
  177. rect.getHeight(), mViewerImage,
  178. LLColor4::white);
  179. }
  180. gGL.matrixMode(LLRender::MM_TEXTURE);
  181. gGL.popMatrix();
  182. gGL.matrixMode(LLRender::MM_MODELVIEW);
  183. }
  184. }
  185. //static
  186. void LLFloaterPostcard::onClickCancel(void* userdata)
  187. {
  188. LLFloaterPostcard* self = (LLFloaterPostcard*)userdata;
  189. if (self)
  190. {
  191. self->close();
  192. }
  193. }
  194. //static
  195. void LLFloaterPostcard::onClickSend(void* userdata)
  196. {
  197. LLFloaterPostcard* self = (LLFloaterPostcard*)userdata;
  198. if (!self)
  199. {
  200. return;
  201. }
  202. std::string to = self->childGetValue("to_form").asString();
  203. if (to.empty())
  204. {
  205. gNotifications.add("PromptRecipientEmail");
  206. return;
  207. }
  208. std::string from = self->mFromLine->getText();
  209. if (!gIsInSecondLife && from.empty())
  210. {
  211. self->mFromLine->setEnabled(true);
  212. gNotifications.add("PromptSelfEmail");
  213. return;
  214. }
  215. try
  216. {
  217. std::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,})*");
  218. if (!std::regex_match(to, email_format))
  219. {
  220. gNotifications.add("PromptRecipientEmail");
  221. return;
  222. }
  223. if (!gIsInSecondLife && !std::regex_match(from, email_format))
  224. {
  225. self->mFromLine->setEnabled(true);
  226. gNotifications.add("PromptSelfEmail");
  227. return;
  228. }
  229. }
  230. catch (std::regex_error& e)
  231. {
  232. llwarns << "Regex error: " << e.what() << llendl;
  233. }
  234. std::string subject(self->childGetValue("subject_form").asString());
  235. if (subject.empty() || !self->mHasFirstMsgFocus)
  236. {
  237. gNotifications.add("PromptMissingSubjMsg", LLSD(), LLSD(),
  238. boost::bind(&LLFloaterPostcard::missingSubjMsgAlertCallback,
  239. self, _1, _2));
  240. return;
  241. }
  242. if (self->mJPEGImage.notNull())
  243. {
  244. self->sendPostcard();
  245. }
  246. else
  247. {
  248. gNotifications.add("ErrorProcessingSnapshot");
  249. }
  250. }
  251. void LLFloaterPostcard::onMsgFormFocusReceived(LLFocusableElement* receiver,
  252. void* userdata)
  253. {
  254. LLFloaterPostcard* self = (LLFloaterPostcard*)userdata;
  255. if (self)
  256. {
  257. if (!self->mHasFirstMsgFocus &&
  258. receiver == self->mMessageText && self->mMessageText->hasFocus())
  259. {
  260. self->mHasFirstMsgFocus = true;
  261. self->mMessageText->setText(LLStringUtil::null);
  262. }
  263. }
  264. }
  265. bool LLFloaterPostcard::missingSubjMsgAlertCallback(const LLSD& notification,
  266. const LLSD& response)
  267. {
  268. if (LLNotification::getSelectedOption(notification, response) == 0)
  269. {
  270. // User clicked OK
  271. if ((childGetValue("subject_form").asString()).empty())
  272. {
  273. // Stuff the subject back into the form.
  274. LLUIString subject = getString("default_subject");
  275. subject.setArg("[GRID]",
  276. LLGridManager::getInstance()->getGridLabel());
  277. childSetValue("subject_form", LLSD::String(subject));
  278. }
  279. if (!mHasFirstMsgFocus)
  280. {
  281. // The user never switched focus to the messagee window.
  282. // Using the default string.
  283. mMessageText->setValue(getString("default_message"));
  284. }
  285. sendPostcard();
  286. }
  287. return false;
  288. }
  289. //static
  290. void LLFloaterPostcard::sendPostcardFinished(LLSD result, void* userdata)
  291. {
  292. std::string state = result["state"].asString();
  293. llinfos << state << llendl;
  294. LLFloaterPostcard* self = (LLFloaterPostcard*)userdata;
  295. if (self && sInstances.count(self))
  296. {
  297. self->close();
  298. }
  299. }
  300. void LLFloaterPostcard::sendPostcard()
  301. {
  302. // Remove any dependency on another floater so that we can be sure to
  303. // outlive it while we need to.
  304. LLFloater* dependee = getDependee();
  305. if (dependee)
  306. {
  307. dependee->removeDependentFloater(this);
  308. }
  309. // Upload the image
  310. const std::string& url = gAgent.getRegionCapability("SendPostcard");
  311. if (!url.empty())
  312. {
  313. llinfos << "Sending Postcard via capability" << llendl;
  314. std::string name_from = childGetValue("name_form").asString();
  315. std::string email_to = childGetValue("to_form").asString();
  316. std::string subject = childGetValue("subject_form").asString();
  317. std::string message = mMessageText->getValue().asString();
  318. LLResourceUploadInfo::ptr_t
  319. info(new LLPostcardUploadInfo(mFromLine->getText(), name_from,
  320. email_to, subject, message,
  321. mPosTakenGlobal, mJPEGImage,
  322. boost::bind(&LLFloaterPostcard::sendPostcardFinished,
  323. _4, (void*)this)));
  324. LLViewerAssetUpload::enqueueInventoryUpload(url, info);
  325. // Give the user some feedback of the event
  326. gViewerWindowp->playSnapshotAnimAndSound();
  327. }
  328. else
  329. {
  330. gNotifications.add("PostcardsUnavailable");
  331. close();
  332. }
  333. }
  334. //static
  335. void LLFloaterPostcard::updateUserInfo(const std::string& email)
  336. {
  337. if (gIsInSecondLife) return;
  338. sUserEmail = email;
  339. for (instance_list_t::iterator iter = sInstances.begin();
  340. iter != sInstances.end(); ++iter)
  341. {
  342. LLFloaterPostcard* instance = *iter;
  343. if (instance->mFromLine->getText().empty())
  344. {
  345. // there's no text in this field yet, pre-populate
  346. instance->mFromLine->setText(email);
  347. instance->mFromLine->setEnabled(false);
  348. }
  349. }
  350. }