-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathevents.rs
More file actions
139 lines (130 loc) · 4.65 KB
/
events.rs
File metadata and controls
139 lines (130 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use serde::Serialize;
use tauri::{AppHandle, Emitter, Manager, Url};
use tauri_plugin_notification::NotificationExt;
use crate::{
window_manager::{WindowManager, NEW_UI_WINDOW_ID},
ConnectionType,
};
// Match src/pages/client/types.ts.
#[non_exhaustive]
pub enum EventKey {
ConnectionChanged,
InstanceUpdate,
LocationUpdate,
AppVersionFetch,
ConfigChanged,
DeadConnectionDropped,
DeadConnectionReconnected,
ApplicationConfigChanged,
AddInstance,
MfaTrigger,
VersionMismatch,
UuidMismatch,
}
impl From<EventKey> for &'static str {
fn from(key: EventKey) -> &'static str {
match key {
EventKey::ConnectionChanged => "connection-changed",
EventKey::InstanceUpdate => "instance-update",
EventKey::LocationUpdate => "location-update",
EventKey::AppVersionFetch => "app-version-fetch",
EventKey::ConfigChanged => "config-changed",
EventKey::DeadConnectionDropped => "dead-connection-dropped",
EventKey::DeadConnectionReconnected => "dead-connection-reconnected",
EventKey::ApplicationConfigChanged => "application-config-changed",
EventKey::AddInstance => "add-instance",
EventKey::MfaTrigger => "mfa-trigger",
EventKey::VersionMismatch => "version-mismatch",
EventKey::UuidMismatch => "uuid-mismatch",
}
}
}
/// Used as payload for [`DEAD_CONNECTION_DROPPED`] event
#[derive(Clone, Serialize)]
pub struct DeadConnDroppedOut {
pub(crate) name: String,
pub(crate) con_type: ConnectionType,
pub(crate) peer_alive_period: i64,
}
impl DeadConnDroppedOut {
/// Emits [`DEAD_CONNECTION_DROPPED`] event with corresponding side effects.
pub(crate) fn emit(self, app_handle: &AppHandle) {
if let Err(err) = app_handle
.notification()
.builder()
// .id(&app_handle.config().identifier)
.title(format!("{} {} disconnected", self.con_type, self.name))
.body("Connection activity timeout.")
.show()
{
warn!("Dead connection dropped notification not shown. Reason: {err}");
}
if let Err(err) = app_handle.emit(EventKey::DeadConnectionDropped.into(), self) {
error!("Event Dead Connection Dropped was not emitted. Reason: {err}");
}
}
}
/// Used as payload for [`DEAD_CONNECTION_RECONNECTED`] event
#[derive(Clone, Serialize)]
pub struct DeadConnReconnected {
pub(crate) name: String,
pub(crate) con_type: ConnectionType,
pub(crate) peer_alive_period: i64,
}
impl DeadConnReconnected {
/// Emits [`DEAD_CONNECTION_RECONNECTED`] event with corresponding side effects.
pub(crate) fn emit(self, app_handle: &AppHandle) {
if let Err(err) = app_handle
.notification()
.builder()
// .id(&app_handle.config().identifier)
.title(format!("{} {} reconnected", self.con_type, self.name))
.body("Connection activity timeout.")
.show()
{
warn!("Dead connection reconnected notification not shown. Reason: {err}");
}
if let Err(err) = app_handle.emit(EventKey::DeadConnectionReconnected.into(), self) {
error!("Event Dead Connection Reconnected was not emitted. Reason: {err}");
}
}
}
#[derive(Clone, Serialize)]
pub struct AddInstancePayload<'a> {
pub token: &'a str,
pub url: &'a str,
}
/// Handle deep-link URLs.
pub fn handle_deep_link(app_handle: &AppHandle, urls: &[Url]) {
debug!("Deep link received.");
for link in urls {
if link.host_str() == Some("addinstance") {
let mut token = None;
let mut url = None;
for (key, value) in link.query_pairs() {
if key == "token" {
token = Some(value.clone());
}
if key == "url" {
url = Some(value.clone());
}
}
if let (Some(token), Some(url)) = (token, url) {
info!("Valid Deep link received.");
if let Some(tray_win) = app_handle.get_webview_window(NEW_UI_WINDOW_ID) {
let _ = tray_win.hide();
}
if let Err(e) = WindowManager::open_full_view(app_handle) {
warn!("Deep link: failed to open main window: {e}");
}
let _ = app_handle.emit(
EventKey::AddInstance.into(),
AddInstancePayload {
token: &token,
url: &url,
},
);
}
}
}
}