PageRenderTime 32ms CodeModel.GetById 14ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/newview/lleventnotifier.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 278 lines | 193 code | 51 blank | 34 comment | 25 complexity | ddb8ebdbfc1173971a833ca65d95af52 MD5 | raw file
  1/** 
  2 * @file lleventnotifier.cpp
  3 * @brief Viewer code for managing event notifications
  4 *
  5 * $LicenseInfo:firstyear=2004&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, 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 "lleventnotifier.h"
 30
 31#include "llnotificationsutil.h"
 32#include "message.h"
 33
 34#include "llfloaterreg.h"
 35#include "llfloaterworldmap.h"
 36#include "llfloaterevent.h"
 37#include "llagent.h"
 38#include "llcommandhandler.h"	// secondlife:///app/... support
 39
 40class LLEventHandler : public LLCommandHandler
 41{
 42public:
 43	// requires trusted browser to trigger
 44	LLEventHandler() : LLCommandHandler("event", UNTRUSTED_THROTTLE) { }
 45	bool handle(const LLSD& params, const LLSD& query_map,
 46				LLMediaCtrl* web)
 47	{
 48		if (params.size() < 2)
 49		{
 50			return false;
 51		}
 52		std::string event_command = params[1].asString();
 53		S32 event_id = params[0].asInteger();
 54		if(event_command == "details")
 55		{
 56			LLFloaterEvent* floater = LLFloaterReg::getTypedInstance<LLFloaterEvent>("event");
 57			if (floater)
 58			{
 59				floater->setEventID(event_id);
 60				LLFloaterReg::showTypedInstance<LLFloaterEvent>("event");
 61				return true;
 62			}
 63		}
 64		else if(event_command == "notify")
 65		{
 66			// we're adding or removing a notification, so grab the date, name and notification bool
 67			if (params.size() < 3)
 68			{
 69				return false;
 70			}			
 71			if(params[2].asString() == "enable")
 72			{
 73				gEventNotifier.add(event_id);
 74				// tell the server to modify the database as this was a slurl event notification command
 75				gEventNotifier.serverPushRequest(event_id, true);
 76			
 77			}
 78			else
 79			{
 80				gEventNotifier.remove(event_id);
 81			}
 82			return true;
 83		}
 84
 85		
 86		return false;
 87	}
 88};
 89LLEventHandler gEventHandler;
 90
 91
 92LLEventNotifier gEventNotifier;
 93
 94LLEventNotifier::LLEventNotifier()
 95{
 96}
 97
 98
 99LLEventNotifier::~LLEventNotifier()
100{
101	en_map::iterator iter;
102
103	for (iter = mEventNotifications.begin();
104		 iter != mEventNotifications.end();
105		 iter++)
106	{
107		delete iter->second;
108	}
109}
110
111
112void LLEventNotifier::update()
113{
114	if (mNotificationTimer.getElapsedTimeF32() > 30.f)
115	{
116		// Check our notifications again and send out updates
117		// if they happen.
118
119		F64 alert_time = LLDate::now().secondsSinceEpoch() + 5 * 60;
120		en_map::iterator iter;
121		for (iter = mEventNotifications.begin();
122			 iter != mEventNotifications.end();)
123		{
124			LLEventNotification *np = iter->second;
125
126			iter++;
127			if (np->getEventDateEpoch() < alert_time)
128			{
129				LLSD args;
130				args["NAME"] = np->getEventName();
131				
132				args["DATE"] = np->getEventDateStr();
133				LLNotificationsUtil::add("EventNotification", args, LLSD(),
134					boost::bind(&LLEventNotifier::handleResponse, this, np->getEventID(), _1, _2));
135				remove(np->getEventID());
136				
137			}
138		}
139		mNotificationTimer.reset();
140	}
141}
142
143
144
145bool LLEventNotifier::handleResponse(U32 eventId, const LLSD& notification, const LLSD& response)
146{
147	S32 option = LLNotificationsUtil::getSelectedOption(notification, response);
148	switch (option)
149	{
150		case 0:
151		{
152			LLFloaterEvent* floater = LLFloaterReg::getTypedInstance<LLFloaterEvent>("event");
153			if (floater)
154			{
155				floater->setEventID(eventId);
156				LLFloaterReg::showTypedInstance<LLFloaterEvent>("event");
157			}
158			break;
159		}
160		case 1:
161			break;
162	}
163	return true;
164}
165
166bool LLEventNotifier::add(U32 eventId, F64 eventEpoch, const std::string& eventDateStr, const std::string &eventName)
167{
168	LLEventNotification *new_enp = new LLEventNotification(eventId, eventEpoch, eventDateStr, eventName);
169	
170	llinfos << "Add event " << eventName << " id " << eventId << " date " << eventDateStr << llendl;
171	if(!new_enp->isValid())
172	{
173		delete new_enp;
174		return false;
175	}
176	
177	mEventNotifications[new_enp->getEventID()] = new_enp;
178	return true;
179	
180}
181
182void LLEventNotifier::add(U32 eventId)
183{
184	
185	gMessageSystem->newMessageFast(_PREHASH_EventInfoRequest);
186	gMessageSystem->nextBlockFast(_PREHASH_AgentData);
187	gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
188	gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID() );
189	gMessageSystem->nextBlockFast(_PREHASH_EventData);
190	gMessageSystem->addU32Fast(_PREHASH_EventID, eventId);
191	gAgent.sendReliableMessage();
192
193}
194
195//static 
196void LLEventNotifier::processEventInfoReply(LLMessageSystem *msg, void **)
197{
198	// extract the agent id
199	LLUUID agent_id;
200	U32 event_id;
201	std::string event_name;
202	std::string eventd_date;
203	U32 event_time_utc;
204	
205	msg->getUUIDFast(_PREHASH_AgentData, _PREHASH_AgentID, agent_id );
206	msg->getU32("EventData", "EventID", event_id);
207	msg->getString("EventData", "Name", event_name);
208	msg->getString("EventData", "Date", eventd_date);
209	msg->getU32("EventData", "DateUTC", event_time_utc);
210	
211	gEventNotifier.add(event_id, (F64)event_time_utc, eventd_date, event_name);
212}	
213	
214	
215void LLEventNotifier::load(const LLSD& event_options)
216{
217	for(LLSD::array_const_iterator resp_it = event_options.beginArray(),
218		end = event_options.endArray(); resp_it != end; ++resp_it)
219	{
220		LLSD response = *resp_it;
221
222		add(response["event_id"].asInteger(), response["event_date_ut"], response["event_date"].asString(), response["event_name"].asString());
223	}
224}
225
226
227BOOL LLEventNotifier::hasNotification(const U32 event_id)
228{
229	if (mEventNotifications.find(event_id) != mEventNotifications.end())
230	{
231		return TRUE;
232	}
233	return FALSE;
234}
235
236void LLEventNotifier::remove(const U32 event_id)
237{
238	en_map::iterator iter;
239	iter = mEventNotifications.find(event_id);
240	if (iter == mEventNotifications.end())
241	{
242		// We don't have a notification for this event, don't bother.
243		return;
244	}
245
246	serverPushRequest(event_id, false);
247	delete iter->second;
248	mEventNotifications.erase(iter);
249}
250
251
252void LLEventNotifier::serverPushRequest(U32 event_id, bool add)
253{
254	// Push up a message to tell the server we have this notification.
255	gMessageSystem->newMessage(add?"EventNotificationAddRequest":"EventNotificationRemoveRequest");
256	gMessageSystem->nextBlockFast(_PREHASH_AgentData);
257	gMessageSystem->addUUIDFast(_PREHASH_AgentID, gAgent.getID() );
258	gMessageSystem->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
259	gMessageSystem->nextBlock("EventData");
260	gMessageSystem->addU32("EventID", event_id);
261	gAgent.sendReliableMessage();
262}
263
264
265LLEventNotification::LLEventNotification(U32 eventId, F64 eventEpoch, const std::string& eventDateStr, const std::string &eventName) :
266	mEventID(eventId),
267	mEventName(eventName),
268	mEventDateEpoch(eventEpoch),
269    mEventDateStr(eventDateStr)
270{
271	
272}
273
274
275
276
277
278