PageRenderTime 105ms CodeModel.GetById 12ms app.highlight 88ms RepoModel.GetById 2ms app.codeStats 0ms

/indra/newview/llpanelsnapshotpostcard.cpp

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