PageRenderTime 52ms CodeModel.GetById 26ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llpostcard.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 155 lines | 94 code | 23 blank | 38 comment | 3 complexity | c952d0908bec916966d43bc2cf80195f MD5 | raw file
  1/** 
  2 * @file llpostcard.cpp
  3 * @brief Sending postcards.
  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 "llpostcard.h"
 30
 31#include "llvfile.h"
 32#include "llvfs.h"
 33#include "llviewerregion.h"
 34
 35#include "message.h"
 36
 37#include "llagent.h"
 38#include "llassetuploadresponders.h"
 39
 40///////////////////////////////////////////////////////////////////////////////
 41// misc
 42
 43static void postcard_upload_callback(const LLUUID& asset_id, void *user_data, S32 result, LLExtStat ext_status)
 44{
 45	LLSD* postcard_data = (LLSD*)user_data;
 46
 47	if (result)
 48	{
 49		// TODO: display the error messages in UI
 50		llwarns << "Failed to send postcard: " << LLAssetStorage::getErrorString(result) << llendl;
 51		LLPostCard::reportPostResult(false);
 52	}
 53	else
 54	{
 55		// only create the postcard once the upload succeeds
 56
 57		// request the postcard
 58		const LLSD& data = *postcard_data;
 59		LLMessageSystem* msg = gMessageSystem;
 60		msg->newMessage("SendPostcard");
 61		msg->nextBlock("AgentData");
 62		msg->addUUID("AgentID",			gAgent.getID());
 63		msg->addUUID("SessionID",		gAgent.getSessionID());
 64		msg->addUUID("AssetID",			data["asset-id"].asUUID());
 65		msg->addVector3d("PosGlobal",	LLVector3d(data["pos-global"]));
 66		msg->addString("To",			data["to"]);
 67		msg->addString("From",			data["from"]);
 68		msg->addString("Name",			data["name"]);
 69		msg->addString("Subject",		data["subject"]);
 70		msg->addString("Msg",			data["msg"]);
 71		msg->addBOOL("AllowPublish",	FALSE);
 72		msg->addBOOL("MaturePublish",	FALSE);
 73		gAgent.sendReliableMessage();
 74
 75		LLPostCard::reportPostResult(true);
 76	}
 77
 78	delete postcard_data;
 79}
 80
 81
 82///////////////////////////////////////////////////////////////////////////////
 83// LLPostcardSendResponder
 84
 85class LLPostcardSendResponder : public LLAssetUploadResponder
 86{
 87	LOG_CLASS(LLPostcardSendResponder);
 88
 89public:
 90	LLPostcardSendResponder(const LLSD &post_data,
 91							const LLUUID& vfile_id,
 92							LLAssetType::EType asset_type):
 93	    LLAssetUploadResponder(post_data, vfile_id, asset_type)
 94	{
 95	}
 96
 97	/*virtual*/ void uploadComplete(const LLSD& content)
 98	{
 99		llinfos << "Postcard sent" << llendl;
100		LL_DEBUGS("Snapshots") << "content: " << content << llendl;
101		LLPostCard::reportPostResult(true);
102	}
103
104	/*virtual*/ void uploadFailure(const LLSD& content)
105	{
106		llwarns << "Sending postcard failed: " << content << llendl;
107		LLPostCard::reportPostResult(false);
108	}
109};
110
111///////////////////////////////////////////////////////////////////////////////
112// LLPostCard
113
114LLPostCard::result_callback_t LLPostCard::mResultCallback;
115
116// static
117void LLPostCard::send(LLPointer<LLImageFormatted> image, const LLSD& postcard_data)
118{
119	LLTransactionID transaction_id;
120	LLAssetID asset_id;
121
122	transaction_id.generate();
123	asset_id = transaction_id.makeAssetID(gAgent.getSecureSessionID());
124	LLVFile::writeFile(image->getData(), image->getDataSize(), gVFS, asset_id, LLAssetType::AT_IMAGE_JPEG);
125
126	// upload the image
127	std::string url = gAgent.getRegion()->getCapability("SendPostcard");
128	if (!url.empty())
129	{
130		llinfos << "Sending postcard via capability" << llendl;
131		// the capability already encodes: agent ID, region ID
132		LL_DEBUGS("Snapshots") << "url: " << url << llendl;
133		LL_DEBUGS("Snapshots") << "body: " << postcard_data << llendl;
134		LL_DEBUGS("Snapshots") << "data size: " << image->getDataSize() << llendl;
135		LLHTTPClient::post(url, postcard_data,
136			new LLPostcardSendResponder(postcard_data, asset_id, LLAssetType::AT_IMAGE_JPEG));
137	}
138	else
139	{
140		llinfos << "Sending postcard" << llendl;
141		LLSD* data = new LLSD(postcard_data);
142		(*data)["asset-id"] = asset_id;
143		gAssetStorage->storeAssetData(transaction_id, LLAssetType::AT_IMAGE_JPEG,
144			&postcard_upload_callback, (void *)data, FALSE);
145	}
146}
147
148// static
149void LLPostCard::reportPostResult(bool ok)
150{
151	if (mResultCallback)
152	{
153		mResultCallback(ok);
154	}
155}