llnotifications.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /**
  2. * @file llnotifications.h
  3. * @brief Non-UI manager and support for keeping a prioritized list of
  4. * notifications
  5. * @author Q (with assistance from Richard and Coco)
  6. *
  7. * $LicenseInfo:firstyear=2008&license=viewergpl$
  8. *
  9. * Copyright (c) 2008-2009, Linden Research, Inc.
  10. *
  11. * Second Life Viewer Source Code
  12. * The source code in this file ("Source Code") is provided by Linden Lab
  13. * to you under the terms of the GNU General Public License, version 2.0
  14. * ("GPL"), unless you have obtained a separate licensing agreement
  15. * ("Other License"), formally executed by you and Linden Lab. Terms of
  16. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  17. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  18. *
  19. * There are special exceptions to the terms and conditions of the GPL as
  20. * it is applied to this Source Code. View the full text of the exception
  21. * in the file doc/FLOSS-exception.txt in this software distribution, or
  22. * online at
  23. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  24. *
  25. * By copying, modifying or distributing this software, you acknowledge
  26. * that you have read and understood your obligations described above,
  27. * and agree to abide by those obligations.
  28. *
  29. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  30. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  31. * COMPLETENESS OR PERFORMANCE.
  32. * $/LicenseInfo$
  33. */
  34. #ifndef LL_LLNOTIFICATIONS_H
  35. #define LL_LLNOTIFICATIONS_H
  36. /**
  37. * This system is intended to provide a mechanism for adding notifications to
  38. * one of an arbitrary set of event channels.
  39. *
  40. * Controlling JIRA: DEV-9061
  41. *
  42. * Every notification has (see code for full list):
  43. * - a textual name, which is used to look up its template in the XML files
  44. * - a payload, which is a block of LLSD
  45. * - a channel, which is normally extracted from the XML files but
  46. * can be overridden.
  47. * - a timestamp, used to order the notifications
  48. * - expiration time -- if nonzero, specifies a time after which the
  49. * notification will no longer be valid.
  50. * - a callback name and a couple of status bits related to callbacks (see below)
  51. *
  52. * There is a management class called LLNotifications.
  53. * The class maintains a collection of all of the notifications received
  54. * or processed during this session, and also manages the persistence
  55. * of those notifications that must be persisted.
  56. *
  57. * We also have Channels. A channel is a view on a collection of notifications;
  58. * The collection is defined by a filter function that controls which
  59. * notifications are in the channel, and its ordering is controlled by
  60. * a comparator.
  61. *
  62. * There is a hierarchy of channels; notifications flow down from
  63. * the management class (LLNotifications, which itself inherits from
  64. * The channel base class) to the individual channels.
  65. * Any change to notifications (add, delete, modify) is
  66. * automatically propagated through the channel hierarchy.
  67. *
  68. * We provide methods for adding a new notification, for removing
  69. * one, and for managing channels. Channels are relatively cheap to construct
  70. * and maintain, so in general, human interfaces should use channels to
  71. * select and manage their lists of notifications.
  72. *
  73. * We also maintain a collection of templates that are loaded from the
  74. * XML file of template translations. The system supports substitution
  75. * of named variables from the payload into the XML file.
  76. *
  77. * By default, only the "unknown message" template is built into the system.
  78. * It is not an error to add a notification that's not found in the
  79. * template system, but it is logged.
  80. *
  81. */
  82. #include <iomanip>
  83. #include <list>
  84. #include <map>
  85. #include <memory>
  86. #include <set>
  87. #include <sstream>
  88. #include <string>
  89. #include <vector>
  90. #include "boost/utility.hpp"
  91. #include "boost/type_traits.hpp"
  92. #include "llfunctorregistry.h"
  93. #include "llinstancetracker.h"
  94. #include "llsd.h"
  95. #include "llui.h"
  96. #include "llxmlnode.h"
  97. class LLNotification;
  98. typedef std::shared_ptr<LLNotification> LLNotificationPtr;
  99. /*****************************************************************************
  100. * Signal and handler declarations
  101. * Using a single handler signature means that we can have a common handler
  102. * type, rather than needing a distinct one for each different handler.
  103. *****************************************************************************/
  104. /**
  105. * A boost::signals2 Combiner that stops the first time a handler returns true
  106. * We need this because we want to have our handlers return bool, so that we
  107. * have the option to cause a handler to stop further processing. The default
  108. * handler fails when the signal returns a value but has no slots.
  109. */
  110. struct LLStopWhenNotificationHandled
  111. {
  112. typedef bool result_type;
  113. template<typename InputIterator>
  114. result_type operator()(InputIterator first, InputIterator last) const
  115. {
  116. for (InputIterator si = first; si != last; ++si)
  117. {
  118. if (*si)
  119. {
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. };
  126. typedef enum e_notification_priority
  127. {
  128. NOTIFICATION_PRIORITY_UNSPECIFIED,
  129. NOTIFICATION_PRIORITY_LOW,
  130. NOTIFICATION_PRIORITY_NORMAL,
  131. NOTIFICATION_PRIORITY_HIGH,
  132. NOTIFICATION_PRIORITY_CRITICAL
  133. } ENotificationPriority;
  134. /**
  135. * We want to have a standard signature for all signals; this way, we can
  136. * easily document a protocol for communicating across dlls and into
  137. * scripting languages someday. We want to return a bool to indicate whether
  138. * the signal has been handled and should NOT be passed on to other listeners
  139. * Return true to stop further handling of the signal, and false to continue.
  140. * We take an LLSD because this way the contents of the signal are independent
  141. * of the API used to communicate it.
  142. * It is const ref because then there's low cost to pass it;
  143. * if you only need to inspect it, it's very cheap.
  144. */
  145. typedef boost::function<void (const LLSD&, const LLSD&)> LLNotificationResponder;
  146. typedef LLFunctorRegistry<LLNotificationResponder> LLNotificationFunctorRegistry;
  147. typedef LLFunctorRegistration<LLNotificationResponder> LLNotificationFunctorRegistration;
  148. typedef boost::signals2::signal<bool(const LLSD&),
  149. LLStopWhenNotificationHandled> LLStandardNotificationSignal;
  150. // Context data that can be looked up via a notification's payload by the
  151. // display logic; derive from this class to implement specific contexts.
  152. class LLNotificationContext : public LLInstanceTracker<LLNotificationContext, LLUUID>
  153. {
  154. public:
  155. LLNotificationContext()
  156. : LLInstanceTracker<LLNotificationContext, LLUUID>(LLUUID::generateNewID())
  157. {
  158. }
  159. LL_INLINE LLSD asLLSD() const { return getKey(); }
  160. };
  161. // Contains notification form data, such as buttons and text fields along with
  162. // manipulator functions
  163. class LLNotificationForm
  164. {
  165. protected:
  166. LOG_CLASS(LLNotificationForm);
  167. public:
  168. typedef enum e_ignore_type
  169. {
  170. IGNORE_NO,
  171. IGNORE_WITH_DEFAULT_RESPONSE,
  172. IGNORE_WITH_LAST_RESPONSE,
  173. IGNORE_SHOW_AGAIN
  174. } EIgnoreType;
  175. LLNotificationForm();
  176. LLNotificationForm(const LLSD& sd);
  177. LLNotificationForm(const std::string& name, const LLXMLNodePtr xml_node);
  178. LL_INLINE LLSD asLLSD() const { return mFormData; }
  179. LL_INLINE S32 getNumElements() { return mFormData.size(); }
  180. LL_INLINE LLSD getElement(U32 i) { return mFormData.get(i); }
  181. LLSD getElement(const std::string& element_name);
  182. bool hasElement(const std::string& element_name);
  183. void addElement(const std::string& type, const std::string& name,
  184. const LLSD& value = LLSD());
  185. void formatElements(const LLSD& substitutions);
  186. // Appends form elements from another form serialized as LLSD
  187. void append(const LLSD& sub_form);
  188. std::string getDefaultOption();
  189. LL_INLINE EIgnoreType getIgnoreType() { return mIgnore; }
  190. LL_INLINE std::string getIgnoreMessage() { return mIgnoreMsg; }
  191. private:
  192. LLSD mFormData;
  193. EIgnoreType mIgnore;
  194. std::string mIgnoreMsg;
  195. };
  196. typedef std::shared_ptr<LLNotificationForm> LLNotificationFormPtr;
  197. // This is the class of object read from the XML file (notifications.xml,
  198. // from the appropriate local language directory).
  199. struct LLNotificationTemplate
  200. {
  201. LLNotificationTemplate();
  202. // the name of the notification -- the key used to identify it Ideally, the
  203. // key should follow variable naming rules (no spaces or punctuation).
  204. std::string mName;
  205. // The type of the notification used to control which queue it's stored in
  206. std::string mType;
  207. // The text used to display the notification. Replaceable parameters are
  208. // enclosed in square brackets like this [].
  209. std::string mMessage;
  210. // The label for the notification; used for certain classes of notification
  211. // (those with a window and a window title).
  212. // Also used when a notification pops up underneath the current one.
  213. // Replaceable parameters can be used in the label.
  214. std::string mLabel;
  215. // The name of the icon image. This should include an extension.
  216. std::string mIcon;
  217. // This is the Highlander bit -- "There Can Be Only One"
  218. // An outstanding notification with this bit set is updated by an incoming
  219. // notification with the same name, rather than creating a new entry in the
  220. // queue (used for things like progress indications, or repeating warnings
  221. // like "the grid is going down in N minutes")
  222. bool mUnique;
  223. // if we want to be unique only if a certain part of the payload is
  224. // constant specify the field names for the payload. The notification will
  225. // only be combined if all of the fields named in the context are identical
  226. // in the new and the old notification; otherwise, the notification will be
  227. // duplicated. This is to support suppressing duplicate offers from the
  228. // same sender but still differentiating different offers. Example:
  229. // Invitation to conference chat.
  230. std::vector<std::string> mUniqueContext;
  231. // If this notification expires automatically, this value will be nonzero,
  232. // and indicates the number of seconds for which the notification will be
  233. // valid (a teleport offer, for example, might be valid for 300 seconds).
  234. U32 mExpireSeconds;
  235. // if the offer expires, one of the options is chosen automatically
  236. // based on its "value" parameter. This controls which one.
  237. // If expireSeconds is specified, expireOption should also be specified.
  238. U32 mExpireOption;
  239. // if the notification contains a url, it's stored here (and replaced
  240. // into the message where [_URL] is found)
  241. std::string mURL;
  242. // if there's a URL in the message, this controls which option visits
  243. // that URL. Obsolete this and eliminate the buttons for affected
  244. // messages when we allow clickable URLs in the UI
  245. U32 mURLOption;
  246. // does this notification persist across sessions? if so, it will be
  247. // serialized to disk on first receipt and read on startup
  248. bool mPersist;
  249. // This is the name of the default functor, if present, to be
  250. // used for the notification's callback. It is optional, and used only if
  251. // the notification is constructed without an identified functor.
  252. std::string mDefaultFunctor;
  253. // The form data associated with a given notification (buttons, text boxes,
  254. // etc)
  255. LLNotificationFormPtr mForm;
  256. // default priority for notifications of this type
  257. ENotificationPriority mPriority;
  258. // UUID of the audio file to be played when this notification arrives this
  259. // is loaded as a name, but looked up to get the UUID upon template load.
  260. // If null, it was not specified.
  261. LLUUID mSoundEffect;
  262. };
  263. // We want to keep a map of these by name, and it's best to manage them
  264. // with smart pointers
  265. typedef std::shared_ptr<LLNotificationTemplate> LLNotificationTemplatePtr;
  266. // The class that expresses the details of a notification.
  267. // The enable_shared_from_this flag ensures that if we construct a smart
  268. // pointer from a notification, we will always get the same shared pointer.
  269. class LLNotification : public std::enable_shared_from_this<LLNotification>
  270. {
  271. friend class LLNotifications;
  272. protected:
  273. LOG_CLASS(LLNotification);
  274. public:
  275. // We make this non-copyable because we want to manage these through
  276. // LLNotificationPtr, and only ever create one instance of any given
  277. // notification.
  278. LLNotification(const LLNotification&) = delete;
  279. LLNotification& operator=(const LLNotification&) = delete;
  280. // Parameter object used to instantiate a new notification
  281. class Params : public LLParamBlock<Params>
  282. {
  283. friend class LLNotification;
  284. public:
  285. Params(const std::string& _name)
  286. : name(_name),
  287. mTemporaryResponder(false),
  288. functor_name(_name),
  289. priority(NOTIFICATION_PRIORITY_UNSPECIFIED),
  290. timestamp(LLDate::now())
  291. {
  292. }
  293. // Pseudo-param
  294. Params& functor(LLNotificationFunctorRegistry::ResponseFunctor f)
  295. {
  296. functor_name = LLUUID::generateNewID().asString();
  297. LLNotificationFunctorRegistry::getInstance()->registerFunctor(functor_name,
  298. f);
  299. mTemporaryResponder = true;
  300. return *this;
  301. }
  302. LLMandatoryParam<std::string> name;
  303. // optional
  304. LLOptionalParam<LLSD> substitutions;
  305. LLOptionalParam<LLSD> payload;
  306. LLOptionalParam<ENotificationPriority> priority;
  307. LLOptionalParam<LLSD> form_elements;
  308. LLOptionalParam<LLDate> timestamp;
  309. LLOptionalParam<LLNotificationContext*> context;
  310. LLOptionalParam<std::string> functor_name;
  311. private:
  312. bool mTemporaryResponder;
  313. };
  314. // Constructor from a saved notification
  315. LLNotification(const LLSD& sd);
  316. // This is a string formatter for substituting into the message directly
  317. // from LLSD.
  318. static std::string format(const std::string& text, const LLSD& substitutions);
  319. void setResponseFunctor(const std::string& functor_name);
  320. typedef enum e_response_template_type
  321. {
  322. WITHOUT_DEFAULT_BUTTON,
  323. WITH_DEFAULT_BUTTON
  324. } EResponseTemplateType;
  325. // Returns response LLSD filled in with default form contents and
  326. // (optionally) the default button selected.
  327. LLSD getResponseTemplate(EResponseTemplateType type = WITHOUT_DEFAULT_BUTTON);
  328. // Returns index of first button with value==true; usually this the button
  329. // the user clicked on. Returns -1 if no button clicked (e.g. form has not
  330. // been displayed).
  331. static S32 getSelectedOption(const LLSD& notification, const LLSD& response);
  332. // Returns name of first button with value==true
  333. static std::string getSelectedOptionName(const LLSD& notification);
  334. // After someone responds to a notification (usually by clicking a button,
  335. // but sometimes by filling out a little form and THEN clicking a button),
  336. // the result of the response (the name and value of the button clicked,
  337. // plus any other data) should be packaged up as LLSD, then passed as a
  338. // parameter to the notification's respond() method here. This will look up
  339. // and call the appropriate responder.
  340. //
  341. // response is notification serialized as LLSD:
  342. // ["name"] = notification name
  343. // ["form"] = LLSD tree that includes form description and any prefilled form data
  344. // ["response"] = form data filled in by user
  345. // (including, but not limited to which button they clicked on)
  346. // ["payload"] = transaction specific data, such as ["source_id"] (originator of notification),
  347. // ["item_id"] (attached inventory item), etc.
  348. // ["substitutions"] = string substitutions used to generate notification message
  349. // from the template
  350. // ["time"] = time at which notification was generated;
  351. // ["expiry"] = time at which notification expires;
  352. // ["responseFunctor"] = name of registered functor that handles responses to notification;
  353. LLSD asLLSD();
  354. void respond(const LLSD& sd, bool save = true);
  355. LL_INLINE void setIgnored(bool ignore) { mIgnored = ignore; }
  356. LL_INLINE bool isCancelled() const { return mCancelled; }
  357. LL_INLINE bool isRespondedTo() const { return mRespondedTo; }
  358. LL_INLINE bool isIgnored() const { return mIgnored; }
  359. LL_INLINE const std::string& getName() const { return mTemplatep->mName; }
  360. LL_INLINE const LLUUID& getID() const { return mId; }
  361. LL_INLINE const LLSD& getPayload() const { return mPayload; }
  362. LL_INLINE const LLSD& getSubstitutions() const { return mSubstitutions; }
  363. LL_INLINE const LLDate& getDate() const { return mTimestamp; }
  364. LL_INLINE std::string getType() const { return mTemplatep ? mTemplatep->mType
  365. : ""; }
  366. std::string getMessage() const;
  367. std::string getLabel() const;
  368. LL_INLINE std::string getURL() const { return mTemplatep ? mTemplatep->mURL
  369. : ""; }
  370. LL_INLINE S32 getURLOption() const { return mTemplatep ? mTemplatep->mURLOption
  371. : -1; }
  372. LL_INLINE const LLNotificationFormPtr getForm() { return mForm; }
  373. LL_INLINE const LLDate& getExpiration() const { return mExpiresAt; }
  374. LL_INLINE ENotificationPriority getPriority() const
  375. {
  376. return mPriority;
  377. }
  378. // Comparing two notifications normally means comparing them by UUID (so we
  379. // can look them up quickly this way)
  380. LL_INLINE bool operator<(const LLNotification& rhs) const
  381. {
  382. return mId < rhs.mId;
  383. }
  384. LL_INLINE bool operator==(const LLNotification& rhs) const
  385. {
  386. return mId == rhs.mId;
  387. }
  388. LL_INLINE bool operator!=(const LLNotification& rhs) const
  389. {
  390. return mId != rhs.mId;
  391. }
  392. LL_INLINE bool isSameObjectAs(const LLNotification* rhs) const
  393. {
  394. return this == rhs;
  395. }
  396. // This object has been updated, so tell all our clients
  397. void update();
  398. void updateFrom(LLNotificationPtr other);
  399. // A fuzzy equals comparator.
  400. // true only if both notifications have the same template and
  401. // 1) flagged as unique (there can be only one of these) OR
  402. // 2) all required payload fields of each also exist in the other.
  403. bool isEquivalentTo(LLNotificationPtr that) const;
  404. // If the current time is greater than the expiration, the notification is
  405. // expired
  406. LL_INLINE bool isExpired() const
  407. {
  408. if (mExpiresAt.secondsSinceEpoch() == 0)
  409. {
  410. return false;
  411. }
  412. LLDate rightnow = LLDate::now();
  413. return rightnow > mExpiresAt;
  414. }
  415. std::string summarize() const;
  416. LL_INLINE bool hasUniquenessConstraints() const { return mTemplatep && mTemplatep->mUnique; }
  417. virtual ~LLNotification() = default;
  418. private:
  419. void init(const std::string& template_name, const LLSD& form_elements);
  420. LLNotification(const Params& p);
  421. // this is just for making it easy to look things up in a set organized by
  422. // UUID -- DON'T USE IT for anything real !
  423. LLNotification(LLUUID uuid) : mId(uuid) {}
  424. LL_INLINE void cancel() { mCancelled = true; }
  425. bool payloadContainsAll(const std::vector<std::string>& required_fields) const;
  426. private:
  427. LLUUID mId;
  428. ENotificationPriority mPriority;
  429. LLNotificationFormPtr mForm;
  430. // a reference to the template
  431. LLNotificationTemplatePtr mTemplatep;
  432. LLDate mTimestamp;
  433. LLDate mExpiresAt;
  434. LLSD mPayload;
  435. LLSD mSubstitutions;
  436. /*
  437. We want to be able to store and reload notifications so that they can
  438. survive a shutdown/restart of the client. So we can't simply pass in
  439. callbacks; we have to specify a callback mechanism that can be used by
  440. name rather than by some arbitrary pointer -- and then people have to
  441. initialize callbacks in some useful location.
  442. So we use LLNotificationFunctorRegistry to manage them.
  443. */
  444. std::string mResponseFunctorName;
  445. bool mCancelled;
  446. bool mIgnored;
  447. // Once the notification has been responded to, this becomes true
  448. bool mRespondedTo;
  449. /*
  450. In cases where we want to specify an explict, non-persisted callback,
  451. we store that in the callback registry under a dynamically generated
  452. key, and store the key in the notification, so we can still look it up
  453. using the same mechanism.
  454. */
  455. bool mTemporaryResponder;
  456. };
  457. std::ostream& operator<<(std::ostream& s, const LLNotification& notification);
  458. namespace LLNotificationFilters
  459. {
  460. // a sample filter
  461. bool includeEverything(LLNotificationPtr p);
  462. typedef enum e_comparison
  463. {
  464. EQUAL,
  465. LESS,
  466. GREATER,
  467. LESS_EQUAL,
  468. GREATER_EQUAL
  469. } EComparison;
  470. // Generic filter functor that takes method or member variable reference
  471. template<typename T>
  472. struct filterBy
  473. {
  474. typedef boost::function<T (LLNotificationPtr)> field_t;
  475. typedef typename boost::remove_reference<T>::type value_t;
  476. filterBy(field_t field, value_t value, EComparison comparison = EQUAL)
  477. : mField(field),
  478. mFilterValue(value),
  479. mComparison(comparison)
  480. {
  481. }
  482. bool operator()(LLNotificationPtr p)
  483. {
  484. switch (mComparison)
  485. {
  486. case EQUAL: return mField(p) == mFilterValue;
  487. case LESS: return mField(p) < mFilterValue;
  488. case GREATER: return mField(p) > mFilterValue;
  489. case LESS_EQUAL: return mField(p) <= mFilterValue;
  490. case GREATER_EQUAL: return mField(p) >= mFilterValue;
  491. default: return false;
  492. }
  493. }
  494. field_t mField;
  495. value_t mFilterValue;
  496. EComparison mComparison;
  497. };
  498. };
  499. namespace LLNotificationComparators
  500. {
  501. typedef enum e_direction { ORDER_DECREASING, ORDER_INCREASING } EDirection;
  502. // generic order functor that takes method or member variable reference
  503. template<typename T>
  504. struct orderBy
  505. {
  506. typedef boost::function<T (LLNotificationPtr)> field_t;
  507. orderBy(field_t field, EDirection = ORDER_INCREASING)
  508. : mField(field)
  509. {
  510. }
  511. LL_INLINE bool operator()(LLNotificationPtr lhs, LLNotificationPtr rhs)
  512. {
  513. if (mDirection == ORDER_DECREASING)
  514. {
  515. return mField(lhs) > mField(rhs);
  516. }
  517. return mField(lhs) < mField(rhs);
  518. }
  519. field_t mField;
  520. EDirection mDirection;
  521. };
  522. struct orderByUUID : public orderBy<const LLUUID&>
  523. {
  524. orderByUUID(EDirection direction = ORDER_INCREASING)
  525. : orderBy<const LLUUID&>(&LLNotification::getID, direction)
  526. {
  527. }
  528. };
  529. struct orderByDate : public orderBy<const LLDate&>
  530. {
  531. orderByDate(EDirection direction = ORDER_INCREASING)
  532. : orderBy<const LLDate&>(&LLNotification::getDate, direction)
  533. {
  534. }
  535. };
  536. };
  537. typedef boost::function<bool (LLNotificationPtr)> LLNotificationFilter;
  538. typedef boost::function<bool (LLNotificationPtr, LLNotificationPtr)> LLNotificationComparator;
  539. typedef std::set<LLNotificationPtr, LLNotificationComparator> LLNotificationSet;
  540. typedef std::multimap<std::string, LLNotificationPtr> LLNotificationMap;
  541. // ========================================================
  542. // Abstract base class (interface) for a channel; also used for the master container.
  543. // This lets us arrange channels into a call hierarchy.
  544. // We maintain a heirarchy of notification channels; events are always started
  545. // at the top and propagated through the hierarchy only if they pass a filter.
  546. // Any channel can be created with a parent. A null parent (empty string) means
  547. // it's tied to the root of the tree (the LLNotifications class itself).
  548. // The default hierarchy looks like this:
  549. //
  550. // LLNotifications -+- Expiration -+- Mute -+- Ignore -+- Visible -+- History
  551. // +-- Alerts
  552. // +-- Notifications
  553. //
  554. // In general, new channels that want to only see notifications that pass through
  555. // all of the built-in tests should attach to the "Visible" channel
  556. //
  557. class LLNotificationChannelBase : public boost::signals2::trackable
  558. {
  559. protected:
  560. LOG_CLASS(LLNotificationChannelBase);
  561. public:
  562. LLNotificationChannelBase(LLNotificationFilter filter,
  563. LLNotificationComparator comp)
  564. : mFilter(filter),
  565. mItems(comp)
  566. {
  567. }
  568. virtual ~LLNotificationChannelBase() = default;
  569. // You can also connect to a Channel, so you can be notified of
  570. // changes to this channel
  571. virtual void connectChanged(const LLStandardNotificationSignal::slot_type& slot);
  572. virtual void connectPassedFilter(const LLStandardNotificationSignal::slot_type& slot);
  573. virtual void connectFailedFilter(const LLStandardNotificationSignal::slot_type& slot);
  574. // use this when items change or to add a new one
  575. bool updateItem(const LLSD& payload);
  576. LL_INLINE const LLNotificationFilter& getFilter() { return mFilter; }
  577. protected:
  578. LLNotificationSet mItems;
  579. LLStandardNotificationSignal mChanged;
  580. LLStandardNotificationSignal mPassedFilter;
  581. LLStandardNotificationSignal mFailedFilter;
  582. // these are action methods that subclasses can override to take action on
  583. // specific types of changes; the management of the mItems list is still
  584. // handled by the generic handler.
  585. LL_INLINE virtual void onLoad(LLNotificationPtr p) {}
  586. LL_INLINE virtual void onAdd(LLNotificationPtr p) {}
  587. LL_INLINE virtual void onDelete(LLNotificationPtr p) {}
  588. LL_INLINE virtual void onChange(LLNotificationPtr p) {}
  589. bool updateItem(const LLSD& payload, LLNotificationPtr notifp);
  590. LLNotificationFilter mFilter;
  591. };
  592. // Type of the pointers that we are going to manage in the NotificationQueue
  593. // system. The shared_ptr model will ensure the notification channel will
  594. // self-destroy on LLNotifications destruction.
  595. class LLNotificationChannel;
  596. typedef std::shared_ptr<LLNotificationChannel> LLNotificationChannelPtr;
  597. // Manages a list of notifications.
  598. // Note: LLNotificationChannel is self-registering. The *correct* way to create
  599. // one is to do something like:
  600. // LLNotificationChannel::buildChannel("name", "parent"...);
  601. // This returns an LLNotificationChannelPtr, which you can store, or you can
  602. // then retrieve the channel by using the registry:
  603. // gNotifications.getChannel("name")...
  604. class LLNotificationChannel : public LLNotificationChannelBase
  605. {
  606. protected:
  607. LOG_CLASS(LLNotificationChannel);
  608. public:
  609. // We make this non-copyable because if this is ever copied around, we
  610. // might find ourselves with multiple copies of a queue with notifications
  611. // being added to different nonequivalent copies. We instead use a map of
  612. // shared_ptr to manage it.
  613. LLNotificationChannel(const LLNotificationChannel&) = delete;
  614. LLNotificationChannel& operator=(const LLNotificationChannel&) = delete;
  615. typedef LLNotificationSet::iterator Iterator;
  616. LL_INLINE std::string getName() const { return mName; }
  617. LL_INLINE std::string getParentChannelName() { return mParent; }
  618. LL_INLINE bool isEmpty() const { return mItems.empty(); }
  619. LL_INLINE Iterator begin() { return mItems.begin(); }
  620. LL_INLINE Iterator end() { return mItems.end(); }
  621. // Channels have a comparator to control sort order; the default sorts by
  622. // arrival date
  623. void setComparator(LLNotificationComparator comparator);
  624. std::string summarize();
  625. // Factory method for constructing these channels; since they are
  626. // self-registering, we want to make sure that you cannot use new to make
  627. // them.
  628. static LLNotificationChannelPtr buildChannel(const std::string& name,
  629. const std::string& parent,
  630. LLNotificationFilter filter = LLNotificationFilters::includeEverything,
  631. LLNotificationComparator comparator = LLNotificationComparators::orderByUUID());
  632. protected:
  633. // Notification Channels have a filter, which determines which
  634. // notifications will be added to this channel. Channel filters cannot
  635. // change. Channels have a protected constructor so you can't make smart
  636. // pointers that don't come from our internal reference; call
  637. // NotificationChannel::build(args)
  638. LLNotificationChannel(const std::string& name, const std::string& parent,
  639. LLNotificationFilter filter,
  640. LLNotificationComparator comparator);
  641. private:
  642. std::string mName;
  643. std::string mParent;
  644. LLNotificationComparator mComparator;
  645. };
  646. class LLNotifications : public LLNotificationChannelBase
  647. {
  648. protected:
  649. LOG_CLASS(LLNotifications);
  650. public:
  651. LLNotifications();
  652. // Must be called once before notifications are used. Call done in the
  653. // constructor of LLViewerWindow.
  654. void initClass();
  655. // Load notification descriptions from file; OK to call more than once
  656. // because it will reload
  657. bool loadTemplates();
  658. LLXMLNodePtr checkForXMLTemplate(LLXMLNodePtr item);
  659. // We provide a collection of simple add notification functions so that
  660. // it's reasonable to create notifications in one line
  661. LLNotificationPtr add(const std::string& name,
  662. const LLSD& substitutions = LLSD(),
  663. const LLSD& payload = LLSD());
  664. LLNotificationPtr add(const std::string& name,
  665. const LLSD& substitutions,
  666. const LLSD& payload,
  667. const std::string& functor_name);
  668. LLNotificationPtr add(const std::string& name,
  669. const LLSD& substitutions,
  670. const LLSD& payload,
  671. LLNotificationFunctorRegistry::ResponseFunctor functor);
  672. LLNotificationPtr add(const LLNotification::Params& p);
  673. void add(const LLNotificationPtr notifp);
  674. void cancel(LLNotificationPtr notifp);
  675. void update(const LLNotificationPtr notifp);
  676. LLNotificationPtr find(LLUUID uuid);
  677. typedef boost::function<void (LLNotificationPtr)> NotificationProcess;
  678. void forEachNotification(NotificationProcess process);
  679. // This is all stuff for managing the templates
  680. // take your template out
  681. LLNotificationTemplatePtr getTemplate(const std::string& name);
  682. // get the whole collection
  683. typedef std::vector<std::string> TemplateNames;
  684. TemplateNames getTemplateNames() const; // returns a list of notification names
  685. typedef std::map<std::string, LLNotificationTemplatePtr> TemplateMap;
  686. LL_INLINE TemplateMap::const_iterator templatesBegin() { return mTemplates.begin(); }
  687. LL_INLINE TemplateMap::const_iterator templatesEnd() { return mTemplates.end(); }
  688. // Tests for existence
  689. LL_INLINE bool templateExists(const std::string& name) { return mTemplates.count(name) != 0; }
  690. // Useful if you are reloading the file
  691. LL_INLINE void clearTemplates() { mTemplates.clear(); }
  692. void forceResponse(const LLNotification::Params& params, S32 option);
  693. void createDefaultChannels();
  694. typedef std::map<std::string, LLNotificationChannelPtr> ChannelMap;
  695. ChannelMap mChannels;
  696. void addChannel(LLNotificationChannelPtr channelp);
  697. LLNotificationChannelPtr getChannel(const std::string& chan_name);
  698. const std::string& getGlobalString(const std::string& key) const;
  699. private:
  700. void loadPersistentNotifications();
  701. bool expirationFilter(LLNotificationPtr notifp);
  702. bool expirationHandler(const LLSD& payload);
  703. bool uniqueFilter(LLNotificationPtr notifp);
  704. bool uniqueHandler(const LLSD& payload);
  705. bool failedUniquenessTest(const LLSD& payload);
  706. // Puts your template in
  707. bool addTemplate(const std::string& name, LLNotificationTemplatePtr temp);
  708. private:
  709. TemplateMap mTemplates;
  710. std::string mFileName;
  711. typedef std::map<std::string, LLXMLNodePtr> XMLTemplateMap;
  712. XMLTemplateMap mXmlTemplates;
  713. LLNotificationMap mUniqueNotifications;
  714. typedef std::map<std::string, std::string> GlobalStringMap;
  715. GlobalStringMap mGlobalStrings;
  716. };
  717. extern LLNotifications gNotifications;
  718. #endif//LL_LLNOTIFICATIONS_H