PageRenderTime 101ms CodeModel.GetById 86ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llfloaterdisplayname.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 223 lines | 146 code | 36 blank | 41 comment | 9 complexity | 562126f279291b0db6ddd9365a7ed6a0 MD5 | raw file
  1/** 
  2 * @file llfloaterdisplayname.cpp
  3 * @author Leyla Farazha
  4 * @brief Implementation of the LLFloaterDisplayName class.
  5 *
  6 * $LicenseInfo:firstyear=2002&license=viewerlgpl$
  7 * Second Life Viewer Source Code
  8 * Copyright (C) 2010, Linden Research, Inc.
  9 * 
 10 * This library is free software; you can redistribute it and/or
 11 * modify it under the terms of the GNU Lesser General Public
 12 * License as published by the Free Software Foundation;
 13 * version 2.1 of the License only.
 14 * 
 15 * This library is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 18 * Lesser General Public License for more details.
 19 * 
 20 * You should have received a copy of the GNU Lesser General Public
 21 * License along with this library; if not, write to the Free Software
 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 23 * 
 24 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 25 * $/LicenseInfo$
 26 */
 27
 28
 29#include "llviewerprecompiledheaders.h"
 30#include "llfloaterreg.h"
 31#include "llfloater.h"
 32
 33#include "llnotificationsutil.h"
 34#include "llviewerdisplayname.h"
 35
 36#include "llnotifications.h"
 37#include "llfloaterdisplayname.h"
 38#include "llavatarnamecache.h"
 39
 40#include "llagent.h"
 41
 42
 43class LLFloaterDisplayName : public LLFloater
 44{
 45public:
 46	LLFloaterDisplayName(const LLSD& key);
 47	virtual ~LLFloaterDisplayName() {};
 48	/*virtual*/	BOOL	postBuild();
 49	void onSave();
 50	void onReset();
 51	void onCancel();
 52	/*virtual*/ void onOpen(const LLSD& key);
 53	
 54private:
 55	
 56	void onCacheSetName(bool success,
 57										  const std::string& reason,
 58										  const LLSD& content);
 59};
 60
 61LLFloaterDisplayName::LLFloaterDisplayName(const LLSD& key)
 62	: LLFloater(key)
 63{
 64}
 65
 66void LLFloaterDisplayName::onOpen(const LLSD& key)
 67{
 68	getChild<LLUICtrl>("display_name_editor")->clear();
 69	getChild<LLUICtrl>("display_name_confirm")->clear();
 70
 71	LLAvatarName av_name;
 72	LLAvatarNameCache::get(gAgent.getID(), &av_name);
 73
 74	F64 now_secs = LLDate::now().secondsSinceEpoch();
 75
 76	if (now_secs < av_name.mNextUpdate)
 77	{
 78		// ...can't update until some time in the future
 79		F64 next_update_local_secs =
 80			av_name.mNextUpdate - LLStringOps::getLocalTimeOffset();
 81		LLDate next_update_local(next_update_local_secs);
 82		// display as "July 18 12:17 PM"
 83		std::string next_update_string =
 84		next_update_local.toHTTPDateString("%B %d %I:%M %p");
 85		getChild<LLUICtrl>("lockout_text")->setTextArg("[TIME]", next_update_string);
 86		getChild<LLUICtrl>("lockout_text")->setVisible(true);
 87		getChild<LLUICtrl>("save_btn")->setEnabled(false);
 88		getChild<LLUICtrl>("display_name_editor")->setEnabled(false);
 89		getChild<LLUICtrl>("display_name_confirm")->setEnabled(false);
 90		getChild<LLUICtrl>("cancel_btn")->setFocus(TRUE);
 91		
 92	}
 93	else
 94	{
 95		getChild<LLUICtrl>("lockout_text")->setVisible(false);
 96		getChild<LLUICtrl>("save_btn")->setEnabled(true);
 97		getChild<LLUICtrl>("display_name_editor")->setEnabled(true);
 98		getChild<LLUICtrl>("display_name_confirm")->setEnabled(true);
 99
100	}
101}
102
103BOOL LLFloaterDisplayName::postBuild()
104{
105	getChild<LLUICtrl>("reset_btn")->setCommitCallback(boost::bind(&LLFloaterDisplayName::onReset, this));	
106	getChild<LLUICtrl>("cancel_btn")->setCommitCallback(boost::bind(&LLFloaterDisplayName::onCancel, this));	
107	getChild<LLUICtrl>("save_btn")->setCommitCallback(boost::bind(&LLFloaterDisplayName::onSave, this));	
108	
109	center();
110
111	return TRUE;
112}
113
114void LLFloaterDisplayName::onCacheSetName(bool success,
115										  const std::string& reason,
116										  const LLSD& content)
117{
118	if (success)
119	{
120		// Inform the user that the change took place, but will take a while
121		// to percolate.
122		LLSD args;
123		args["DISPLAY_NAME"] = content["display_name"];
124		LLNotificationsUtil::add("SetDisplayNameSuccess", args);
125
126		// Re-fetch my name, as it may have been sanitized by the service
127		//LLAvatarNameCache::get(getAvatarId(),
128		//	boost::bind(&LLPanelMyProfileEdit::onNameCache, this, _1, _2));
129		return;
130	}
131
132	// Request failed, notify the user
133	std::string error_tag = content["error_tag"].asString();
134	llinfos << "set name failure error_tag " << error_tag << llendl;
135
136	// We might have a localized string for this message
137	// error_args will usually be empty from the server.
138	if (!error_tag.empty()
139		&& LLNotifications::getInstance()->templateExists(error_tag))
140	{
141		LLNotificationsUtil::add(error_tag);
142		return;
143	}
144
145	// The server error might have a localized message for us
146	std::string lang_code = LLUI::getLanguage();
147	LLSD error_desc = content["error_description"];
148	if (error_desc.has( lang_code ))
149	{
150		LLSD args;
151		args["MESSAGE"] = error_desc[lang_code].asString();
152		LLNotificationsUtil::add("GenericAlert", args);
153		return;
154	}
155
156	// No specific error, throw a generic one
157	LLNotificationsUtil::add("SetDisplayNameFailedGeneric");
158}
159
160void LLFloaterDisplayName::onCancel()
161{
162	setVisible(false);
163}
164
165void LLFloaterDisplayName::onReset()
166{
167	if (LLAvatarNameCache::useDisplayNames())
168	{
169		LLViewerDisplayName::set("",
170			boost::bind(&LLFloaterDisplayName::onCacheSetName, this, _1, _2, _3));
171	}	
172	else
173	{
174		LLNotificationsUtil::add("SetDisplayNameFailedGeneric");
175	}
176	
177	setVisible(false);
178}
179
180
181void LLFloaterDisplayName::onSave()
182{
183	std::string display_name_utf8 = getChild<LLUICtrl>("display_name_editor")->getValue().asString();
184	std::string display_name_confirm = getChild<LLUICtrl>("display_name_confirm")->getValue().asString();
185
186	if (display_name_utf8.compare(display_name_confirm))
187	{
188		LLNotificationsUtil::add("SetDisplayNameMismatch");
189		return;
190	}
191
192	const U32 DISPLAY_NAME_MAX_LENGTH = 31; // characters, not bytes
193	LLWString display_name_wstr = utf8string_to_wstring(display_name_utf8);
194	if (display_name_wstr.size() > DISPLAY_NAME_MAX_LENGTH)
195	{
196		LLSD args;
197		args["LENGTH"] = llformat("%d", DISPLAY_NAME_MAX_LENGTH);
198		LLNotificationsUtil::add("SetDisplayNameFailedLength", args);
199		return;
200	}
201	
202	if (LLAvatarNameCache::useDisplayNames())
203	{
204		LLViewerDisplayName::set(display_name_utf8,
205			boost::bind(&LLFloaterDisplayName::onCacheSetName, this, _1, _2, _3));	
206	}
207	else
208	{
209		LLNotificationsUtil::add("SetDisplayNameFailedGeneric");
210	}
211
212	setVisible(false);
213}
214
215
216//////////////////////////////////////////////////////////////////////////////
217// LLInspectObjectUtil
218//////////////////////////////////////////////////////////////////////////////
219void LLFloaterDisplayNameUtil::registerFloater()
220{
221	LLFloaterReg::add("display_name", "floater_display_name.xml",
222					  &LLFloaterReg::build<LLFloaterDisplayName>);
223}