game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
notification_service.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// notification_service.h
4// ==============================================================================
5// Abstract notification service - UI-agnostic interface for user notifications
6// Business logic uses this interface; UI provides the implementation
7// ==============================================================================
8
9#pragma once
10
11#include <functional>
12#include <memory>
13#include <string>
14
15namespace core
16{
17
18// ==============================================================================
19// Notification Types
20// ==============================================================================
21
29
31{
33 std::string message;
34 float duration = 3.0f; // Display duration in seconds (0 = persistent)
35 std::string actionLabel; // Optional action button label
36 std::function<void()> onAction; // Optional action callback
37};
38
39// ==============================================================================
40// INotificationService
41// ==============================================================================
42// Interface that UI implementations must provide.
43// Business logic calls this to notify users without knowing about UI specifics.
44// ==============================================================================
45
47{
48 public:
49 virtual ~INotificationService() = default;
50
51 /// Show an info notification
52 virtual void info(const std::string& message, float duration = 3.0f) = 0;
53
54 /// Show a success notification
55 virtual void success(const std::string& message, float duration = 3.0f) = 0;
56
57 /// Show a warning notification
58 virtual void warning(const std::string& message, float duration = 5.0f) = 0;
59
60 /// Show an error notification
61 virtual void error(const std::string& message, float duration = 5.0f) = 0;
62
63 /// Show a notification with full control
64 virtual void show(const Notification& notification) = 0;
65
66 /// Clear all notifications
67 virtual void clearAll() = 0;
68};
69
70// ==============================================================================
71// NotificationService Singleton Accessor
72// ==============================================================================
73// Global access point for notification service.
74// UI layer sets the implementation at startup.
75// ==============================================================================
76
78{
79 public:
80 /// Get the current notification service instance.
81 /// Returns an empty shared_ptr if not set.
82 ///
83 /// **Lifetime contract**: returns a shared_ptr (not a raw pointer)
84 /// because at QApplication teardown a background thread may still
85 /// fire while main()'s scope releases the last reference. Holding a
86 /// shared_ptr for the duration of the call keeps the underlying
87 /// service alive past any racing setInstance(nullptr).
88 static std::shared_ptr<INotificationService> instance();
89
90 /// Set the notification service implementation
91 /// UI layer calls this at startup with its implementation
92 static void setInstance(std::shared_ptr<INotificationService> service);
93
94 /// Convenience methods that safely handle null instance
95 static void info(const std::string& message, float duration = 3.0f);
96 static void success(const std::string& message, float duration = 3.0f);
97 static void warning(const std::string& message, float duration = 5.0f);
98 static void error(const std::string& message, float duration = 5.0f);
99
100 private:
101 static std::shared_ptr<INotificationService> instance_;
102
103 /// Take a snapshot of `instance_` under the singleton mutex. Returning
104 /// a `shared_ptr` (not raw) keeps the underlying object alive for the
105 /// duration of the caller's invocation, even if another thread races
106 /// to `setInstance(nullptr)`. Used internally by info/success/warn/error.
107 static std::shared_ptr<INotificationService> snapshotInstance();
108};
109
110// ==============================================================================
111// NullNotificationService
112// ==============================================================================
113// No-op implementation for testing or headless operation
114// ==============================================================================
115
117{
118 public:
119 void info(const std::string& /*message*/, float /*duration*/) override {}
120 void success(const std::string& /*message*/, float /*duration*/) override {}
121 void warning(const std::string& /*message*/, float /*duration*/) override {}
122 void error(const std::string& /*message*/, float /*duration*/) override {}
123 void show(const Notification& /*notification*/) override {}
124 void clearAll() override {}
125};
126
127} // namespace core
virtual ~INotificationService()=default
virtual void error(const std::string &message, float duration=5.0f)=0
Show an error notification.
virtual void success(const std::string &message, float duration=3.0f)=0
Show a success notification.
virtual void show(const Notification &notification)=0
Show a notification with full control.
virtual void warning(const std::string &message, float duration=5.0f)=0
Show a warning notification.
virtual void clearAll()=0
Clear all notifications.
virtual void info(const std::string &message, float duration=3.0f)=0
Show an info notification.
static void setInstance(std::shared_ptr< INotificationService > service)
Set the notification service implementation UI layer calls this at startup with its implementation.
static void success(const std::string &message, float duration=3.0f)
static void error(const std::string &message, float duration=5.0f)
static std::shared_ptr< INotificationService > instance()
Get the current notification service instance.
static void warning(const std::string &message, float duration=5.0f)
static void info(const std::string &message, float duration=3.0f)
Convenience methods that safely handle null instance.
void show(const Notification &) override
Show a notification with full control.
void error(const std::string &, float) override
Show an error notification.
void success(const std::string &, float) override
Show a success notification.
void clearAll() override
Clear all notifications.
void info(const std::string &, float) override
Show an info notification.
void warning(const std::string &, float) override
Show a warning notification.
std::function< void()> onAction