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
213
214
215
216
mod directory;
mod image;
mod status;
mod text;
use super::{Feature, Page};
use gtk::{gio::Cancellable, glib::Uri};
use std::rc::Rc;
/// Local files client driver
pub struct File {
page: Rc<Page>,
}
impl File {
// Constructors
/// Create new `Self`
pub fn init(page: &Rc<Page>) -> Self {
Self { page: page.clone() } // @TODO
}
pub fn handle(
&self,
uri: Uri,
feature: Rc<Feature>,
cancellable: Cancellable,
is_snap_history: bool,
) {
use directory::Directory;
use gtk::{
gio::{File, FileQueryInfoFlags, FileType},
glib::Priority,
prelude::FileExt,
};
use image::Image;
use status::Status;
use text::Text;
{
let mut i = self.page.navigation.request.info.borrow_mut();
i.set_request(Some(uri.to_string()));
self.page.navigation.request.update_secondary_icon(&i)
}
let url = uri.to_string();
let file = File::for_uri(&url);
let page = self.page.clone();
match file.query_file_type(FileQueryInfoFlags::NONE, Some(&cancellable)) {
FileType::Directory => Directory { file }.handle(&page, is_snap_history),
_ => file.clone().query_info_async(
"standard::content-type,standard::size",
FileQueryInfoFlags::NONE,
Priority::DEFAULT,
Some(&cancellable.clone()),
move |result| match result {
Ok(file_info) => {
page.navigation
.request
.info
.borrow_mut()
.set_size(Some(file_info.size() as usize));
match file_info.content_type() {
Some(content_type) => {
{
page.navigation
.request
.info
.borrow_mut()
.set_mime(Some(content_type.to_string()));
}
match content_type.as_str() {
"text/gemini" => {
if matches!(*feature, Feature::Source) {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Source(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
} else {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Gemini(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
}
}
"text/plain" => {
if matches!(*feature, Feature::Source) {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Source(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
} else if url.ends_with(".txt") {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Plain(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
});
} else if url.ends_with(".md") || url.ends_with(".markdown")
{
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Markdown(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
} else {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Gemini(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
}
}
"text/markdown" => {
if matches!(*feature, Feature::Source) {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Source(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
} else {
load_contents_async(file, cancellable, move |result| {
match result {
Ok(data) => {
Text::Markdown(uri, data).handle(&page)
}
Err(message) => {
Status::Failure(message).handle(&page)
}
}
})
}
}
"image/png" | "image/gif" | "image/jpeg" | "image/webp" => {
match gtk::gdk::Texture::from_file(&file) {
Ok(texture) => {
Image::Bitmap(uri, texture).handle(&page)
}
Err(e) => Status::Failure(e.to_string()).handle(&page),
}
}
mime => Status::Failure(format!(
"Content type `{mime}` yet not supported"
))
.handle(&page),
}
}
None => Status::Failure("Undetectable content type".to_string())
.handle(&page),
}
}
Err(e) => Status::Failure(e.to_string()).handle(&page),
},
),
}
}
}
// Tools
fn load_contents_async(
file: gtk::gio::File,
cancellable: Cancellable,
callback: impl FnOnce(Result<String, String>) + 'static,
) {
use gtk::prelude::FileExtManual;
file.load_contents_async(Some(&cancellable), {
move |result| {
callback(match result {
Ok((ref buffer, _)) => match String::from_utf8(buffer.to_vec()) {
Ok(data) => Ok(data),
Err(e) => Err(e.to_string()),
},
Err(e) => Err(e.to_string()),
})
}
})
}