PageRenderTime 146ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

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