PageRenderTime 223ms CodeModel.GetById 2ms app.highlight 57ms RepoModel.GetById 1ms app.codeStats 1ms

/indra/newview/llnotificationhandlerutil.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 507 lines | 375 code | 69 blank | 63 comment | 112 complexity | c7a0920330ae7e45b7fa6ec397f177ef MD5 | raw file
  1/**
  2 * @file llnotificationofferhandler.cpp
  3 * @brief Provides set of utility methods for notifications processing.
  4 *
  5 * $LicenseInfo:firstyear=2000&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
 28#include "llviewerprecompiledheaders.h" // must be first include
 29
 30#include "llavatarnamecache.h"
 31
 32#include "llfloaterreg.h"
 33#include "llnotifications.h"
 34#include "llurlaction.h"
 35
 36#include "llagent.h"
 37#include "llimfloater.h"
 38#include "llimview.h"
 39#include "llnearbychat.h"
 40#include "llnotificationhandler.h"
 41
 42using namespace LLNotificationsUI;
 43
 44// static
 45std::list< std::set<std::string> > LLSysHandler::sExclusiveNotificationGroups;
 46
 47// static
 48void LLSysHandler::init()
 49{
 50	std::set<std::string> online_offline_group;
 51	online_offline_group.insert("FriendOnline");
 52	online_offline_group.insert("FriendOffline");
 53
 54	sExclusiveNotificationGroups.push_back(online_offline_group);
 55}
 56
 57LLSysHandler::LLSysHandler()
 58{
 59	if(sExclusiveNotificationGroups.empty())
 60	{
 61		init();
 62	}
 63}
 64
 65void LLSysHandler::removeExclusiveNotifications(const LLNotificationPtr& notif)
 66{
 67	LLScreenChannel* channel = dynamic_cast<LLScreenChannel *>(mChannel);
 68	if (channel == NULL)
 69	{
 70		return;
 71	}
 72
 73	class ExclusiveMatcher: public LLScreenChannel::Matcher
 74	{
 75	public:
 76		ExclusiveMatcher(const std::set<std::string>& excl_group,
 77				const std::string& from_name) :
 78			mExclGroup(excl_group), mFromName(from_name)
 79		{
 80		}
 81		bool matches(const LLNotificationPtr notification) const
 82		{
 83			for (std::set<std::string>::const_iterator it = mExclGroup.begin(); it
 84					!= mExclGroup.end(); it++)
 85			{
 86				std::string from_name = LLHandlerUtil::getSubstitutionName(notification);
 87				if (notification->getName() == *it && from_name == mFromName)
 88				{
 89					return true;
 90				}
 91			}
 92			return false;
 93		}
 94	private:
 95		const std::set<std::string>& mExclGroup;
 96		const std::string& mFromName;
 97	};
 98
 99
100	for (exclusive_notif_sets::iterator it = sExclusiveNotificationGroups.begin(); it
101			!= sExclusiveNotificationGroups.end(); it++)
102	{
103		std::set<std::string> group = *it;
104		std::set<std::string>::iterator g_it = group.find(notif->getName());
105		if (g_it != group.end())
106		{
107			channel->killMatchedToasts(ExclusiveMatcher(group,
108					LLHandlerUtil::getSubstitutionName(notif)));
109		}
110	}
111}
112
113const static std::string GRANTED_MODIFY_RIGHTS("GrantedModifyRights"),
114		REVOKED_MODIFY_RIGHTS("RevokedModifyRights"),
115		OBJECT_GIVE_ITEM("ObjectGiveItem"),
116		OBJECT_GIVE_ITEM_UNKNOWN_USER("ObjectGiveItemUnknownUser"),
117						PAYMENT_RECEIVED("PaymentReceived"),
118						PAYMENT_SENT("PaymentSent"),
119						ADD_FRIEND_WITH_MESSAGE("AddFriendWithMessage"),
120						USER_GIVE_ITEM("UserGiveItem"),
121						INVENTORY_ACCEPTED("InventoryAccepted"),
122						INVENTORY_DECLINED("InventoryDeclined"),
123						OFFER_FRIENDSHIP("OfferFriendship"),
124						FRIENDSHIP_ACCEPTED("FriendshipAccepted"),
125						FRIENDSHIP_OFFERED("FriendshipOffered"),
126						FRIENDSHIP_ACCEPTED_BYME("FriendshipAcceptedByMe"),
127						FRIENDSHIP_DECLINED_BYME("FriendshipDeclinedByMe"),
128						FRIEND_ONLINE("FriendOnline"), FRIEND_OFFLINE("FriendOffline"),
129						SERVER_OBJECT_MESSAGE("ServerObjectMessage"),
130						TELEPORT_OFFERED("TeleportOffered"),
131						TELEPORT_OFFER_SENT("TeleportOfferSent"),
132						IM_SYSTEM_MESSAGE_TIP("IMSystemMessageTip");
133
134
135// static
136bool LLHandlerUtil::canLogToIM(const LLNotificationPtr& notification)
137{
138	return GRANTED_MODIFY_RIGHTS == notification->getName()
139			|| REVOKED_MODIFY_RIGHTS == notification->getName()
140			|| PAYMENT_RECEIVED == notification->getName()
141			|| PAYMENT_SENT == notification->getName()
142			|| OFFER_FRIENDSHIP == notification->getName()
143			|| FRIENDSHIP_OFFERED == notification->getName()
144			|| FRIENDSHIP_ACCEPTED == notification->getName()
145			|| FRIENDSHIP_ACCEPTED_BYME == notification->getName()
146			|| FRIENDSHIP_DECLINED_BYME == notification->getName()
147			|| SERVER_OBJECT_MESSAGE == notification->getName()
148			|| INVENTORY_ACCEPTED == notification->getName()
149			|| INVENTORY_DECLINED == notification->getName()
150			|| USER_GIVE_ITEM == notification->getName()
151			|| TELEPORT_OFFERED == notification->getName()
152			|| TELEPORT_OFFER_SENT == notification->getName()
153			|| IM_SYSTEM_MESSAGE_TIP == notification->getName();
154}
155
156// static
157bool LLHandlerUtil::canLogToNearbyChat(const LLNotificationPtr& notification)
158{
159	return notification->getType() == "notifytip"
160			&&  FRIEND_ONLINE != notification->getName()
161			&& FRIEND_OFFLINE != notification->getName()
162			&& INVENTORY_ACCEPTED != notification->getName()
163			&& INVENTORY_DECLINED != notification->getName()
164			&& IM_SYSTEM_MESSAGE_TIP != notification->getName();
165}
166
167// static
168bool LLHandlerUtil::canSpawnIMSession(const LLNotificationPtr& notification)
169{
170	return OFFER_FRIENDSHIP == notification->getName()
171			|| USER_GIVE_ITEM == notification->getName()
172			|| TELEPORT_OFFERED == notification->getName();
173}
174
175// static
176bool LLHandlerUtil::canAddNotifPanelToIM(const LLNotificationPtr& notification)
177{
178	return OFFER_FRIENDSHIP == notification->getName()
179					|| USER_GIVE_ITEM == notification->getName()
180					|| TELEPORT_OFFERED == notification->getName();
181}
182
183// static
184bool LLHandlerUtil::isNotificationReusable(const LLNotificationPtr& notification)
185{
186	return OFFER_FRIENDSHIP == notification->getName()
187		|| USER_GIVE_ITEM == notification->getName()
188		|| TELEPORT_OFFERED == notification->getName();
189}
190
191// static
192bool LLHandlerUtil::canSpawnSessionAndLogToIM(const LLNotificationPtr& notification)
193{
194	return canLogToIM(notification) && canSpawnIMSession(notification);
195}
196
197// static
198bool LLHandlerUtil::canSpawnToast(const LLNotificationPtr& notification)
199{
200	if(INVENTORY_DECLINED == notification->getName() 
201		|| INVENTORY_ACCEPTED == notification->getName())
202	{
203		// return false for inventory accepted/declined notifications if respective IM window is open (EXT-5909)
204		return ! isIMFloaterOpened(notification);
205	}
206
207	if(FRIENDSHIP_ACCEPTED == notification->getName())
208	{
209		// don't show FRIENDSHIP_ACCEPTED if IM window is opened and focused - EXT-6441
210		return ! isIMFloaterFocused(notification);
211	}
212
213	if(OFFER_FRIENDSHIP == notification->getName()
214		|| USER_GIVE_ITEM == notification->getName()
215		|| TELEPORT_OFFERED == notification->getName())
216	{
217		// When ANY offer arrives, show toast, unless IM window is already open - EXT-5904
218		return ! isIMFloaterOpened(notification);
219	}
220
221	return true;
222}
223
224// static
225LLIMFloater* LLHandlerUtil::findIMFloater(const LLNotificationPtr& notification)
226{
227	LLUUID from_id = notification->getPayload()["from_id"];
228	LLUUID session_id = LLIMMgr::computeSessionID(IM_NOTHING_SPECIAL, from_id);
229	return LLFloaterReg::findTypedInstance<LLIMFloater>("impanel", session_id);
230}
231
232// static
233bool LLHandlerUtil::isIMFloaterOpened(const LLNotificationPtr& notification)
234{
235	bool res = false;
236
237	LLIMFloater* im_floater = findIMFloater(notification);
238	if (im_floater != NULL)
239	{
240		res = im_floater->getVisible() == TRUE;
241	}
242
243	return res;
244}
245
246bool LLHandlerUtil::isIMFloaterFocused(const LLNotificationPtr& notification)
247{
248	bool res = false;
249
250	LLIMFloater* im_floater = findIMFloater(notification);
251	if (im_floater != NULL)
252	{
253		res = im_floater->hasFocus() == TRUE;
254	}
255
256	return res;
257}
258
259// static
260void LLHandlerUtil::logToIM(const EInstantMessage& session_type,
261		const std::string& session_name, const std::string& from_name,
262		const std::string& message, const LLUUID& session_owner_id,
263		const LLUUID& from_id)
264{
265	std::string from = from_name;
266	if (from_name.empty())
267	{
268		from = SYSTEM_FROM;
269	}
270
271	LLUUID session_id = LLIMMgr::computeSessionID(session_type,
272			session_owner_id);
273	LLIMModel::LLIMSession* session = LLIMModel::instance().findIMSession(
274			session_id);
275	if (session == NULL)
276	{
277		// replace interactive system message marker with correct from string value
278		if (INTERACTIVE_SYSTEM_FROM == from_name)
279		{
280			from = SYSTEM_FROM;
281		}
282
283		// Build a new format username or firstname_lastname for legacy names
284		// to use it for a history log filename.
285		std::string user_name = LLCacheName::buildUsername(session_name);
286		LLIMModel::instance().logToFile(user_name, from, from_id, message);
287	}
288	else
289	{
290		// store active session id
291		const LLUUID & active_session_id =
292				LLIMModel::instance().getActiveSessionID();
293
294		// set searched session as active to avoid IM toast popup
295		LLIMModel::instance().setActiveSessionID(session_id);
296
297		S32 unread = session->mNumUnread;
298		S32 participant_unread = session->mParticipantUnreadMessageCount;
299		LLIMModel::instance().addMessageSilently(session_id, from, from_id,
300				message);
301		// we shouldn't increment counters when logging, so restore them
302		session->mNumUnread = unread;
303		session->mParticipantUnreadMessageCount = participant_unread;
304
305		// update IM floater messages
306		updateIMFLoaterMesages(session_id);
307
308		// restore active session id
309		if (active_session_id.isNull())
310		{
311			LLIMModel::instance().resetActiveSessionID();
312		}
313		else
314		{
315			LLIMModel::instance().setActiveSessionID(active_session_id);
316		}
317	}
318}
319
320// static
321void LLHandlerUtil::logToIMP2P(const LLNotificationPtr& notification)
322{
323	logToIMP2P(notification, false);
324}
325
326void log_name_callback(const std::string& full_name, const std::string& from_name, 
327					   const std::string& message, const LLUUID& from_id)
328
329{
330	LLHandlerUtil::logToIM(IM_NOTHING_SPECIAL, full_name, from_name, message,
331					from_id, LLUUID());
332}
333
334// static
335void LLHandlerUtil::logToIMP2P(const LLNotificationPtr& notification, bool to_file_only)
336{
337	// don't create IM p2p session with objects, it's necessary condition to log
338	if (notification->getName() != OBJECT_GIVE_ITEM)
339	{
340		LLUUID from_id = notification->getPayload()["from_id"];
341
342		if (from_id.isNull())
343		{
344			llwarns << " from_id for notification " << notification->getName() << " is null " << llendl;
345			return;
346		}
347
348		if(to_file_only)
349		{
350			gCacheName->get(from_id, false, boost::bind(&log_name_callback, _2, "", notification->getMessage(), LLUUID()));
351		}
352		else
353		{
354			gCacheName->get(from_id, false, boost::bind(&log_name_callback, _2, INTERACTIVE_SYSTEM_FROM, notification->getMessage(), from_id));
355		}
356	}
357}
358
359// static
360void LLHandlerUtil::logGroupNoticeToIMGroup(
361		const LLNotificationPtr& notification)
362{
363
364	const LLSD& payload = notification->getPayload();
365	LLGroupData groupData;
366	if (!gAgent.getGroupData(payload["group_id"].asUUID(), groupData))
367	{
368		llwarns
369						<< "Group notice for unknown group: "
370								<< payload["group_id"].asUUID() << llendl;
371		return;
372	}
373
374	const std::string group_name = groupData.mName;
375	const std::string sender_name = payload["sender_name"].asString();
376
377	// we can't retrieve sender id from group notice system message, so try to lookup it from cache
378	LLUUID sender_id;
379	gCacheName->getUUID(sender_name, sender_id);
380
381	logToIM(IM_SESSION_GROUP_START, group_name, sender_name, payload["message"],
382			payload["group_id"], sender_id);
383}
384
385// static
386void LLHandlerUtil::logToNearbyChat(const LLNotificationPtr& notification, EChatSourceType type)
387{
388	LLNearbyChat* nearby_chat = LLNearbyChat::getInstance();
389	if(nearby_chat)
390	{
391		LLChat chat_msg(notification->getMessage());
392		chat_msg.mSourceType = type;
393		chat_msg.mFromName = SYSTEM_FROM;
394		chat_msg.mFromID = LLUUID::null;
395		nearby_chat->addMessage(chat_msg);
396	}
397}
398
399// static
400LLUUID LLHandlerUtil::spawnIMSession(const std::string& name, const LLUUID& from_id)
401{
402	LLUUID session_id = LLIMMgr::computeSessionID(IM_NOTHING_SPECIAL, from_id);
403
404	LLIMModel::LLIMSession* session = LLIMModel::instance().findIMSession(
405			session_id);
406	if (session == NULL)
407	{
408		session_id = LLIMMgr::instance().addSession(name, IM_NOTHING_SPECIAL, from_id);
409	}
410
411	return session_id;
412}
413
414// static
415std::string LLHandlerUtil::getSubstitutionName(const LLNotificationPtr& notification)
416{
417	std::string res = notification->getSubstitutions().has("NAME")
418		? notification->getSubstitutions()["NAME"]
419		: notification->getSubstitutions()["[NAME]"];
420	if (res.empty())
421	{
422		LLUUID from_id = notification->getPayload()["FROM_ID"];
423
424		//*TODO all keys everywhere should be made of the same case, there is a mix of keys in lower and upper cases
425		if (from_id.isNull()) 
426		{
427			from_id = notification->getPayload()["from_id"];
428		}
429		if(!gCacheName->getFullName(from_id, res))
430		{
431			res = "";
432		}
433	}
434	return res;
435}
436
437// static
438void LLHandlerUtil::addNotifPanelToIM(const LLNotificationPtr& notification)
439{
440	const std::string name = LLHandlerUtil::getSubstitutionName(notification);
441	LLUUID from_id = notification->getPayload()["from_id"];
442
443	LLUUID session_id = spawnIMSession(name, from_id);
444	// add offer to session
445	LLIMModel::LLIMSession * session = LLIMModel::getInstance()->findIMSession(
446			session_id);
447	llassert_always(session != NULL);
448
449	LLSD offer;
450	offer["notification_id"] = notification->getID();
451	offer["from"] = SYSTEM_FROM;
452	offer["time"] = LLLogChat::timestamp(false);
453	offer["index"] = (LLSD::Integer)session->mMsgs.size();
454	session->mMsgs.push_front(offer);
455
456
457	// update IM floater and counters
458	LLSD arg;
459	arg["session_id"] = session_id;
460	arg["num_unread"] = ++(session->mNumUnread);
461	arg["participant_unread"] = ++(session->mParticipantUnreadMessageCount);
462	LLIMModel::getInstance()->mNewMsgSignal(arg);
463}
464
465// static
466void LLHandlerUtil::updateIMFLoaterMesages(const LLUUID& session_id)
467{
468	LLIMFloater* im_floater = LLIMFloater::findInstance(session_id);
469	if (im_floater != NULL && im_floater->getVisible())
470	{
471		im_floater->updateMessages();
472	}
473}
474
475// static
476void LLHandlerUtil::updateVisibleIMFLoaterMesages(const LLNotificationPtr& notification)
477{
478	const std::string name = LLHandlerUtil::getSubstitutionName(notification);
479	LLUUID from_id = notification->getPayload()["from_id"];
480	LLUUID session_id = spawnIMSession(name, from_id);
481
482	updateIMFLoaterMesages(session_id);
483}
484
485// static
486void LLHandlerUtil::decIMMesageCounter(const LLNotificationPtr& notification)
487{
488	const std::string name = LLHandlerUtil::getSubstitutionName(notification);
489	LLUUID from_id = notification->getPayload()["from_id"];
490	LLUUID session_id = LLIMMgr::computeSessionID(IM_NOTHING_SPECIAL, from_id);
491
492	LLIMModel::LLIMSession * session = LLIMModel::getInstance()->findIMSession(
493			session_id);
494
495	if (session == NULL)
496	{
497		return;
498	}
499
500	LLSD arg;
501	arg["session_id"] = session_id;
502	session->mNumUnread--;
503	arg["num_unread"] = session->mNumUnread;
504	session->mParticipantUnreadMessageCount--;
505	arg["participant_unread"] = session->mParticipantUnreadMessageCount;
506	LLIMModel::getInstance()->mNewMsgSignal(arg);
507}