llnotificationhandlerutil.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:9k
源码类别:

游戏引擎

开发平台:

C++ Builder

  1. /**
  2.  * @file llnotificationofferhandler.cpp
  3.  * @brief Provides set of utility methods for notifications processing.
  4.  *
  5.  * $LicenseInfo:firstyear=2000&license=viewergpl$
  6.  * 
  7.  * Copyright (c) 2000-2010, Linden Research, Inc.
  8.  * 
  9.  * Second Life Viewer Source Code
  10.  * The source code in this file ("Source Code") is provided by Linden Lab
  11.  * to you under the terms of the GNU General Public License, version 2.0
  12.  * ("GPL"), unless you have obtained a separate licensing agreement
  13.  * ("Other License"), formally executed by you and Linden Lab.  Terms of
  14.  * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15.  * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16.  * 
  17.  * There are special exceptions to the terms and conditions of the GPL as
  18.  * it is applied to this Source Code. View the full text of the exception
  19.  * in the file doc/FLOSS-exception.txt in this software distribution, or
  20.  * online at
  21.  * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22.  * 
  23.  * By copying, modifying or distributing this software, you acknowledge
  24.  * that you have read and understood your obligations described above,
  25.  * and agree to abide by those obligations.
  26.  * 
  27.  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28.  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29.  * COMPLETENESS OR PERFORMANCE.
  30.  * $/LicenseInfo$
  31.  */
  32. #include "llviewerprecompiledheaders.h" // must be first include
  33. #include "llnotificationhandler.h"
  34. #include "llnotifications.h"
  35. #include "llimview.h"
  36. #include "llagent.h"
  37. #include "llfloaterreg.h"
  38. #include "llnearbychat.h"
  39. #include "llimfloater.h"
  40. using namespace LLNotificationsUI;
  41. const static std::string GRANTED_MODIFY_RIGHTS("GrantedModifyRights"),
  42. REVOKED_MODIFY_RIGHTS("RevokedModifyRights"), OBJECT_GIVE_ITEM(
  43. "ObjectGiveItem"), OBJECT_GIVE_ITEM_UNKNOWN_USER(
  44. "ObjectGiveItemUnknownUser"), PAYMENT_RECIVED("PaymentRecived"),
  45. ADD_FRIEND_WITH_MESSAGE("AddFriendWithMessage"),
  46. USER_GIVE_ITEM("UserGiveItem"),
  47. INVENTORY_ACCEPTED("InventoryAccepted"),
  48. INVENTORY_DECLINED("InventoryDeclined"),
  49. OFFER_FRIENDSHIP("OfferFriendship"),
  50. FRIENDSHIP_ACCEPTED("FriendshipAccepted"),
  51. FRIENDSHIP_OFFERED("FriendshipOffered"),
  52. FRIEND_ONLINE("FriendOnline"), FRIEND_OFFLINE("FriendOffline"),
  53. SERVER_OBJECT_MESSAGE("ServerObjectMessage"),
  54. TELEPORT_OFFERED("TeleportOffered");
  55. // static
  56. bool LLHandlerUtil::canLogToIM(const LLNotificationPtr& notification)
  57. {
  58. return GRANTED_MODIFY_RIGHTS == notification->getName()
  59. || REVOKED_MODIFY_RIGHTS == notification->getName()
  60. || PAYMENT_RECIVED == notification->getName()
  61. || OFFER_FRIENDSHIP == notification->getName()
  62. || FRIENDSHIP_OFFERED == notification->getName()
  63. || SERVER_OBJECT_MESSAGE == notification->getName()
  64. || INVENTORY_ACCEPTED == notification->getName()
  65. || INVENTORY_DECLINED == notification->getName();
  66. }
  67. // static
  68. bool LLHandlerUtil::canLogToNearbyChat(const LLNotificationPtr& notification)
  69. {
  70. return notification->getType() == "notifytip"
  71. &&  FRIEND_ONLINE != notification->getName()
  72. && FRIEND_OFFLINE != notification->getName()
  73. && INVENTORY_ACCEPTED != notification->getName()
  74. && INVENTORY_DECLINED != notification->getName();
  75. }
  76. // static
  77. bool LLHandlerUtil::canSpawnIMSession(const LLNotificationPtr& notification)
  78. {
  79. return OFFER_FRIENDSHIP == notification->getName()
  80. || FRIENDSHIP_ACCEPTED == notification->getName()
  81. || USER_GIVE_ITEM == notification->getName()
  82. || INVENTORY_ACCEPTED == notification->getName()
  83. || INVENTORY_DECLINED == notification->getName();
  84. }
  85. // static
  86. bool LLHandlerUtil::canAddNotifPanelToIM(const LLNotificationPtr& notification)
  87. {
  88. return OFFER_FRIENDSHIP == notification->getName()
  89. || USER_GIVE_ITEM == notification->getName();
  90. }
  91. // static
  92. bool LLHandlerUtil::canSpawnSessionAndLogToIM(const LLNotificationPtr& notification)
  93. {
  94. return canLogToIM(notification) && canSpawnIMSession(notification);
  95. }
  96. // static
  97. void LLHandlerUtil::logToIM(const EInstantMessage& session_type,
  98. const std::string& session_name, const std::string& from_name,
  99. const std::string& message, const LLUUID& session_owner_id,
  100. const LLUUID& from_id)
  101. {
  102. LLUUID session_id = LLIMMgr::computeSessionID(session_type,
  103. session_owner_id);
  104. LLIMModel::LLIMSession* session = LLIMModel::instance().findIMSession(
  105. session_id);
  106. if (session == NULL)
  107. {
  108. LLIMModel::instance().logToFile(session_name, from_name, from_id, message);
  109. }
  110. else
  111. {
  112. // store active session id
  113. const LLUUID & active_session_id =
  114. LLIMModel::instance().getActiveSessionID();
  115. // set searched session as active to avoid IM toast popup
  116. LLIMModel::instance().setActiveSessionID(session_id);
  117. LLIMModel::instance().addMessage(session_id, from_name, from_id,
  118. message);
  119. // restore active session id
  120. if (active_session_id.isNull())
  121. {
  122. LLIMModel::instance().resetActiveSessionID();
  123. }
  124. else
  125. {
  126. LLIMModel::instance().setActiveSessionID(active_session_id);
  127. }
  128. }
  129. }
  130. // static
  131. void LLHandlerUtil::logToIMP2P(const LLNotificationPtr& notification)
  132. {
  133. logToIMP2P(notification, false);
  134. }
  135. // static
  136. void LLHandlerUtil::logToIMP2P(const LLNotificationPtr& notification, bool to_file_only)
  137. {
  138. const std::string name = LLHandlerUtil::getSubstitutionName(notification);
  139. std::string session_name = notification->getPayload().has(
  140. "SESSION_NAME") ? notification->getPayload()["SESSION_NAME"].asString() : name;
  141. // don't create IM p2p session with objects, it's necessary condition to log
  142. if (notification->getName() != OBJECT_GIVE_ITEM && notification->getName()
  143. != OBJECT_GIVE_ITEM_UNKNOWN_USER)
  144. {
  145. LLUUID from_id = notification->getPayload()["from_id"];
  146. //*HACK for ServerObjectMessage the sesson name is really weird, see EXT-4779
  147. if (SERVER_OBJECT_MESSAGE == notification->getName())
  148. {
  149. session_name = "chat";
  150. }
  151. //there still appears a log history file with weird name " .txt"
  152. if (" " == session_name || "{waiting}" == session_name || "{nobody}" == session_name)
  153. {
  154. llwarning("Weird session name (" + session_name + ") for notification " + notification->getName(), 666)
  155. }
  156. if(to_file_only)
  157. {
  158. logToIM(IM_NOTHING_SPECIAL, session_name, name, notification->getMessage(),
  159. LLUUID(), LLUUID());
  160. }
  161. else
  162. {
  163. logToIM(IM_NOTHING_SPECIAL, session_name, name, notification->getMessage(),
  164. from_id, from_id);
  165. }
  166. }
  167. }
  168. // static
  169. void LLHandlerUtil::logGroupNoticeToIMGroup(
  170. const LLNotificationPtr& notification)
  171. {
  172. const LLSD& payload = notification->getPayload();
  173. LLGroupData groupData;
  174. if (!gAgent.getGroupData(payload["group_id"].asUUID(), groupData))
  175. {
  176. llwarns
  177. << "Group notice for unkown group: "
  178. << payload["group_id"].asUUID() << llendl;
  179. }
  180. const std::string group_name = groupData.mName;
  181. const std::string sender_name = payload["sender_name"].asString();
  182. // we can't retrieve sender id from group notice system message, so try to lookup it from cache
  183. LLUUID sender_id;
  184. gCacheName->getUUID(sender_name, sender_id);
  185. logToIM(IM_SESSION_GROUP_START, group_name, sender_name, payload["message"],
  186. payload["group_id"], sender_id);
  187. }
  188. // static
  189. void LLHandlerUtil::logToNearbyChat(const LLNotificationPtr& notification, EChatSourceType type)
  190. {
  191. LLNearbyChat* nearby_chat = LLFloaterReg::getTypedInstance<LLNearbyChat>("nearby_chat", LLSD());
  192. if(nearby_chat)
  193. {
  194. LLChat chat_msg(notification->getMessage());
  195. chat_msg.mSourceType = type;
  196. chat_msg.mFromName = SYSTEM_FROM;
  197. nearby_chat->addMessage(chat_msg);
  198. }
  199. }
  200. // static
  201. LLUUID LLHandlerUtil::spawnIMSession(const std::string& name, const LLUUID& from_id)
  202. {
  203. LLUUID session_id = LLIMMgr::computeSessionID(IM_NOTHING_SPECIAL, from_id);
  204. LLIMModel::LLIMSession* session = LLIMModel::instance().findIMSession(
  205. session_id);
  206. if (session == NULL)
  207. {
  208. session_id = LLIMMgr::instance().addSession(name, IM_NOTHING_SPECIAL, from_id);
  209. }
  210. return session_id;
  211. }
  212. // static
  213. std::string LLHandlerUtil::getSubstitutionName(const LLNotificationPtr& notification)
  214. {
  215. return notification->getSubstitutions().has("NAME")
  216. ? notification->getSubstitutions()["NAME"]
  217. : notification->getSubstitutions()["[NAME]"];
  218. }
  219. // static
  220. void LLHandlerUtil::addNotifPanelToIM(const LLNotificationPtr& notification)
  221. {
  222. const std::string name = LLHandlerUtil::getSubstitutionName(notification);
  223. LLUUID from_id = notification->getPayload()["from_id"];
  224. LLUUID session_id = spawnIMSession(name, from_id);
  225. // add offer to session
  226. LLIMModel::LLIMSession * session = LLIMModel::getInstance()->findIMSession(
  227. session_id);
  228. llassert_always(session != NULL);
  229. LLSD offer;
  230. offer["notification_id"] = notification->getID();
  231. offer["from_id"] = notification->getPayload()["from_id"];
  232. offer["from"] = name;
  233. offer["time"] = LLLogChat::timestamp(true);
  234. offer["index"] = (LLSD::Integer)session->mMsgs.size();
  235. session->mMsgs.push_front(offer);
  236. LLIMFloater::show(session_id);
  237. }