ClientConnection.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ClientConnection.h"
  7. #include "NotificationWindow.h"
  8. #include <AK/HashMap.h>
  9. #include <NotificationServer/NotificationClientEndpoint.h>
  10. namespace NotificationServer {
  11. static HashMap<int, RefPtr<ClientConnection>> s_connections;
  12. ClientConnection::ClientConnection(NonnullRefPtr<Core::LocalSocket> client_socket, int client_id)
  13. : IPC::ClientConnection<NotificationClientEndpoint, NotificationServerEndpoint>(*this, move(client_socket), client_id)
  14. {
  15. s_connections.set(client_id, *this);
  16. }
  17. ClientConnection::~ClientConnection()
  18. {
  19. }
  20. void ClientConnection::die()
  21. {
  22. s_connections.remove(client_id());
  23. }
  24. void ClientConnection::handle(const Messages::NotificationServer::Greet&)
  25. {
  26. }
  27. void ClientConnection::handle(const Messages::NotificationServer::ShowNotification& message)
  28. {
  29. auto window = NotificationWindow::construct(client_id(), message.text(), message.title(), message.icon());
  30. window->show();
  31. }
  32. void ClientConnection::handle([[maybe_unused]] const Messages::NotificationServer::CloseNotification& message)
  33. {
  34. auto window = NotificationWindow::get_window_by_id(client_id());
  35. if (window) {
  36. window->close();
  37. }
  38. }
  39. Messages::NotificationServer::UpdateNotificationIconResponse ClientConnection::handle(const Messages::NotificationServer::UpdateNotificationIcon& message)
  40. {
  41. auto window = NotificationWindow::get_window_by_id(client_id());
  42. if (window) {
  43. window->set_image(message.icon());
  44. }
  45. return !!window;
  46. }
  47. Messages::NotificationServer::UpdateNotificationTextResponse ClientConnection::handle(const Messages::NotificationServer::UpdateNotificationText& message)
  48. {
  49. auto window = NotificationWindow::get_window_by_id(client_id());
  50. if (window) {
  51. window->set_text(message.text());
  52. window->set_title(message.title());
  53. }
  54. return !!window;
  55. }
  56. Messages::NotificationServer::IsShowingResponse ClientConnection::handle(const Messages::NotificationServer::IsShowing&)
  57. {
  58. auto window = NotificationWindow::get_window_by_id(client_id());
  59. return !!window;
  60. }
  61. }