Notification.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/Notification.h>
  7. #include <LibIPC/ServerConnection.h>
  8. #include <NotificationServer/NotificationClientEndpoint.h>
  9. #include <NotificationServer/NotificationServerEndpoint.h>
  10. namespace GUI {
  11. class NotificationServerConnection : public IPC::ServerConnection<NotificationClientEndpoint, NotificationServerEndpoint>
  12. , public NotificationClientEndpoint {
  13. C_OBJECT(NotificationServerConnection)
  14. friend class Notification;
  15. public:
  16. virtual void handshake() override
  17. {
  18. greet();
  19. }
  20. virtual void die() override
  21. {
  22. m_notification->connection_closed();
  23. }
  24. private:
  25. explicit NotificationServerConnection(Notification* notification)
  26. : IPC::ServerConnection<NotificationClientEndpoint, NotificationServerEndpoint>(*this, "/tmp/portal/notify")
  27. , m_notification(notification)
  28. {
  29. }
  30. virtual void dummy() override { }
  31. Notification* m_notification;
  32. };
  33. Notification::Notification()
  34. {
  35. }
  36. Notification::~Notification()
  37. {
  38. }
  39. void Notification::show()
  40. {
  41. VERIFY(!m_shown && !m_destroyed);
  42. auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
  43. m_connection = NotificationServerConnection::construct(this);
  44. m_connection->show_notification(m_text, m_title, icon);
  45. m_shown = true;
  46. }
  47. void Notification::close()
  48. {
  49. VERIFY(m_shown);
  50. if (!m_destroyed) {
  51. m_connection->close_notification();
  52. connection_closed();
  53. return;
  54. }
  55. }
  56. bool Notification::update()
  57. {
  58. VERIFY(m_shown);
  59. if (m_destroyed) {
  60. return false;
  61. }
  62. if (m_text_dirty || m_title_dirty) {
  63. m_connection->update_notification_text(m_text, m_title);
  64. m_text_dirty = false;
  65. m_title_dirty = false;
  66. }
  67. if (m_icon_dirty) {
  68. m_connection->update_notification_icon(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
  69. m_icon_dirty = false;
  70. }
  71. return true;
  72. }
  73. void Notification::connection_closed()
  74. {
  75. m_connection.clear();
  76. m_destroyed = true;
  77. }
  78. }