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