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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
mod content;
mod database;
mod error;
mod input;
mod navigation;
mod search;
use super::{Action as ItemAction, BrowserAction, Profile, TabAction, WindowAction};
use adw::TabPage;
use content::Content;
use error::Error;
use input::Input;
use navigation::Navigation;
use search::Search;
use sqlite::Transaction;
use std::rc::Rc;
pub struct Page {
pub profile: Rc<Profile>,
// Actions
pub browser_action: Rc<BrowserAction>,
pub item_action: Rc<ItemAction>,
pub window_action: Rc<WindowAction>,
// Components
pub content: Rc<Content>,
pub search: Rc<Search>,
pub input: Rc<Input>,
pub navigation: Rc<Navigation>,
// System
/// Reference to [TabPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.TabPage.html)
/// wanted to update title, loading status and other features related with page.
/// * this member sensitively dependent of parental HashMap index,\
/// as connection drivers interact with `Page` API,\
/// let's keep it private to isolate direct access and prevent their implementation errors
tab_page: TabPage,
}
impl Page {
// Constructors
pub fn build(
profile: &Rc<Profile>,
(browser_action, window_action, tab_action, item_action): (
&Rc<BrowserAction>,
&Rc<WindowAction>,
&Rc<TabAction>,
&Rc<ItemAction>,
),
tab_page: &TabPage,
) -> Self {
// Init components
let content = Rc::new(Content::build((window_action, tab_action, item_action)));
let search = Rc::new(Search::new());
let navigation = Rc::new(Navigation::build(
profile,
(window_action, tab_action, item_action),
));
let input = Rc::new(Input::new());
// Done
Self {
profile: profile.clone(),
tab_page: tab_page.clone(),
// Actions
browser_action: browser_action.clone(),
item_action: item_action.clone(),
window_action: window_action.clone(),
// Components
content,
search,
input,
navigation,
}
}
// Actions
/// Toggle bookmark for current `profile` by navigation request value
/// * return `true` on bookmark created, `false` on deleted
pub fn bookmark(&self) -> Result<bool, Error> {
let result = match self
.profile
.bookmark
.toggle(self.navigation.request().as_str())
{
Ok(result) => Ok(result),
Err(_) => Err(Error::Bookmark), // @TODO
};
result
}
/// Request `Escape` action for all page components
pub fn escape(&self) {
self.search.hide()
}
/// Toggle `Find` widget
pub fn find(&self) {
self.search.show()
}
/// Cleanup session for `Self`
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, record.id) {
Ok(_) => {
// Delegate clean action to the item childs
self.navigation.clean(transaction, &record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
/// Restore `Self` session from database
pub fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
// Begin page restore
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
// Restore `Self`
if let Some(title) = record.title {
self.set_title(title.as_str());
}
self.set_needs_attention(record.is_needs_attention);
// Restore child components
self.navigation.restore(transaction, &record.id)?;
// Make initial page history snap using `navigation` values restored
if let Some(uri) = self.navigation.uri() {
self.profile.history.memory.request.set(uri);
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
/// Save `Self` session to database
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
// Keep value in memory until operation complete
let title = self.tab_page.title();
match database::insert(
transaction,
app_browser_window_tab_item_id,
self.tab_page.needs_attention(),
match title.is_empty() {
true => None,
false => Some(title.as_str()),
},
) {
Ok(_) => {
let id = database::last_insert_id(transaction);
// Delegate save action to childs
self.navigation.save(transaction, &id)?;
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
// Setters
/// Set title for `Self`
/// * this method allows to keep `tab_page` isolated from driver implementation
pub fn set_title(&self, title: &str) {
self.tab_page.set_title(title)
}
pub fn set_needs_attention(&self, is_needs_attention: bool) {
self.tab_page.set_needs_attention(is_needs_attention)
}
pub fn set_progress(&self, progress_fraction: f64) {
self.navigation.set_progress_fraction(progress_fraction);
self.tab_page.set_loading(progress_fraction > 0.0)
}
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
// Delegate migration to childs
navigation::migrate(tx)?;
// Success
Ok(())
}